Advertisement
kovakovi2000

CC: P++ TurtleMonitor

Apr 9th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.76 KB | None | 0 0
  1. os.loadAPI("ldb2")
  2. os.loadAPI("rnc")
  3.  
  4. local MaxWait = 2 * 60 * 1000 --mls
  5. local db = "turtle"
  6.  
  7. mon = peripheral.find("monitor")
  8. tms = peripheral.find("timeSensor")
  9.  
  10. function dtf(str)
  11.     return string.format("%.2d",str)
  12. end
  13. function formatTime(v_time)
  14.     return "[" ..
  15.            dtf(v_time.hour) .. ":" ..
  16.            dtf(v_time.minute) .. ":" ..
  17.            dtf(v_time.second) .. "]"
  18. end
  19.  
  20. local function heading(text)
  21.     w,h = mon.getSize()
  22.     mon.setCursorPos((w - string.len(text))/2+1, 1)
  23.     mon.setTextColor(colors.white)
  24.     mon.write(text)
  25. end
  26.  
  27. local function clearLine()
  28.     w,h = mon.getSize()
  29.     x,y = mon.getCursorPos()
  30.     mon.setCursorPos(1,y)
  31.     for i = 1, w do
  32.         mon.setCursorPos(i,y)
  33.         mon.write(" ")
  34.     end
  35.     mon.setCursorPos(x,y)
  36. end
  37.  
  38. mon.clear()
  39. heading("Turtle Monitor")
  40.  
  41. local started = 1
  42. local function WriteAll()
  43.     vlist = ldb2.list(db)
  44.     heading("Turtle Monitor")
  45.     for i=1, table.getn(vlist) do
  46.         tmp = ldb2.get(db, vlist[i])
  47.         if tmp.re == 1 or started == 1 then
  48.             mon.setCursorPos(1, i+2)
  49.             clearLine()
  50.             mon.setTextColor(colors.white)
  51.             mon.write(tmp.time .. " ")
  52.             if tmp.timestamp < tms.getTime() - MaxWait then
  53.                 mon.setTextColor(colors.red)
  54.                 mon.write(tmp.name)
  55.             else
  56.                 mon.setTextColor(colors.lime)
  57.                 mon.write(tmp.name)
  58.             end
  59.             mon.setTextColor(colors.white)
  60.             mon.write(" > ")
  61.             mon.setTextColor(colors.lightGray)
  62.            
  63.             mon.write("D:" .. tmp.depth .. " | " .. tmp.state .. " | " .. tmp.detail)
  64.             tmp.re = 0
  65.             ldb2.set(db, vlist[i], tmp)
  66.         end
  67.     end
  68.     started = 0
  69. end
  70.  
  71. local tm = tms.getTime()
  72. rnc.setTimeOut(1)
  73. while true do
  74.     msg = rnc.resive()
  75.     if msg ~= nil then
  76.         msg.time = formatTime(tms.getDate())
  77.         msg.timestamp = tms.getTime()
  78.         msg.re = 1
  79.         ldb2.set(db, msg.name, msg)
  80.     end
  81.     WriteAll()
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement