Advertisement
jhnphm

InvMonitor

Feb 24th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. local modem = peripheral.wrap("right")
  2. local dispModem = peripheral.wrap("left")
  3. local monitor = peripheral.wrap("back")
  4. local inventory
  5.  
  6. master = false
  7.  
  8. modem.setDefaultChannel(1)
  9. modem.setListening(1,true)
  10. if dispModem ~= nil then
  11.     dispModem.setDefaultChannel(1)
  12.     dispModem.setListening(1,true)
  13. end
  14. function newLine()
  15.   local cX,cY= monitor.getCursorPos()
  16.   monitor.setCursorPos(1,cY+1)
  17. end
  18.  
  19.  
  20. local netListen = function()
  21.     local msg = {Command = nil, Payload = nil}
  22.     local id
  23.     while true do
  24.         local inventoryKeys = {}
  25.         local event,side,id,channel,srmsg = os.pullEvent("lan_message")
  26.         local msg = textutils.unserialize(srmsg)
  27.         if type(msg) == "table" then
  28.             print(msg.Command)
  29.             if msg.Command == "invSensor.report" then
  30.                 inventory = msg.Payload.Inventory
  31.                 for k,v in pairs(inventory) do
  32.                     table.insert(inventoryKeys,k)
  33.                 end
  34.                 table.sort(inventoryKeys, function(a,b) return a>b end)
  35.             elseif msg.Command == "invDisplay.continuation" then
  36.                 inventory = msg.Payload.Inventory
  37.                 inventoryKeys = msg.Payload.InventoryKeys
  38.                 --print(textutils.serialize(inventory))
  39.                 --print(textutils.serialize(inventoryKeys))
  40.             end
  41.             if msg.Command == "invSensor.report" or msg.Command == "invDisplay.continuation" then
  42.                 monitor.clear()
  43.                 monitor.setCursorPos(1,1)
  44.                 local xSz,ySz = monitor.getSize()
  45.                 local count = 0
  46.                 while #inventoryKeys ~= 0 do
  47.                     local k = table.remove(inventoryKeys)
  48.                     monitor.write(k..': '..inventory[k])
  49.                     newLine()
  50.                     count = count+1
  51.                     if count > ySz-1 then
  52.                         if dispModem ~= nil then
  53.                             dispModem.send(textutils.serialize({Command="invDisplay.continuation",Payload={Inventory=inventory,InventoryKeys = inventoryKeys}}))
  54.                         end
  55.                         break
  56.                     end
  57.                 end
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. local pollSensor = function()
  64.     os.startTimer(0)
  65.     while true do
  66.         os.pullEvent("timer")
  67.         print("Requesting Inventory Info...")
  68.         modem.send(textutils.serialize({Command="invDisplay.reportReq",Payload=nil}))
  69.         os.startTimer(10)
  70.     end
  71. end
  72.  
  73. if master then
  74.     parallel.waitForAll(pollSensor,netListen)
  75. else
  76.     netListen()
  77. end
  78.  
  79. -- vim: set ft=lua:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement