Advertisement
melzneni

sys2_boot_unit

Feb 10th, 2020
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | None | 0 0
  1. --sys2_boot_unit
  2. os.loadAPI("sys/syslib")
  3.  
  4. --syslib end
  5.  
  6. rednet.open("back")
  7.  
  8. function searchForUnits()
  9.     local units = {}
  10.     if peripheral.isPresent("left") and peripheral.getType("left") == "computer" then
  11.         units[peripheral.call("left", "getID")] = "left"
  12.     end
  13.     if peripheral.isPresent("right") and peripheral.getType("right") == "computer" then
  14.         units[peripheral.call("right", "getID")] = "right"
  15.     end
  16.     if peripheral.isPresent("top") and peripheral.getType("top") == "computer" then
  17.         units[peripheral.call("top", "getID")] = "top"
  18.     end
  19.     if peripheral.isPresent("bottom") and peripheral.getType("bottom") == "computer" then
  20.         units[peripheral.call("bottom", "getID")] = "bottom"
  21.     end
  22.     return units
  23. end
  24.  
  25. local connectedUnits = searchForUnits()
  26.  
  27. function identify()
  28.     local label = os.getComputerLabel()
  29.     if label == nil then
  30.         label = "<unnamed>"
  31.     end
  32.     local txtUnits = ""
  33.     for id, side in pairs(connectedUnits) do
  34.         if txtUnits ~= "" then
  35.             txtUnits = txtUnits .. "|"
  36.         end
  37.         txtUnits = txtUnits .. id
  38.     end
  39.     rednet.broadcast("@root:identifyBootUnit," .. label .. "," .. txtUnits)
  40. end
  41.  
  42. print("<bootUnit>")
  43.  
  44. identify()
  45. local rootId
  46.  
  47. function main()
  48.     local id, msg = syslib.receiveRednet()
  49.     local tag, pts = syslib.getMsgData(msg)
  50.     if tag == "@bootUnit" then
  51.         if pts[1] == "initialise" then
  52.             rootId = id
  53.             identify()
  54.             local connectedUnits = searchForUnits()
  55.             for id, side in pairs(connectedUnits) do
  56.                 syslib.sendRednet(id, "@serviceUnit:identifyToRoot," .. rootId)
  57.             end
  58.         elseif pts[1] == "reboot" then
  59.             if pts[2] == nil then
  60.                 local connectedUnits = searchForUnits()
  61.                 for id, side in pairs(connectedUnits) do
  62.                     peripheral.call(side, "reboot")
  63.                 end
  64.                 os.reboot()
  65.             else
  66.                 for id, side in pairs(connectedUnits) do
  67.                     if id == tonumber(pts[2]) then
  68.                         syslib.log("root", "rebooting unit " .. id .. " (label '" .. pts[3] .. "')")
  69.                         peripheral.call(side, "reboot")
  70.                     end
  71.                 end
  72.             end
  73.         elseif pts[1] == "action" then
  74.             if pts[2] == "setStartup" then
  75.                 shell.run("delete", "startup")
  76.                 local f = fs.open("startup", "w")
  77.                 f.write("shell.run(\"delete\",\"unitHandler\")\n")
  78.                 f.write("shell.run(\"pastebin\",\"get\",\"" .. pts[3] .. "\",\"unitHandler\")\n")
  79.                 f.write("shell.run(\"unitHandler\")\n")
  80.                 f.close()
  81.             else print("unknown message>action: ", msg)
  82.             end
  83.         else print("unknown message: ", msg)
  84.         end
  85.     end
  86. end
  87.  
  88. local cErr = 0
  89. while true do
  90.     local status, err = pcall(main)
  91.     if err == "Terminated" then
  92.         break
  93.     elseif not status then
  94.         print("error thrown: " .. err)
  95.         sleep(3)
  96.         cErr = cErr + 1
  97.         if cErr == 3 then
  98.             os.reboot()
  99.         end
  100.         rednet.broadcast("@err:" .. os.getComputerID() .. "," .. os.getComputerLabel() .. ",bootUnit")
  101.     end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement