View difference between Paste ID: 4XYCedMC and VSXDJYYE
SHOW: | | - or go back to the newest paste.
1
--[[
2-
	Base Monitor 1.0
2+
	Base Monitor 1.9.1
3-
	This script allows you to monitor caches, tanks, barrels and chests
3+
		This script allows you to monitor caches, tanks, barrels and chests
4-
	Monitoring chests requires an open peripheral proxy, the same goes for
4+
		Monitoring chests requires an open peripheral proxy, the same goes for
5-
	buildcraft tanks.
5+
		buildcraft tanks.
6-
	You can build one of more computers and set one of them as the main server by
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.
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
8+
		For the main server to receive info from other computers you need to attach a
9-
	wireless modem to each computer.
9+
		wireless modem to each computer.
10-
	Peripherals can be attached to any side you like, the script will automatically
10+
		Peripherals can be attached to any side you like, the script will automatically
11-
	work out where the peripherals are.
11+
		work out where the peripherals are.
12
	Changes since 1.9
13
		Added support for minecraft chests behind Advanced Peripherals
14
		Added support for Liquid tanks behind Advanced Peripherals
15-
local isMain = false;
15+
	Changes since 1.8
16-
--local id = os.computerID();
16+
		Added support for the Advanced Peripherals Block Reader
17-
local id = "The Base";
17+
		Added support for Liquids in the RS Bridge
18
	Changes since 1.7
19
		Updated the script for ComputerCraft Tweaked.
20
		Chests and Tanks have different function names when getting info via modems.
21
		Added checks to support both new and old methods.
22
		Added support for industrial foregoing blackhole tanks.
23
		Replacing multiple spaces in names with a single space.
24
		Updated Energy section to support new function names.
25
	Changes since 1.6
26
		Made significant changed to how peripherals are checked and processed.
27
		Redesigned how the contents is processed and stored so that there is a timestamp.
28
		The timestamp allows remote computer information to be processed correctly.
29
		Each computer must have a unique id set, if the same id is used for two systems,
30-
local NameLen = 21;
30+
		then the last one to update the table will win and you won't see all the content.
31
		Added ItemsFullPercentAt and FluidFullPercentAt to set what level is considered full.
32
	Changes since 1.5
33
        Added a workaround to ignore BuildCraft pipes if attached
34
    Changes since 1.4
35
        Added support for PeripheralPlusOne on Minecraft 1.12.2
36
        Currently only supports ME Systems via the ME Bridge
37
	Changes since 1.3
38
		Changed filterexclude to blacklist and added whitelist
39
	Changes since 1.2
40
		Changed the way ME systems are processed as it was very slow.
41
		Added filtering so that certain names of items can be excluded.
42
		This was again a problem on ME systems with a lot of items in them.
43
	Changes since 1.1
44
		Fixed a bug with tanks. Used to check if getInventoryName existed to determine if the
45
		peripheral is a tank or not. Now using the getType function instead.
46
		Changed the string.match to string.find
47
	http://youtu.be/KT-2hKjUpGA
48
--]]
49
-- Below are values you can change
50
local isMain = true;
51
local id = "Main Base";
52
local UpdateIntervalSeconds = 60;
53
local ItemsFullPercentAt = 256;
54
local FluidFullPercentAt = 256;
55
56
local VersionInfo = "Base Monitor 1.9.1";
57
58
local titleTextColor = colors.blue;
59
local titleBackColor = colors.white;
60
local tankTextColor = colors.black;
61
local tankBackColor = colors.lime;
62
local chestTextColor = colors.white;
63
local chestBackColor = colors.purple;
64
local cacheTextColor = colors.white;
65
local cacheBackColor = colors.cyan;
66
local powerTextColor = colors.black;
67
local powerBackColor = colors.orange;
68
69
--[[
70
	Add or remove values here to exclude items from being displayed.
71
	It is case insensitive and it will look for matches.
72
	So an item called "Nether Brick" will not be displayed if any of the following is set
73
	"Brick","brick","rick","ck"
74
	This also means that of you set a filter call "iron" then it will remove all items containing iron.
75
	it will not show Iron Ore, Iron Block, Iron Helmet, etc.
76-
function updateTable(strSource,strName,strAmount,strMax,strLegend)
76+
77-
	ContentData[strSource] = {};
77+
local blacklist = {"Flesh","Nugget","Brain","Sapling","Seed","egg","eye"};
78-
	ContentData[strSource]["displayname"] = strName;
78+
79-
	ContentData[strSource]["count"] = strAmount;
79+
	Add or remove values here to only show items matching these names.
80-
	ContentData[strSource]["max"] = strMax;
80+
	It is case insensitive and it will look for matches.
81-
	ContentData[strSource]["legend"] = strLegend;
81+
	So an item called "Nether Brick" will be displayed if any of the following is set
82
	"Brick","brick","rick","ck"
83
	This also means that of you set a filter called "iron" then it will show all items containing iron.
84
	it will show show Iron Ore, Iron Block, Iron Helmet, etc.
85
	To disable the whitelist set it to local whitelist = "";
86
	To enable the whitelist set it to local whitelist = {"Diamond","Gold"};
87
--]]
88
local whitelist = "";
89
90
local NameLen = 18;
91
local mainChannel = 2;
92
93
-- Above are values you can change
94
95
print("Starting "..VersionInfo);
96
local peripherals = peripheral.getNames();
97
local mon;
98
local wmod;
99
local x,y;
100
local CurColumn = 0;
101
local MaxColumn;
102
local ColumnWidth;
103
local CurLine = 2;
104
local ContentData = {};
105
106
function padString (sText, iLen)
107
	local iTextLen = string.len(sText);
108
	-- Too short, pad
109
	if (iTextLen < iLen) then
110
		local iDiff = iLen - iTextLen;
111
		return(sText..string.rep(" ",iDiff));
112
	end
113
	-- Too long, trim
114
	if (iTextLen > iLen) then
115
		return(string.sub(sText,1,iLen));
116
	end
117
	-- Exact length
118
	return(sText);
119-
	--print("CurX:"..CurX);
119+
120
121-
	local percent = strAmount / strMax * 100;
121+
122
	mon = peripheral.wrap(objectmon);
123-
	if (strMax == 0) then
123+
124-
		percent = 0;
124+
125
		titleBackColor = colors.white;
126
		tankTextColor = colors.black;
127
		tankBackColor = colors.white;
128
		chestTextColor = colors.black;
129
		chestBackColor = colors.white;
130
		cacheTextColor = colors.black;
131
		cacheBackColor = colors.white;
132
		powerTextColor = colors.black;
133
		powerBackColor = colors.white;
134
	end
135
end
136
137
function updateTable(strSource,strName,strAmount,timestamp,strLegend)
138
	local isWhitelisted = true;
139
	if (type(whitelist) == "table") then
140
		isWhitelisted = false;
141
		for l,filter in pairs(whitelist) do
142
			if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
143
				isWhitelisted = true;
144
			end
145
		end
146
	end
147
	if (isWhitelisted == false) then
148
		return;
149
	end
150
	if (type(blacklist) == "table") then
151
		for l,filter in pairs(blacklist) do
152
			if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
153
				return;
154
			end
155
		end
156
	end
157
	if (ContentData[strSource] == nil) then
158
		ContentData[strSource] = {};
159
	end
160
	if (ContentData[strSource][timestamp] == nil) then
161
		ContentData[strSource][timestamp] = {};
162
	end
163
	if (ContentData[strSource][timestamp][strName] == nil) then
164
		ContentData[strSource][timestamp][strName] = {};
165
	end
166
	if (ContentData[strSource][timestamp][strName]["count"] == nil) then
167
		ContentData[strSource][timestamp][strName]["count"] = strAmount;
168
	else
169
		ContentData[strSource][timestamp][strName]["count"] = ContentData[strSource][timestamp][strName]["count"] + strAmount;
170
	end
171
	ContentData[strSource][timestamp][strName]["legend"] = strLegend;
172
end
173
174
function printmon(strName,strAmount,strMax,strLegend)
175
	local textColor;
176
	local backColor;
177
	local FullPercentAt = ItemsFullPercentAt;
178
	if (strLegend == "#") then
179
		textColor = chestTextColor;
180
		backColor = chestBackColor;
181
		strLegend = "";
182
	end
183
	if (strLegend == "+") then
184
		textColor = tankTextColor;
185
		backColor = tankBackColor;
186
		FullPercentAt = FluidFullPercentAt;
187
		strLegend = "";
188
	end
189
	if (strLegend == "*") then
190-
		local isTank = false;
190+
191-
		local isCache = false;
191+
192-
		local isChest = false;
192+
193-
		local isCell = false;
193+
194
	if (strLegend == "$") then
195-
			if (method == 'getTankInfo') then  
195+
196-
				isTank = true;
196+
197-
				break;
197+
198
	end
199-
			if (method == 'getStoredItems') then
199+
200-
				isCache = true;
200+
201-
				isChest = false;
201+
202-
				break;
202+
203
		line = string.format("%s  %3iK%s",padString(strName,NameLen),math.floor(strAmount/1000),padString(strLegend,1));
204-
			if (method == 'getMaxEnergyStored') then
204+
205-
				isCell = true;
205+
206-
				isChest = false;
206+
207-
				if (string.match(peripheral.call(name,"getInventoryName"),"cache") == "cache") then
207+
208-
					isCell = false;
208+
209-
					isChest = false;
209+
210-
					isCache = true;
210+
211
 	if (CurColumn == 0) then
212-
				break;
212+
		--  print("CurX:"..CurX);
213
 	end
214-
			if ((method == 'getStackInSlot') and (not isCache)) then
214+
215-
				isChest = true;
215+
	--local percent = strAmount / strMax * 100;
216
	mon.setBackgroundColor(backColor);
217
	local percent = strAmount / FullPercentAt * 100;
218-
		if (isTank) then
218+
	if (percent > 100) then percent = 100; end
219-
			local p = peripheral.wrap(name);
219+
220-
			local iteminfo = p.getTankInfo();
220+
 	--if (CurColumn == 0) then
221-
			local maxItems = iteminfo[1].capacity;
221+
   	--	barlength = barlength + 1;
222-
			maxItems = math.floor(maxItems/1000);
222+
	--end
223-
			local displayname = "Empty";
223+
224-
			local amount = 0;
224+
225-
			if (iteminfo[1].contents) then
225+
226-
				displayname = iteminfo[1].contents.rawName;
226+
227-
				amount = iteminfo[1].contents.amount;
227+
228-
				amount = math.floor(amount/1000);
228+
229
			mon.setBackgroundColor();
230-
			--printmon(displayname,amount,maxItems,"+");
230+
231-
			updateTable(id.."-"..name,displayname,amount,maxItems,"+");
231+
232
		end--]]
233-
		if (isCache) then
233+
234-
			local p = peripheral.wrap(name);
234+
235
		mon.write(string.sub(line,barlength+1,-2))
236
	else
237-
				local maxItems = p.getMaxStoredItems();
237+
238
		mon.write(line);
239-
				--print(name..",".."cache,"..displayname..","..iteminfo.qty..","..maxItems);
239+
240-
				--printmon(displayname,iteminfo.qty,maxItems,"#");
240+
241-
				updateTable(id.."-"..name,displayname,iteminfo.qty,maxItems,"$");
241+
242
	mon.setTextColor(colors.white);
243
	CurColumn = CurColumn + 1;
244-
		if (isCell) then
244+
245-
			local p = peripheral.wrap(name);
245+
246-
			local energy = p.getEnergyStored();
246+
247-
			local maxEnergy = p.getMaxEnergyStored();
247+
248-
			local percent = (energy/maxEnergy*100);
248+
249-
			--printmon("Power Cell",energy,maxEnergy,"*");
249+
250-
			updateTable(id.."-"..name,"Power Cell",energy,maxEnergy,"*");
250+
251
-- Find a monitor
252-
		if (isChest) then
252+
253-
			--print("is a Chest");
253+
254-
			local p = peripheral.wrap(name);
254+
255-
			local chestSize = p.getInventorySize();
255+
256-
			local items = {};
256+
257-
			for j=1,chestSize,1 do
257+
258-
				local iteminfo = p.getStackInSlot(j);
258+
259-
				if (iteminfo) then
259+
260-
					displayname = iteminfo.display_name;
260+
261
262
-- Find a wireless modem
263-
							items[displayname] = iteminfo.qty;
263+
264
	local foundWireless = false;
265-
							items[displayname] = items[displayname] + iteminfo.qty;
265+
266
		for j,method in pairs(peripheral.getMethods(name)) do
267
			if (method == 'isWireless') then
268
				wmod = peripheral.wrap(name);
269
				if (wmod.isWireless()) then
270-
			local k = 0;
270+
271-
			for key,val in pairs(items) do
271+
272-
				k = k + 1;
272+
273-
				--printmon(key,val,0,"#");
273+
274-
				updateTable(id.."-"..name.."_"..k,key,val,0,"#");
274+
275
				end
276
			end
277
		end
278
		if (foundWireless) then
279
			break;
280
		end
281
	end
282
end
283
284
function collectLocalInfo()
285
	local timestamp = os.clock();
286
	for i,name in pairs(peripherals) do
287
		local p = peripheral.wrap(name);
288
		local displayNames = {};
289
		if (p.getBlockData ~= nil) then
290
			print("Processing Advanced Peripherals");
291-
	mon.write("Base Monitor 1.0");
291+
			local blockdata = p.getBlockData();
292-
	local sorted = {};
292+
			if (blockdata.Items ~= nil) then
293
				local blockitems;
294-
		table.insert(sorted, n);
294+
				if (blockdata.Items.Items ~= nil) then
295
					blockitems = blockdata.Items.Items;
296-
	table.sort(sorted);
296+
297
					blockitems = blockdata.Items;
298-
	for i,source in ipairs(sorted) do
298+
299-
		local curname = string.sub(string.match(source,"^.*-"),1,-2);
299+
				if (blockitems) then
300-
		if (name ~= curname) then
300+
					print("Processing Item Storage via Advanced Peripherals");
301-
			name = curname;
301+
					local items = {};
302
					for j in pairs(blockitems) do
303
						local iteminfo = blockitems[j];
304
						displayname = iteminfo.id;
305
						if (blockitems[j].Item ~= nil) then -- Workaround for Mekanism bins
306
							displayname = blockitems[j].Item.id;
307
						end
308
						if (displayname) then
309-
		displayname = ContentData[source]["displayname"];
309+
							if(string.find(displayname,":") ~= nil) then
310-
		count = ContentData[source]["count"];
310+
								k = string.find(displayname,":")+1;
311-
		max = ContentData[source]["max"];
311+
								displayname = string.sub(displayname,k);
312-
		legend = ContentData[source]["legend"];
312+
							end
313-
		printmon(displayname,count,max,legend);
313+
							displayname = string.gsub(displayname,"%s+"," ");
314
							local itemcount = iteminfo.Count;
315
							if (itemcount == nil and iteminfo.SizeOverride ~= nil) then
316
								itemcount = iteminfo.SizeOverride;
317
							end
318
							if (itemcount == nil) then
319
								itemcount = 0;
320
							end
321
							if (not items[displayname]) then
322-
	if (wmod) then
322+
								items[displayname] = itemcount;
323
							else
324
								items[displayname] = items[displayname] + itemcount;
325
							end
326
						end
327
					end
328
					local k = 0;
329
					for key,val in pairs(items) do
330
						k = k + 1;
331-
local timerUpdate = os.startTimer(10);
331+
						updateTable(id,key,val,timestamp,"#");
332
					end
333
				end
334
			end
335
			if (blockdata.GasTanks) then
336
				print("Processing Gas Storage via Advanced Peripherals");
337
				local items = {};
338
				for j in pairs(blockdata.GasTanks) do
339
					local iteminfo = blockdata.GasTanks[j];
340
					displayname = iteminfo.stored.gasName;
341
					if (displayname) then
342
						if(string.find(displayname,":") ~= nil) then
343-
			updateCount = updateCount + 1;
343+
							k = string.find(displayname,":")+1;
344-
			if (updateCount >= 3) then
344+
							displayname = string.sub(displayname,k);
345-
				updateCount = 0;
345+
346-
				ContentData = {};
346+
						displayname = string.gsub(displayname,"%s+"," ");
347
						local amount = 0;
348
						if (not items[displayname]) then
349
							amount = iteminfo.stored.amount;
350
							if (amount == nil) then
351
								amount = 0;
352
							end
353
							amount = math.floor(amount/1000);
354-
			if (updateCount == 2) then
354+
							items[displayname] = amount;
355-
				updateMonitor();
355+
356
							amount = iteminfo.stored.amount;
357
							if (amount == nil) then
358-
			timerUpdate = os.startTimer(5);
358+
								amount = 0;
359
							end	
360
							amount = math.floor(amount/1000);
361
							items[displayname] = items[displayname] + amount;
362
						end
363
					end
364
				end
365-
				ContentData[source] = {};
365+
				local k = 0;
366-
				ContentData[source]["displayname"] = param4[source]["displayname"];
366+
				for key,val in pairs(items) do
367-
				ContentData[source]["count"] = param4[source]["count"];
367+
					k = k + 1;
368-
				ContentData[source]["max"] = param4[source]["max"];
368+
					updateTable(id,key,val,timestamp,"+");
369-
				ContentData[source]["legend"] = param4[source]["legend"];
369+
370
			end
371
			if (blockdata.FluidTanks) then
372
				print("Processing Fluid Storage via Advanced Peripherals");
373
				local items = {};
374
				for j in pairs(blockdata.FluidTanks) do
375
					local iteminfo = blockdata.FluidTanks[j];
376
					displayname = iteminfo.stored.FluidName;
377
					if (displayname) then
378
						if(string.find(displayname,":") ~= nil) then
379
							k = string.find(displayname,":")+1;
380
							displayname = string.sub(displayname,k);
381
						end
382
						displayname = string.gsub(displayname,"%s+"," ");
383
						local amount = 0;
384-
end
384+
385
							amount = iteminfo.stored.Amount;
386
							if (amount == nil) then
387
								amount = 0;
388
							end
389
							amount = math.floor(amount/1000);
390
							items[displayname] = amount;
391
						else
392
							amount = iteminfo.stored.Amount;
393
							if (amount == nil) then
394
								amount = 0;
395
							end	
396
							amount = math.floor(amount/1000);
397
							items[displayname] = items[displayname] + amount;
398
						end
399
					end
400
				end
401
				local k = 0;
402
				for key,val in pairs(items) do
403
					k = k + 1;
404
					updateTable(id,key,val,timestamp,"+");
405
				end
406
			end
407
		end
408
		if (p.getTankInfo ~= nil or p.getFilledPercentage ~= nil or p.tanks) then
409
			print("Processing Tank");
410
			local iteminfo;
411
			if (p.getTankInfo ~= nil) then
412
				iteminfo = p.getTankInfo();
413
			end
414
			if (p.getFilledPercentage ~= nil) then
415
				iteminfo = p.getStored();
416
			end
417
			if (p.tanks ~= nil) then
418
				tankinfo = p.tanks(); -- Industrial Foregoing blackhole tank
419
				if (tankinfo[1] ~= nil) then
420
					iteminfo = tankinfo[1];
421
				end
422
			end
423
			if (iteminfo ~= nil) then
424
				local displayname = "Empty";
425
				local amount = 0;
426
				if (iteminfo[1] ~= nil) then
427
					if (iteminfo[1].contents) then
428
						displayname = iteminfo[1].contents.rawName;
429
						amount = iteminfo[1].contents.amount;
430
						amount = math.floor(amount/1000);
431
					end
432
				end
433
				if (iteminfo.name ~= nil) then
434
					displayname = iteminfo.name;
435
436
				end
437
				if (iteminfo.amount ~= nil) then
438
					amount = iteminfo.amount;
439
					amount = math.floor(amount/1000);
440
				end
441
				if(string.find(displayname,":") ~= nil) then
442
					k = string.find(displayname,":")+1;
443
					displayname = string.sub(displayname,k);
444
				end
445
				displayname = string.gsub(displayname,"%s+"," ");
446
447
				if (amount ~= 0) then
448
					updateTable(id,displayname,amount,timestamp,"+");
449
				end
450
			end
451
		end
452
		if (p.getStoredItems ~= nil) then
453
			print("Processing Cache");
454
			local iteminfo = p.getStoredItems();
455
			if (iteminfo) then
456
				local displayname = iteminfo.display_name;
457
				updateTable(id,displayname,iteminfo.qty,timestamp,"$");
458
			end
459
		end
460
		if (p.getEnergyStored ~= nil or p.getTotalEnergy ~= nil or p.getEnergy ~= nil) then
461
			print("Processing Energy");
462
			local energy;
463
			if (p.getEnergyStored ~= nil) then
464
				energy = p.getEnergyStored();
465
			end
466
			if (p.getTotalEnergy ~= nil) then
467
				energy = p.getTotalEnergy();
468
			end
469
			if (p.getEnergy ~= nil) then
470
				energy = p.getEnergy();
471
			end
472
			if (energy ~= nil) then
473
				updateTable(id,"Energy",energy,timestamp,"*");
474
			end
475
		end
476
		if (p.getInventorySize ~= nil or p.size ~= nil) then
477
			print("Processing Chest");
478
			local chestSize = 0;
479
			if (p.getInventorySize) then
480
				chestSize = p.getInventorySize();
481
			end
482
			if (p.size) then
483
				chestSize = p.size();
484
			end
485
			if (chestSize ~= nil) then
486
				local items = {};
487
				for j=1,chestSize,1 do
488
					local iteminfo = nil;
489
					if (p.getStackInSlot) then
490
						iteminfo = p.getStackInSlot(j);
491
					end
492
					if (p.getItemDetail) then
493
						iteminfo = p.getItemDetail(j);
494
					end
495
					if (iteminfo) then
496
						displayname = iteminfo.display_name;
497
						if (displayname == nil) then
498
							displayname = iteminfo.displayName;
499
						end
500
						if (displayname) then
501
							displayname = string.gsub(displayname,"%s+"," ");
502
							if (not items[displayname]) then
503
								local itemcount = iteminfo.qty;
504
								if (itemcount == nil) then
505
									itemcount = iteminfo.count;
506
								end
507
								items[displayname] = itemcount;
508
							else
509
								local itemcount = iteminfo.qty;
510
								if (itemcount == nil) then
511
									itemcount = iteminfo.count;
512
								end
513
								items[displayname] = items[displayname] + itemcount;
514
							end
515
						end
516
					end
517
				end
518
				local k = 0;
519
				for key,val in pairs(items) do
520
					k = k + 1;
521
					updateTable(id,key,val,timestamp,"#");
522
				end
523
			end
524
		end
525
		if (p.getAvailableItems ~= nil) then
526
			print("Processing ME System");
527
			local itemarray = p.getAvailableItems();
528
			if (itemarray ~= nil) then
529
				for j=1,table.getn(itemarray),1 do
530
					if (itemarray[j].size > 0) then
531
						local fingerprint = itemarray[j].fingerprint;
532
						if ( (displayNames[fingerprint.id] == nil) or (displayNames[fingerprint.id] == fingerprint.id)) then
533
							local ItemDetail = p.getItemDetail(fingerprint);
534
							if (ItemDetail ~= nil) then
535
								displayNames[fingerprint.id] = ItemDetail.basic().display_name;
536
							else
537
								displayNames[fingerprint.id] = fingerprint.id;
538
							end
539
						end
540
						updateTable(id,displayNames[fingerprint.id],itemarray[j].size,timestamp,"#");
541
					end
542
				end
543
			end
544
		end
545
        if (p.listItems ~= nil) then
546
			print("Processing ME/RS Bridge");
547
			local itemarray = p.listItems();
548
			if (itemarray ~= nil) then
549
				for j=1,table.getn(itemarray),1 do
550
					if (itemarray[j].amount > 0) then
551
						local fingerprint = itemarray[j].name;
552
						local displayName = itemarray[j].displayName;
553
						displayName = string.gsub(displayName,"%s+"," ");
554
						updateTable(id,displayName,itemarray[j].amount,timestamp,"#");
555
					end
556
				end
557
			end
558
			if (p.listFluids ~= nil) then
559
				local fluidarray = p.listFluids();
560
				if (fluidarray ~= nil) then
561
					for j=1,table.getn(fluidarray),1 do
562
						if (fluidarray[j].amount > 0) then
563
							local fingerprint = fluidarray[j].name;
564
							local displayName = fluidarray[j].displayName;
565
							displayName = string.gsub(displayName,"%s+"," ");
566
							local amount = fluidarray[j].amount;
567
							amount = math.floor(amount/1000);
568
							updateTable(id,displayName,amount,timestamp,"+");
569
						end
570
					end
571
				end
572
			end
573
		end
574
		print("Done");
575
	end
576
end
577
578
function updateMonitor()
579
	x,y = mon.getSize();
580
	ColumnWidth = NameLen + 7;
581
	MaxColumn = math.floor(x / (ColumnWidth))-1;
582
	mon.setBackgroundColor(colors.black);
583
	mon.clear();
584
	CurColumn = 0;
585
	CurLine = 1;
586
	mon.setCursorPos(1,1);
587
	mon.setTextColor(colors.white);
588
	mon.setTextScale(0.5);
589
	mon.write(VersionInfo);
590
	-- Sort by Base names
591
	local sortedSources = {};
592
	for n in pairs(ContentData) do
593
		table.insert(sortedSources, n);
594
	end
595
	table.sort(sortedSources);
596
	local name = "";
597
	for i,source in ipairs(sortedSources) do
598
		if (name ~= source) then
599
			name = source;
600
			CurColumn = 0;
601
			mon.setTextColor(titleTextColor);
602
			mon.setBackgroundColor(titleBackColor);
603
			mon.setCursorPos(1,CurLine+1);
604
			mon.write(padString("Contents for "..name,x-1));
605
			CurLine = CurLine + 2;
606
		end
607
		sortedTimestamps = {};
608
		for timestamp in pairs(ContentData[source]) do
609
			table.insert(sortedTimestamps,timestamp);
610
		end
611
		table.sort(sortedTimestamps);
612
		latest = nil;
613
		for j=table.maxn(sortedTimestamps),1,-1 do
614
			if (latest == nil) then
615
				latest = sortedTimestamps[j];
616
			else
617
				ContentData[source][sortedTimestamps[j]] = nil;
618
			end
619
		end
620
		for itemname in pairs(ContentData[source][latest]) do
621
			displayname = itemname;
622
			count = ContentData[source][latest][itemname]["count"];
623
			legend = ContentData[source][latest][itemname]["legend"];
624
			max = 0;
625
			printmon(displayname,count,max,legend);
626
		end
627
	end
628
end
629
-- This is the main section of the script
630
631
findMonitor();
632
findWirelessModem();
633
634
if (isMain == true) then
635
	if (type(wmod.isWireless) == "function") then
636
		wmod.open(mainChannel);
637
	else
638
		print("You don't have a wireless modem, and this is set as the main computer");
639
	end
640
end
641
642
ContentData = {};
643
644
local timerUpdate = os.startTimer(UpdateIntervalSeconds);
645
local updateCount = 0;
646
local wirelessEventCount = 0;
647
-- Perform Initial Collection and Update the Monitor
648
collectLocalInfo();
649
updateMonitor();
650
651
-- Main program loop
652
while true do	
653
	local event, param1, param2, param3, param4, param5 = os.pullEvent();
654
	print("Received event:"..event);
655
	if (event == "timer") then
656
		if (param1 == timerUpdate) then
657
			collectLocalInfo();
658
			if (wmod) then
659
				if (isMain == false) then
660
					wmod.transmit(mainChannel,1,ContentData);
661
				end
662
			end
663
			updateMonitor();
664
			wirelessEventCount = 0;
665
			timerUpdate = os.startTimer(UpdateIntervalSeconds);
666
		end
667
	end
668
	if (event == "modem_message") then
669
		if (isMain == true) then
670
			wirelessEventCount = wirelessEventCount + 1;
671
			for source,data in pairs(param4) do
672
				if (ContentData[source] == nil) then
673
					ContentData[source] = {};
674
				end
675
				ContentData[source] = param4[source];
676
			end
677
			if (wirelessEventCount >= 10) then
678
				timerUpdate = os.startTimer(1);
679
			end
680
		end
681
	end
682
	if (event == "monitor_touch") or (event == "monitor_resize") then
683
		print("Updating the Monitor");
684
		updateMonitor();
685
	end
686
	if (event == "peripheral") or (event == "peripheral_detach") then
687
		print("Updating the peripheral list");
688
		peripherals = peripheral.getNames();
689
	end
690
end
691