Advertisement
mathiaas

display_turtles

Apr 18th, 2024 (edited)
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. local function printTurtleData(monitor, turtle)
  2.     local x, y = monitor.getCursorPos()
  3.  
  4.     monitor.setTextColor(colors.white)
  5.     monitor.write("#" .. turtle.id .. " ")
  6.     x = x + 5
  7.    
  8.     monitor.setCursorPos(x, y)
  9.     monitor.setTextColor(colors.yellow)
  10.     monitor.write(turtle.label)
  11.     x = x + 11
  12.    
  13.     monitor.setCursorPos(x, y)
  14.     monitor.setTextColor(colors.white)
  15.     monitor.write(" (" .. turtle.coordinate_x .. "," .. turtle.coordinate_y .. "," .. turtle.coordinate_z .. ") ")
  16.     x = x + 20
  17.  
  18.     monitor.setCursorPos(x, y)
  19.     monitor.setTextColor(colors.lightBlue)
  20.     monitor.write(turtle.current_script .. " ")
  21.     x = x + 11
  22.  
  23.     monitor.setCursorPos(x, y)
  24.     if turtle.status == "Error" then
  25.         monitor.setBackgroundColor(colors.red)
  26.     elseif turtle.status == "Running" then
  27.         monitor.setBackgroundColor(colors.green)
  28.     elseif turtle.status == "Idle" then
  29.         monitor.setBackgroundColor(colors.orange)
  30.     else
  31.         monitor.setBackgroundColor(colors.lightBlue)
  32.     end
  33.     monitor.setTextColor(colors.white)
  34.     monitor.write(turtle.status)
  35.     monitor.setBackgroundColor(colors.black)
  36.     x = x + 8
  37.  
  38.     monitor.setCursorPos(x, y)
  39.     monitor.write(" Fuel: " .. turtle.fuel_lvl)
  40.    
  41.     monitor.setCursorPos(1, y + 1)
  42. end
  43.  
  44.  
  45. local function getMonitor()
  46.     local monitor = peripheral.find("monitor")
  47.     if not monitor then
  48.         error("No monitor found", 2)
  49.     end
  50.     return monitor
  51. end
  52.  
  53. local function getApiClient()
  54.     local apiClient = loadfile("api_client")()
  55.     if not apiClient then
  56.         error("Failed to load API client", 2)
  57.     end
  58.     return apiClient.new("http://35.228.225.211:8000")
  59. end
  60.  
  61.  
  62. local function refresh_monitor()
  63.     local client = getApiClient()
  64.     local monitor = getMonitor()
  65.     while true do
  66.         local turtles = client:get("/turtles")
  67.        
  68.         monitor.setBackgroundColor(colors.black)
  69.         monitor.clear()
  70.         monitor.setTextScale(0.9)
  71.        
  72.         monitor.setCursorPos(1, 1)
  73.         monitor.write("Turtle Information:")
  74.        
  75.         monitor.setCursorPos(1, 3)
  76.         for _, turtle in ipairs(turtles) do
  77.             printTurtleData(monitor, turtle)
  78.         end
  79.         sleep(30)
  80.     end
  81. end
  82.  
  83.  
  84. refresh_monitor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement