Advertisement
Marlingaming

Power plant central manager

Nov 28th, 2022 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. local M_Gens ={}--PU,State,Orders,ID
  2.  
  3. local R_PU = 0--required power units
  4.  
  5. local BoxStatus = {}--Client,R_PU,Status
  6.  
  7. --generator frequencies
  8. local G_SCh = 0
  9. local G_LCh = 0
  10. --security frequencies
  11. local S_SCh = 0
  12. local S_LCh = 0
  13. --logistics frequencies
  14.  
  15. --powerbox frequencies
  16. local P_SCh = 0
  17. local P_LCh = 0
  18.  
  19. local Modem = peripheral.wrap("top")
  20. local WM = peripheral.wrap("bottom")
  21.  
  22. local function Logger(text)
  23. local T = os.day().." // "..text
  24. local file = fs.open("Logs.txt","a")
  25. file.writeLine(T)
  26. file.close()
  27. end
  28.  
  29. local function GenComms(select,item,action)
  30. local D = {{select,item},action}
  31. Modem.transmit(G_SCh,G_LCh,textutils.serialize(D))
  32. Modem.open(G_LCh)
  33. local a, b, c, d, e
  34. repeat
  35. a, b, c, d, e = os.pullEvent("modem_message")
  36. until a == "modem_message" and b == G_LCh
  37. D = textutils.unserialize(e)
  38. return D
  39. end
  40.  
  41. local function PowerBoxNet()
  42.  
  43. end
  44.  
  45. local function PowerManager()
  46. local SU = 0
  47. local MU_1 = 0--main gens max power
  48. for i = 1, #M_Gens do
  49. M_Gens[i][2] = GenComms("ID",M_Gens[i][4],"status")[1]
  50. MU = MU + M_Gens[i][1]
  51. if M_Gens[i][2] == true then SU = SU + M_Gens[i][1] end
  52. end
  53. local De = false
  54. if SU > R_PU then De = true end
  55. repeat
  56. for i = 1, #M_Gens do
  57. if (SU >= R_PU and De == false and M_Gens[i][3] ~= De) then M_Gens[i][3] = false SU = SU - M_Gens[i][1] elseif (De == true and SU <= R_PU and M_Gens[i][3] ~= De) then M_Gens[i][3] = true SU = SU + M_Gens[i][1] end
  58. end
  59. until (SU >= R_PU and De == false) or (De == true and SU <= R_PU)
  60. for i = 1, #M_Gens do
  61. local Act
  62. if M_Gens[i][3] == true then Act = "startup" else Act = "shutdown" end
  63. GenComms("ID",M_Gens[i][4],Act)
  64. end
  65. end
  66.  
  67. local function SecuritySys(item,action)
  68.  
  69. end
  70.  
  71. local function OpenBoxFile(ID)
  72. local file = fs.open("PwrBoxes/"..ID,"r")--client,cords,R_PU,payment
  73. local Data = textutils.unserialize(file.readAll())
  74. file.close()
  75. return Data
  76. end
  77.  
  78. local function ChargeBox()
  79.  
  80. end
  81.  
  82. local function CheckPayment(ID)
  83. local Res
  84. local Data = OpenBoxFile(ID)
  85. if Data[4] <= 0 then Res = false else Res = true end
  86. return Res
  87. end
  88.  
  89. local function PwrBoxAct(e)
  90. local D = textutils.unserialize(e)--unitID,request,data
  91. local Response = {}
  92. if D[2] == "on" then
  93. BoxStatus[D[1]][3] = CheckPayment(D[1])
  94. Response = {BoxStatus[D[1]][3]}
  95. elseif D[2] == "off" then
  96. BoxStatus[D[1]][3] = false
  97. Response = {true}
  98. elseif D[2] == "payment" then
  99.  
  100. end
  101. return textutils.serialize(Response)
  102. end
  103.  
  104. local function SecAct(e)
  105. local D = textutils.unserialize(e)
  106.  
  107. return false
  108. end
  109.  
  110. local function Loop()
  111. WM.open(P_LCh)
  112. Modem.open(S_LCh)
  113. if fs.exists("restart.txt") then fs.delete("restart.txt") end
  114. while true do
  115. local a, b, c, d, e = os.pullEvent("modem_message")
  116. if b == P_LCh then
  117. WM.transmit(P_SCh,P_LCh,PwrBoxAct(e))
  118. elseif b == S_LCh then
  119. Modem.transmit(S_SCh,S_LCh,SecAct(e))
  120. end
  121. PowerManager()
  122. end
  123. end
  124.  
  125. function Startup()
  126. Logger("system boot start")
  127. Logger("enforcing lockdown")
  128. SecuritySys("lockdown",true)
  129. Logger("power target set to 5")
  130. PowerManager()
  131. Logger("requst sent: activating power box breakers")
  132. PowerBoxNet("close")
  133. Logger("boot complete")
  134. Loop()
  135. end
  136.  
  137. Startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement