Advertisement
SuPeRMiNoR3

Power Monitor Dev

Dec 18th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.49 KB | None | 0 0
  1. --Made by SuPeRMiNoR2
  2. version = "1.5.2"
  3. supported_config_version = "0.3"
  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 should 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.   storedPower = powerdb[uid]["stored"]
  65.   powerCapacity = powerdb[uid]["capacity"]
  66.   return superlib.pgen(storedPower, powerCapacity, config.display_precision) .. "%"
  67. end
  68.  
  69. function readPower(proxy, ltype)
  70.   capacity = 0
  71.   stored = 0
  72.    
  73.   if ltype == 1 then
  74.     capacity = proxy.getCapacity()
  75.     stored = proxy.getStored()
  76.   end
  77.    
  78.   if ltype == 2 then
  79.     capacity = proxy.getMaxEnergyStored()
  80.     stored = proxy.getEnergyStored()
  81.   end
  82.  
  83.   return capacity, stored
  84. end
  85.  
  86. function getPower()
  87.   total_stored = 0
  88.   powerdb = {}
  89.   for uid in pairs(mlist) do
  90.     proxy = mlist[uid]["proxy"]
  91.     ltype = mlist[uid]["type"]
  92.     lname = mlist[uid]["name"]
  93.     c, s = readPower(proxy, ltype)
  94.     if s > c then --Stupid IC2 Bug, full ic2 blocks read over their capacity sometimes
  95.         s = c
  96.     end
  97.     total_stored = total_stored + s
  98.     powerdb[uid] = {capacity=c, stored=s, name=lname}
  99.   end  
  100.   powerdb["total"] = {capacity=total_capacity, stored=total_stored}
  101.   return powerdb
  102. end
  103.  
  104. function scan()
  105.   unit_id = 1
  106.   mlist = {}
  107.   total_capacity = 0
  108.   for address, ctype in component.list() do
  109.     for stype in pairs(supported_types) do
  110.       if stype == ctype then
  111.         t = component.proxy(address)
  112.         ltype = supported_types[stype]["type"]
  113.         name = supported_types[stype]["name"]
  114.         mlist[unit_id] = {address=address, proxy=t, type=ltype, name=name}
  115.         unit_id = unit_id + 1
  116.         c, s = readPower(t, ltype)
  117.         total_capacity = total_capacity + c
  118.       end
  119.     end
  120.     if ctype == "glasses" and glasses_connected == false then
  121.       print("Detected glasses block, loading")
  122.       glasses = component.proxy(address)
  123.       glasses.removeAll()
  124.       glasses_text = glasses.addTextLabel()
  125.       glasses_text.setText("Loading.")
  126.       --os.sleep(0.8)
  127.       glasses_connected = true
  128.       glasses_text.setColor(.37, .83, .03)
  129.       glasses_text.setText("Loading..")
  130.       --os.sleep(0.8)
  131.       glasses_text.setPosition(2, 2)
  132.       glasses_text.setText("Loading...")
  133.       --os.sleep(0.8)
  134.       glasses_text.setScale(1)
  135.       glasses_text.setText("Loading....")
  136.       --os.sleep(1)
  137.       --print(glasses.getBindPlayers())
  138.     end
  139.   end
  140.   total_units = unit_id - 1
  141.   return mlist, total_capacity, total_units
  142. end
  143.  
  144. function buffer(text)
  145.   text_buffer = text_buffer .. text .. "\n"
  146. end
  147.  
  148. supported_types = {tile_thermalexpansion_cell_basic_name={type=2, name="Leadstone Cell"},
  149. tile_thermalexpansion_cell_hardened_name={type=2, name="Hardened Cell"},
  150. tile_thermalexpansion_cell_reinforced_name={type=2, name="Redstone Cell"},
  151. tile_thermalexpansion_cell_resonant_name={type=2, name="Resonant Cell"},
  152. mfsu={type=1, name="MFSU"}, mfe={type=1, name="MFE"}, cesu={type=1,
  153. name="CESU"}, batbox={type=1, name="BatBox"}, capacitor_bank={type=2, name="Capacitor Bank"}}  
  154.  
  155. --Program
  156. term.clear()
  157. print("Applying scale of " .. config.scale)
  158. w, h = gpu.maxResolution()
  159. gpu.setResolution(w / config.scale, h / config.scale)
  160.  
  161. print("Scanning for energy storage units")
  162. if glasses_connected then
  163.   glasses_text.setText("Scanning.")
  164. end
  165. mlist, total_capacity, total_units = scan()
  166.  
  167. if glasses_connected then
  168.   glasses_text.setText("Found "..total_units)
  169. end
  170.  
  171. print("Found ".. total_units .. " storage unit[s]")
  172. print("Total capacity detected: "..total_capacity)
  173. print("Press ctrl + alt + c to close the program")
  174. print("Waiting startup delay of: "..config.startup_delay)
  175. os.sleep(config.startup_delay + 0)
  176.  
  177. loops = 0
  178. while true do
  179.   loops = loops + 1
  180.   if loops == 50 then
  181.     loops = 0
  182.     scan()
  183.   end
  184.  
  185.   success, powerdb = pcall(getPower)
  186.   if success == false then
  187.     scan()
  188.     powerdb = {total= {stored=1, capacity=1}}
  189.   end
  190.    
  191.   term.clear()
  192.   text_buffer = ""
  193.  
  194.   if total_units == 0 then
  195.     total = 0
  196.   else
  197.     total = superlib.pgen(powerdb["total"]["stored"], powerdb["total"]["capacity"], 2)
  198.   end
  199.  
  200.   if glasses_connected then
  201.     if total > 50 then glasses_text.setColor(.37, .83, .03) glasses_text.setScale(1) end
  202.     if total <= 50 and total > 25 then glasses_text.setColor(0.93,0.91,0.09) glasses_text.setScale(1.5) end
  203.     if total <= 25 then glasses_text.setColor(0.96,0.07,0.09,1) glasses_text.setScale(2) end
  204.     glasses_buffer = total.."%" .. " ["..total_units.."]"
  205.     if config.glasses_banner ~= false then
  206.       glasses_buffer = config.glasses_banner .. glasses_buffer
  207.     end
  208.     glasses_text.setText(glasses_buffer)
  209.   end
  210.  
  211.   if config.banner ~= false then
  212.     buffer(config.banner)
  213.   end
  214.   buffer("Currently monitoring ".. total_units .. " units")
  215.   buffer("")
  216.   buffer("Total".. ": ".. total .." [".. powerdb["total"]["stored"] .. "/" .. powerdb["total"]["capacity"] .."]")
  217.   buffer("")
  218.    
  219.   for lid in pairs(powerdb) do
  220.     if lid ~= "total" then
  221.       first_half = superlib.pad("#"..lid.. ": ".. percent_gen_db(powerdb, lid), 10)
  222.       middle = superlib.pad(" [".. powerdb[lid]["stored"] .. "/" .. powerdb[lid]["capacity"] .. "] ", 30)
  223.       second_half = " ["..powerdb[lid]["name"].."]"
  224.  
  225.       if config.display_units == false then output = first_half .. second_half end
  226.       if config.display_units == true then output = first_half .. middle .. second_half end
  227.  
  228.       buffer(output)
  229.     end
  230.   end
  231.   print(text_buffer)
  232.   if total_units == 0 then
  233.     os.sleep(10)
  234.   else
  235.     os.sleep(config.loop_speed)
  236.   end
  237. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement