Advertisement
Corbinhol

PowerController

Feb 3rd, 2023 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. --Energy Controller
  2. local protocol = "ENERGY"; --Do All Caps
  3. local hostname = "InductionMatrix";
  4. local modemSide = "right";
  5.  
  6. local outputData = {};
  7. local logOutput = {};
  8. rednet.open(modemSide);
  9.  
  10. local inductionMatrix = peripheral.wrap("back");
  11. outputData["energyOutput"] = 0;
  12. outputData["energy"] = 0;
  13. outputData["maxEnergy"] = 0;
  14.  
  15. print("Starting Up Local Monitor Service");
  16.  
  17. if os.getComputerLabel() == nil then
  18. print("System Does Not Have ID...Attempting to obtain one")
  19. print("Protocol: " .. protocol);
  20. print("Hostname: " .. hostname);
  21. local i = 1;
  22. while true do
  23. local tempHostname = hostname .. "-" .. i;
  24. if rednet.lookup(protocol, tempHostname) == nil then
  25. os.setComputerLabel(tempHostname);
  26. print("Found Free Name. Set it to " .. tempHostname);
  27. break;
  28. end
  29. i = i + 1;
  30. end
  31. end
  32.  
  33. function log(value)
  34. table.insert(logOutput, value);
  35. if table.getn(logOutput) >= 12 then
  36. table.remove(logOutput, 1);
  37. end
  38. end
  39.  
  40.  
  41. function mainLoop()
  42. while true do
  43. outputData["energyOutput"] = (math.floor(inductionMatrix.getOutput() / 2.5));
  44. outputData["energy"] = (math.floor(inductionMatrix.getEnergy() / 2.5));
  45. outputData["maxEnergy"] = (math.floor(inductionMatrix.getMaxEnergy() / 2.5));
  46. sleep(1);
  47. end
  48. end
  49.  
  50. function display()
  51. shell.run("clear");
  52. term.setCursorPos(1, 19);
  53. term.write(string.rep("=", 51));
  54. while true do
  55. term.setCursorPos(1,1);
  56. print(string.rep("=", 51));
  57. print("Protocol: " .. protocol);
  58. print("Hostname: " .. os.getComputerLabel());
  59. term.clearLine()
  60. print("Energy Output: " .. outputData["energyOutput"] .. " rf/t");
  61. term.clearLine()
  62. print("Energy Amount: " .. outputData["energy"] .. " rf");
  63. print(string.rep("=", 51));
  64. sleep(.5);
  65. for t=1, 12, 1 do
  66. term.clearLine()
  67. if logOutput[t] ~= nil then
  68. print(logOutput[t]);
  69. end
  70. end
  71. sleep(0);
  72. end
  73. end
  74.  
  75. function modemActivity()
  76. while true do
  77. local from, message = rednet.receive(protocol);
  78. if message == "getdata" then
  79. local out = textutils.serialize(outputData);
  80. rednet.send(from, out, protocol);
  81. log("Recieved Message From " .. from .. " Responding...");
  82. end
  83. end
  84. end
  85.  
  86. if os.getComputerLabel() ~= nil then
  87. rednet.host(protocol, os.getComputerLabel())
  88. print("Set Hostname");
  89. end
  90.  
  91. parallel.waitForAll(display, modemActivity, mainLoop);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement