Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --sys2_root
- local CONST_SERVICES_CONFIG = "services.config"
- local CONST_DOOR_PWD = "n145vdrt1v5f"
- os.loadAPI("sys/syslib")
- --syslib end
- if not fs.exists(CONST_SERVICES_CONFIG) then
- fs.open(CONST_SERVICES_CONFIG, "w").close()
- end
- function writeConfig(path, data)
- local txt = ""
- for key, value in pairs(data) do
- txt = txt .. key .. "=" .. value .. "\n"
- end
- local f = fs.open(path, "w")
- f.write(txt)
- f.close()
- end
- function readConfig(path)
- local config = {}
- local f = fs.open(path, "r")
- while true do
- local line = f.readLine()
- if line == nil then
- return config
- end
- local i, j = string.find(line, "=")
- if i ~= nil then
- config[string.sub(line, 1, i - 1)] = string.sub(line, i + 1, -1)
- end
- end
- end
- local services = readConfig("services.config")
- for label, exId in pairs(services) do
- print("loaded label Service: label=" .. label .. ", id=" .. exId)
- end
- local bootUnits = {}
- local serviceUnits = {}
- local turtles = {}
- function reconnectAll()
- syslib.log("root", " ")
- syslib.log("root", "reconnecting all devices...")
- print()
- print("reconnecting network")
- bootUnits = {}
- serviceUnits = {}
- rednet.broadcast("@bootUnit:initialise")
- rednet.broadcast("@turtle:initialise")
- end
- rednet.open("back")
- local rootId = "1fJmTiyA"
- local bootUnitId = "N25Gefez"
- local serviceUnitId = "X8Y9J7SA"
- local serviceTerminalId = "Pn2GagtY"
- local turtleUnitId = "6Bg1ZDv1"
- local f = fs.open("startup", "w")
- f.write("shell.run(\"delete\",\"unitHandler\")\n")
- f.write("shell.run(\"pastebin\",\"get\",\"" .. rootId .. "\",\"unitHandler\")\n")
- f.write("shell.run(\"unitHandler\")\n")
- f.close()
- print("<root>")
- reconnectAll()
- function main()
- local id, msg = syslib.receiveRednet()
- local tag, pts = syslib.getMsgData(msg)
- if tag == "@root" then
- if pts[1] == "identifyBootUnit" then
- local label = pts[2]
- local ids = {}
- local pts = syslib.split(pts[3], "|")
- for i, p in pairs(pts) do
- table.insert(ids, tonumber(p))
- end
- bootUnits[id] = { id = id, label = label, serviceIds = ids }
- syslib.log("root", "registeredBootUnit: " .. id .. " - " .. label)
- print("bootUnit: ", id)
- syslib.sendRednet(id, "@bootUnit:action,setStartup," .. bootUnitId)
- elseif pts[1] == "identifyTurtleUnit" then
- local label = pts[2]
- turtles[id] = { id = id, label = label, free = true }
- syslib.log("root", "registeredTurtleUnit: " .. id .. " - " .. label)
- print("turtleUnit: ", id)
- syslib.sendRednet(id, "@turtle:action,setStartup," .. turtleUnitId)
- elseif pts[1] == "identifyServiceUnit" then
- local label = pts[2]
- serviceUnits[id] = { id = id, label = label, free = true }
- local valid
- for bid, data in pairs(bootUnits) do
- for i, sid in pairs(data["serviceIds"]) do
- if sid == id then
- valid = data["id"]
- break
- end
- end
- if valid then break end
- end
- if valid then
- syslib.log("root", "registeredServiceUnit: " .. id .. " - " .. label)
- print("serviceUnit: ", id)
- syslib.sendRednet(id, "@serviceUnit:action,setStartup," .. serviceUnitId)
- syslib.sendRednet(id, "@serviceUnit:setRootData," .. valid)
- local label=syslib.getLabelParts(label)
- print("label: ",label)
- if label == "service:terminal" then
- syslib.sendRednet(id, "@serviceUnit:action,executePastebin," .. serviceTerminalId)
- serviceUnits[id]["free"] = false
- else
- for name, exId in pairs(services) do
- if name == label then
- syslib.sendRednet(id, "@serviceUnit:action,executePastebin," .. exId)
- end
- end
- end
- else
- syslib.log("root", "unconnected serviceunit: ", id, ", label: ", label, " - make sure the service unit is connected to a running rootUnit and try the command 'reconnect'")
- print("unconnected serviceunit: ", id, ", label: ", label, " - make sure the service unit is connected to a running rootUnit and try the command 'reconnect'")
- end
- elseif pts[1] == "answer" then
- if pts[2] == "executionTerminated" then
- if serviceUnits[id] ~= nil then
- serviceUnits[id]["free"] = true
- end
- elseif pts[2] == "renamedUnit" then
- if serviceUnits[id] ~= nil then
- serviceUnits[id]["label"] = pts[3]
- end
- elseif pts[2] == "renamedTurtle" then
- if turtles[id] ~= nil then
- turtles[id]["label"] = pts[3]
- end
- else print("unknown message>answer: ", msg)
- end
- elseif pts[1] == "command" then
- if pts[2] == "reboot" then
- if pts[3] ~= nil then
- if pts[3] == "unit" then
- local label = pts[4]
- for bid, data in pairs(bootUnits) do
- for i, sid in pairs(data["serviceIds"]) do
- if serviceUnits[sid] ~= null and serviceUnits[sid]["label"] == label then
- syslib.sendRednet(bid, "@bootUnit:reboot," .. sid .. "," .. label)
- break
- end
- end
- end
- elseif pts[3] == "turtle" then
- local label = pts[4]
- for id, data in pairs(turtles) do
- if data["label"] == label then
- syslib.sendRednet(id,"@turtle:reboot,"..id)
- end
- end
- end
- else
- rednet.broadcast("@bootUnit:reboot")
- for i = 1, 3 do
- syslib.log("root", "rebooting in " .. (4 - i) .. "...")
- print("rebooting in " .. (4 - i) .. "...")
- sleep(1)
- end
- os.reboot()
- end
- --elseif pts[2] == "reconnect" then reconnectAll()
- elseif pts[2] == "unit" then
- if pts[3] == "list" then
- for bid, data in pairs(bootUnits) do
- print(data["label"])
- syslib.log("terminal", data["label"] .. " [bootUnit]")
- for i, sid1 in pairs(data["serviceIds"]) do
- for sid, sdata in pairs(serviceUnits) do
- if sid1 == sid then
- print("hu", sdata["label"])
- syslib.log("terminal", " " .. sdata["label"] .. " [serviceUnit]")
- break
- end
- end
- end
- end
- else print("unknown message>command>unit: ", msg)
- end
- elseif pts[2] == "label" then
- if pts[3] == "renameUnit" then
- for id, data in pairs(serviceUnits) do
- if data["label"] == pts[4] then
- syslib.sendRednet(id, "@serviceUnit:action,setLabel," .. pts[5])
- break
- end
- end
- --todo not found message
- elseif pts[3] == "renameTurtle" then
- for id, data in pairs(turtles) do
- if data["label"] == pts[4] then
- syslib.sendRednet(id, "@turtle:action,setLabel," .. pts[5])
- end
- end
- else print("unknown message>command>label: ", msg)
- end
- elseif pts[2] == "service" then
- if pts[3] == "register" then
- local exId = pts[4]
- local label = pts[5]
- services[label] = exId
- syslib.log("root", "successfully registered service '" .. exId .. "' to label '" .. label .. "'")
- writeConfig(CONST_SERVICES_CONFIG, services)
- elseif pts[3] == "remove" then
- local label = pts[4]
- local c = 1
- local success = false
- for l, id in pairs(services) do
- if l == label then
- services[label] = nil
- success = true
- syslib.log("root", "successfully removed service from label '" .. label .. "'")
- break
- end
- c = c + 1
- end
- if not success then
- syslib.log("root", "there are no services registered to label '" .. label .. "'")
- end
- writeConfig(CONST_SERVICES_CONFIG, services)
- else print("unknown message>command>service: ", msg)
- end
- else print("unknown message>command: ", msg)
- end
- else print("unknown message: ", msg)
- end
- end
- end
- while true do
- local status, err = pcall(main)
- if err == "Terminated" then
- break
- elseif not status then
- print("error thrown: " .. err)
- rednet.broadcast("@err:" .. os.getComputerID() .. "," .. os.getComputerLabel() .. ",root")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement