Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local oldPull = os.pullEvent
- os.pullEvent = os.pullEventRaw
- local Version = "2.1.2"
- local Modem = peripheral.wrap("right")
- local Output = "top"
- local Switch = "front"
- local LCh = 6734
- local SCh = 2334
- local ID = 1
- local State = 1--0=setup,1=allowspower,2=disabled
- local PwrFlow = false
- local Usage = {0,false}
- local Sponsor = "WST"
- local w, h = term.getSize()
- local function Display(text)
- local L = w/2 - string.len(text)/2
- term.clear()
- term.setCursorPos(1,h/2 -1)
- term.write("Power Box 2 - "..Sponsor.." V:"..Version)
- term.setCursorPos(L,h/2)
- term.write(text)
- term.setCursorPos(1,h/2 +1)
- term.write("ID: "..ID)
- end
- local function Setup()
- local Data = {"right","bottom","top",0,1,2334,6734}
- local file = fs.open("data.txt","w")
- file.write(textutils.serialize(Data))
- file.close()
- local St = fs.open("startup","w")
- St.writeLine("shell.run('PWR')")
- St.writeLine("os.sleep(5)")
- St.writeLine("os.reboot()")
- St.close()
- print("please edit data.txt now")
- end
- local function Boot()
- local file = fs.open("data.txt","r")
- local Data = textutils.unserialize(file.readAll())--ModemSide,SwitchSide,Output,State,ID,SCh,LCh
- file.close()
- Modem = peripheral.wrap(Data[1])
- Switch = Data[2]
- Output = Data[3]
- State = Data[4]
- ID = Data[5]
- SCh = Data[6]
- LCh = Data[7]
- end
- local function SaveData()
- local Data = {"right","bottom","top",0,1,2334,6734}
- local Doc = fs.open("data.txt","r")
- local Tes = textutils.unserialize(Doc.readAll())
- Doc.close()
- Data = Tes
- Data[4] = State
- local file = fs.open("data.txt","w")
- file.write(textutils.serialize(Data))
- file.close()
- end
- local function UpdateUsage()
- local file = fs.open("PowerUsage.txt","a")
- file.writeLine((os.clock() - Usage[1]))
- file.close()
- end
- local function GetUsage()
- local file = fs.open("PowerUsage.txt","r")
- local A
- local Total = 0
- repeat
- A = file.readLine()
- if A ~= nil then Total = Total + tonumber(A) end
- until A == nil
- file.close()
- return Total
- end
- local function Action(e)
- local Data = textutils.unserialize(e)
- local Response = {ID}
- if Data[1] ~= ID then
- elseif Data[2] == "details" then
- if Data[3] == "state" then
- Response[2] = State
- elseif Data[3] == "usage" then
- Response[2] = GetUsage()
- elseif Data[3] == "version" then
- Response[2] = Version
- end
- elseif Data[2] == "command" then
- if Data[3] == "setup" then
- State = 0
- elseif Data[3] == "enable" then
- State = 1
- elseif Data[3] == "disable" then
- State = 2
- end
- SaveData()
- os.sleep(2)
- os.reboot()
- elseif Data[2] == "shutdown" then
- redstone.setOutput(Output,true)
- Display("Shutdown command received")
- os.sleep(3)
- os.shutdown()
- end
- if Data[1] == ID then
- Modem.transmit(SCh,LCh,textutils.serialize(Response))
- end
- end
- local function Loop()
- Display("Starting System")
- Modem.open(LCh)
- while true do
- if State == 1 and redstone.getInput(Switch) == true then
- PwrFlow = true
- Display("Power on")
- elseif State ~= 1 or redstone.getInput(Switch) == false then
- PwrFlow = false
- Display("Power Off")
- end
- local a, b, c, d, e = os.pullEvent()
- if a == "modem_message" then Action(e) end
- 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
- if os.clock() > 300 then UpdateUsage() SaveData() os.reboot() end
- os.queueEvent("pass")
- if State == 0 and os.pullEvent ~= oldPull then os.pullEvent = oldPull end
- end
- end
- if fs.exists("PowerUsage.txt") then else local T = fs.open("PowerUsage.txt","w") T.writeLine(0) T.close() end
- if fs.exists("data.txt") then Boot() end
- if fs.exists("startup") then
- Loop()
- else
- Setup()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement