Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.wrap("right")
- local dispModem = peripheral.wrap("left")
- local monitor = peripheral.wrap("back")
- local inventory
- master = false
- modem.setDefaultChannel(1)
- modem.setListening(1,true)
- if dispModem ~= nil then
- dispModem.setDefaultChannel(1)
- dispModem.setListening(1,true)
- end
- function newLine()
- local cX,cY= monitor.getCursorPos()
- monitor.setCursorPos(1,cY+1)
- end
- local netListen = function()
- local msg = {Command = nil, Payload = nil}
- local id
- while true do
- local inventoryKeys = {}
- local event,side,id,channel,srmsg = os.pullEvent("lan_message")
- local msg = textutils.unserialize(srmsg)
- if type(msg) == "table" then
- print(msg.Command)
- if msg.Command == "invSensor.report" then
- inventory = msg.Payload.Inventory
- for k,v in pairs(inventory) do
- table.insert(inventoryKeys,k)
- end
- table.sort(inventoryKeys, function(a,b) return a>b end)
- elseif msg.Command == "invDisplay.continuation" then
- inventory = msg.Payload.Inventory
- inventoryKeys = msg.Payload.InventoryKeys
- --print(textutils.serialize(inventory))
- --print(textutils.serialize(inventoryKeys))
- end
- if msg.Command == "invSensor.report" or msg.Command == "invDisplay.continuation" then
- monitor.clear()
- monitor.setCursorPos(1,1)
- local xSz,ySz = monitor.getSize()
- local count = 0
- while #inventoryKeys ~= 0 do
- local k = table.remove(inventoryKeys)
- monitor.write(k..': '..inventory[k])
- newLine()
- count = count+1
- if count > ySz-1 then
- if dispModem ~= nil then
- dispModem.send(textutils.serialize({Command="invDisplay.continuation",Payload={Inventory=inventory,InventoryKeys = inventoryKeys}}))
- end
- break
- end
- end
- end
- end
- end
- end
- local pollSensor = function()
- os.startTimer(0)
- while true do
- os.pullEvent("timer")
- print("Requesting Inventory Info...")
- modem.send(textutils.serialize({Command="invDisplay.reportReq",Payload=nil}))
- os.startTimer(10)
- end
- end
- if master then
- parallel.waitForAll(pollSensor,netListen)
- else
- netListen()
- end
- -- vim: set ft=lua:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement