Advertisement
Flawedspirit

Energy Monitor Client 2.1.5

Mar 28th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. local cellTable = {}
  2. local isTE2 = false
  3. local linesDrawn = 0
  4.  
  5. function detect()
  6.   for per, side in pairs(rs.getSides()) do
  7.     if peripheral.isPresent(side) then
  8.       if peripheral.getType(side) == "monitor" then
  9.         mon = peripheral.wrap(side)
  10.       elseif peripheral.getType(side) == "modem" then
  11.         modem = side
  12.       end
  13.     end
  14.   end
  15.   return mon, modem
  16. end
  17.  
  18. function updateCells(_cell, _energy, _max, _state, _isTE2)
  19.   cellTable[_cell] = {energy = _energy, cellMax = _max, state = _state, isTE2 = _isTE2}
  20. end
  21.  
  22. function getEnergyStats()
  23.   currentSum = 0
  24.   maxSum = 0
  25.  
  26.   for cell, data in pairs(cellTable) do
  27.     currentSum = currentSum + data.energy
  28.     maxSum = maxSum + data.cellMax
  29.   end
  30. end
  31.  
  32. function updateStatus(cell, energy, state)
  33.   if state == "EMPTY" then
  34.     mon.write("Cell "..cell.." : ")
  35.     mon.setTextColor(colors.red)
  36.     mon.write("EMPTY")
  37.   elseif state == "FULL" then
  38.     mon.write("Cell "..cell.." : ")
  39.     mon.setTextColor(colors.lightBlue)
  40.     mon.write("FULL")
  41.   elseif state == "CHARGING" then
  42.     mon.write("Cell "..cell.." : ")
  43.     mon.setTextColor(colors.lime)
  44.     mon.write(energy.."%")
  45.   elseif state == "DISCHARGING" then
  46.     mon.write("Cell "..cell.." : ")
  47.     mon.setTextColor(colors.orange)
  48.     mon.write(energy.."%")
  49.   elseif state == "NO CHANGE" then
  50.     mon.write("Cell "..cell.." : "..energy.."%")
  51.   else
  52.     mon.setBackgroundColor(colors.red)
  53.     mon.setTextColor(colors.black)
  54.     mon.write("Cell "..cell.." : ERROR")
  55.   end
  56.   mon.setBackgroundColor(colors.black)
  57. end
  58.  
  59. function round(num, places)
  60.   local multi = 10 ^ (places or 0)
  61.   return math.floor(num * multi + 0.5) / multi
  62. end
  63.  
  64. function getPercentage(current, max)
  65.   return (current / max) * 100
  66. end
  67.  
  68. function drawBar(val, x, y)
  69.   local percent
  70.   local modifier
  71.   local color
  72.  
  73.   --If the monitor is 39 cols wide or wider
  74.   --then extend the bar using a static modifier
  75.   if monWidth >= 61 then
  76.     percent = val * 0.025
  77.     modifier = 4
  78.   elseif monWidth >= 39 then
  79.     percent = val * 0.05
  80.     modifier = 2
  81.   elseif monWidth < 39 then
  82.     percent = val * 0.1
  83.     modifier = 1
  84.   end
  85.  
  86.   --Determine what color the bar should be
  87.   if percent >= 10 / modifier then
  88.     color = colors.lightBlue
  89.   elseif percent >= 8 / modifier then
  90.     color = colors.cyan
  91.   elseif percent >= 6 / modifier then
  92.     color = colors.lime
  93.   elseif percent >= 4 / modifier then
  94.     color = colors.yellow
  95.   elseif percent >= 2 / modifier then
  96.     color = colors.orange
  97.   else
  98.     color = colors.red
  99.   end
  100.  
  101.   --Print bar "brackets"
  102.   mon.setTextColor(colors.white)
  103.   mon.setCursorPos(x, y)
  104.   mon.write("[")
  105.  
  106.   --Raise modifier to the power of 2 if
  107.   --monitor is large enough
  108.   --[Monitor size to segment size:]
  109.   --3m wide: 10% per segment
  110.   --4m wide: 5% per segment
  111.   --6m wide: 2.5% per segment
  112.   for i = 1, percent * (modifier ^ 2) do
  113.     mon.setTextColor(color)
  114.     mon.write("#")
  115.   end
  116.  
  117.   if monWidth >= 61 then
  118.     mon.setCursorPos(x + 41, y)
  119.   elseif monWidth >= 39 then
  120.     mon.setCursorPos(x + 21, y)
  121.   elseif monWidth < 39 then
  122.     mon.setCursorPos(x + 11, y)
  123.   end
  124.   mon.setTextColor(colors.white)
  125.   mon.write("]")  
  126. end
  127.  
  128. --Draw all on-screen data
  129. function draw(serverID)
  130.   local header = "-- Energy Storage Status --"
  131.  
  132.   --Energy summary
  133.   mon.setBackgroundColor(colors.black)
  134.   mon.setTextColor(colors.white)
  135.   mon.clear()
  136.   mon.setCursorPos((monWidth / 2) - (#header / 2) + 1, 1)
  137.   mon.setBackgroundColor(colors.gray)
  138.   mon.clearLine()
  139.   mon.write(header)
  140.   mon.setCursorPos(1, 3)
  141.   mon.setBackgroundColor(colors.black)
  142.   mon.write("System Energy   : ")
  143.  
  144.   if isTE2 then
  145.     mon.write(currentSum.." MJ")
  146.   else
  147.     mon.write(currentSum.." RF")
  148.   end
  149.   mon.setCursorPos(1, 4)
  150.   mon.write("Max Sys. Energy : ")
  151.  
  152.   if isTE2 then
  153.     mon.write(maxSum.." MJ")
  154.   else
  155.     mon.write(maxSum.." RF")
  156.   end
  157.   mon.setCursorPos(1, 6)
  158.  
  159.   --Render individual cell percentages,
  160.   --states, and colored bars
  161.   for cell, data in pairs(cellTable) do    
  162.     mon.setCursorPos(1, 5 + cell)
  163.     print(serverID.." -> Cell "..cell.." : "..data.energy.."/"..data.cellMax.." ["..data.state.."]")
  164.     linesDrawn = linesDrawn + 1
  165.     updateStatus(cell, round(getPercentage(data.energy, data.cellMax), 2), data.state)
  166.     --mon.setCursorPos(17, 5 + cell)
  167.     drawBar(round(getPercentage(data.energy, data.cellMax), 2), 17, 5 + cell)
  168.   end
  169.  
  170.   --Bottom bar on monitor
  171.   mon.setCursorPos(1, monHeight)
  172.   mon.setBackgroundColor(colors.gray)
  173.   mon.clearLine()
  174.   --Reset button
  175.   mon.setBackgroundColor(colors.red)
  176.   mon.write(" RESET ")
  177.   --Client ID
  178.   local cidText = "Client ID: "..os.getComputerID()
  179.  
  180.   mon.setCursorPos(monWidth - #cidText, monHeight)
  181.   mon.setBackgroundColor(colors.gray)
  182.   mon.write(cidText)
  183. end
  184.  
  185. function init()
  186.   --Print client ID reminder in terminal
  187.   term.clear()
  188.   term.setCursorPos(1, 1)
  189.   term.write("This client's ID is "..os.getComputerID()..".")
  190.   term.setCursorPos(1, 3)
  191. end
  192.  
  193. term.clear()
  194. mon, modem = detect()
  195. --Get initial monitor size
  196. monWidth, monHeight = mon.getSize()
  197. rednet.open(modem)
  198. init()
  199.  
  200. --End program if screen is too small
  201. --Screen must be 3 blocks wide by
  202. --two blocks tall to display any info
  203. if monWidth < 29 or monHeight < 12 then
  204.   error("Monitor must be 3 blocks wide by 2 blocks high to display data.")
  205. end
  206.  
  207. local idle = "No Signal"
  208. mon.setCursorPos((monWidth / 2) - (#idle / 2) + 1, (monHeight / 2) + 1)
  209. mon.setBackgroundColor(colors.red)
  210. mon.write(idle)
  211.  
  212. while true do
  213.   event, p1, p2, p3 = os.pullEvent()
  214.  
  215.   if event == "monitor_touch" then
  216.     if p2 <= 7 and p3 == monHeight then
  217.       term.setTextColor(colors.red)
  218.       print("Reboot event requested by user.")
  219.       os.sleep(2)
  220.       os.reboot()
  221.     end
  222.   end
  223.   local sender, message, dist = rednet.receive()
  224.   local data = textutils.unserialize(message)
  225.   --Get monitor size in case size changes while client is running
  226.   monWidth, monHeight = mon.getSize()
  227.  
  228.   updateCells(data._cellNum, data._current, data._max, data._state, data._isTE2)
  229.   getEnergyStats()
  230.   isTE2 = data._isTE2
  231.   draw(sender)
  232.  
  233.   if linesDrawn > 8 then
  234.     term.clear()
  235.     linesDrawn = 0
  236.     init()
  237.   end
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement