Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. -- gpu.setResolution(160,50)
  4. local gui = require("gui")
  5. local charts = require("charts")
  6. local event = require("event")
  7. local serialization = require("serialization")
  8. local modem = component.modem
  9.  
  10. -- Begin GUI stuff
  11.  
  12. gui.checkVersion(2,5)
  13.  
  14. local prgName = "reactor1"
  15. local version = "v1.0"
  16. local w, h = gpu.getResolution()
  17.  
  18. -- Begin: Callbacks
  19. local function exitButtonCallback(guiID, id)
  20. local result = gui.getYesNo("", "Do you really want to exit?", "")
  21. if result == true then
  22. gui.exit()
  23. end
  24. gui.displayGui(mainGui)
  25. refresh()
  26. end
  27. -- End: Callbacks
  28.  
  29. -- Begin: Menu definitions
  30. mainGui = gui.newGui(1, 1, 159, 49, true, nil, 0xcccccc, 0x22ff22)
  31. reactor1_label_title = gui.newLabel(mainGui, 1, 1, "Reactor 1", 0xc0c0c0, 0x0, 7)
  32. reactor1_label_status = gui.newLabel(mainGui, 52, 1, "Disabled", 0xc0c0c0, 0x0, 7)
  33. reactor1_label_energy = gui.newLabel(mainGui, 1, 3, "Energy", 0xc0c0c0, 0x0, 7)
  34. reactor1_text_energy = gui.newLabel(mainGui, 52, 4, "0.0", 0xc0c0c0, 0x0, 7)
  35. reactor1_bar_energy = gui.newProgress(mainGui, 1, 4, 50, 100, 1, nil, false)
  36. reactor1_label_heat = gui.newLabel(mainGui, 1, 6, "Heat", 0xc0c0c0, 0x0, 7)
  37. reactor1_text_heat = gui.newLabel(mainGui, 52, 7, "0.0", 0xc0c0c0, 0x0, 7)
  38. reactor1_bar_heat = gui.newProgress(mainGui, 1, 7, 50, 100, 1, nil, false)
  39.  
  40. reactor2_label_title = gui.newLabel(mainGui, 81, 1, "Reactor 2", 0xc0c0c0, 0x0, 7)
  41. reactor2_label_status = gui.newLabel(mainGui, 132, 1, "Disabled", 0xc0c0c0, 0x0, 7)
  42. reactor2_label_energy = gui.newLabel(mainGui, 81, 3, "Energy", 0xc0c0c0, 0x0, 7)
  43. reactor2_text_energy = gui.newLabel(mainGui, 132, 4, "0.0", 0xc0c0c0, 0x0, 7)
  44. reactor2_bar_energy = gui.newProgress(mainGui, 81, 4, 50, 100, 1, nil, false)
  45. reactor2_label_heat = gui.newLabel(mainGui, 81, 6, "Heat", 0xc0c0c0, 0x0, 7)
  46. reactor2_text_heat = gui.newLabel(mainGui, 132, 7, "0.0", 0xc0c0c0, 0x0, 7)
  47. reactor2_bar_heat = gui.newProgress(mainGui, 81, 7, 50, 100, 1, nil, false)
  48.  
  49.  
  50. battery1_label_title = gui.newLabel(mainGui, 1, 20, "Battery Bank 1", 0xc0c0c0, 0)
  51. battery1_label_max = gui.newLabel(mainGui, 1, 22, "Capacity: ", 0xc0c0c0, 0)
  52. battery1_text_max = gui.newLabel(mainGui, 14, 22, "0.0 RF", 0xc0c0c0, 0)
  53. battery1_label_current = gui.newLabel(mainGui, 30, 22, "Current: ", 0xc0c0c0, 0)
  54. battery1_text_current = gui.newLabel(mainGui, 45, 22, "0.0 RF", 0xc0c0c0, 0)
  55. battery1_label_percent = gui.newLabel(mainGui, 60, 22, "Usage: ", 0xc0c0c0, 0)
  56. battery1_text_percent = gui.newLabel(mainGui, 70, 22, "0.0 %", 0xc0c0c0, 0)
  57. battery1_bar_energy = gui.newProgress(mainGui, 1, 24, 100, 100, 1, nil, false)
  58.  
  59. --battery histogram
  60. battery1_hist = charts.Histogram {
  61. max = 100,
  62. align = charts.sides.RIGHT,
  63. colorFunc = function(index, norm, value, self, container)
  64. return 0x20ff20
  65. end
  66. }
  67. battery1_container = charts.Container {
  68. x=4,
  69. y=28,
  70. width=140,
  71. height=20,
  72. payload = battery1_hist,
  73. }
  74.  
  75.  
  76. exitButton = gui.newButton(mainGui, 143, 47, "exit", exitButtonCallback)
  77. -- End: Menu definitions
  78.  
  79. gui.clearScreen()
  80. gui.setTop("Reactor Management Program")
  81. gui.setBottom("")
  82.  
  83. local stat = 0
  84.  
  85. -- Begin Reactor Handling
  86.  
  87. local function reactor1_update(msg)
  88.  
  89. local update = msg.data
  90.  
  91. --Update gui
  92. if update.active then
  93. gui.setText(mainGui, reactor1_label_status, "Enabled ")
  94. else
  95. gui.setText(mainGui, reactor1_label_status, "Disabled")
  96. end
  97. gui.setText(mainGui, reactor1_text_energy, string.format("%.2f", update.energy))
  98. gui.setValue(mainGui, reactor1_bar_energy, update.energy)
  99. gui.setText(mainGui, reactor1_text_heat, string.format("%.2f", update.heat))
  100. gui.setValue(mainGui, reactor1_bar_heat, update.heat)
  101. end
  102.  
  103. local function reactor2_update(msg)
  104.  
  105. local update = msg.data
  106.  
  107. --Update gui
  108. if update.active then
  109. gui.setText(mainGui, reactor2_label_status, "Enabled ")
  110. else
  111. gui.setText(mainGui, reactor2_label_status, "Disabled")
  112. end
  113. gui.setText(mainGui, reactor2_text_energy, string.format("%.2f", update.energy))
  114. gui.setValue(mainGui, reactor2_bar_energy, update.energy)
  115. gui.setText(mainGui, reactor2_text_heat, string.format("%.2f", update.heat))
  116. gui.setValue(mainGui, reactor2_bar_heat, update.heat)
  117. end
  118.  
  119. local function battery1_update(msg)
  120. local update = msg.data
  121.  
  122. if update == nil then
  123. return
  124. end
  125.  
  126. gui.setText(mainGui, battery1_text_max, string.format("%.2f kRF", update.energy_max/1000))
  127. gui.setText(mainGui, battery1_text_current, string.format("%.2f kRF", update.energy/1000))
  128. gui.setText(mainGui, battery1_text_percent, string.format("%.2f ", (update.energy/update.energy_max)*100))
  129. gui.setValue(mainGui, battery1_bar_energy, (update.energy/update.energy_max)*100)
  130. table.insert(battery1_hist.values, (update.energy/update.energy_max)*100)
  131. battery1_container:draw()
  132.  
  133. end
  134.  
  135. --Begin Event Handling
  136.  
  137. ems_handlers = {}
  138. ems_handlers['reactor1'] = reactor1_update
  139. ems_handlers['reactor2'] = reactor2_update
  140. ems_handlers['battery1'] = battery1_update
  141.  
  142. local function handleModem(dst, src, port, dist, frame, ...)
  143. msg = serialization.unserialize(frame)
  144.  
  145. --TODO validation, error checking, etc
  146. ems_handlers[msg.id](msg)
  147. end
  148.  
  149. local function handleEvent(eventID, ...)
  150. if eventID == "modem_message" then
  151. handleModem(...)
  152. end
  153. end
  154.  
  155.  
  156. -- Main loop
  157. modem.open(10)
  158. local stat = 0
  159. while true do
  160. stat = stat + 1
  161. gui.runGui(mainGui)
  162. handleEvent(event.pull())
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement