Advertisement
joebodo

powerMonitor.lua

Apr 22nd, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  1. vos.loadApi('core.api')
  2. vos.loadApi('relay.api')
  3.  
  4. local net = peripheral.wrap("bottom")
  5. Relay.find()
  6.  
  7. local percent
  8. local banks = 0
  9. local rsside = 'top'
  10. local lastReading
  11.  
  12. rs.setOutput(rsside, false)
  13.  
  14. local function getCells()
  15.   local cells = {}
  16.   local names = peripheral.getNames()
  17.  
  18.   for _,name in pairs(names) do
  19.     if string.find(name, "energycell") then
  20.       table.insert(cells, name)
  21.     end
  22.   end
  23.   return cells
  24. end
  25.  
  26. Event.addHandler('heartbeat', function()
  27.   local total = 0
  28.   local max = 0
  29.   banks = 0
  30.   for k, cellName in pairs(getCells()) do
  31.     local cell = peripheral.wrap(cellName)
  32.     local cellStored = peripheral.call(cellName, 'getEnergyStored', "unknown")
  33.     local cellMax = peripheral.call(cellName, 'getMaxEnergyStored', "unknown")
  34.     total = total + cellStored
  35.     max = max + cellMax
  36.     if cellStored < cellMax then
  37.       banks = banks + 1
  38.     end
  39.   end
  40.  
  41.   percent = math.floor(total * 100 / max)
  42.   print(total .. " (" .. percent .. "%)")
  43.  
  44.   if percent < 90 then
  45.     rs.setOutput(rsside, true)
  46.   elseif percent >= 95 then
  47.     rs.setOutput(rsside, false)
  48.   end
  49.  
  50.   local meterColor = colors.green
  51.   if percent < 75 then
  52.     meterColor = colors.red
  53.   elseif percent < 90 then
  54.     meterColor = colors.orange
  55.   end
  56.  
  57.   Relay.send('panelUpdate', {
  58.     uid = 'power',
  59.     type = 'meter',
  60.     text = 'Power',
  61.     value = percent,
  62.     color = meterColor
  63.   })
  64.   Relay.send('panelUpdate', {
  65.     uid = 'dynamos',
  66.     text = 'Dynamos',
  67.     type = 'radio',
  68.     active = rs.getOutput(rsside),
  69.   })
  70.   if lastReading then
  71.     local diff = math.floor((total - lastReading) / 600)
  72. print('diff: ' .. diff)
  73.     Relay.send('panelUpdate', {
  74.       uid = 'powerDiff',
  75.       type = 'text',
  76.       text = string.format('Power Usage: %+d RF/t', diff),
  77.     })
  78.   end
  79.   lastReading = total
  80. end)
  81.  
  82. Event.addHandler('char', function()
  83.   Event.exitPullEvents()
  84. end)
  85.  
  86. os.queueEvent('heartbeat')
  87. Event.heartbeat(30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement