Advertisement
Hack-much

Raccs Power Managment

Jan 19th, 2021 (edited)
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 44.59 KB | None | 0 0
  1. -- V2
  2. -- All Rights Reserved, if file bellow is edited in anyway do not redistribute!
  3.  
  4. --[[
  5. Credits ©:
  6.     Steam:      Closet Raccoon
  7.     Discord:    Closet Raccoon#5092  -- Bug reports are welcome
  8.     Minecraft:  ClosetRedPanda
  9. ]]
  10.  
  11. if false then     ClosetAPI = _G._ClosetAPI     end -- Visual Studio Include
  12.  
  13. local name = "Racc's Power Management"
  14. local short_name = "~R-P-M~"
  15. local name_color = colors.blue
  16.  
  17. local motd = "Complete Rewrite FTW"
  18. local motd_color = colors.pink
  19.  
  20. local enable_logging = true
  21. local log_to = "monitor_8"
  22. local log_file = "RPM LOG"
  23. local Power_Scope_Modifier = nil --[[ Will divide the capacity of connected powerdevices by this number when
  24. -- making the progress bar to allow for more finite storage on large storage devices this is skipped if the number is <1 or nil
  25. ]]
  26.  
  27. local auto_detect = true -- if true then it will auto add all connected peripherals
  28.  
  29. local mons = {}
  30. local reacs = {}
  31. local pds = {}
  32. local turbs = {}
  33.  
  34. local reacs_data = {}
  35. local pds_data = {}
  36. local mons_data = {}
  37. local turbs_data = {}
  38. local theme_colors = {
  39.     power = colors.pink,
  40.     reacs = colors.purple,
  41.     turbs = colors.blue,
  42.     all = colors.cyan,
  43. }
  44.  
  45. local always_load_settings = false -- WARNING this can cause problems if problems are common while restarting the program then delete the save file!
  46. local settings_save_file = "RPM2_Monitor_Settings"
  47.  
  48. local default_monitor_scale = .5
  49. local default_monitor_tab = "all"
  50. local monitor_settings
  51. local function defineSettings()
  52.     monitor_settings = {
  53.         monitor_7 = {
  54.             currentTab = "reacs",
  55.             scale = 1,
  56.         },
  57.         monitor_8 = {
  58.             scale = 1,
  59.         },
  60.     }
  61. end
  62.  
  63.  
  64. capi = {  -- Standalone API table acts just as if it were loaded using os.loadAPI()
  65.     drawText = function(x,y,text,color_text,color_background,lmon)
  66.  
  67.         if color_text == nil then
  68.             color_text = colors.white
  69.         end
  70.         if color_background == nil then
  71.             color_background = colors.black
  72.         end
  73.         if lmon == nil then
  74.             lmon = term
  75.         end
  76.         local lastcolor = lmon.getTextColor() --saving last color
  77.         local lastbcolor = lmon.getBackgroundColor()
  78.         lmon.setCursorPos(x,y)
  79.         lmon.setBackgroundColor(color_background)
  80.         lmon.setTextColor(color_text)
  81.         lmon.write(text)
  82.         lastposX, lastposY = lmon.getCursorPos()
  83.         lmon.setBackgroundColor(lastbcolor)  --restoring last color
  84.         lmon.setTextColor(lastcolor)
  85.     end,
  86.     numbShorten2 = function(numb,places,returntype) -- Input Number, Floor Significance | ver 4.0
  87.         if places == nil then places = 1 end
  88.         numb = tostring(numb)
  89.         local count = math.floor(#numb/3)
  90.         local letters = {"K","M","B","T","Q","Qi","Sx","Sp","N","D","Ud","Du","Te","Qa","Qid"}
  91.         if returntype == nil or returntype == 1 then
  92.             pfnumb = math.floor( numb / 10^(count*3) *10^places )/10^places
  93.             return pfnumb..letters[count]
  94.         elseif returntype == 2 then
  95.             pfnumb = math.floor( numb / 10^(count*3) *10^places )/10^places
  96.             return pfnumb
  97.         elseif returntype == 3 then
  98.             pfnumb = math.floor( numb* (10^places+1) / 10^#numb  )
  99.             return pfnumb.."*10^"..#numb
  100.         elseif returntype == 4 then
  101.             pfnumb = math.floor( numb* (10^places+1) / 10^#numb  )
  102.             return pfnumb.."E"..#numb
  103.         end
  104.     end,
  105.     getAllPeripherals = function(silent) --Silent makes it not return the full table(Non-Silent by default)
  106.  
  107.         --clear all peripheral tabels
  108.         local monitors = {}
  109.         local reactors = {}
  110.         local powerdevices = {}
  111.         local tanks = {}
  112.         local turbines = {}
  113.         local others = {}
  114.         local cp = p.getNames()
  115.         for i = 1,#cp do
  116.             local current = cp[i]
  117.             local curtype = p.getType(current)
  118.             if curtype == "monitor" then
  119.                 table.insert(monitors,current)
  120.             elseif curtype == "BigReactors-Reactor" then
  121.                 table.insert(reactors,current)
  122.             elseif curtype == "tile_blockcapacitorbank_name"    or string.find(curtype,"tile_thermalexpansion_cell") == 1    or curtype == "draconic_rf_storage" then
  123.                 table.insert(powerdevices,current)
  124.             elseif curtype == "openblocks_tank"     or curtype == "rcirontankvalvetile" then
  125.                 table.insert(tanks,current)
  126.             elseif curtype == "BigReactors-Turbine" then
  127.                 table.insert(turbines,current)
  128.             else
  129.                 table.insert(others,current)
  130.             end
  131.         end
  132.         local gap = {
  133.         mons = monitors,
  134.         reacs = reactors,
  135.         pds = powerdevices,
  136.         tnks = tanks,
  137.         turbs = turbines,
  138.         others = others
  139.         }
  140.         if silent == false or silent == nil then   return gap     end
  141.     end,
  142.     FileManager = {
  143.         CurrentFileW = "",
  144.         CurrentFileR = "",
  145.         OpenFileW = function(file)
  146.             if fs.getFreeSpace(file) <= 0 then APIdebug.log("Error no space left on computer!",5,"current","current","current") end
  147.             FileManager.CurrentFileW = fs.open(file,"w")
  148.         end,
  149.         OpenFileR = function(file)
  150.             FileManager.CurrentFileR = fs.open(file,"r")
  151.         end,
  152.         WriteLine = function(text,file)
  153.             if FileManager.CurrentFileW == nil then return {"File Manager - [3] - ","File Not Found"} end
  154.             FileManager.CurrentFileW:writeLine(text)
  155.             FileManager.CurrentFileW:flush()
  156.         end,
  157.         CloseCurrentW = function()
  158.             if FileManager.OpenFileW ~= nil then
  159.                 if FileManager.CurrentFileW.close ~= nil then
  160.                     FileManager.CurrentFileW:close()
  161.                 end
  162.             end
  163.         end,
  164.         CloseCurrentR = function()
  165.             if FileManager.CurrentFileR ~= nil then
  166.                 if FileManager.CurrentFileR.close ~= nil then
  167.                     FileManager.CurrentFileR:close()
  168.                 end
  169.             end
  170.         end,
  171.     },
  172.     APIdebug = {
  173.         currentlog = 1,
  174.         log = function(text,level,debugvar,lmon,logfile)
  175.             local FileWrite = FileManager.WriteLine
  176.             if logfile ~= nil and logfile ~= false then
  177.                 if APIdebug.currentlog > 500 then
  178.                     FileManager.CloseCurrentW()
  179.                     if fs.isReadOnly(logfile) == false then
  180.                         fs.delete(logfile)
  181.                     end
  182.                     FileManager.OpenFileW(logfile)
  183.                 end
  184.                 if level == 1 then
  185.                     FileWrite("["..APIdebug.currentlog.."] -  Info - "..text)
  186.                 elseif level == 2 then
  187.                     FileWrite("["..APIdebug.currentlog.."]-  Warn - "..text)
  188.                 elseif level == 3 then
  189.                     FileWrite("["..APIdebug.currentlog.."] -  Error - "..text)
  190.                 elseif level == 4 then
  191.                     FileWrite("["..APIdebug.currentlog.."] -  Fatal No Halt - "..text)
  192.                 elseif level == 5 then
  193.                     FileWrite("["..APIdebug.currentlog.."] -  Fatal - "..text.." - HALTING")
  194.                 end
  195.             end
  196.             if debugvar == true then
  197.                 if level == 1 then
  198.                     drawLineText("["..APIdebug.currentlog.."] - Info - "..text,colors.white,lmon,true)
  199.                 elseif level == 2 then
  200.                     drawLineText("["..APIdebug.currentlog.."] - Warn - "..text,colors.yellow,lmon,true)
  201.                 elseif level == 3 then
  202.                     drawLineText("["..APIdebug.currentlog.."] - Error - "..text,colors.orange,lmon,true)
  203.                 elseif level == 4 then
  204.                     drawLineText("["..APIdebug.currentlog.."] - PANIC NO HALT - "..text,colors.red,lmon,true)
  205.                     os.queueEvent("panic")
  206.                 elseif level == 5 then
  207.                     drawLineText("["..APIdebug.currentlog.."] - PANIC FORCE HALT  - "..text.." - HALTING",colors.red,lmon,true)
  208.                     os.queueEvent("panic","halt")
  209.                     print("["..APIdebug.currentlog.."] - PANIC - "..text)
  210.                     os.queueEvent("terminate")
  211.                 end
  212.             end
  213.             APIdebug.currentlog = APIdebug.currentlog+1
  214.             if level == 5 then os.queueEvent("terminate") os.queueEvent("panic","halt") end
  215.         end
  216.     },
  217.     tabeSave = function(table,name)
  218.         if type(name) ~= "string" then return end
  219.         if table == nil then return end
  220.         local file = fs.open(name,"w")
  221.         file.write(textutils.serialize(table))
  222.         file.close()
  223.     end,
  224.     tabeLoad = function(name)
  225.         if fs.exists(name) == false then return false end
  226.         local file = fs.open(name,"r")
  227.         local data = file.readAll()
  228.         file.close()
  229.         return textutils.unserialize(data)
  230.     end,
  231. }
  232.  
  233.  
  234. term.setTextScale = function() return end -- Fixes the only thing that monitors can do that computers will error on
  235.  
  236. if capi == nil then error("API was not found") end
  237.  
  238. local log_to_original_input
  239. function prepDebugMon()
  240.     if enable_logging == false then return end
  241.     if log_to == term then return end
  242.     if peripheral.isPresent(log_to) == true then
  243.         local temp = peripheral.wrap(log_to)
  244.         temp.clear()
  245.         temp.setCursorPos(1,0)
  246.     end
  247. end
  248. prepDebugMon()
  249. log = function(text,level)
  250.     if enable_logging == false then return end
  251.     if (type(log_to) == "string" and peripheral.isPresent(log_to) == true) then
  252.         log_to_original_input = log_to
  253.         log_to = peripheral.wrap(log_to)
  254.     elseif log_to_original_input == nil then
  255.         log_to_original_input = log_to
  256.     end
  257.  
  258.     if log_to.write == nil then return end
  259.     if capi.FileManager.CurrentFileW == "" and log_file ~= nil then capi.FileManager.OpenFileW(log_file) end
  260.     capi.APIdebug.log(text,level,enable_logging,log_to,log_file)
  261. end
  262. log("Logging is enabled",1)
  263. log("Log output is ready",1)
  264.  
  265. function loadSettings()
  266.     local temp = capi.tabeLoad(settings_save_file)
  267.     if type(temp) == "table"  then
  268.         monitor_settings = temp
  269.     else
  270.         log("File, "..settings_save_file.." ,was not able to load!",3)
  271.     end
  272. end
  273.  
  274. function autoDetect()
  275.     log("Running autoDetect()",1)
  276.     local d = capi.getAllPeripherals()
  277.     if d == {} or d == nil or type(d) ~= "table" then
  278.         log("autoDetect returned nothing",4)
  279.     else
  280.         reacs = {}
  281.         pds = {}
  282.         mons = {}
  283.         turbs = {}
  284.         turbs = d.turbs
  285.         reacs = d.reacs
  286.         pds = d.pds
  287.         mons = d.mons
  288.     end
  289. end
  290.  
  291. function prep()
  292.     log("running prep()",1)
  293.     log("Removing faulty peripherals",1)
  294.     for i = 1,#mons do
  295.         if mons[i] ~= nil then
  296.             if peripheral.isPresent(mons[i]) == false then
  297.                 log("'"..mons[i].."' Not found, Removing",2)
  298.                 table.remove(mons,i)
  299.             end
  300.             if mons[i] == log_to_original_input and enable_logging == true then
  301.                 log("'"..mons[i].."' is debug mon, Removing",2)
  302.                 table.remove(mons,i)
  303.             end
  304.         end
  305.     end
  306.     for i = 1,#reacs do
  307.         if peripheral.isPresent(reacs[i]) == false then
  308.             log("'"..reacs[i].."' Not found, Removing",2)
  309.             table.remove(reacs,i)
  310.         end
  311.     end
  312.     for i = 1,#pds do
  313.         if peripheral.isPresent(pds[i]) == false then
  314.             log("'"..pds[i].."' Not found, Removing",2)
  315.             table.remove(pds,i)
  316.         end
  317.     end
  318. end
  319.  
  320. function getInfo()
  321.     for i = 1,#pds do
  322.         if peripheral.isPresent(pds[i]) == false then
  323.             log("'"..pds[i].."' was not found while getting info!",3)
  324.         else
  325.             local a,b,c,d,e,f,g,h   --Indvidual
  326.             local crnt = peripheral.wrap(pds[i])
  327.             a = crnt.getEnergyStored()
  328.             b = crnt.getMaxEnergyStored()
  329.             f = math.floor(a/b*10)/10
  330.  
  331.             pds_data[pds[i]] = {
  332.                 energyStored = math.floor(crnt.getEnergyStored()*10) /10                ,-- 1  Energy
  333.                 maxEnergy    = b                                                        ,-- 2  Max Energy
  334.                 shortEnergy = capi.numbShorten2(a,3), -- Shortened Energy
  335.                 f, -- Percentage
  336.                 d = peripheral.getType(pds[i]), -- Type
  337.                 g = pds[i], -- Name
  338.                 h = "Draconic", -- Human name (Old and will not display proper name)
  339.             }
  340.             log("'"..pds[i].."' data retreived",1)
  341.         end
  342.     end
  343.     for i = 1,#reacs do
  344.         if peripheral.isPresent(reacs[i]) == false then
  345.             log("'"..reacs[i].."' was not found while getting info!",3)
  346.         else
  347.             local crnt = peripheral.wrap(reacs[i])
  348.             local cdata = {
  349.                 energyStored = math.floor(crnt.getEnergyStored()*10) /10                ,-- 1  Energy
  350.                 maxEnergy    = 10000000                                                 ,-- 2  Max Energy
  351.                 fuelUsage    = math.floor( crnt.getFuelConsumedLastTick()*100 ) / 100   ,-- 3  Fuel Usage
  352.                 energyOut    = crnt.getEnergyProducedLastTick()                         ,-- 4  Energy Produced
  353.                 active       = crnt.getActive()                                         ,-- 6  Is Active (Bool)
  354.                 rodLevel     = crnt.getControlRodLevel(0)                               ,-- 7  Rod Level
  355.                 fuelLevel    = crnt.getFuelAmount()                                     ,-- 8  Fuel Level
  356.                 fuelMax      = crnt.getFuelAmountMax()                                  ,-- 9  Fuel Max
  357.                 type         = peripheral.getType(reacs[i])                             ,-- 11 Type
  358.                 name         = reacs[i]                                                 ,-- 12 Name
  359.                 actCooled    = crnt.isActivelyCooled()                                  ,-- 13 true if its a steam producing reactor (Bool)
  360.             }
  361.             cdata["RFFuel"]          = math.floor(cdata["energyOut"]/cdata["fuelUsage"]*100)/100
  362.             cdata["fuelPercentage"]  = math.floor(cdata["fuelLevel"]/cdata["fuelMax"]*1000)/10
  363.             reacs_data[ reacs[i] ] = cdata
  364.             log("'"..reacs[i].."' data retrieved",1)
  365.         end
  366.     end
  367.     for i = 1,#turbs do
  368.         if peripheral.isPresent(turbs[i]) == false then
  369.             log("'"..turbs[i].."' was not found while getting info!",3)
  370.         else
  371.             local crnt = peripheral.wrap(turbs[i])
  372.             local cdata = {
  373.                 energyStored = math.floor(crnt.getEnergyStored()*10) /10                ,-- 1  Energy
  374.                 maxEnergy    = 10000000                                                 ,-- 2  Max Energy
  375.                 energyOut    = crnt.getEnergyProducedLastTick()                         ,-- 4  Energy Produced
  376.                 active       = crnt.getActive()                                         ,-- 6  Is Active (Bool)
  377.                 type         = peripheral.getType(turbs[i])                             ,-- 11 Type
  378.                 name         = reacs[i]                                                 ,-- 12 Name
  379.                 rpm          = crnt.getRotorSpeed()                                     ,-- 13 Rotor Speed
  380.                 hotConsumed  = crnt.getFluidFlowRate()                                  ,-- 14 Steam consumed
  381.             }
  382.             turbs_data[ turbs[i] ] = cdata
  383.             log("'"..turbs[i].."' data retrieved",1)
  384.         end
  385.     end
  386. end
  387.  
  388. function totalTable(intable)
  389.     log("Totaling "..tostring(intable),1)
  390.     local outtable = {
  391.         activeAmount = 0,
  392.         amount = 0,
  393.         name = "total_table"
  394.     }
  395.     for k,v in pairs(intable) do
  396.         outtable.amount = outtable.amount+1
  397.        for l,b in pairs(v) do
  398.             if type(b) == "number" then
  399.                 --log(tostring(k).."    "..tostring(v).."    "..tostring(l).."    "..tostring(b),1)
  400.                 if outtable[l] == nil then outtable[l] = 0 end
  401.                 outtable[l] = outtable[l] + b
  402.                 --log(outtable[l],1)
  403.             elseif l == "active" and b == true then
  404.                 outtable.activeAmount = outtable.activeAmount + 1
  405.                 --log(tostring(k).."    "..tostring(v).."    "..tostring(l).."    "..tostring(b).." ADDED 1 ACTIVE",1)
  406.             end
  407.         end
  408.     end
  409.     return outtable
  410. end
  411.  
  412.  
  413.  
  414. function bwrite(text,x,y,mon,clear)
  415.     if mon == nil or type(mon) ~= "table" then log("Basic write got bad mon variable",2) return end
  416.     mon.setCursorPos(x,y)
  417.     if clear == true then mon.clearLine() end
  418.     mon.write(text)
  419. end
  420.  
  421. function comma_value(amount,places) --stolen right off 'http://lua-users.org/wiki/FormattingNumbers'
  422.     if places == nil then places = 2 end
  423.     local formatted = math.floor(amount*10^places)/10^places
  424.     while true do  
  425.       formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  426.       if (k==0) then
  427.         break
  428.       end
  429.     end
  430.     return formatted
  431.   end
  432.  
  433. function reShellTerm()
  434.     log("reshelling term",1)
  435.     term.clear()
  436.     local mX,mY = term.getSize()
  437.     term.setCursorPos( (mX/2-#name/2) ,1)
  438.     term.setTextColor(name_color)
  439.     print(name)
  440.     term.setCursorPos( (mX/2-#motd/2) ,2)
  441.     term.setTextColor(motd_color)
  442.     print(motd)
  443.     term.setCursorPos(1,3)
  444. end
  445.  
  446. local buttons = {}
  447. local button = {
  448.     process = function (self,event)
  449.         log("Processing possible button.",1)
  450.         local call = event[1]
  451.         local monitor = event[2]
  452.         local cX = event[3]
  453.         local cY = event[4]
  454.         if buttons[monitor] == {} or buttons[monitor] == nil then log("'"..monitor.."' has no buttons!",1) return end
  455.         for k,v in pairs(buttons[monitor]) do
  456.             log("Testing "..k,1)
  457.             if cX >= v.minX and cX <= v.maxX   then
  458.                 if cY >= v.minY and cY <= v.maxY   then
  459.                     log("Button named '"..v.name.."' was called",1)
  460.                     v:run()
  461.                 end
  462.             end
  463.         end
  464.     end,
  465.     createButton = function(self,maxX,maxY,minX,minY,funct,name,monitor,tablename) -- WARNING FUNCTIONS MUST HAVE A SELF DELCARATION!
  466.         if type(maxX) ~= "number" or type(maxY) ~= "number" or type(minY) ~= "number" or type(minX) ~= "number" or type(funct) ~= "function" or type(monitor) ~= "string" or type(tablename) ~= "string" then
  467.             log("Atempted to create button with args that wernt the correct type",4)
  468.             return
  469.         end
  470.         buttons[monitor][tablename] = {
  471.             maxX = maxX,
  472.             maxY = maxY,
  473.             minX = minX,
  474.             minY = minY,
  475.             monitor = monitor,
  476.             name = name,
  477.             run = funct,
  478.         }
  479.  
  480.     end,
  481.     createButtonTable = function(self,table,tablename) -- WARNING FUNCTIONS MUST HAVE A SELF DELCARATION!
  482.         if type(table) ~= "table" or type(tablename) ~= "string" then
  483.             log("Atempted to create button with table, with args that wernt the correct type",4)
  484.             return false
  485.         end
  486.         buttons[tablename] = table
  487.     end,
  488. }
  489.  
  490.  
  491. terminate = false
  492. running = true
  493. function listAllButtons()
  494.     if enable_logging == true then
  495.         log(" ",1)
  496.         log("Buttons: ",1)
  497.         for k,v in pairs(buttons) do
  498.             log("  "..k,1)
  499.             for l,b in pairs(v) do
  500.                 log("    "..l.."   "..tostring(b.name),1)
  501.             end
  502.         end
  503.     else
  504.         print(" ")
  505.         print("Buttons: ")
  506.         for k,v in pairs(buttons) do
  507.             print("  "..k)
  508.             for l,b in pairs(v) do
  509.                 print("    "..l.."   "..tostring(b.name))
  510.             end
  511.         end
  512.     end
  513. end
  514. function listAllButtonsInMonitor(monitor)
  515.     if enable_logging == true then
  516.         log(" ",1)
  517.         log("Buttons in "..monitor..": ",1)
  518.         for k,v in pairs(buttons[monitor]) do
  519.             log("  "..k,1)
  520.             for l,b in pairs(v) do
  521.                 log("    "..l.."   "..tostring(b),1)
  522.             end
  523.         end
  524.     else
  525.         print(" ")
  526.         print("Buttons in "..monitor..": ")
  527.         for k,v in pairs(buttons[monitor]) do
  528.             print("  "..k)
  529.             for l,b in pairs(v) do
  530.                 print("    "..l.."   "..tostring(b))
  531.             end
  532.         end
  533.     end
  534. end
  535.  
  536.  
  537. local function wait ( time ) -- Off wiki to allow for events to handle during sleep timers without using parallel
  538.     local timer_finished = false
  539.     local timer = os.startTimer(time)
  540.    
  541.     while timer_finished == false do
  542.       local event = {os.pullEventRaw()}
  543.      
  544.       if (event[1] == "timer" and event[2] == timer) or (event[1] == "key_up" and event[2] == keys.f4) then
  545.         timer_finished = true
  546.       else
  547.         eventHandler(event)
  548.       end
  549.     end
  550. end
  551.  
  552.  
  553. local tabs
  554. function tabDef()
  555.     tabs = {}
  556.     if #reacs > 0 then
  557.         tabs.reacs = {
  558.                 display = function(self,mon,data)
  559.                     if data == nil then data =  self.cortable() end
  560.                     if peripheral.isPresent(mon) == true then
  561.                         log(mon.." displaying 'reacs'",1)
  562.                         local mon = peripheral.wrap(mon)
  563.                         local mx,my = mon.getSize()
  564.                         local size = 4
  565.  
  566.                         for g = 1,#reacs do
  567.                             local cur = reacs[g]
  568.                             local cdata = data[cur]
  569.                             if peripheral.isPresent(cur) == true and cdata ~= nil then
  570.                                 local cx,cy = mon.getCursorPos()
  571.                                 if cy+size > my then break end
  572.                                 mon.setTextColor(self.theme)
  573.                                 bwrite("Reactor "..g..":",1,cy,mon)
  574.                                 if cdata["name"] == "DISCONNECTED" then
  575.                                     local r = "DISCONNECTED"
  576.                                     capi.drawText(mx-#r+1,cy, r, colors.red, nil, mon)
  577.                                 elseif cdata["active"] == true then
  578.                                     local r = "Running"
  579.                                     capi.drawText(mx-#r+1,cy, r, colors.green, nil, mon)
  580.                                 elseif cdata["active"] == false then
  581.                                     local r = "Halted"
  582.                                     capi.drawText(mx-#r+1,cy, r, colors.gray, nil, mon)
  583.                                 else
  584.                                     local r = "ERROR"
  585.                                     capi.drawText(mx-#r+1,cy, r, colors.red, nil, mon)
  586.                                 end
  587.                                 bwrite("Fuel Percentage: "..cdata["fuelPercentage"],2,cy+1,mon,true)
  588.                                 bwrite("Fuel Usage: "..cdata["fuelUsage"],2,cy+2,mon,true)
  589.                                 local react_out
  590.                                 if cdata.actCooled == true then
  591.                                     react_out = "Steam Output: "..math.floor(cdata.energyOut).." MB/t"
  592.                                 else
  593.                                     react_out = "Power Output: "..comma_value(cdata.energyOut).." RF/t"
  594.                                 end
  595.                                 bwrite(react_out,2,cy+3,mon,true)
  596.                                 mon.setCursorPos(1,cy+size+1)
  597.                             end
  598.                         end
  599.                     else
  600.                     end
  601.                 end,
  602.                 name = "Reactors",
  603.                 theme = theme_colors.reacs,
  604.                 delete = function()
  605.                     return false
  606.                 end,
  607.                 cortable = function()
  608.                     return reacs_data
  609.                 end,
  610.         }
  611.     end
  612.     if #pds > 0 then
  613.         tabs.pds = {
  614.                 display = function(self,mon,data)
  615.                     if data == nil then data =  self.cortable() end
  616.                     if peripheral.isPresent(mon) == true then
  617.                         log(mon.." displaying 'pds'",1)
  618.                         local mon = peripheral.wrap(mon)
  619.                         local mx,my = mon.getSize()
  620.                         local size = 3 -- size of all the elements of the for loop input not including spacer
  621.                         for g = 1,#pds do
  622.                             local cur = pds[g]
  623.                             local cdata = data[cur]
  624.                             local cx,cy = mon.getCursorPos()
  625.                             if cy+size > my then break end
  626.                             if peripheral.isPresent(cur) == true and cdata ~= nil then
  627.                                 mon.setTextColor(self.theme)
  628.                                 bwrite("Power Device "..g..":",1,cy,mon)
  629.                                 bwrite("Energy Stored: "..comma_value(cdata["energyStored"]),2,cy+1,mon)
  630.                                 local scope
  631.                                 if Power_Scope_Modifier ~= nil and Power_Scope_Modifier > 1 then scope = Power_Scope_Modifier else scope = 1 end
  632.                                 capi.drawProg(2,cy+2,1,mx-2,cdata["energyStored"],cdata["maxEnergy"]/scope,colors.gray,self.theme,mon)
  633.                                 mon.setCursorPos(1,cy+size+1)
  634.  
  635.                             end
  636.                         end
  637.                     end
  638.                 end,
  639.                 name = "Power",
  640.                 theme = theme_colors.power,
  641.                 delete = function()
  642.                     return false
  643.                 end,
  644.                 cortable = function()
  645.                     return pds_data
  646.                 end,
  647.         }
  648.     end
  649.     if #turbs > 0 then
  650.         tabs.turbs = {
  651.             display = function(self,mon,data)
  652.                 if data == nil then data =  self.cortable() end
  653.                 if peripheral.isPresent(mon) == true then
  654.                     log(mon.." displaying 'pds'",1)
  655.                     local mon = peripheral.wrap(mon)
  656.                     local mx,my = mon.getSize()
  657.                     local size = 3 -- size of all the elements of the for loop input not including spacer
  658.                     mon.setTextColor(self.theme)
  659.                     for i = 1,#turbs do
  660.                         local cur = turbs[i]
  661.                         local cdata = data[cur]
  662.                         local cx,cy = mon.getCursorPos()
  663.                         if cy+size > my then break end
  664.                         bwrite("Turbine "..i..": ",1,cy,mon)
  665.                         if cdata["name"] == "DISCONNECTED" then
  666.                             local r = "DISCONNECTED"
  667.                             capi.drawText(mx-#r+1,cy, r, colors.red, nil, mon)
  668.                         elseif cdata["active"] == true then
  669.                             local r = "Running"
  670.                             capi.drawText(mx-#r+1,cy, r, colors.green, nil, mon)
  671.                         elseif cdata["active"] == false then
  672.                             local r = "Halted"
  673.                             capi.drawText(mx-#r+1,cy, r, colors.gray, nil, mon)
  674.                         else
  675.                             local r = "ERROR"
  676.                             capi.drawText(mx-#r+1,cy, r, colors.red, nil, mon)
  677.                         end
  678.                         bwrite("Energy Out: "..cdata.energyOut,1,cy+1,mon)
  679.                         bwrite("RPM: "..cdata.rpm,1,cy+2,mon)
  680.                         mon.setCursorPos(1,cy+size+1)
  681.                     end
  682.                 end
  683.             end,
  684.             name = "Turbines",
  685.             theme = theme_colors.turbs,
  686.             delete = function()
  687.                 return false
  688.             end,
  689.             cortable = function()
  690.                 return turbs_data
  691.             end,
  692.         }
  693.     end
  694.  
  695.     tabs.all = {
  696.             display = function(self,mon,data)
  697.                 if data == nil then data =  self.cortable() end
  698.                 if peripheral.isPresent(mon) == true then
  699.                     log(mon.." displaying 'all'",1)
  700.                     local mon = peripheral.wrap(mon)
  701.                     local mx,my = mon.getSize()
  702.                     if #reacs > 0 then
  703.                         local size = 4
  704.                         local cdata = data["reacs"]
  705.                         mon.setTextColor(theme_colors.reacs)
  706.                         local cx,cy = mon.getCursorPos()
  707.                         bwrite("Reactors: ",1,cy,mon,true)
  708.                         local acttext = cdata.activeAmount.."/"..cdata.amount
  709.                         bwrite(acttext,mx-#acttext,cy,mon)
  710.                         bwrite("Fuel amount: "..cdata.fuelPercentage,2,cy+1,mon,true)
  711.                         bwrite("Fuel Usage: "..cdata.fuelUsage,2,cy+2,mon,true)
  712.  
  713.                         local amntActCooled = 0
  714.                         local amntPassiveCooled = 0
  715.                         local totalSteamOut = 0
  716.                         local totalPowerOut = 0
  717.                         for i = 1,#reacs do
  718.                             if reacs_data[reacs[i]].actCooled then
  719.                                 amntActCooled = amntActCooled + 1
  720.                                 totalSteamOut = reacs_data[reacs[i]].energyOut + totalSteamOut
  721.                             else
  722.                                 amntPassiveCooled = amntPassiveCooled+ 1
  723.                                 totalPowerOut = reacs_data[reacs[i]].energyOut + totalPowerOut
  724.                             end
  725.                         end
  726.                         if amntActCooled > 0 then
  727.                             local cx,cy = mon.getCursorPos()
  728.                             bwrite("Steam Output: "..math.floor(totalSteamOut).." MB/t",2,cy+1,mon,true)
  729.                             if amntActCooled ~= cdata.amount then
  730.                                 local endstring = tostring(amntActCooled).."/"..cdata.amount
  731.                                 bwrite(endstring,mx-#endstring,cy+1,mon,false)
  732.                             end
  733.                         end
  734.                         if amntPassiveCooled > 0 then
  735.                             local cx,cy = mon.getCursorPos()
  736.                             bwrite("Power Output: "..comma_value(totalPowerOut).." RF/t",2,cy+1,mon,true)
  737.                             if amntActCooled ~= cdata.amount then
  738.                                 local endstring = tostring(amntActCooled).."/"..cdata.amount
  739.                                 bwrite(endstring,mx-#endstring,cy+1,mon,false)
  740.                             end
  741.                         end
  742.  
  743.                         local ex,ey = mon.getCursorPos()
  744.                         mon.setCursorPos(1,ey+2)
  745.                     end
  746.                     if #pds > 0 then
  747.                         local size = 4
  748.                         local cdata = data["pds"]
  749.                         mon.setTextColor(theme_colors.power)
  750.                         local cx,cy = mon.getCursorPos()
  751.                         bwrite("Power Storage: ",1,cy,mon)
  752.                         bwrite("Power Stored: "..capi.numbShorten2(cdata.energyStored,2),2,cy+1,mon,true)
  753.                         local scope
  754.                         if Power_Scope_Modifier ~= nil and Power_Scope_Modifier > 1 then scope = Power_Scope_Modifier else scope = 1 end
  755.                         capi.drawProg(2,cy+2,1,mx-2,cdata["energyStored"],cdata["maxEnergy"]/scope,colors.gray,theme_colors.power,mon)
  756.                         mon.setCursorPos(1,cy+size)
  757.                     end
  758.                     if #turbs > 0 then
  759.                         local size = 4
  760.                         local cdata = data["turbs"]
  761.                         mon.setTextColor(theme_colors.turbs)
  762.                         local cx,cy = mon.getCursorPos()
  763.                         bwrite("Turbines: ",1,cy,mon,true)
  764.                         local acttext = cdata.activeAmount.."/"..cdata.amount
  765.                         bwrite(acttext,mx-#acttext,cy,mon)
  766.                         bwrite("Energy Output: "..comma_value(cdata.energyOut).." RF/t",2,cy+1,mon,true)
  767.                         bwrite("Rotor Speed: "..cdata.rpm,2,cy+2,mon,true)
  768.                         bwrite("Steam Consumed: "..cdata.hotConsumed,2,cy+3,mon,true)
  769.                         mon.setCursorPos(1,cy+size)
  770.                     end
  771.                 end
  772.             end,
  773.             name = "All",
  774.             theme = theme_colors.all,
  775.             delete = function()
  776.                 return false
  777.             end,
  778.             cortable = function()
  779.                 return totals
  780.             end,
  781.     }
  782.     local tabcount = 0
  783.     for v,k in pairs(tabs) do
  784.         if k ~= "amount" then
  785.             tabcount = tabcount +1
  786.         end
  787.     end
  788.     tabs.amount = tabcount
  789.     log("Tabs registred: ",1)
  790.     for k,v in pairs(tabs) do
  791.         log(" "..k,1)
  792.     end
  793. end
  794.  
  795.  
  796.  
  797. bottomBar = {
  798.     display = function(self,mon,data)
  799.         local lmon = mon
  800.         local lTabs = {
  801.             amount = 0
  802.         }
  803.         if peripheral.isPresent(mon) == true then
  804.        
  805.             --log("monitor_settings for "..lmon.." has exlusions!",2)
  806.             for k,v in pairs(tabs) do
  807.                 if k ~= "amount" then
  808.                     if type(monitor_settings[lmon].excludeTabs) ~= "table" then
  809.                         lTabs[k] = tabs[k]
  810.                         lTabs.amount = lTabs.amount +1
  811.                     else
  812.                        
  813.                         if monitor_settings[lmon].excludeTabs[k] == true then
  814.                             log("monitor_settings for "..lmon.." exludes "..k.." from displaying",1)
  815.                         else
  816.                             lTabs[k] = tabs[k]
  817.                             lTabs.amount = lTabs.amount +1
  818.                         end
  819.                     end
  820.                 end
  821.             end
  822.  
  823.             mon = peripheral.wrap(mon)
  824.             local mX,mY = mon.getSize()
  825.             local temp = math.abs(math.floor( mX /lTabs.amount))
  826.             if temp < lTabs.amount then return end
  827.             mon.setCursorPos(1,mY)
  828.             local oldbgc = mon.getBackgroundColor()
  829.             local i = 1
  830.             mon.setTextColor(colors.black)
  831.             for k,v in pairs(lTabs) do
  832.                 if k ~= "amount" then
  833.                     local tempX,tempY = mon.getCursorPos()
  834.                     local color
  835.                     if v.theme == nil then color = colors.green else color = v.theme end
  836.                     mon.setBackgroundColor(color)
  837.                     mon.write(string.rep(" ",temp+1))
  838.                     mon.setCursorPos( (tempX+(temp/2)+1)-#v.name/2,tempY)                    
  839.                     mon.write(v.name)
  840.                     mon.setCursorPos(tempX+temp+1,tempY)
  841.                 end
  842.                 i = i + 1
  843.             end
  844.             mon.setBackgroundColor(oldbgc)
  845.         end
  846.     end,
  847.     addButtons = function (self,lmon)
  848.         local lTabs = {
  849.             amount = 0
  850.         }
  851.         buttons[lmon] = {
  852.             default_refresh = {
  853.                 maxX = 4,
  854.                 minX = 1,
  855.                 minY = 1,
  856.                 maxY = 4,
  857.                 run = function (self)
  858.                     running = false
  859.                     os.queueEvent("key_up",keys.f4)
  860.                 end,
  861.                 monitor = "all",
  862.                 name = "refresh",
  863.                 delete = function()
  864.                     return
  865.                 end,
  866.             }
  867.         }
  868.         for k,v in pairs(tabs) do
  869.             if k ~= "amount" then
  870.                 if type(monitor_settings[lmon].excludeTabs) ~= "table" then
  871.                     lTabs[k] = tabs[k]
  872.                     lTabs.amount = lTabs.amount +1
  873.                 else
  874.                     if monitor_settings[lmon].excludeTabs[k] == true then
  875.                         log("monitor_settings for "..lmon.." exludes "..k.." from creating button",1)
  876.                     else
  877.                         lTabs[k] = tabs[k]
  878.                         lTabs.amount = lTabs.amount + 1
  879.                     end
  880.                 end
  881.             end
  882.         end
  883.  
  884.         local mon = peripheral.wrap(lmon)
  885.         local mX,mY = mon.getSize()
  886.         local temp = math.abs(math.floor(mX/lTabs.amount))
  887.         if temp < lTabs.amount then return end
  888.         mon.setCursorPos(1,mY)
  889.         for k,v in pairs(lTabs) do
  890.                 if k ~= "amount" then
  891.                 local tempX,tempY = mon.getCursorPos()
  892.                 button:createButton(tempX+temp,mY,tempX,mY,
  893.                     function(self)
  894.                         monitor_settings[lmon].currentTab = k
  895.                         os.queueEvent("key_up",keys.f4)
  896.                     end,"TabSwitch".."_"..k.."_"..lmon,lmon,"TabSwitch".."_"..k.."_"..lmon
  897.                 )
  898.                 mon.setCursorPos(tempX+temp,mY)
  899.             end
  900.         end
  901.     end,
  902.     name = "BottomBar",
  903.     theme = name_color,
  904.     theme2 = motd_color,
  905.     cortable = function()
  906.         return mons
  907.     end,
  908. }
  909. topBar = {
  910.     display = function(self,mon,data)
  911.         if peripheral.isPresent(mon) == true then
  912.             local mon = peripheral.wrap(mon)
  913.             local cx,cy = mon.getCursorPos()
  914.             local mx,my = mon.getSize()
  915.             mon.setTextColor(colors.pink)
  916.             mon.setTextColor(self.theme)
  917.             bwrite(name, mx/2 - (#name/2) + 1 ,cy,mon)
  918.             mon.setTextColor(self.theme2)
  919.             bwrite(motd, mx/2 - (#motd/2) + 1 ,cy+1,mon)
  920.             mon.setCursorPos(1,cy+4)
  921.         end
  922.     end,
  923.     name = "TopBar",
  924.     theme = name_color,
  925.     theme2 = motd_color,
  926.     cortable = function()
  927.         return mons
  928.     end,
  929. }
  930.  
  931. function AutoComplete(input,complete_from,type_needed)
  932.     if input == "" then return "" end
  933.     local out = {}
  934.     for k,v in pairs(complete_from) do
  935.         local s,f = string.find(k,input)
  936.         if s == 1 then
  937.             if type_needed ~= nil then
  938.                 if type(v) == type_needed then
  939.                     table.insert(out,#out+1,string.sub(k,f+1))
  940.                 end
  941.             else
  942.                 table.insert(out,#out+1,string.sub(k,f+1))
  943.             end
  944.         end
  945.     end
  946.     for i = 1,#complete_from do
  947.         local s,f = string.find(complete_from[i],input)
  948.         if s == 1 then
  949.             if type_needed ~= nil then
  950.                 if type(complete_from[i]) == type_needed then
  951.                     table.insert(out,#out+1,string.sub(complete_from[i],f+1))
  952.                 end
  953.             else
  954.                 table.insert(out,#out+1,string.sub(complete_from[i],f+1))
  955.             end
  956.         end
  957.     end
  958.     return out
  959. end
  960.  
  961. commands = {
  962.     resetSettings = function ()
  963.         defineSettings()
  964.         os.queueEvent("key_up",keys.f5)
  965.     end,
  966.     disable_logging = function ()
  967.         enable_logging = false
  968.         os.queueEvent("key_up",keys.f5)
  969.     end,
  970.     run_function = function()
  971.         local x,y = term.getCursorPos()
  972.         term.setCursorPos(2,y)
  973.         local t2 = read()
  974.         if _G[t2] ~= nil and type(_G[t2]) == "function" then
  975.             local x,y = term.getCursorPos()
  976.             term.setCursorPos(3,y)
  977.             print("Args: ")
  978.             term.setCursorPos(4,y+1)
  979.             local arg = read(nil,{},function(s) return AutoComplete(s,_G,"function") end)
  980.             print( _G[t2](arg) )
  981.         else
  982.             print("Function Not Found")
  983.         end
  984.     end,
  985.     list_buttons = function()
  986.         listAllButtons()
  987.     end,    
  988.     list_buttons_monitor = function ()
  989.         local x,y = term.getCursorPos()
  990.         term.setCursorPos(2,y)
  991.         local t2 = read(nil,{},function(s) return AutoComplete(s,mons) end)
  992.         if buttons[t2] ~= nil then
  993.             listAllButtonsInMonitor(t2)
  994.         end
  995.     end,
  996.     change_color = function ()
  997.         print("Change whats color: ")
  998.         local t2 = read(nil,nil,function(s) return AutoComplete(s,theme_colors) end)
  999.         local result
  1000.         for k,v in pairs(theme_colors) do
  1001.             if t2 == k then
  1002.                 result = k
  1003.             end
  1004.         end
  1005.         if result == nil then
  1006.             print("You didnt enter the name or key of a tab")
  1007.             return
  1008.         end
  1009.         print("To what color: ")
  1010.         local t2 = read(nil,nil,function(s) return AutoComplete(s,colors,"number") end)
  1011.         if colors[t2] ~= nil then
  1012.             theme_colors[result] = colors[t2]
  1013.             print("Color changed to "..t2.." for tab"..result)
  1014.         else
  1015.             print("You didnt enter a proper color")
  1016.             return
  1017.         end
  1018.     end
  1019. }
  1020. commandhist = {}
  1021.  
  1022. function eventHandler(event)
  1023.     log("eventHandler passed: "..event[1],1)
  1024.     if event[1] == "terminate" then
  1025.         term.clear()
  1026.         term.setTextColor(colors.red)
  1027.         term.setCursorPos(1,1)
  1028.         print("TERMINATING PLEASE WAIT")
  1029.         terminate = true
  1030.         running = false
  1031.         os.queueEvent("key_up",keys.f4)
  1032.     elseif event[1] == "monitor_touch" then
  1033.         button:process(event)
  1034.     elseif event[1] == "panic" then
  1035.         if event[2] == "halt" then
  1036.             terminate = true
  1037.             running = false
  1038.             term.setTextColor(colors.red)
  1039.             print("PANIC HALT WAS CALLED")
  1040.             os.queueEvent("key_up",keys.f4)
  1041.         else
  1042.             running = false
  1043.         end
  1044.     elseif event[1] == "peripheral_detach" then
  1045.         running = false
  1046.     elseif event[1] == "key_up" then        -- Keys
  1047.  
  1048.         if event[2] == keys.f5 then
  1049.             running = false
  1050.             os.queueEvent("key_up",keys.f4)
  1051.         elseif event[2] == keys.l then
  1052.             print("Command Line V.1:")
  1053.             local reading = true
  1054.             while reading ~= false do
  1055.                 local temp = read(nil,commandhist,function(s) return AutoComplete(s,commands) end)
  1056.                 if temp == "continue" or temp == "exit" then
  1057.                     reading = false
  1058.                     os.queueEvent("key_up",keys.f5)
  1059.                 elseif commands[temp] ~= nil then
  1060.                     table.insert(commandhist,#commandhist+1,temp)
  1061.                     commands[temp]()
  1062.                 else
  1063.                     print("Command Not Found")
  1064.                 end
  1065.             end
  1066.  
  1067.         elseif event[2] == keys.p then
  1068.             reShellTerm()
  1069.         end
  1070.     end
  1071. end
  1072.  
  1073. if always_load_settings == true then
  1074.     loadSettings()
  1075. else
  1076.     defineSettings()
  1077. end
  1078.  
  1079. reShellTerm()
  1080. while terminate == false do
  1081.     running = true
  1082.     log("refreshing",1)
  1083.     if auto_detect == true then
  1084.         autoDetect()
  1085.     end
  1086.     prep()
  1087.     tabDef()
  1088.     for i = 1,#mons do
  1089.         if monitor_settings[mons[i]] == nil then
  1090.             monitor_settings[mons[i]] = {}
  1091.         end
  1092.         if monitor_settings[mons[i]].currentTab == nil then
  1093.             monitor_settings[mons[i]].currentTab = default_monitor_tab
  1094.         end
  1095.         if monitor_settings[mons[i]].scale == nil then
  1096.             monitor_settings[mons[i]].scale = default_monitor_scale
  1097.         end
  1098.         if monitor_settings[mons[i]].excludeTabs == nil then
  1099.             monitor_settings[mons[i]].excludeTabs = {}
  1100.         end
  1101.         local crnt = peripheral.wrap(mons[i])
  1102.         crnt.setCursorPos(1,1)
  1103.         crnt.clear()
  1104.         crnt.setBackgroundColor(colors.black)
  1105.         crnt.setTextScale(monitor_settings[mons[i]].scale)
  1106.         topBar:display(mons[i])
  1107.         bottomBar:display(mons[i])
  1108.         bottomBar:addButtons(mons[i])
  1109.  
  1110.     end
  1111.     log("Starting loop",1)
  1112.     while running == true do
  1113.         getInfo()
  1114.         totals = {
  1115.             turbs = totalTable(turbs_data),
  1116.             pds = totalTable(pds_data),
  1117.             reacs = totalTable(reacs_data)
  1118.         }
  1119.         capi.tabeSave({totals,os.time()},"test2")
  1120.         log("Drawing to mons",1)
  1121.         for i = 1,#mons do
  1122.             local crnt = peripheral.wrap(mons[i])
  1123.             local crnttab = monitor_settings[mons[i]].currentTab
  1124.             if monitor_settings[mons[i]].lastTab ~= crnttab then
  1125.                 crnt.clear()
  1126.                 crnt.setCursorPos(1,1)
  1127.                 crnt.setBackgroundColor(colors.black)
  1128.                 topBar:display(mons[i])
  1129.                 bottomBar:display(mons[i])
  1130.                 log("'"..mons[i].."' Tab changed clearing",1)
  1131.             end
  1132.             monitor_settings[mons[i]].lastTab = crnttab
  1133.             crnt.setCursorPos(1,4)
  1134.             if tabs[crnttab] then
  1135.                 if tabs[crnttab].display then
  1136.                     tabs[crnttab]:display(mons[i])
  1137.                 else
  1138.                     log("Tab switched to a nil tab!",3)
  1139.                     monitor_settings[mons[i]].currentTab = "all"
  1140.                 end
  1141.             else
  1142.                 log("Tab switched to a nil tab!",3)
  1143.                 monitor_settings[mons[i]].currentTab = "all"
  1144.             end
  1145.         end
  1146.         wait(10)
  1147.     end
  1148. end
  1149.  
  1150. if always_load_settings == true then
  1151.     capi.tabeSave(monitor_settings,settings_save_file)
  1152. end
  1153.  
  1154. log("EOF reached",1)
  1155. capi.FileManager.CloseCurrentW()
  1156.  
  1157. -- You actually scrolled this far down...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement