ftibo

logMonitor.lua

Jul 24th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. os.loadAPI('apis.lua')
  2.  
  3. Peripheral.wrap('wireless_modem')
  4.  
  5. local ids = { }
  6.  
  7. Logger.disable()
  8.  
  9. local terminal = UI.term
  10.  
  11. if Peripheral.isPresent('openperipheral_glassesbridge') then
  12. terminal = UI.Glasses({
  13. height = 40,
  14. width = 64,
  15. textScale = .5,
  16. backgroundOpacity = .75
  17. })
  18. terminal:clear()
  19. elseif Peripheral.isPresent('monitor') then
  20. terminal = UI.Device({
  21. deviceType = 'monitor',
  22. textScale = .5
  23. })
  24. end
  25.  
  26. terminal:clear()
  27. local window = UI.Window({ parent = terminal })
  28.  
  29. function getClient(id)
  30. if not ids[id] then
  31. ids[id] = {
  32. titleBar = UI.TitleBar({ title = 'ID: ' .. id, parent = window }),
  33. scrollingText = UI.ScrollingText({ parent = window })
  34. }
  35. local clientCount = Util.size(ids)
  36. local clientHeight = math.floor((terminal.height - clientCount) / clientCount)
  37. window:clear()
  38. local y = 1
  39. for k,v in pairs(ids) do
  40. v.titleBar.y = y
  41. y = y + 1
  42. v.scrollingText.height = clientHeight
  43. v.scrollingText.y = y
  44. y = y + clientHeight
  45. v.scrollingText:clear()
  46.  
  47. v.titleBar:draw()
  48. v.scrollingText:draw()
  49. end
  50. end
  51. return ids[id]
  52. end
  53.  
  54. Message.addHandler('log', function(h, id, msg)
  55. local client = getClient(id)
  56. client.scrollingText:write(string.format('%d %s', os.time()*1000, msg.contents))
  57. end)
  58.  
  59. Event.addHandler('monitor_touch', function()
  60. terminal:clear()
  61. ids = { }
  62. end)
  63.  
  64. Event.addHandler('char', function()
  65. Event.exitPullEvents()
  66. end)
  67.  
  68. Message.addHandler('logClient', function(h, id)
  69. Message.send(id, 'logServer')
  70. end)
  71.  
  72. Event.pullEvents()
  73. UI.term:reset()
Add Comment
Please, Sign In to add comment