Advertisement
Yamazouki

YamaCorpMachineController

Apr 22nd, 2022 (edited)
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. -- YamaCorpMachineController, 2022, Yamazouki
  2. REDNET_SIDE = "top"
  3.  
  4. rednet.open(REDNET_SIDE)
  5.  
  6. function generateErrorMessage (str)
  7.     return {"Error :", 0xFF0000, str, 0xFF0000}
  8. end
  9.  
  10. function generateInfoTable ()
  11.     infos = {}
  12.  
  13.     for i, machine in pairs(machines) do
  14.         table.insert(infos, {machine.name .. " :", 0x00FFFF, machine.info, 0xFF0000})
  15.     end
  16.  
  17.     return infos
  18. end
  19.  
  20. machines = {}
  21.  
  22. while true do
  23.     rnID, rnMsg, rnProto = rednet.receive("YamaCorp")
  24.  
  25.     if (rnProto == "YamaCorp") then
  26.         write("from " .. rnID .. " : ")
  27.         msg = {}
  28.         msg.protocol = "info"
  29.         msg.body = {}
  30.  
  31.         rnMsg =  textutils.unserializeJSON(rnMsg)
  32.        
  33.         if (rnMsg == nil) then
  34.             write("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 == 'connect') then
  47.                 machine = {}
  48.                 machine.name = body
  49.                 machine.info = "connected"
  50.                 machines[tostring(rnID)] = machine
  51.                 msg.body = "OK"
  52.             elseif (proto == 'update') then
  53.                 machines[tostring(rnID)].info = body
  54.                 msg.body = "OK"
  55.             elseif (proto == 'get_info') then
  56.                 msg.body = generateInfoTable()
  57.             end
  58.         end
  59.     end
  60.  
  61.     rednet.send(rnID, textutils.serializeJSON(msg), "YamaCorp")
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement