Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --0x0B4387
- --0x4E8741
- local terminalGlassesBridge
- local wrappedPeripherals = {}
- local storageFunctions
- local textObjects = {}
- local boxObjects = {}
- local function printInfoText(text)
- textObjects["infoText"] = terminalGlassesBridge.addText(2,2,text,0xFFFFFF)
- end
- local function addPeripheral(peripheralName,barColor,label)
- if not peripheral.getType(peripheralName) then
- textObjects["infoText"].setText("Invalid peripheral")
- return
- elseif not storageFunctions[peripheral.getType(peripheralName)] then
- textObjects["infoText"].setText("Function rules not found for peripheral type '"..peripheral.getType(peripheralName).."'. To add them, use the command '$$rule <type> <getStoredRule> <getCapacityRule>', e.g. '$$rule batbox getStored getCapacity' for a BatBox, MFE or MFSU.")
- return
- end
- if type(barColor) ~= "number" then
- barColor = math.random(0,16777215)
- end
- if type(label) ~= "string" then
- label = ""
- end
- if not wrappedPeripherals[peripheralName] then
- wrappedPeripherals[peripheralName] = peripheral.wrap(peripheralName)
- textObjects[peripheralName] = {}
- textObjects[peripheralName][1] = terminalGlassesBridge.addText(0,0,label,0xFFFFFF)
- --textObjects[peripheralName][1] = terminalGlassesBridge.addText(1,0,"",barColor)
- textObjects[peripheralName][2] = terminalGlassesBridge.addText(0,70,"",0xFFFFFF)
- boxObjects[peripheralName] = {}
- boxObjects[peripheralName][1] = terminalGlassesBridge.addBox(0,66,20,0,barColor,0.8)
- boxObjects[peripheralName][2] = terminalGlassesBridge.addBox(0,66,20,0,0x000000,0.5)
- end
- textObjects[peripheralName][1].setText(label)
- textObjects[peripheralName][1].setScale(1.75-#textObjects[peripheralName][1].getText()*0.25)
- textObjects[peripheralName][1].setY(55)
- textObjects[peripheralName][2].setScale(0.5)
- textObjects[peripheralName][2].setZIndex(4)
- boxObjects[peripheralName][1].setZIndex(3)
- boxObjects[peripheralName][1].setColor(barColor)
- 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].setX(22*peripheralCounter+8)
- --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()
- local function add(words)
- local barColor
- local label = ""
- for k,v in pairs(words) do
- if string.sub(v,1,6) == "color:" then
- barColor = tonumber(string.match(v,"%d.+"))
- elseif string.sub(v,1,6) == "label:" then
- label = string.match(v,"[^:]+$")
- end
- end
- addPeripheral(words[2],barColor,label)
- local fileHandle = io.open(".energyMonitor_peripherals","w")
- for k,v in pairs(wrappedPeripherals) do
- fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor(),textObjects[k][1].getText()}).."\n")
- end
- fileHandle:close()
- end
- local function rule(words)
- storageFunctions[words[2]] = {words[3],words[4]}
- local fileHandle = io.open(".energyMonitor_functions","w")
- fileHandle:write(textutils.serialize(storageFunctions))
- fileHandle:close()
- end
- local function remove(words)
- 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()
- end
- local function exit()
- terminalGlassesBridge.clear()
- error()
- end
- local function reload()
- shell.run("hud") -- autodetect the path
- error()
- end
- local commands = {
- ["add"]=add,
- ["rule"]=rule,
- ["remove"]=remove,
- ["exit"]=exit,
- ["reload"]=reload
- }
- while true do
- local words = {}
- local eventData = {os.pullEvent("chat_command")}
- textObjects["infoText"].setText("")
- for word in string.gmatch(eventData[2],"%S+") do
- table.insert(words,word)
- end
- if commands[words[1]] then
- commands[words[1]](words)
- 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()
- terminalGlassesBridge.clear()
- local fileHandle = io.open(".energyMonitor_peripherals","r")
- for line in fileHandle:lines() do
- line = textutils.unserialize(line)
- addPeripheral(line[1],line[2],line[3])
- end
- fileHandle:close()
- boxObjects["backgroundShader"] = terminalGlassesBridge.addBox(5,45,0,165,0x000000,0.45)
- boxObjects["backgroundShader"].setZIndex(1)
- parallel.waitForAny(updateReadings,commandListener)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement