Advertisement
Marlingaming

Power Box 2

Dec 16th, 2022 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. local oldPull = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3.  
  4. local Version = "2.1.2"
  5. local Modem = peripheral.wrap("right")
  6. local Output = "top"
  7. local Switch = "front"
  8. local LCh = 6734
  9. local SCh = 2334
  10. local ID = 1
  11. local State = 1--0=setup,1=allowspower,2=disabled
  12. local PwrFlow = false
  13.  
  14. local Usage = {0,false}
  15.  
  16. local Sponsor = "WST"
  17.  
  18. local w, h = term.getSize()
  19.  
  20. local function Display(text)
  21. local L = w/2 - string.len(text)/2
  22. term.clear()
  23. term.setCursorPos(1,h/2 -1)
  24. term.write("Power Box 2 - "..Sponsor.." V:"..Version)
  25. term.setCursorPos(L,h/2)
  26. term.write(text)
  27. term.setCursorPos(1,h/2 +1)
  28. term.write("ID: "..ID)
  29. end
  30.  
  31. local function Setup()
  32. local Data = {"right","bottom","top",0,1,2334,6734}
  33. local file = fs.open("data.txt","w")
  34. file.write(textutils.serialize(Data))
  35. file.close()
  36. local St = fs.open("startup","w")
  37. St.writeLine("shell.run('PWR')")
  38. St.writeLine("os.sleep(5)")
  39. St.writeLine("os.reboot()")
  40. St.close()
  41. print("please edit data.txt now")
  42. end
  43.  
  44. local function Boot()
  45. local file = fs.open("data.txt","r")
  46. local Data = textutils.unserialize(file.readAll())--ModemSide,SwitchSide,Output,State,ID,SCh,LCh
  47. file.close()
  48. Modem = peripheral.wrap(Data[1])
  49. Switch = Data[2]
  50. Output = Data[3]
  51. State = Data[4]
  52. ID = Data[5]
  53. SCh = Data[6]
  54. LCh = Data[7]
  55. end
  56.  
  57. local function SaveData()
  58. local Data = {"right","bottom","top",0,1,2334,6734}
  59. local Doc = fs.open("data.txt","r")
  60. local Tes = textutils.unserialize(Doc.readAll())
  61. Doc.close()
  62. Data = Tes
  63. Data[4] = State
  64. local file = fs.open("data.txt","w")
  65. file.write(textutils.serialize(Data))
  66. file.close()
  67. end
  68.  
  69. local function UpdateUsage()
  70. local file = fs.open("PowerUsage.txt","a")
  71. file.writeLine((os.clock() - Usage[1]))
  72. file.close()
  73. end
  74.  
  75. local function GetUsage()
  76. local file = fs.open("PowerUsage.txt","r")
  77. local A
  78. local Total = 0
  79. repeat
  80. A = file.readLine()
  81. if A ~= nil then Total = Total + tonumber(A) end
  82. until A == nil
  83. file.close()
  84. return Total
  85. end
  86.  
  87. local function Action(e)
  88. local Data = textutils.unserialize(e)
  89. local Response = {ID}
  90. if Data[1] ~= ID then
  91.  
  92. elseif Data[2] == "details" then
  93. if Data[3] == "state" then
  94. Response[2] = State
  95. elseif Data[3] == "usage" then
  96. Response[2] = GetUsage()
  97. elseif Data[3] == "version" then
  98. Response[2] = Version
  99. end
  100. elseif Data[2] == "command" then
  101. if Data[3] == "setup" then
  102. State = 0
  103. elseif Data[3] == "enable" then
  104. State = 1
  105. elseif Data[3] == "disable" then
  106. State = 2
  107. end
  108. SaveData()
  109. os.sleep(2)
  110. os.reboot()
  111. elseif Data[2] == "shutdown" then
  112. redstone.setOutput(Output,true)
  113. Display("Shutdown command received")
  114. os.sleep(3)
  115. os.shutdown()
  116. end
  117. if Data[1] == ID then
  118. Modem.transmit(SCh,LCh,textutils.serialize(Response))
  119. end
  120. end
  121.  
  122. local function Loop()
  123. Display("Starting System")
  124. Modem.open(LCh)
  125. while true do
  126. if State == 1 and redstone.getInput(Switch) == true then
  127. PwrFlow = true
  128. Display("Power on")
  129. elseif State ~= 1 or redstone.getInput(Switch) == false then
  130. PwrFlow = false
  131. Display("Power Off")
  132. end
  133. local a, b, c, d, e = os.pullEvent()
  134. if a == "modem_message" then Action(e) end
  135. if PwrFlow == true then redstone.setOutput(Output,false) if Usage[2] == false then Usage[1] = os.clock() Usage[2] = true end else redstone.setOutput(Output,true) if a == "redstone" and Usage[2] == true then Usage[2] = false UpdateUsage() end end
  136. if os.clock() > 300 then UpdateUsage() SaveData() os.reboot() end
  137. os.queueEvent("pass")
  138. if State == 0 and os.pullEvent ~= oldPull then os.pullEvent = oldPull end
  139. end
  140. end
  141. if fs.exists("PowerUsage.txt") then else local T = fs.open("PowerUsage.txt","w") T.writeLine(0) T.close() end
  142. if fs.exists("data.txt") then Boot() end
  143. if fs.exists("startup") then
  144. Loop()
  145. else
  146. Setup()
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement