Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local terminalGlassesBridge
- local wrappedPeripherals = {}
- local storageFunctions
- local textObjects = {}
- local boxObjects = {}
- local function addPeripheral(peripheralName,barColor)
- wrappedPeripherals[peripheralName] = peripheral.wrap(peripheralName)
- textObjects[peripheralName] = {}
- textObjects[peripheralName][1] = terminalGlassesBridge.addText(1,0,"",barColor)
- textObjects[peripheralName][2] = terminalGlassesBridge.addText(0,70,"",0xFFFFFF)
- textObjects[peripheralName][2].setScale(0.5)
- textObjects[peripheralName][2].setZIndex(4)
- boxObjects[peripheralName] = {}
- boxObjects[peripheralName][1] = terminalGlassesBridge.addBox(0,66,20,0,barColor,0.8)
- boxObjects[peripheralName][1].setZIndex(3)
- boxObjects[peripheralName][2] = terminalGlassesBridge.addBox(0,66,20,0,0x000000,0.5)
- boxObjects[peripheralName][2].setZIndex(2)
- end
- local function updateReadings()
- while true do
- local peripheralCounter = 0
- for k,v in pairs(wrappedPeripherals) do
- textObjects[k][1].setY(peripheralCounter*9+1)
- textObjects[k][1].setText(k..": "..tostring(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()))
- textObjects[k][2].setText(tostring(math.floor(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()/wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][2]]()*100)).."%")
- textObjects[k][2].setX(22*peripheralCounter+11+math.ceil((24-textObjects[k][2].getWidth())/4))
- local pixelCount = math.floor(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()/wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][2]]()*142)
- boxObjects[k][1].setHeight(pixelCount)
- boxObjects[k][1].setY(66+(142-pixelCount))
- boxObjects[k][1].setX(22*peripheralCounter+7)
- boxObjects[k][2].setHeight(142-pixelCount)
- boxObjects[k][2].setX(22*peripheralCounter+7)
- peripheralCounter = peripheralCounter + 1
- end
- boxObjects["backgroundShader"].setWidth(22*peripheralCounter+2)
- sleep(0)
- end
- end
- local function commandListener()
- while true do
- local words = {}
- local eventData = {os.pullEvent("chat_command")}
- for word in string.gmatch(eventData[2],"%S+") do
- table.insert(words,word)
- end
- if words[1] == "add" and not wrappedPeripherals[words[2]] then
- addPeripheral(words[2],math.random(0,16777215))
- for k,v in pairs(words) do
- if string.sub(v,1,6) == "color:" then
- textObjects[words[2]][1].setColor(tonumber(string.match(v,"%d.+")))
- boxObjects[words[2]][1].setColor(tonumber(string.match(v,"%d.+")))
- elseif string.sub(v,1,6) == "label:" then
- tonumber(string.match(v,"[^:]+$"))
- end
- end
- local fileHandle = io.open(".energyMonitor_peripherals","w")
- for k,v in pairs(wrappedPeripherals) do
- fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor()}).."\n")
- end
- fileHandle:close()
- elseif words[1] == "remove" then
- wrappedPeripherals[words[2]] = nil
- textObjects[words[2]][1].delete()
- textObjects[words[2]][2].delete()
- textObjects[words[2]] = nil
- boxObjects[words[2]][1].delete()
- boxObjects[words[2]][2].delete()
- boxObjects[words[2]] = nil
- local fileHandle = io.open(".energyMonitor_peripherals","w")
- for k,v in pairs(wrappedPeripherals) do
- fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor()}).."\n")
- end
- fileHandle:close()
- elseif words[1] == "exit" then
- return
- end
- end
- end
- for k,v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "terminal_glasses_bridge" then
- terminalGlassesBridge = peripheral.wrap(v)
- end
- end
- if not terminalGlassesBridge then
- print("Please attach a Terminal Glasses Bridge to any side.")
- return
- end
- if not fs.exists(".energyMonitor_functions") then io.open(".energyMonitor_functions","w"):close() end
- if not fs.exists(".energyMonitor_peripherals") then io.open(".energyMonitor_peripherals","w"):close() end
- local fileHandle = io.open(".energyMonitor_functions","r")
- storageFunctions = textutils.unserialize(fileHandle:read())
- fileHandle:close()
- local fileHandle = io.open(".energyMonitor_peripherals","r")
- for line in fileHandle:lines() do
- line = textutils.unserialize(line)
- addPeripheral(line[1],line[2])
- end
- fileHandle:close()
- terminalGlassesBridge.clear()
- boxObjects["backgroundShader"] = terminalGlassesBridge.addBox(5,45,0,165,0x000000,0.45)
- boxObjects["backgroundShader"].setZIndex(1)
- parallel.waitForAny(updateReadings,commandListener)
- terminalGlassesBridge.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement