Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- YamaCorpMachineController, 2022, Yamazouki
- REDNET_SIDE = "top"
- rednet.open(REDNET_SIDE)
- function generateErrorMessage (str)
- return {"Error :", 0xFF0000, str, 0xFF0000}
- end
- function generateInfoTable ()
- infos = {}
- for i, machine in pairs(machines) do
- table.insert(infos, {machine.name .. " :", 0x00FFFF, machine.info, 0xFF0000})
- end
- return infos
- end
- machines = {}
- while true do
- rnID, rnMsg, rnProto = rednet.receive("YamaCorp")
- if (rnProto == "YamaCorp") then
- write("from " .. rnID .. " : ")
- msg = {}
- msg.protocol = "info"
- msg.body = {}
- rnMsg = textutils.unserializeJSON(rnMsg)
- if (rnMsg == nil) then
- write("error serializing JSON")
- table.insert(msg.body, generateErrorMessage("JSON error"))
- else
- proto = rnMsg.protocol
- body = rnMsg.body
- print(proto)
- if (proto == 'global') then
- if (body == 'restart') then
- os.reboot()
- end
- elseif (proto == 'connect') then
- machine = {}
- machine.name = body
- machine.info = "connected"
- machines[tostring(rnID)] = machine
- msg.body = "OK"
- elseif (proto == 'update') then
- machines[tostring(rnID)].info = body
- msg.body = "OK"
- elseif (proto == 'get_info') then
- msg.body = generateInfoTable()
- end
- end
- end
- rednet.send(rnID, textutils.serializeJSON(msg), "YamaCorp")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement