SHOW:
|
|
- or go back to the newest paste.
1 | --[[ | |
2 | Base Monitor 1.0 | |
3 | This script allows you to monitor caches, tanks, barrels and chests | |
4 | Monitoring chests requires an open peripheral proxy, the same goes for | |
5 | buildcraft tanks. | |
6 | You can build one of more computers and set one of them as the main server by | |
7 | setting isMain to true. This will let it receive information from other computers. | |
8 | For the main server to receive info from other computers you need to attach a | |
9 | wireless modem to each computer. | |
10 | Peripherals can be attached to any side you like, the script will automatically | |
11 | work out where the peripherals are. | |
12 | http://youtu.be/KT-2hKjUpGA | |
13 | --]] | |
14 | -- Below are values you can change | |
15 | local isMain = false; | |
16 | --local id = os.computerID(); | |
17 | local id = "The Base"; | |
18 | ||
19 | local titleTextColor = colors.blue; | |
20 | local titleBackColor = colors.white; | |
21 | local tankTextColor = colors.black; | |
22 | local tankBackColor = colors.lime; | |
23 | local chestTextColor = colors.white; | |
24 | local chestBackColor = colors.purple; | |
25 | local cacheTextColor = colors.white; | |
26 | local cacheBackColor = colors.cyan; | |
27 | local powerTextColor = colors.black; | |
28 | local powerBackColor = colors.orange; | |
29 | ||
30 | local NameLen = 21; | |
31 | local mainChannel = 2; | |
32 | ||
33 | -- Above are values you can change | |
34 | ||
35 | local peripherals = peripheral.getNames(); | |
36 | local mon; | |
37 | local wmod; | |
38 | local x,y; | |
39 | local CurColumn = 0; | |
40 | local MaxColumn; | |
41 | local ColumnWidth; | |
42 | local CurLine = 2; | |
43 | local ContentData = {}; | |
44 | ||
45 | function padString (sText, iLen) | |
46 | local iTextLen = string.len(sText); | |
47 | -- Too short, pad | |
48 | if (iTextLen < iLen) then | |
49 | local iDiff = iLen - iTextLen; | |
50 | return(sText..string.rep(" ",iDiff)); | |
51 | end | |
52 | -- Too long, trim | |
53 | if (iTextLen > iLen) then | |
54 | return(string.sub(sText,1,iLen)); | |
55 | end | |
56 | -- Exact length | |
57 | return(sText); | |
58 | end | |
59 | ||
60 | function prepmonitor(objectmon) | |
61 | mon = peripheral.wrap(objectmon); | |
62 | if (mon.isColor() == false) then | |
63 | titleTextColor = colors.black; | |
64 | titleBackColor = colors.white; | |
65 | tankTextColor = colors.black; | |
66 | tankBackColor = colors.white; | |
67 | chestTextColor = colors.black; | |
68 | chestBackColor = colors.white; | |
69 | cacheTextColor = colors.black; | |
70 | cacheBackColor = colors.white; | |
71 | powerTextColor = colors.black; | |
72 | powerBackColor = colors.white; | |
73 | end | |
74 | end | |
75 | ||
76 | function updateTable(strSource,strName,strAmount,strMax,strLegend) | |
77 | ContentData[strSource] = {}; | |
78 | ContentData[strSource]["displayname"] = strName; | |
79 | ContentData[strSource]["count"] = strAmount; | |
80 | ContentData[strSource]["max"] = strMax; | |
81 | ContentData[strSource]["legend"] = strLegend; | |
82 | end | |
83 | ||
84 | function printmon(strName,strAmount,strMax,strLegend) | |
85 | local textColor; | |
86 | local backColor; | |
87 | if (strLegend == "#") then | |
88 | textColor = chestTextColor; | |
89 | backColor = chestBackColor; | |
90 | strLegend = ""; | |
91 | end | |
92 | if (strLegend == "+") then | |
93 | textColor = tankTextColor; | |
94 | backColor = tankBackColor; | |
95 | strLegend = ""; | |
96 | end | |
97 | if (strLegend == "*") then | |
98 | textColor = powerTextColor; | |
99 | backColor = powerBackColor; | |
100 | strLegend = ""; | |
101 | end | |
102 | if (strLegend == "$") then | |
103 | textColor = cacheTextColor; | |
104 | backColor = cacheBackColor; | |
105 | strLegend = ""; | |
106 | end | |
107 | local line = string.format("%s %3i%s",padString(strName,NameLen+1),strAmount,padString(strLegend,1)); | |
108 | if (strAmount >= 1000000) then | |
109 | line = string.format("%s %3iM%s",padString(strName,NameLen),math.floor(strAmount/1000000),padString(strLegend,1)); | |
110 | elseif (strAmount >= 1000) then | |
111 | line = string.format("%s %3iK%s",padString(strName,NameLen),math.floor(strAmount/1000),padString(strLegend,1)); | |
112 | end | |
113 | ||
114 | local ColPadding = 0; | |
115 | if (CurColumn > 0) then | |
116 | ColPadding = 1; | |
117 | end | |
118 | local CurX = math.floor((CurColumn*ColumnWidth))+math.floor(CurColumn*ColPadding)+1; | |
119 | --print("CurX:"..CurX); | |
120 | mon.setCursorPos(CurX,CurLine); | |
121 | local percent = strAmount / strMax * 100; | |
122 | mon.setBackgroundColor(backColor); | |
123 | if (strMax == 0) then | |
124 | percent = 0; | |
125 | end | |
126 | local barlength = math.floor(percent / 100 * (string.len(line)-1)); | |
127 | ||
128 | if (string.len(line) > barlength) then | |
129 | local msg = string.sub(line,1,barlength); | |
130 | mon.setTextColor(textColor); | |
131 | mon.write(msg); | |
132 | --[[if (percent == 0) then | |
133 | mon.setBackgroundColor(); | |
134 | else | |
135 | mon.setBackgroundColor(colors.black); | |
136 | end--]] | |
137 | mon.setBackgroundColor(colors.black); | |
138 | mon.setTextColor(backColor); | |
139 | mon.write(string.sub(line,barlength+1,-2)) | |
140 | else | |
141 | local spaces = barlength - string.len(line); | |
142 | mon.write(line); | |
143 | mon.write(string.rep(" ",spaces)); | |
144 | end | |
145 | ||
146 | mon.setTextColor(colors.white); | |
147 | CurColumn = CurColumn + 1; | |
148 | if (CurColumn > MaxColumn) then | |
149 | CurColumn = 0; | |
150 | CurLine = CurLine + 1; | |
151 | end | |
152 | return true; | |
153 | end | |
154 | ||
155 | -- Find a monitor | |
156 | function findMonitor() | |
157 | for i,name in pairs(peripherals) do | |
158 | for j,method in pairs(peripheral.getMethods(name)) do | |
159 | if (method == 'getCursorPos') then | |
160 | prepmonitor(name); | |
161 | end | |
162 | end | |
163 | end | |
164 | end | |
165 | ||
166 | -- Find a wireless modem | |
167 | function findWirelessModem() | |
168 | local foundWireless = false; | |
169 | for i,name in pairs(peripherals) do | |
170 | for j,method in pairs(peripheral.getMethods(name)) do | |
171 | if (method == 'isWireless') then | |
172 | wmod = peripheral.wrap(name); | |
173 | if (wmod.isWireless()) then | |
174 | wmod.closeAll(); | |
175 | foundWireless = true; | |
176 | break; | |
177 | else | |
178 | wmod = {}; | |
179 | end | |
180 | end | |
181 | end | |
182 | if (foundWireless) then | |
183 | break; | |
184 | end | |
185 | end | |
186 | end | |
187 | ||
188 | function collectLocalInfo() | |
189 | for i,name in pairs(peripherals) do | |
190 | local isTank = false; | |
191 | local isCache = false; | |
192 | local isChest = false; | |
193 | local isCell = false; | |
194 | for j,method in pairs(peripheral.getMethods(name)) do | |
195 | if (method == 'getTankInfo') then | |
196 | isTank = true; | |
197 | break; | |
198 | end | |
199 | if (method == 'getStoredItems') then | |
200 | isCache = true; | |
201 | isChest = false; | |
202 | break; | |
203 | end | |
204 | if (method == 'getMaxEnergyStored') then | |
205 | isCell = true; | |
206 | isChest = false; | |
207 | if (string.match(peripheral.call(name,"getInventoryName"),"cache") == "cache") then | |
208 | isCell = false; | |
209 | isChest = false; | |
210 | isCache = true; | |
211 | end | |
212 | break; | |
213 | end | |
214 | if ((method == 'getStackInSlot') and (not isCache)) then | |
215 | isChest = true; | |
216 | end | |
217 | end | |
218 | if (isTank) then | |
219 | local p = peripheral.wrap(name); | |
220 | local iteminfo = p.getTankInfo(); | |
221 | local maxItems = iteminfo[1].capacity; | |
222 | maxItems = math.floor(maxItems/1000); | |
223 | local displayname = "Empty"; | |
224 | local amount = 0; | |
225 | if (iteminfo[1].contents) then | |
226 | displayname = iteminfo[1].contents.rawName; | |
227 | amount = iteminfo[1].contents.amount; | |
228 | amount = math.floor(amount/1000); | |
229 | end | |
230 | --printmon(displayname,amount,maxItems,"+"); | |
231 | updateTable(id.."-"..name,displayname,amount,maxItems,"+"); | |
232 | end | |
233 | if (isCache) then | |
234 | local p = peripheral.wrap(name); | |
235 | local iteminfo = p.getStoredItems(); | |
236 | if (iteminfo) then | |
237 | local maxItems = p.getMaxStoredItems(); | |
238 | local displayname = iteminfo.display_name; | |
239 | --print(name..",".."cache,"..displayname..","..iteminfo.qty..","..maxItems); | |
240 | --printmon(displayname,iteminfo.qty,maxItems,"#"); | |
241 | updateTable(id.."-"..name,displayname,iteminfo.qty,maxItems,"$"); | |
242 | end | |
243 | end | |
244 | if (isCell) then | |
245 | local p = peripheral.wrap(name); | |
246 | local energy = p.getEnergyStored(); | |
247 | local maxEnergy = p.getMaxEnergyStored(); | |
248 | local percent = (energy/maxEnergy*100); | |
249 | --printmon("Power Cell",energy,maxEnergy,"*"); | |
250 | updateTable(id.."-"..name,"Power Cell",energy,maxEnergy,"*"); | |
251 | end | |
252 | if (isChest) then | |
253 | --print("is a Chest"); | |
254 | local p = peripheral.wrap(name); | |
255 | local chestSize = p.getInventorySize(); | |
256 | local items = {}; | |
257 | for j=1,chestSize,1 do | |
258 | local iteminfo = p.getStackInSlot(j); | |
259 | if (iteminfo) then | |
260 | displayname = iteminfo.display_name; | |
261 | if (displayname) then | |
262 | if (not items[displayname]) then | |
263 | items[displayname] = iteminfo.qty; | |
264 | else | |
265 | items[displayname] = items[displayname] + iteminfo.qty; | |
266 | end | |
267 | end | |
268 | end | |
269 | end | |
270 | local k = 0; | |
271 | for key,val in pairs(items) do | |
272 | k = k + 1; | |
273 | --printmon(key,val,0,"#"); | |
274 | updateTable(id.."-"..name.."_"..k,key,val,0,"#"); | |
275 | end | |
276 | end | |
277 | end | |
278 | end | |
279 | ||
280 | function updateMonitor() | |
281 | x,y = mon.getSize(); | |
282 | ColumnWidth = NameLen + 7; | |
283 | MaxColumn = math.floor(x / (ColumnWidth))-1; | |
284 | mon.setBackgroundColor(colors.black); | |
285 | mon.clear(); | |
286 | CurColumn = 0; | |
287 | CurLine = 1; | |
288 | mon.setCursorPos(1,1); | |
289 | mon.setTextColor(colors.white); | |
290 | mon.setTextScale(0.5); | |
291 | mon.write("Base Monitor 1.0"); | |
292 | local sorted = {}; | |
293 | for n in pairs(ContentData) do | |
294 | table.insert(sorted, n); | |
295 | end | |
296 | table.sort(sorted); | |
297 | local name = ""; | |
298 | for i,source in ipairs(sorted) do | |
299 | local curname = string.sub(string.match(source,"^.*-"),1,-2); | |
300 | if (name ~= curname) then | |
301 | name = curname; | |
302 | CurColumn = 0; | |
303 | mon.setTextColor(titleTextColor); | |
304 | mon.setBackgroundColor(titleBackColor); | |
305 | mon.setCursorPos(1,CurLine+1); | |
306 | mon.write(padString("Contents for "..name,x-1)); | |
307 | CurLine = CurLine + 2; | |
308 | end | |
309 | displayname = ContentData[source]["displayname"]; | |
310 | count = ContentData[source]["count"]; | |
311 | max = ContentData[source]["max"]; | |
312 | legend = ContentData[source]["legend"]; | |
313 | printmon(displayname,count,max,legend); | |
314 | end | |
315 | end | |
316 | -- This is the main section of the script | |
317 | ||
318 | findMonitor(); | |
319 | findWirelessModem(); | |
320 | ||
321 | if (isMain == true) then | |
322 | if (wmod) then | |
323 | wmod.open(mainChannel); | |
324 | else | |
325 | print("You don't have a wireless modem, and this is set as the main computer"); | |
326 | end | |
327 | end | |
328 | ||
329 | ContentData = {}; | |
330 | ||
331 | local timerUpdate = os.startTimer(10); | |
332 | local updateCount = 0; | |
333 | local wirelessEventCount = 0; | |
334 | -- Perform Initial Collection and Update the Monitor | |
335 | collectLocalInfo(); | |
336 | updateMonitor(); | |
337 | ||
338 | while true do | |
339 | local event, param1, param2, param3, param4, param5 = os.pullEvent(); | |
340 | print("Received event:"..event); | |
341 | if (event == "timer") then | |
342 | if (param1 == timerUpdate) then | |
343 | updateCount = updateCount + 1; | |
344 | if (updateCount >= 3) then | |
345 | updateCount = 0; | |
346 | ContentData = {}; | |
347 | end | |
348 | collectLocalInfo(); | |
349 | if (wmod) then | |
350 | if (isMain == false) then | |
351 | wmod.transmit(mainChannel,1,ContentData); | |
352 | end | |
353 | end | |
354 | if (updateCount == 2) then | |
355 | updateMonitor(); | |
356 | end | |
357 | wirelessEventCount = 0; | |
358 | timerUpdate = os.startTimer(5); | |
359 | end | |
360 | end | |
361 | if (event == "modem_message") then | |
362 | if (isMain == true) then | |
363 | wirelessEventCount = wirelessEventCount + 1; | |
364 | for source,data in pairs(param4) do | |
365 | ContentData[source] = {}; | |
366 | ContentData[source]["displayname"] = param4[source]["displayname"]; | |
367 | ContentData[source]["count"] = param4[source]["count"]; | |
368 | ContentData[source]["max"] = param4[source]["max"]; | |
369 | ContentData[source]["legend"] = param4[source]["legend"]; | |
370 | end | |
371 | if (wirelessEventCount >= 10) then | |
372 | timerUpdate = os.startTimer(1); | |
373 | end | |
374 | end | |
375 | end | |
376 | if (event == "monitor_touch") or (event == "monitor_resize") then | |
377 | print("Updating the Monitor"); | |
378 | updateMonitor(); | |
379 | end | |
380 | if (event == "peripheral") or (event == "peripheral_detach") then | |
381 | print("Updating the peripheral list"); | |
382 | peripherals = peripheral.getNames(); | |
383 | end | |
384 | end |