SHOW:
|
|
- or go back to the newest paste.
1 | - | --0x0B4387 |
1 | + | |
2 | - | --0x4E8741 |
2 | + | |
3 | local storageFunctions | |
4 | local textObjects = {} | |
5 | local boxObjects = {} | |
6 | ||
7 | local function addPeripheral(peripheralName,barColor) | |
8 | wrappedPeripherals[peripheralName] = peripheral.wrap(peripheralName) | |
9 | textObjects[peripheralName] = {} | |
10 | - | local function printInfoText(text) |
10 | + | textObjects[peripheralName][1] = terminalGlassesBridge.addText(1,0,"",barColor) |
11 | - | textObjects["infoText"] = terminalGlassesBridge.addText(2,2,text,0xFFFFFF) |
11 | + | textObjects[peripheralName][2] = terminalGlassesBridge.addText(0,70,"",0xFFFFFF) |
12 | textObjects[peripheralName][2].setScale(0.5) | |
13 | textObjects[peripheralName][2].setZIndex(4) | |
14 | - | local function addPeripheral(peripheralName,barColor,label) |
14 | + | boxObjects[peripheralName] = {} |
15 | - | if not peripheral.getType(peripheralName) then |
15 | + | boxObjects[peripheralName][1] = terminalGlassesBridge.addBox(0,66,20,0,barColor,0.8) |
16 | - | textObjects["infoText"].setText("Invalid peripheral") |
16 | + | |
17 | - | return |
17 | + | boxObjects[peripheralName][2] = terminalGlassesBridge.addBox(0,66,20,0,0x000000,0.5) |
18 | - | elseif not storageFunctions[peripheral.getType(peripheralName)] then |
18 | + | |
19 | - | 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.") |
19 | + | |
20 | - | return |
20 | + | |
21 | local function updateReadings() | |
22 | - | if type(barColor) ~= "number" then |
22 | + | |
23 | - | barColor = math.random(0,16777215) |
23 | + | |
24 | for k,v in pairs(wrappedPeripherals) do | |
25 | - | if type(label) ~= "string" then |
25 | + | textObjects[k][1].setY(peripheralCounter*9+1) |
26 | - | label = "" |
26 | + | textObjects[k][1].setText(k..": "..tostring(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]())) |
27 | textObjects[k][2].setText(tostring(math.floor(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()/wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][2]]()*100)).."%") | |
28 | - | if not wrappedPeripherals[peripheralName] then |
28 | + | |
29 | - | wrappedPeripherals[peripheralName] = peripheral.wrap(peripheralName) |
29 | + | |
30 | - | textObjects[peripheralName] = {} |
30 | + | |
31 | - | textObjects[peripheralName][1] = terminalGlassesBridge.addText(0,0,label,0xFFFFFF) |
31 | + | |
32 | - | --textObjects[peripheralName][1] = terminalGlassesBridge.addText(1,0,"",barColor) |
32 | + | |
33 | - | textObjects[peripheralName][2] = terminalGlassesBridge.addText(0,70,"",0xFFFFFF) |
33 | + | |
34 | - | boxObjects[peripheralName] = {} |
34 | + | |
35 | - | boxObjects[peripheralName][1] = terminalGlassesBridge.addBox(0,66,20,0,barColor,0.8) |
35 | + | |
36 | - | boxObjects[peripheralName][2] = terminalGlassesBridge.addBox(0,66,20,0,0x000000,0.5) |
36 | + | |
37 | boxObjects["backgroundShader"].setWidth(22*peripheralCounter+2) | |
38 | - | textObjects[peripheralName][1].setText(label) |
38 | + | |
39 | - | textObjects[peripheralName][1].setScale(1.75-#textObjects[peripheralName][1].getText()*0.25) |
39 | + | |
40 | - | textObjects[peripheralName][1].setY(55) |
40 | + | |
41 | ||
42 | local function commandListener() | |
43 | while true do | |
44 | - | boxObjects[peripheralName][1].setColor(barColor) |
44 | + | |
45 | local eventData = {os.pullEvent("chat_command")} | |
46 | for word in string.gmatch(eventData[2],"%S+") do | |
47 | table.insert(words,word) | |
48 | end | |
49 | if words[1] == "add" and not wrappedPeripherals[words[2]] then | |
50 | addPeripheral(words[2],math.random(0,16777215)) | |
51 | for k,v in pairs(words) do | |
52 | - | --textObjects[k][1].setY(peripheralCounter*9+1) |
52 | + | if string.sub(v,1,6) == "color:" then |
53 | - | textObjects[k][1].setX(22*peripheralCounter+8) |
53 | + | textObjects[words[2]][1].setColor(tonumber(string.match(v,"%d.+"))) |
54 | - | --textObjects[k][1].setText(k..": "..tostring(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]())) |
54 | + | boxObjects[words[2]][1].setColor(tonumber(string.match(v,"%d.+"))) |
55 | elseif string.sub(v,1,6) == "label:" then | |
56 | tonumber(string.match(v,"[^:]+$")) | |
57 | end | |
58 | end | |
59 | local fileHandle = io.open(".energyMonitor_peripherals","w") | |
60 | for k,v in pairs(wrappedPeripherals) do | |
61 | fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor()}).."\n") | |
62 | end | |
63 | fileHandle:close() | |
64 | elseif words[1] == "remove" then | |
65 | wrappedPeripherals[words[2]] = nil | |
66 | textObjects[words[2]][1].delete() | |
67 | textObjects[words[2]][2].delete() | |
68 | textObjects[words[2]] = nil | |
69 | boxObjects[words[2]][1].delete() | |
70 | boxObjects[words[2]][2].delete() | |
71 | - | |
71 | + | boxObjects[words[2]] = nil |
72 | - | local function add(words) |
72 | + | local fileHandle = io.open(".energyMonitor_peripherals","w") |
73 | - | local barColor |
73 | + | for k,v in pairs(wrappedPeripherals) do |
74 | - | local label = "" |
74 | + | fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor()}).."\n") |
75 | - | for k,v in pairs(words) do |
75 | + | |
76 | - | if string.sub(v,1,6) == "color:" then |
76 | + | fileHandle:close() |
77 | - | barColor = tonumber(string.match(v,"%d.+")) |
77 | + | elseif words[1] == "exit" then |
78 | - | elseif string.sub(v,1,6) == "label:" then |
78 | + | return |
79 | - | label = string.match(v,"[^:]+$") |
79 | + | |
80 | end | |
81 | end | |
82 | - | addPeripheral(words[2],barColor,label) |
82 | + | |
83 | - | local fileHandle = io.open(".energyMonitor_peripherals","w") |
83 | + | |
84 | if peripheral.getType(v) == "terminal_glasses_bridge" then | |
85 | - | fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor(),textObjects[k][1].getText()}).."\n") |
85 | + | |
86 | end | |
87 | - | fileHandle:close() |
87 | + | |
88 | ||
89 | - | |
89 | + | |
90 | - | local function rule(words) |
90 | + | |
91 | - | storageFunctions[words[2]] = {words[3],words[4]} |
91 | + | |
92 | - | local fileHandle = io.open(".energyMonitor_functions","w") |
92 | + | |
93 | - | fileHandle:write(textutils.serialize(storageFunctions)) |
93 | + | |
94 | - | fileHandle:close() |
94 | + | |
95 | if not fs.exists(".energyMonitor_peripherals") then io.open(".energyMonitor_peripherals","w"):close() end | |
96 | - | |
96 | + | |
97 | - | local function remove(words) |
97 | + | |
98 | - | wrappedPeripherals[words[2]] = nil |
98 | + | |
99 | - | textObjects[words[2]][1].delete() |
99 | + | |
100 | - | textObjects[words[2]][2].delete() |
100 | + | |
101 | - | textObjects[words[2]] = nil |
101 | + | |
102 | - | boxObjects[words[2]][1].delete() |
102 | + | |
103 | - | boxObjects[words[2]][2].delete() |
103 | + | |
104 | - | boxObjects[words[2]] = nil |
104 | + | addPeripheral(line[1],line[2]) |
105 | - | local fileHandle = io.open(".energyMonitor_peripherals","w") |
105 | + | |
106 | fileHandle:close() | |
107 | - | fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor()}).."\n") |
107 | + | |
108 | terminalGlassesBridge.clear() | |
109 | - | fileHandle:close() |
109 | + | |
110 | boxObjects["backgroundShader"].setZIndex(1) | |
111 | - | |
111 | + | parallel.waitForAny(updateReadings,commandListener) |
112 | - | local function exit() |
112 | + | terminalGlassesBridge.clear() |