Yamazouki

YamaCorpMainController

Apr 22nd, 2022 (edited)
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | None | 0 0
  1. -- YamaCorpMainController, 2022, Yamazouki
  2. REDNET_SIDE = "top"
  3. MACHINE_CONTROLLER_ID = 3
  4. DISPLAY_CONTROLLER_ID = 2
  5. REFRESH_RATE = 5
  6. TIMEOUT_DELAY = 5
  7.  
  8. rednet.open(REDNET_SIDE)
  9.  
  10. function generateErrorMessage (str)
  11.     return {"Error :", 0xFF0000, str, 0xFF0000}
  12. end
  13.  
  14. while true do
  15.     msg = {}
  16.     msg.protocol = "get_info"
  17.     rednet.send(MACHINE_CONTROLLER_ID, textutils.serializeJSON(msg), "YamaCorp")
  18.  
  19.     rnID, rnMsg, rnProto = rednet.receive("YamaCorp", TIMEOUT_DELAY)
  20.  
  21.     msg = {}
  22.     msg.protocol = "display"
  23.     msg.body = {}
  24.  
  25.     if rnID == nil then
  26.         print("No signals since " .. TIMEOUT_DELAY .. "s")
  27.         table.insert(msg.body, generateErrorMessage("no signal from machine controller"))
  28.     else
  29.         if (rnProto == "YamaCorp") then
  30.             write("from " .. rnID .. " : ")
  31.             rnMsg =  textutils.unserializeJSON(rnMsg)
  32.            
  33.             if (rnMsg == nil) then
  34.                 print("error serializing JSON")
  35.                 table.insert(msg.body, generateErrorMessage("JSON error"))
  36.             else
  37.                 proto = rnMsg.protocol
  38.                 body = rnMsg.body
  39.  
  40.                 print(proto)
  41.  
  42.                 if (proto == 'global') then
  43.                     if (body == 'restart') then
  44.                         os.reboot()
  45.                     end
  46.                 elseif (proto == 'info') then
  47.                     msg.body = body
  48.                 end
  49.             end
  50.         end
  51.     end
  52.    
  53.     rednet.send(DISPLAY_CONTROLLER_ID, textutils.serializeJSON(msg), "YamaCorp")
  54.     sleep(REFRESH_RATE)
  55. end
Add Comment
Please, Sign In to add comment