Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- -- Code made by Tcharim --
- - Energy monitor for Computer Craft
- - This code allows you to show, on all pluged monitor, the quantity available in each pluged energy storage device (in percent).
- - This program can handle advanced monitors and regular ones.
- - The advised size for the monitor is 4 blocks width.
- - You can rename each device in file $(ALIAS_FILE_NAME) by associating "peripheral_name display_name"
- example:
- ae2:controller_0 My applied energestics 2 network controller
- - When writing in $(ALIAS_FILE_NAME) this text you display "My applied energestics 2 network controller" in place of ae2:controller_0
- ]]
- --[[
- - On some devices like powah energy cells, each update can disconnect the reconnect the device.
- - To counter this problem, I used a timer that remove cells that stays more than $(ENERGY_CELL_REMOVE_DELAY)
- seconds in the $(removedCells) list.
- - Each peripheral remove call add that peripheral in this list and associate it the time.
- - Each peripheral add call removes that peripheral from $(removedCells) list.
- ]]
- local ENERGY_CELL_REMOVE_DELAY = 1
- local removedCells = {}
- -- Declaration of some list to classify the devices
- local monitors = {}
- local energyCells = {}
- local otherPeripheral = {}
- -- Declariation of list of aliases
- local ALIAS_FILE_NAME = "/hosts"
- local aliases = {}
- -- Function that write $(text) into $(monitor) with $(textColor) foreground and $(bgColor) background at x=$(posx), y=$(posy)
- local function writeText(monitor, text, textColor, bgColor, posx, posy)
- if not monitor then
- return -1
- end
- local oldTextColor = monitor.getTextColor()
- local oldBgColor = monitor.getBackgroundColor()
- if not textColor or not monitor.isColor() then
- textColor = colors.white
- end
- if not bgColor or not monitor.isColor()then
- bgColor = colors.black
- end
- posx = posx or 1
- posy = posy or 1
- monitor.setTextColor(textColor)
- monitor.setBackgroundColor(bgColor)
- monitor.setCursorPos(posx, posy)
- monitor.write(text)
- monitor.setTextColor(oldTextColor)
- monitor.setBackgroundColor(oldBgColor)
- return 0
- end
- -- Function that list all the energy storage connnected devices and associate it the stored energy, in percent, then show it on $(monitor)
- local function showOnMonitor(monitor)
- local RED_LEVEL = 10
- local ORANGE_LEVEL = 50
- local YELLOW_LEVEL = 70
- local stored, capacity
- local row = 1
- local monitor_width, _ = monitor.getSize()
- local text = ""
- monitor.clear()
- text = "Energy Monitor"
- writeText(monitor, text, colors.red, colors.yellow, (monitor_width-string.len(text))/2,1)
- for energyCellName, cell in pairs(energyCells) do
- if cell then
- name = aliases[energyCellName] or energyCellName
- name = string.sub(name, 1, monitor_width - 12)
- if not pcall(function() stored = cell.getEnergy() end) then goto continue end
- if not pcall(function() capacity = cell.getEnergyCapacity() end) then goto continue end
- if not (stored and capacity) then goto continue end
- row = row + 1
- if capacity == 0 then
- writeText(monitor, "Capacity of "..name.." is 0??", red, nil, 1, row)
- goto continue
- end
- writeText(monitor, name, nil, nil, 1, row)
- writeText(monitor, "|", nil, nil, monitor_width - 10, row)
- color = colors.white
- energyLevel = stored/capacity * 100
- if(energyLevel < RED_LEVEL) then
- color = colors.red
- elseif(energyLevel < ORANGE_LEVEL)then
- color = colors.orange
- elseif(energyLevel < YELLOW_LEVEL) then
- color = colors.yellow
- else
- color = colors.green
- end
- text = string.format("%6.2f %%", energyLevel)
- writeText(monitor, text, color, nil, monitor_width - 7, row)
- ::continue::
- end
- end
- if row == 1 then
- text = "There is no energy device connected."
- writeText(monitor, text, colors.red, nil, 1, row+1)
- end
- end
- -- Function that add the peripheral $(name) into the associated list.
- local function addPeripheral(name)
- if peripheral.hasType(name, "monitor") then
- monitors[name] = peripheral.wrap(name)
- elseif peripheral.hasType(name, "energy_storage") then
- cell = peripheral.wrap(name)
- if cell then
- energyCells[name] = peripheral.wrap(name)
- removedCells[name] = nil
- end
- else
- otherPeripheral[name] = peripheral.getType(name)
- end
- end
- -- Function that remove the peripheral $(name) from the lists.
- local function removePeripheral(name)
- if monitors[name] then monitors[name] = nil end
- if otherPeripheral[name] then otherPeripheral[name] = nil end
- if energyCells[name] then
- removedCells[name] = os.clock()
- end
- end
- -- Function that refreshes all the connected monitors.
- local function refresh()
- local REFRESH_DELAY = 2
- local previousTime = 0
- while true do
- sleep(0.05)
- if os.clock()-previousTime > REFRESH_DELAY then
- for monitorName, monitor in pairs(monitors) do
- showOnMonitor(monitor)
- end
- previousTime = os.clock()
- end
- for name, time in pairs(removedCells) do
- if os.clock() - time > ENERGY_CELL_REMOVE_DELAY then
- energyCells[name] = nil
- removedCells[name] = nil
- end
- end
- end
- end
- -- Function that handle event and manage them.
- local function eventManager()
- while true do
- local eventData = {os.pullEventRaw()}
- if eventData[1] == "peripheral" then
- addPeripheral(eventData[2])
- elseif eventData[1] == "peripheral_detach" then
- removePeripheral(eventData[2])
- elseif eventData[1] == "terminate" then
- print("Caught a terminate event")
- break
- end
- end
- end
- -- Associate device name to alias from file $(ALIAS_FILE_NAME) in $(aliases) list
- if fs.exists(ALIAS_FILE_NAME) then
- for line in io.lines(ALIAS_FILE_NAME, "l") do
- local original = ""
- local alias = ""
- local i = 1
- for token in string.gmatch(line, "[^%s]+") do
- if i == 1 then
- original = token
- elseif i == 2 then
- alias = token
- else
- alias = alias.." "..token
- end
- i = i + 1
- end
- aliases[original] = alias
- end
- end
- -- At program startup this loop allows us to list all plugged devices.
- for _,name in ipairs(peripheral.getNames()) do
- addPeripheral(name)
- end
- -- Run in parallel eventManager function and refredh() function
- parallel.waitForAny(eventManager, refresh)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement