Advertisement
melzneni

sys2_root

Feb 10th, 2020
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.11 KB | None | 0 0
  1. --sys2_root
  2.  
  3. local CONST_SERVICES_CONFIG = "services.config"
  4. local CONST_DOOR_PWD = "n145vdrt1v5f"
  5.  
  6. os.loadAPI("sys/syslib")
  7.  
  8. --syslib end
  9.  
  10. if not fs.exists(CONST_SERVICES_CONFIG) then
  11.     fs.open(CONST_SERVICES_CONFIG, "w").close()
  12. end
  13.  
  14. function writeConfig(path, data)
  15.     local txt = ""
  16.     for key, value in pairs(data) do
  17.         txt = txt .. key .. "=" .. value .. "\n"
  18.     end
  19.     local f = fs.open(path, "w")
  20.     f.write(txt)
  21.     f.close()
  22. end
  23.  
  24. function readConfig(path)
  25.     local config = {}
  26.     local f = fs.open(path, "r")
  27.     while true do
  28.         local line = f.readLine()
  29.         if line == nil then
  30.             return config
  31.         end
  32.         local i, j = string.find(line, "=")
  33.         if i ~= nil then
  34.             config[string.sub(line, 1, i - 1)] = string.sub(line, i + 1, -1)
  35.         end
  36.     end
  37. end
  38.  
  39.  
  40. local services = readConfig("services.config")
  41.  
  42. for label, exId in pairs(services) do
  43.     print("loaded label Service: label=" .. label .. ", id=" .. exId)
  44. end
  45.  
  46. local bootUnits = {}
  47. local serviceUnits = {}
  48. local turtles = {}
  49. function reconnectAll()
  50.     syslib.log("root", " ")
  51.     syslib.log("root", "reconnecting all devices...")
  52.     print()
  53.     print("reconnecting network")
  54.     bootUnits = {}
  55.     serviceUnits = {}
  56.     rednet.broadcast("@bootUnit:initialise")
  57.     rednet.broadcast("@turtle:initialise")
  58. end
  59.  
  60. rednet.open("back")
  61.  
  62. local rootId = "1fJmTiyA"
  63. local bootUnitId = "N25Gefez"
  64. local serviceUnitId = "X8Y9J7SA"
  65. local serviceTerminalId = "Pn2GagtY"
  66. local turtleUnitId = "6Bg1ZDv1"
  67.  
  68. local f = fs.open("startup", "w")
  69. f.write("shell.run(\"delete\",\"unitHandler\")\n")
  70. f.write("shell.run(\"pastebin\",\"get\",\"" .. rootId .. "\",\"unitHandler\")\n")
  71. f.write("shell.run(\"unitHandler\")\n")
  72. f.close()
  73.  
  74. print("<root>")
  75.  
  76. reconnectAll()
  77.  
  78. function main()
  79.     local id, msg = syslib.receiveRednet()
  80.     local tag, pts = syslib.getMsgData(msg)
  81.     if tag == "@root" then
  82.         if pts[1] == "identifyBootUnit" then
  83.             local label = pts[2]
  84.             local ids = {}
  85.             local pts = syslib.split(pts[3], "|")
  86.             for i, p in pairs(pts) do
  87.                 table.insert(ids, tonumber(p))
  88.             end
  89.             bootUnits[id] = { id = id, label = label, serviceIds = ids }
  90.             syslib.log("root", "registeredBootUnit: " .. id .. " - " .. label)
  91.             print("bootUnit: ", id)
  92.             syslib.sendRednet(id, "@bootUnit:action,setStartup," .. bootUnitId)
  93.         elseif pts[1] == "identifyTurtleUnit" then
  94.             local label = pts[2]
  95.             turtles[id] = { id = id, label = label, free = true }
  96.             syslib.log("root", "registeredTurtleUnit: " .. id .. " - " .. label)
  97.             print("turtleUnit: ", id)
  98.             syslib.sendRednet(id, "@turtle:action,setStartup," .. turtleUnitId)
  99.         elseif pts[1] == "identifyServiceUnit" then
  100.             local label = pts[2]
  101.             serviceUnits[id] = { id = id, label = label, free = true }
  102.             local valid
  103.             for bid, data in pairs(bootUnits) do
  104.                 for i, sid in pairs(data["serviceIds"]) do
  105.                     if sid == id then
  106.                         valid = data["id"]
  107.                         break
  108.                     end
  109.                 end
  110.                 if valid then break end
  111.             end
  112.             if valid then
  113.                 syslib.log("root", "registeredServiceUnit: " .. id .. " - " .. label)
  114.                 print("serviceUnit: ", id)
  115.                 syslib.sendRednet(id, "@serviceUnit:action,setStartup," .. serviceUnitId)
  116.                 syslib.sendRednet(id, "@serviceUnit:setRootData," .. valid)
  117.                 local label=syslib.getLabelParts(label)
  118.                 print("label: ",label)
  119.                 if label == "service:terminal" then
  120.                     syslib.sendRednet(id, "@serviceUnit:action,executePastebin," .. serviceTerminalId)
  121.                     serviceUnits[id]["free"] = false
  122.                 else
  123.                     for name, exId in pairs(services) do
  124.                         if name == label then
  125.                             syslib.sendRednet(id, "@serviceUnit:action,executePastebin," .. exId)
  126.                         end
  127.                     end
  128.                 end
  129.             else
  130.                 syslib.log("root", "unconnected serviceunit: ", id, ", label: ", label, " - make sure the service unit is connected to a running rootUnit and try the command 'reconnect'")
  131.                 print("unconnected serviceunit: ", id, ", label: ", label, " - make sure the service unit is connected to a running rootUnit and try the command 'reconnect'")
  132.             end
  133.         elseif pts[1] == "answer" then
  134.             if pts[2] == "executionTerminated" then
  135.                 if serviceUnits[id] ~= nil then
  136.                     serviceUnits[id]["free"] = true
  137.                 end
  138.             elseif pts[2] == "renamedUnit" then
  139.                 if serviceUnits[id] ~= nil then
  140.                     serviceUnits[id]["label"] = pts[3]
  141.                 end
  142.             elseif pts[2] == "renamedTurtle" then
  143.                 if turtles[id] ~= nil then
  144.                     turtles[id]["label"] = pts[3]
  145.                 end
  146.             else print("unknown message>answer: ", msg)
  147.             end
  148.         elseif pts[1] == "command" then
  149.             if pts[2] == "reboot" then
  150.                 if pts[3] ~= nil then
  151.                     if pts[3] == "unit" then
  152.                         local label = pts[4]
  153.                         for bid, data in pairs(bootUnits) do
  154.                             for i, sid in pairs(data["serviceIds"]) do
  155.                                 if serviceUnits[sid] ~= null and serviceUnits[sid]["label"] == label then
  156.                                     syslib.sendRednet(bid, "@bootUnit:reboot," .. sid .. "," .. label)
  157.                                     break
  158.                                 end
  159.                             end
  160.                         end
  161.                     elseif pts[3] == "turtle" then
  162.                         local label = pts[4]
  163.                         for id, data in pairs(turtles) do
  164.                             if data["label"] == label then
  165.                                 syslib.sendRednet(id,"@turtle:reboot,"..id)
  166.                             end
  167.                         end
  168.                     end
  169.                 else
  170.                     rednet.broadcast("@bootUnit:reboot")
  171.                     for i = 1, 3 do
  172.                         syslib.log("root", "rebooting in " .. (4 - i) .. "...")
  173.                         print("rebooting in " .. (4 - i) .. "...")
  174.                         sleep(1)
  175.                     end
  176.                     os.reboot()
  177.                 end
  178.                 --elseif pts[2] == "reconnect" then reconnectAll()
  179.             elseif pts[2] == "unit" then
  180.                 if pts[3] == "list" then
  181.                     for bid, data in pairs(bootUnits) do
  182.                         print(data["label"])
  183.                         syslib.log("terminal", data["label"] .. " [bootUnit]")
  184.                         for i, sid1 in pairs(data["serviceIds"]) do
  185.                             for sid, sdata in pairs(serviceUnits) do
  186.                                 if sid1 == sid then
  187.                                     print("hu", sdata["label"])
  188.                                     syslib.log("terminal", "    " .. sdata["label"] .. " [serviceUnit]")
  189.                                     break
  190.                                 end
  191.                             end
  192.                         end
  193.                     end
  194.                 else print("unknown message>command>unit: ", msg)
  195.                 end
  196.             elseif pts[2] == "label" then
  197.                 if pts[3] == "renameUnit" then
  198.                     for id, data in pairs(serviceUnits) do
  199.                         if data["label"] == pts[4] then
  200.                             syslib.sendRednet(id, "@serviceUnit:action,setLabel," .. pts[5])
  201.                             break
  202.                         end
  203.                     end
  204.                     --todo not found message
  205.                 elseif pts[3] == "renameTurtle" then
  206.                     for id, data in pairs(turtles) do
  207.                         if data["label"] == pts[4] then
  208.                             syslib.sendRednet(id, "@turtle:action,setLabel," .. pts[5])
  209.                         end
  210.                     end
  211.                 else print("unknown message>command>label: ", msg)
  212.                 end
  213.             elseif pts[2] == "service" then
  214.                 if pts[3] == "register" then
  215.                     local exId = pts[4]
  216.                     local label = pts[5]
  217.                     services[label] = exId
  218.                     syslib.log("root", "successfully registered service '" .. exId .. "' to label '" .. label .. "'")
  219.                     writeConfig(CONST_SERVICES_CONFIG, services)
  220.                 elseif pts[3] == "remove" then
  221.                     local label = pts[4]
  222.                     local c = 1
  223.                     local success = false
  224.                     for l, id in pairs(services) do
  225.                         if l == label then
  226.                             services[label] = nil
  227.                             success = true
  228.                             syslib.log("root", "successfully removed service from label '" .. label .. "'")
  229.                             break
  230.                         end
  231.                         c = c + 1
  232.                     end
  233.                     if not success then
  234.                         syslib.log("root", "there are no services registered to label '" .. label .. "'")
  235.                     end
  236.                     writeConfig(CONST_SERVICES_CONFIG, services)
  237.                 else print("unknown message>command>service: ", msg)
  238.                 end
  239.             else print("unknown message>command: ", msg)
  240.             end
  241.  
  242.         else print("unknown  message: ", msg)
  243.         end
  244.     end
  245. end
  246.  
  247. while true do
  248.     local status, err = pcall(main)
  249.     if err == "Terminated" then
  250.         break
  251.     elseif not status then
  252.         print("error thrown: " .. err)
  253.         rednet.broadcast("@err:" .. os.getComputerID() .. "," .. os.getComputerLabel() .. ",root")
  254.     end
  255. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement