Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Parte logica del programma di gestione fabbrica
  2.  
  3. -- Variabili Globali
  4. local gpuutils = require("screen_utils")
  5. local component = require("component")
  6. local term = require("term")
  7. local event = require("event")
  8. local serialization = require("serialization")
  9.  
  10. local modem = component.modem
  11. local running = true
  12.  
  13. local charges = {}
  14. local currentChargeX = 30
  15. local currentChargeY = 10
  16. --------------------------------------
  17.  
  18. -- Move this to graphic
  19. function DrawBattery(x,y,charge,name)
  20.  
  21.   fullH = 4
  22.  
  23.   DrawRectangle(x,y,3,fullH,0x990000)
  24.   height = fullH * charge
  25.   y = y+(fullH-height)
  26.   DrawRectangle(x,y,3,height,0x009900)
  27.  
  28. end
  29.  
  30. function GetDataTable()
  31.  
  32. end
  33.  
  34. local eventHandler = setmetatable({}, { __index = function() return unknownEvent end })
  35.  
  36. function unknownEvent()
  37.  
  38. end
  39.  
  40. function eventHandler.touch(id, x, y)
  41.   print("Touch "..x..","..y)
  42.   running = false;
  43. end
  44.  
  45. -- Modem Messages
  46.  
  47. function AddBatteryToList( address )
  48.     newCharge = {}
  49.     newCharge["x"] = currentChargeX;
  50.     newCharge["y"] = currentChargeY;
  51.     currentChargeX = currentChargeX + 15
  52.     charges[address] = newCharge  
  53. end
  54.  
  55. function eventHandler.modem_message(_,_,from,port,_,message)
  56.     if (_) then
  57.       value = serialization.unserialize(_)
  58.       if (value["id"] == "charge_report") then
  59.  
  60.         if (not charges[value["address"]]) then
  61.           AddBatteryToList( value["address"] )
  62.         end
  63.  
  64.         bat = charges[value["address"]]
  65.         DrawBattery(bat["x"], bat["y"], value["charge"], "Charge")
  66.  
  67.       end
  68.     end
  69. end
  70.  
  71. -------------------------------------
  72.  
  73. function HandleEvent(eventId, ...)
  74.  
  75. --  print("event id:"..eventId)
  76.   if (eventId) then
  77.     eventHandler[eventId](...)
  78.   end
  79. end
  80.  
  81. modem.open(20)
  82. print(modem.isOpen(20))
  83.  
  84. while(running) do
  85.   HandleEvent(event.pull())
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement