MrKey2b

energy-monitor.lua

Apr 1st, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 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  -  [email protected]
  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 = 75
  39.  
  40. -- step: run interval in seconds.
  41. local step = 1
  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 = "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. ------
  63. Autorun
  64.  
  65. if fs.exists('/lib/json.lua') == false then
  66.   shell.execute('wget -fq "https://pastebin.com/raw/4UWzY19u" "/lib/json.lua"')
  67. end
  68. local json = require("json")
  69.  
  70.   local shrc = io.open("/home/.shrc","a")
  71.   shrc:write("/home/energy-monitor.lua")
  72.   shrc:close()
  73.   computer.shutdown(true)
  74. end
  75.  
  76. --------------------------------------------------------------------------------
  77. -- Prints the current energy values
  78. --------------------------------------------------------------------------------
  79. function printEnergy (core, term)
  80.   local gpu = term.gpu()
  81.   local w, h = gpu.getResolution()
  82.   gpu.fill(startX,startY,w,h, " ")
  83.  
  84.   printHeader (term, startX, startY, name, w)
  85.  
  86.   -- empty gauge
  87.   gpu.setBackground(0xFF0000)
  88.   gpu.fill (startX, startY+1, w, gaugeHeight, " ")
  89.   local currentWidth = math.ceil (core:getLastPercentStored() * w / 100)
  90.  
  91.   -- stored gauge
  92.   gpu.setBackground(0x00FF00)
  93.   gpu.fill (startX, startY+1, currentWidth, gaugeHeight, " ")
  94.  
  95.   -- percent on moving gauge
  96.   gpu.setForeground(0x000000)
  97.   local textX = currentWidth + 2
  98.   local textBackground = 0xFF0000
  99.   if currentWidth >= (w - 10) then
  100.     textBackground = 0x00FF00
  101.     textX = w - 16
  102.   end
  103.   gpu.setBackground(textBackground)
  104.   term.setCursor(textX, math.ceil (gaugeHeight/2)+startY)
  105.   term.write (string.format("%.2f", core:getLastPercentStored()) .. "%")
  106.  
  107.   -- current stored energy / max energy
  108.   gpu.setBackground(0x000000)
  109.   gpu.setForeground(0xFFFFFF)
  110.   term.setCursor (startX, startY + gaugeHeight + 1)
  111.   term.write (formatNumber(core:getLastEnergyStored()) .. " / " ..
  112.     formatNumber(core:getMaxEnergyStored()) .. "   ("..
  113.     string.format("%.2f", core:getLastPercentStored()) .. "%)")
  114. end
  115.  
  116. --------------------------------------------------------------------------------
  117. -- Prints the change on energy levels
  118. --------------------------------------------------------------------------------
  119. function printEnergyChange (change, term, histogram)
  120.   histogram:render(change)
  121. end
  122.  
  123. --------------------------------------------------------------------------------
  124. -- Updates values on screen continuously (until interrupted)
  125. --------------------------------------------------------------------------------
  126. function run ()
  127.   local core, term, component =
  128.     init (storageName, "graphic", debug, initDelay, threshold)
  129.  
  130.   local histogram = Histogram:create(term, startX, startY + gaugeHeight + 2)
  131.  
  132.   if core == nil then
  133.     return 1
  134.   end
  135.  
  136.   term.clear()
  137.   os.sleep (step)
  138.   while isRunning() do
  139.     local energyChange = core:getEnergyChange()
  140.     printEnergy (core, term)
  141.     printEnergyChange (energyChange, term, histogram)
  142.     checkThreshold (term, core, startX, side)
  143.     os.sleep(step)
  144.   end
  145.  
  146.   return 0
  147. end
  148.  
  149.  
  150. --------------------------------------------------------------------------------
  151. -- Main Program
  152. --------------------------------------------------------------------------------
  153. local exitCode = run ()
  154. if exitCode ~= 0 then
  155.   print ("An internal error occurred. Exit code ".. exitCode)
  156. else
  157.   print ("Exiting... [exit code: "+exitCode+"]")
  158.  
  159.     end
Advertisement
Add Comment
Please, Sign In to add comment