Advertisement
neuroticfox

E-Monitor 24h

Jul 9th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.02 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. -- energy-monitor v0.2 A program to monitor a Draconic Evolution Energy Core.
  3. -- Copyright (C) 2017 by Luisau  -  luisau.mc@gmail.com
  4. --
  5. --    This program is free software: you can redistribute it and/or modify
  6. --    it under the terms of the GNU General Public License as published by
  7. --    the Free Software Foundation, either version 3 of the License, or
  8. --    (at your option) any later version.
  9. --    
  10. --    This program is distributed in the hope that it will be useful,
  11. --    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. --    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. --    GNU General Public License for more details.
  14. --
  15. --    You should have received a copy of the GNU General Public License
  16. --    along with this program.  If not, see <http://www.gnu.org/licenses/>
  17. --
  18. -- For Documentation and License see
  19. --  /usr/share/doc/energy-monitor/README.md
  20. --  
  21. -- Repo URL:
  22. -- https://github.com/OpenPrograms/luisau-Programs/tree/master/src/energy-monitor
  23. --------------------------------------------------------------------------------
  24.  
  25. require("energy-monitor-lib")
  26. local sides = require("sides")
  27.  
  28. --------------------------------------------------------------------------------
  29. -- Configuration
  30. --------------------------------------------------------------------------------
  31.  
  32. -- redstoneSide: side to emit a redstone signal when the threshold is reached
  33. -- nil to disable this feature (i.e.: local side = nil  )
  34. local side = sides.bottom
  35.  
  36. -- threshold: Minimum threshold required to emit a redstone signal
  37. -- Valid values: 1 to 100
  38. local threshold = 100
  39.  
  40. -- step: run interval in seconds.
  41. local step = 86400
  42.  
  43. -- debug: print aditional debug info (usually at start).
  44. local debug = true
  45.  
  46. -- name: Name of your Energy Core
  47. local name = "Draconic Energy Core"
  48.  
  49. --------------------------------------------------------------------------------
  50. -- Constants and Globals
  51. --------------------------------------------------------------------------------
  52. local storageName = "draconic_rf_storage"
  53. local initDelay = 1
  54. local startX = 1
  55. local startY = 1
  56. local gaugeHeight = 3
  57. local highPeak = 1
  58. local lowPeak = -1
  59. local lastX = 1
  60.  
  61. --------------------------------------------------------------------------------
  62. -- Prints the current energy values
  63. --------------------------------------------------------------------------------
  64. function printEnergy (core, term)
  65.   local gpu = term.gpu()
  66.   local w, h = gpu.getResolution()
  67.   gpu.fill(startX,startY,w,h, " ")
  68.  
  69.   printHeader (term, startX, startY, name, w)
  70.  
  71.   -- empty gauge
  72.   gpu.setBackground(0xbf00ff)
  73.   gpu.fill (startX, startY+1, w, gaugeHeight, " ")
  74.   local currentWidth = math.ceil (core:getLastPercentStored() * w / 100)
  75.  
  76.   -- stored gauge
  77.   gpu.setBackground(0xffae00)
  78.   gpu.fill (startX, startY+1, currentWidth, gaugeHeight, " ")
  79.  
  80.   -- percent on moving gauge
  81.   gpu.setForeground(0x000000)
  82.   local textX = currentWidth + 2
  83.   local textBackground = 0xbf00ff
  84.   if currentWidth >= (w - 10) then
  85.     textBackground = 0xbf00ff
  86.     textX = w - 16
  87.   end
  88.   gpu.setBackground(textBackground)
  89.   term.setCursor(textX, math.ceil (gaugeHeight/2)+startY)
  90.   term.write (string.format("%.2f", core:getLastPercentStored()) .. "%")
  91.  
  92.   -- current stored energy / max energy
  93.   gpu.setBackground(0x000000)
  94.   gpu.setForeground(0xFFFFFF)
  95.   term.setCursor (startX, startY + gaugeHeight + 1)
  96.   term.write (formatNumber(core:getLastEnergyStored()) .. " / " ..
  97.     formatNumber(core:getMaxEnergyStored()) .. "   ("..
  98.     string.format("%.2f", core:getLastPercentStored()) .. "%)")
  99. end
  100.  
  101. --------------------------------------------------------------------------------
  102. -- Prints the change on energy levels
  103. --------------------------------------------------------------------------------
  104. function printEnergyChange (change, term, histogram)
  105.   histogram:render(change)
  106. end
  107.  
  108. --------------------------------------------------------------------------------
  109. -- Updates values on screen continuously (until interrupted)
  110. --------------------------------------------------------------------------------
  111. function run ()
  112.   local core, term, component =
  113.     init (storageName, "graphic", debug, initDelay, threshold)
  114.  
  115.   local histogram = Histogram:create(term, startX, startY + gaugeHeight + 2)
  116.  
  117.   if core == nil then
  118.     return 1
  119.   end
  120.  
  121.   term.clear()
  122.   os.sleep (step)
  123.   while isRunning() do
  124.     local energyChange = core:getEnergyChange()
  125.     printEnergy (core, term)
  126.     printEnergyChange (energyChange, term, histogram)
  127.     checkThreshold (term, core, startX, side)
  128.     os.sleep(step)
  129.   end
  130.  
  131.   return 0
  132. end
  133.  
  134.  
  135. --------------------------------------------------------------------------------
  136. -- Main Program
  137. --------------------------------------------------------------------------------
  138. local exitCode = run ()
  139. if exitCode ~= 0 then
  140.   print ("An internal error occurred. Exit code ".. exitCode)
  141. else
  142.   print ("Exiting... [exit code: "+exitCode+"]")
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement