Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local M_Gens ={}--PU,State,Orders,ID
- local R_PU = 0--required power units
- local BoxStatus = {}--Client,R_PU,Status
- --generator frequencies
- local G_SCh = 0
- local G_LCh = 0
- --security frequencies
- local S_SCh = 0
- local S_LCh = 0
- --logistics frequencies
- --powerbox frequencies
- local P_SCh = 0
- local P_LCh = 0
- local Modem = peripheral.wrap("top")
- local WM = peripheral.wrap("bottom")
- local function Logger(text)
- local T = os.day().." // "..text
- local file = fs.open("Logs.txt","a")
- file.writeLine(T)
- file.close()
- end
- local function GenComms(select,item,action)
- local D = {{select,item},action}
- Modem.transmit(G_SCh,G_LCh,textutils.serialize(D))
- Modem.open(G_LCh)
- local a, b, c, d, e
- repeat
- a, b, c, d, e = os.pullEvent("modem_message")
- until a == "modem_message" and b == G_LCh
- D = textutils.unserialize(e)
- return D
- end
- local function PowerBoxNet()
- end
- local function PowerManager()
- local SU = 0
- local MU_1 = 0--main gens max power
- for i = 1, #M_Gens do
- M_Gens[i][2] = GenComms("ID",M_Gens[i][4],"status")[1]
- MU = MU + M_Gens[i][1]
- if M_Gens[i][2] == true then SU = SU + M_Gens[i][1] end
- end
- local De = false
- if SU > R_PU then De = true end
- repeat
- for i = 1, #M_Gens do
- 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
- end
- until (SU >= R_PU and De == false) or (De == true and SU <= R_PU)
- for i = 1, #M_Gens do
- local Act
- if M_Gens[i][3] == true then Act = "startup" else Act = "shutdown" end
- GenComms("ID",M_Gens[i][4],Act)
- end
- end
- local function SecuritySys(item,action)
- end
- local function OpenBoxFile(ID)
- local file = fs.open("PwrBoxes/"..ID,"r")--client,cords,R_PU,payment
- local Data = textutils.unserialize(file.readAll())
- file.close()
- return Data
- end
- local function ChargeBox()
- end
- local function CheckPayment(ID)
- local Res
- local Data = OpenBoxFile(ID)
- if Data[4] <= 0 then Res = false else Res = true end
- return Res
- end
- local function PwrBoxAct(e)
- local D = textutils.unserialize(e)--unitID,request,data
- local Response = {}
- if D[2] == "on" then
- BoxStatus[D[1]][3] = CheckPayment(D[1])
- Response = {BoxStatus[D[1]][3]}
- elseif D[2] == "off" then
- BoxStatus[D[1]][3] = false
- Response = {true}
- elseif D[2] == "payment" then
- end
- return textutils.serialize(Response)
- end
- local function SecAct(e)
- local D = textutils.unserialize(e)
- return false
- end
- local function Loop()
- WM.open(P_LCh)
- Modem.open(S_LCh)
- if fs.exists("restart.txt") then fs.delete("restart.txt") end
- while true do
- local a, b, c, d, e = os.pullEvent("modem_message")
- if b == P_LCh then
- WM.transmit(P_SCh,P_LCh,PwrBoxAct(e))
- elseif b == S_LCh then
- Modem.transmit(S_SCh,S_LCh,SecAct(e))
- end
- PowerManager()
- end
- end
- function Startup()
- Logger("system boot start")
- Logger("enforcing lockdown")
- SecuritySys("lockdown",true)
- Logger("power target set to 5")
- PowerManager()
- Logger("requst sent: activating power box breakers")
- PowerBoxNet("close")
- Logger("boot complete")
- Loop()
- end
- Startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement