agot137

monitor_client.lua

Apr 25th, 2026
3,157
0
Never
4
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. local MODEM_SIDE = "bottom"
  2. local MONITOR_SIDE = "left"
  3. local MASTER_PROTOCOL = "rtm_master_state"
  4. local REQUEST_PROTOCOL = "rtm_master_query"
  5. local CLIENT_NAME = "dispatcher_monitor"
  6. local native = term.current()
  7.  
  8. local monitor = nil
  9. local masterId = nil
  10. local blink = false
  11. local lastState = nil
  12. local lastSeen = 0
  13.  
  14. local DEFAULT_MARKERS = {
  15. green = { 26, 4 },
  16. blue = { 42, 4 },
  17. red = { 26, 19 },
  18. purple = { 42, 19 },
  19. }
  20.  
  21. local SEGMENTS = {
  22. top_left_outer = { points = { {4,6}, {9,6}, {14,6}, {19,6} } },
  23. top_left_inner = { points = { {21,6}, {25,6}, {29,6}, {33,6} } },
  24. top_right_inner = { points = { {39,6}, {43,6}, {47,6}, {51,6} } },
  25. top_right_outer = { points = { {53,6}, {58,6}, {63,6}, {68,6} } },
  26. bot_left_outer = { points = { {4,17}, {9,17}, {14,17}, {19,17} } },
  27. bot_left_inner = { points = { {21,17}, {25,17}, {29,17}, {33,17} } },
  28. bot_right_inner = { points = { {39,17}, {43,17}, {47,17}, {51,17} } },
  29. bot_right_outer = { points = { {53,17}, {58,17}, {63,17}, {68,17} } },
  30. green_diag = { points = { {19,6}, {23,8}, {27,10}, {31,12}, {35,13} } },
  31. red_diag = { points = { {21,16}, {25,14}, {29,12}, {33,10}, {36,9} } },
  32. blue_diag = { points = { {39,10}, {43,9}, {47,7}, {51,6} } },
  33. purple_diag = { points = { {39,12}, {43,14}, {47,16}, {51,18} } },
  34. }
  35.  
  36. local function openNet()
  37. if peripheral.getType(MODEM_SIDE) ~= "modem" then error("Nincs modem a bottom oldalon") end
  38. if not rednet.isOpen(MODEM_SIDE) then rednet.open(MODEM_SIDE) end
  39. end
  40.  
  41. local function initMonitor()
  42. if peripheral.getType(MONITOR_SIDE) ~= "monitor" then error("Nincs monitor a left oldalon") end
  43. monitor = peripheral.wrap(MONITOR_SIDE)
  44. monitor.setTextScale(0.5)
  45. end
  46.  
  47. local function drawLineTerm(target, x1, y1, x2, y2, color)
  48. term.redirect(target)
  49. paintutils.drawLine(x1, y1, x2, y2, color)
  50. end
  51.  
  52. local function drawPolyline(target, points, color)
  53. for i = 1, #points - 1 do
  54. drawLineTerm(target, points[i][1], points[i][2], points[i + 1][1], points[i + 1][2], color)
  55. end
  56. end
  57.  
  58. local function routeByKey(key)
  59. if not lastState or not lastState.routes then return nil end
  60. for _, route in ipairs(lastState.routes) do
  61. if route.key == key then return route end
  62. end
  63. return nil
  64. end
  65.  
  66. local function hasAlert(route)
  67. if not route then return true end
  68. return route.missing or route.offline
  69. end
  70.  
  71. local function routeColor(route)
  72. if hasAlert(route) and blink then return colors.red end
  73. return route and route.color or colors.gray
  74. end
  75.  
  76. local function drawSegment(name, color)
  77. local seg = SEGMENTS[name]
  78. if seg then drawPolyline(monitor, seg.points, color) end
  79. end
  80.  
  81. local function drawMap()
  82. local green = routeByKey("green")
  83. local blue = routeByKey("blue")
  84. local red = routeByKey("red")
  85. local purple = routeByKey("purple")
  86.  
  87. monitor.setBackgroundColor(colors.black)
  88. monitor.clear()
  89.  
  90. drawSegment("top_left_outer", colors.gray)
  91. drawSegment("top_right_outer", colors.gray)
  92. drawSegment("bot_left_outer", colors.gray)
  93. drawSegment("bot_right_outer", colors.gray)
  94.  
  95. drawSegment("top_left_inner", (green and not green.active) and routeColor(green) or colors.gray)
  96. drawSegment("top_right_inner", (blue and not blue.active) and routeColor(blue) or colors.gray)
  97. drawSegment("bot_left_inner", (red and not red.active) and routeColor(red) or colors.gray)
  98. drawSegment("bot_right_inner", (purple and not purple.active) and routeColor(purple) or colors.gray)
  99.  
  100. drawSegment("green_diag", (green and green.active) and routeColor(green) or colors.gray)
  101. drawSegment("blue_diag", (blue and blue.active) and routeColor(blue) or colors.gray)
  102. drawSegment("red_diag", (red and red.active) and routeColor(red) or colors.gray)
  103. drawSegment("purple_diag", (purple and purple.active) and routeColor(purple) or colors.gray)
  104. end
  105.  
  106. local function drawMarkers()
  107. if not lastState or not lastState.routes then return end
  108. for i, route in ipairs(lastState.routes) do
  109. local marker = route.marker or DEFAULT_MARKERS[route.key]
  110. if marker then
  111. monitor.setCursorPos(marker[1], marker[2])
  112. monitor.setBackgroundColor(colors.black)
  113. if hasAlert(route) and blink then
  114. monitor.setTextColor(colors.red)
  115. elseif lastState.selected == i then
  116. monitor.setTextColor(colors.white)
  117. else
  118. monitor.setTextColor(route.color)
  119. end
  120. monitor.write(route.active and "[*]" or "[ ]")
  121. end
  122. end
  123. end
  124.  
  125. local function drawLegend()
  126. if not lastState or not lastState.routes then return end
  127. local _, h = monitor.getSize()
  128. local startY = math.max(1, h - #lastState.routes - 1)
  129. for i, route in ipairs(lastState.routes) do
  130. monitor.setCursorPos(2, startY + i - 1)
  131. monitor.setBackgroundColor(colors.black)
  132. if hasAlert(route) and blink then
  133. monitor.setTextColor(colors.red)
  134. elseif lastState.selected == i then
  135. monitor.setTextColor(colors.white)
  136. else
  137. monitor.setTextColor(route.color)
  138. end
  139. local stateText = route.active and "KERESZT" or "EGYENES"
  140. local line = (route.active and "[*] " or "[ ] ") .. route.name .. " ID:" .. tostring(route.slaveId or "-") .. " " .. stateText
  141. if route.missing then
  142. line = line .. " NINCS ID"
  143. elseif route.offline then
  144. line = line .. " OFFLINE"
  145. end
  146. monitor.write(line)
  147. end
  148. end
  149.  
  150. local function drawStatus()
  151. monitor.setCursorPos(2, 22)
  152. monitor.setBackgroundColor(colors.black)
  153. monitor.setTextColor(colors.white)
  154. if masterId then
  155. monitor.write("MASTER ID: " .. masterId .. " LAST: " .. math.floor(os.clock() - lastSeen) .. "s")
  156. else
  157. monitor.write("MASTER KERESES...")
  158. end
  159. end
  160.  
  161. local function redraw()
  162. term.redirect(monitor)
  163. drawMap()
  164. drawMarkers()
  165. drawLegend()
  166. drawStatus()
  167. term.redirect(native)
  168. end
  169.  
  170. local function findMaster()
  171. local found = rednet.lookup(MASTER_PROTOCOL, "rtm_master", 2)
  172. if found then masterId = found end
  173. end
  174.  
  175. local function requestState()
  176. if not masterId then
  177. findMaster()
  178. return
  179. end
  180. rednet.send(masterId, { cmd = "get_panel_state", client = os.getComputerID() }, REQUEST_PROTOCOL)
  181. local senderId, msg, protocol = rednet.receive(REQUEST_PROTOCOL, 0.8)
  182. if senderId == masterId and protocol == REQUEST_PROTOCOL and type(msg) == "table" and msg.routes then
  183. lastState = msg
  184. lastSeen = os.clock()
  185. else
  186. masterId = nil
  187. end
  188. end
  189.  
  190. openNet()
  191. initMonitor()
  192. redraw()
  193.  
  194. while true do
  195. requestState()
  196. blink = not blink
  197. redraw()
  198. sleep(1)
  199. end
  200.  
Advertisement
Comments
  • User was banned
    • User was banned
  • User was banned
    • User was banned
Add Comment
Please, Sign In to add comment