Advertisement
Tcharim

Computer Craft Energy monitoring V2

Nov 25th, 2024 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.96 KB | Source Code | 0 0
  1. --[[
  2.     -- Code made by Tcharim --
  3.     - Energy monitor for Computer Craft
  4.     - This code allows you to show, on all pluged monitor, the quantity available in each pluged energy storage device (in percent).
  5.     - This program can handle advanced monitors and regular ones.
  6.     - The advised size for the monitor is 4 blocks width.
  7.     - You can rename each device in file $(ALIAS_FILE_NAME) by associating "peripheral_name display_name"
  8.     example:
  9.     ae2:controller_0 My applied energestics 2 network controller
  10.  
  11.     - When writing in $(ALIAS_FILE_NAME) this text you display "My applied energestics 2 network controller" in place of ae2:controller_0
  12. ]]
  13.  
  14. --[[
  15.     - On some devices like powah energy cells, each update can disconnect the reconnect the device.
  16.     - To counter this problem, I used a timer that remove cells that stays more than $(ENERGY_CELL_REMOVE_DELAY)
  17.     seconds in the $(removedCells) list.
  18.     - Each peripheral remove call add that peripheral in this list and associate it the time.
  19.     - Each peripheral add call removes that peripheral from $(removedCells) list.
  20. ]]
  21. local ENERGY_CELL_REMOVE_DELAY = 1
  22. local removedCells = {}
  23.  
  24. -- Declaration of some list to classify the devices
  25. local monitors = {}
  26. local energyCells = {}
  27. local otherPeripheral = {}
  28.  
  29. -- Declariation of list of aliases
  30. local ALIAS_FILE_NAME = "/hosts"
  31. local aliases = {}
  32.  
  33. -- Function that write $(text) into $(monitor) with $(textColor) foreground and $(bgColor) background at x=$(posx), y=$(posy)
  34. local function writeText(monitor, text, textColor, bgColor, posx, posy)
  35.     if not monitor then
  36.         return -1
  37.     end
  38.  
  39.     local oldTextColor = monitor.getTextColor()
  40.     local oldBgColor = monitor.getBackgroundColor()
  41.  
  42.     if not textColor or not monitor.isColor() then
  43.         textColor = colors.white
  44.     end
  45.  
  46.     if not bgColor or not monitor.isColor()then
  47.         bgColor = colors.black
  48.     end
  49.  
  50.     posx = posx or 1
  51.     posy = posy or 1
  52.  
  53.     monitor.setTextColor(textColor)
  54.     monitor.setBackgroundColor(bgColor)
  55.     monitor.setCursorPos(posx, posy)
  56.  
  57.     monitor.write(text)
  58.  
  59.     monitor.setTextColor(oldTextColor)
  60.     monitor.setBackgroundColor(oldBgColor)
  61.  
  62.     return 0
  63. end
  64.  
  65. -- Function that list all the energy storage connnected devices and associate it the stored energy, in percent, then show it on $(monitor)
  66. local function showOnMonitor(monitor)
  67.     local RED_LEVEL = 10
  68.     local ORANGE_LEVEL = 50
  69.     local YELLOW_LEVEL = 70
  70.  
  71.     local stored, capacity
  72.     local row = 1
  73.     local monitor_width, _ = monitor.getSize()
  74.     local text = ""
  75.  
  76.     monitor.clear()
  77.     text = "Energy Monitor"
  78.     writeText(monitor, text, colors.red, colors.yellow, (monitor_width-string.len(text))/2,1)
  79.  
  80.     for energyCellName, cell in pairs(energyCells) do
  81.         if cell then
  82.             name = aliases[energyCellName] or energyCellName
  83.             name = string.sub(name, 1, monitor_width - 12)
  84.             if not pcall(function() stored = cell.getEnergy() end) then goto continue end
  85.             if not pcall(function() capacity = cell.getEnergyCapacity() end) then goto continue end
  86.             if not (stored and capacity) then goto continue end
  87.  
  88.             row = row + 1
  89.             if capacity == 0 then
  90.                 writeText(monitor, "Capacity of "..name.." is 0??", red, nil, 1, row)
  91.                 goto continue
  92.             end
  93.  
  94.             writeText(monitor, name, nil, nil, 1, row)
  95.             writeText(monitor, "|", nil, nil, monitor_width - 10, row)
  96.             color = colors.white
  97.             energyLevel = stored/capacity * 100
  98.  
  99.             if(energyLevel < RED_LEVEL) then
  100.                 color = colors.red
  101.             elseif(energyLevel < ORANGE_LEVEL)then
  102.                 color = colors.orange
  103.             elseif(energyLevel < YELLOW_LEVEL) then
  104.                 color = colors.yellow
  105.             else
  106.                 color = colors.green
  107.             end
  108.  
  109.             text = string.format("%6.2f %%", energyLevel)
  110.             writeText(monitor, text, color, nil, monitor_width - 7, row)
  111.  
  112.             ::continue::
  113.         end
  114.     end
  115.  
  116.     if row == 1 then
  117.         text = "There is no energy device connected."
  118.         writeText(monitor, text, colors.red, nil, 1, row+1)
  119.     end
  120. end
  121.  
  122. -- Function that add the peripheral $(name) into the associated list.
  123. local function addPeripheral(name)
  124.     if peripheral.hasType(name, "monitor") then
  125.         monitors[name] = peripheral.wrap(name)
  126.     elseif peripheral.hasType(name, "energy_storage") then
  127.         cell = peripheral.wrap(name)
  128.         if cell then
  129.             energyCells[name] = peripheral.wrap(name)
  130.             removedCells[name] = nil
  131.         end
  132.     else
  133.         otherPeripheral[name] = peripheral.getType(name)
  134.     end
  135. end
  136.  
  137. -- Function that remove the peripheral $(name) from the lists.
  138. local function removePeripheral(name)
  139.     if monitors[name] then monitors[name] = nil end
  140.     if otherPeripheral[name] then otherPeripheral[name] = nil end
  141.     if energyCells[name] then
  142.         removedCells[name] = os.clock()
  143.     end
  144. end
  145.  
  146. -- Function that refreshes all the connected monitors.
  147. local function refresh()
  148.     local REFRESH_DELAY = 2
  149.     local previousTime = 0
  150.     while true do
  151.         sleep(0.05)
  152.         if os.clock()-previousTime > REFRESH_DELAY then
  153.             for monitorName, monitor in pairs(monitors) do
  154.                 showOnMonitor(monitor)
  155.             end
  156.             previousTime = os.clock()
  157.         end
  158.  
  159.         for name, time in pairs(removedCells) do
  160.             if os.clock() - time > ENERGY_CELL_REMOVE_DELAY then
  161.                 energyCells[name] = nil
  162.                 removedCells[name] = nil
  163.             end
  164.         end
  165.     end
  166. end
  167.  
  168. -- Function that handle event and manage them.
  169. local function eventManager()
  170.     while true do
  171.         local eventData = {os.pullEventRaw()}
  172.  
  173.         if eventData[1] == "peripheral" then
  174.             addPeripheral(eventData[2])
  175.         elseif eventData[1] == "peripheral_detach" then
  176.             removePeripheral(eventData[2])
  177.         elseif eventData[1] == "terminate" then
  178.             print("Caught a terminate event")
  179.             break
  180.         end
  181.     end
  182. end
  183.  
  184. -- Associate device name to alias from file $(ALIAS_FILE_NAME) in $(aliases) list
  185. if fs.exists(ALIAS_FILE_NAME) then
  186.    
  187.     for line in io.lines(ALIAS_FILE_NAME, "l") do
  188.         local original = ""
  189.         local alias = ""
  190.         local i = 1
  191.         for token in string.gmatch(line, "[^%s]+") do
  192.             if i == 1 then
  193.                 original = token
  194.             elseif i == 2 then
  195.                 alias = token
  196.             else
  197.                 alias = alias.." "..token
  198.             end
  199.             i = i + 1
  200.         end
  201.  
  202.         aliases[original] = alias
  203.     end
  204. end
  205.  
  206. -- At program startup this loop allows us to list all plugged devices.
  207. for _,name in ipairs(peripheral.getNames()) do
  208.     addPeripheral(name)
  209. end
  210.  
  211. -- Run in parallel eventManager function and refredh() function
  212. parallel.waitForAny(eventManager, refresh)
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement