Yamazouki

YamaCorpDisplayController

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