Advertisement
SuPeRMiNoR3

power-monitor

Dec 8th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.54 KB | None | 0 0
  1. --Made by SuPeRMiNoR2
  2. version = "1.5.1"
  3. supported_config_version = "0.2"
  4.  
  5. local component = require("component")
  6. local term = require("term")
  7. local fs = require("filesystem")
  8. local gpu = component.gpu
  9. local wget = loadfile("/bin/wget.lua")
  10.  
  11. term.clear()
  12. print("Loading SuPeRMiNoR2's power-monitor version "..version)
  13.  
  14. if not component.isAvailable("internet") then
  15.   io.stderr:write("This program requires an internet card to run.")
  16.   return
  17. end
  18.  
  19. local internet = require("internet")
  20.  
  21. if fs.exists("/usr/lib") == false then
  22.   fs.makeDirectory("/usr/lib")
  23. end
  24.  
  25. if fs.exists("/usr/lib/superlib.lua") == false then
  26.   print("Downloading superlib to /usr/lib/superlib.lua")
  27.   wget("-fq","https://raw.githubusercontent.com/OpenPrograms/SuPeRMiNoR2-Programs/master/lib/superlib.lua", "/usr/lib/superlib.lua")
  28. end
  29.  
  30. local superlib = require("superlib")
  31.  
  32. print("Checking for updates...")
  33. superlib_version = superlib.getVersion()
  34. versions = superlib.checkVersions()
  35.  
  36. if versions == nil then print("Error checking versions.") end
  37. if versions ~= nil then
  38.   if versions["superlib"] ~= superlib_version then print("An update is available for superlib") os.sleep(2) end
  39.   if versions["powermonitor"] ~= version then print("An update is available for power-monitor") os.sleep(2) end
  40. end
  41.  
  42. if fs.exists("/usr/power-monitor.config") == false then
  43.   print("Downloading config file to /usr/power-monitor.config")
  44.   result = superlib.downloadFile("https://raw.githubusercontent.com/OpenPrograms/SuPeRMiNoR2-Programs/master/power-monitor/power-monitor.config", "/usr/power-monitor.config")
  45.   if result == false then
  46.     io.stderr:write("Error downloading the config file")
  47.   end
  48. end
  49.  
  50. local config = loadfile("/usr/power-monitor.config")()
  51. if config.config_version ~= supported_config_version then
  52.   print("Warning, The configuration file has a unsupported version number.")
  53.   print("You shoud save your old settings and delete the config file.")
  54.   print("The new version will be downloaded on next startup.")
  55.   print("If you do not do this, the program may not work.")
  56.   print("Waiting 15 seconds...")
  57.   os.sleep(15)
  58. end
  59.  
  60. --States
  61. glasses_connected = false
  62.  
  63. function percent_gen_db(powerdb, uid)
  64.   return superlib.pgen(powerdb[uid]["stored"], powerdb[uid]["capacity"], config.display_precision) .. "%"
  65. end
  66.  
  67. function readPower(proxy, ltype)
  68.   capacity = 0
  69.   stored = 0
  70.    
  71.   if ltype == 1 then
  72.       capacity = proxy.getCapacity()
  73.       stored = proxy.getStored()
  74.   end
  75.    
  76.   if ltype == 2 then
  77.       capacity = proxy.getMaxEnergyStored()
  78.       stored = proxy.getEnergyStored()
  79.   end
  80.  
  81.   return capacity, stored
  82. end
  83.  
  84. function getPower()
  85.   total_stored = 0
  86.   powerdb = {}
  87.   for uid in pairs(mlist) do
  88.       proxy = mlist[uid]["proxy"]
  89.       ltype = mlist[uid]["type"]
  90.       lname = mlist[uid]["name"]
  91.       c, s = readPower(proxy, ltype)
  92.       if s > c then --Stupid IC2 Bug, full ic2 blocks read over their capacity sometimes
  93.           s = c
  94.       end
  95.       total_stored = total_stored + s
  96.       powerdb[uid] = {capacity=c, stored=s, name=lname}
  97.   end  
  98.   powerdb["total"] = {capacity=total_capacity, stored=total_stored}
  99.   return powerdb
  100. end
  101.  
  102. function scan()
  103.     unit_id = 1
  104.     mlist = {}
  105.     total_capacity = 0
  106.     for address, ctype in component.list() do
  107.         for stype in pairs(supported_types) do
  108.             if stype == ctype then
  109.                 t = component.proxy(address)
  110.                 ltype = supported_types[stype]["type"]
  111.                 name = supported_types[stype]["name"]
  112.                 mlist[unit_id] = {address=address, proxy=t, type=ltype, name=name}
  113.                 unit_id = unit_id + 1
  114.                 c, s = readPower(t, ltype)
  115.                 total_capacity = total_capacity + c
  116.             end
  117.         end
  118.         if ctype == "glasses" and glasses_connected == false then
  119.             print("Detected glasses block, loading")
  120.             glasses = component.proxy(address)
  121.             glasses.removeAll()
  122.             glasses_text = glasses.addTextLabel()
  123.             glasses_text.setText("Loading.")
  124.             --os.sleep(0.8)
  125.             glasses_connected = true
  126.             glasses_text.setColor(.37, .83, .03)
  127.             glasses_text.setText("Loading..")
  128.             --os.sleep(0.8)
  129.             glasses_text.setPosition(2, 2)
  130.             glasses_text.setText("Loading...")
  131.             --os.sleep(0.8)
  132.             glasses_text.setScale(1)
  133.             glasses_text.setText("Loading....")
  134.             --os.sleep(1)
  135.             --print(glasses.getBindPlayers())
  136.         end
  137.     end
  138.     total_units = unit_id - 1
  139.     return mlist, total_capacity, total_units
  140. end
  141.  
  142. function buffer(text)
  143.     text_buffer = text_buffer .. text .. "\n"
  144. end
  145.  
  146. supported_types = {tile_thermalexpansion_cell_basic_name={type=2, name="Leadstone Cell"},
  147. tile_thermalexpansion_cell_hardened_name={type=2, name="Hardened Cell"},
  148. tile_thermalexpansion_cell_reinforced_name={type=2, name="Redstone Cell"},
  149. tile_thermalexpansion_cell_resonant_name={type=2, name="Resonant Cell"},
  150. mfsu={type=1, name="MFSU"}, mfe={type=1, name="MFE"}, cesu={type=1,
  151. name="CESU"}, batbox={type=1, name="BatBox"}, capacitor_bank={type=2, name="Capacitor Bank"}}  
  152.  
  153. --Program
  154. term.clear()
  155. print("Applying scale of " .. config.scale)
  156. w, h = gpu.maxResolution()
  157. gpu.setResolution(w / config.scale, h / config.scale)
  158.  
  159. print("Scanning for energy storage units")
  160. if glasses_connected then
  161.     glasses_text.setText("Scanning.")
  162. end
  163. mlist, total_capacity, total_units = scan()
  164.  
  165. if glasses_connected then
  166.     glasses_text.setText("Found "..total_units)
  167. end
  168.  
  169. print("Found ".. total_units .. " storage unit[s]")
  170. print("Total capacity detected: "..total_capacity)
  171. print("Press ctrl + alt + c to close the program")
  172. print("Waiting startup delay of: "..config.startup_delay)
  173. os.sleep(config.startup_delay + 0)
  174.  
  175. loops = 0
  176. while true do
  177.   loops = loops + 1
  178.   if loops == 50 then
  179.     loops = 0
  180.     scan()
  181.   end
  182.  
  183.   success, powerdb = pcall(getPower)
  184.   if success == false then
  185.     scan()
  186.     powerdb = {total= {stored=1, capacity=1}}
  187.   end
  188.    
  189.   term.clear()
  190.   text_buffer = ""
  191.  
  192.   total = superlib.pgen(powerdb["total"]["stored"], powerdb["total"]["capacity"], 2)
  193.  
  194.   if glasses_connected then
  195.     if total > 50 then glasses_text.setColor(.37, .83, .03) glasses_text.setScale(1) end
  196.     if total <= 50 and total > 25 then glasses_text.setColor(0.93,0.91,0.09) glasses_text.setScale(1.5) end
  197.     if total <= 25 then glasses_text.setColor(0.96,0.07,0.09,1) glasses_text.setScale(2) end
  198.     glasses_buffer = total.."%" .. " ["..total_units.."]"
  199.     if config.glasses_banner ~= false then
  200.       glasses_buffer = config.glasses_banner .. glasses_buffer
  201.     end
  202.     glasses_text.setText(glasses_buffer)
  203.   end
  204.  
  205.   if config.banner ~= false then
  206.     buffer(config.banner)
  207.   end
  208.   buffer("Currently monitoring ".. total_units .. " units")
  209.   buffer("")
  210.   buffer("Total".. ": ".. percent_gen_db(powerdb, "total") .." [".. powerdb["total"]["stored"] .. "/" .. powerdb["total"]["capacity"] .."]")
  211.   buffer("")
  212.    
  213.   for lid in pairs(powerdb) do
  214.     if lid ~= "total" then
  215.       first_half = superlib.pad("#"..lid.. ": ".. percent_gen_db(powerdb, lid), 10)
  216.       middle = superlib.pad(" [".. powerdb[lid]["stored"] .. "/" .. powerdb[lid]["capacity"] .. "] ", 30)
  217.       second_half = " ["..powerdb[lid]["name"].."]"
  218.  
  219.       if config.display_units == false then output = first_half .. second_half end
  220.       if config.display_units == true then output = first_half .. middle .. second_half end
  221.  
  222.       buffer(output)
  223.     end
  224.   end
  225.   print(text_buffer)
  226.   os.sleep(0.2)
  227. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement