Advertisement
XLT_Frank

Multiple Energy Cell Monitor v1.1

Aug 31st, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. modem = peripheral.wrap("right")
  2. monitor = peripheral.wrap("top")
  3. monitor.setTextScale(1)
  4.  
  5. lastStoredEnergy = 0
  6. energyRate = 0
  7. timeEmpty = 0
  8.  
  9. function comma_value(n) -- credit http://richard.warburton.it
  10.     local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  11.     return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
  12. end
  13.  
  14. function SecondsToClock(nSeconds)
  15.     if nSeconds == 0 then
  16.         return "00:00:00"
  17.     elseif nSeconds > -math.huge and nSeconds < math.huge then
  18.         numHours = math.floor(nSeconds/3600)
  19.         numMins = string.format("%.2d", math.floor(nSeconds/60 - (numHours*60)))
  20.         numSecs = string.format("%.2d", math.floor(nSeconds - numHours*3600 - numMins *60))
  21.         return numHours..":"..numMins..":"..numSecs
  22.     else
  23.         return "n/a"
  24.     end
  25. end
  26.  
  27. while(true) do
  28.     numCells = 0
  29.     attachedCells = {}
  30.  
  31.     for index,value in pairs(modem.getNamesRemote()) do
  32.         if string.find(value,'energycell',1,true) then
  33.             numCells = numCells + 1
  34.             attachedCells[numCells] = value
  35.         end
  36.     end
  37.  
  38.     for i = 1,12 do
  39.         storedEnergy = 0
  40.         maxStoredEnergy = 0
  41.         for index,value in pairs(attachedCells) do
  42.             storedEnergy = storedEnergy + modem.callRemote(value, "getEnergyStored","")
  43.             maxStoredEnergy = maxStoredEnergy + modem.callRemote(value, "getMaxEnergyStored","")
  44.         end
  45.         percentStored = math.floor(100*storedEnergy/maxStoredEnergy)
  46.         energyRate = lastStoredEnergy - storedEnergy
  47.         timeEmpty = math.floor(storedEnergy/energyRate)
  48.         monitor.clear()
  49.         monitor.setCursorPos(2,1)
  50.         monitor.write("Connected Cells: "..comma_value(numCells))
  51.         monitor.setCursorPos(2,3)
  52.         monitor.write("Stored Energy: "..comma_value(storedEnergy).." ("..percentStored.."%)")
  53.         monitor.setCursorPos(2,4)
  54.         monitor.write("Energy Use: "..comma_value(energyRate).."/s")
  55.         monitor.setCursorPos(2,6)
  56.         monitor.write("Time to Empty: "..SecondsToClock(timeEmpty))
  57.         print(i)
  58.         os.sleep(1)
  59.         lastStoredEnergy = storedEnergy
  60.     end
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement