Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local isMain = true;
- local id = "Memebase";
- local UpdateIntervalSeconds = 15;
- local ItemsFullPercentAt = 256;
- local FluidFullPercentAt = 64000;
- local VersionInfo = "Base Monitor 2.0";
- local titleTextColor = colors.blue;
- local titleBackColor = colors.white;
- local tankTextColor = colors.white;
- local tankBackColor = colors.red;
- local chestTextColor = colors.white;
- local chestBackColor = colors.purple;
- local cacheTextColor = colors.white;
- local cacheBackColor = colors.cyan;
- local powerTextColor = colors.black;
- local powerBackColor = colors.orange;
- local whitelist = "";
- local blacklist = "";
- local groups = {"Ingot", "Ore", "Pulverized", "Dust", "Crystal"};
- local NameLen = 18;
- local mainChannel = 2;
- -- Above are values you can change
- print("Starting "..VersionInfo);
- local peripherals = peripheral.getNames();
- local mon;
- local wmod;
- local x,y;
- local CurColumn = 0;
- local MaxColumn;
- local ColumnWidth;
- local CurLine = 2;
- local ContentData = {};
- function padString(sText, iLen)
- local iTextLen = string.len(sText);
- -- Too short, pad
- if (iTextLen < iLen) then
- local iDiff = iLen - iTextLen;
- return(sText..string.rep(" ",iDiff));
- end
- -- Too long, trim
- if (iTextLen > iLen) then
- return(string.sub(sText,1,iLen));
- end
- -- Exact length
- return(sText);
- end
- function prepmonitor(objectmon)
- mon = peripheral.wrap(objectmon);
- if (mon.isColor() == false) then
- titleTextColor = colors.black;
- titleBackColor = colors.white;
- tankTextColor = colors.black;
- tankBackColor = colors.white;
- chestTextColor = colors.black;
- chestBackColor = colors.white;
- cacheTextColor = colors.black;
- cacheBackColor = colors.white;
- powerTextColor = colors.black;
- powerBackColor = colors.white;
- end
- end
- function updateTable(strName,strAmount,timestamp,strLegend)
- if string.find(strName, "item.") then
- print(strName);
- end
- if string.find(strName, "item.projectred") then
- strName = "Electrotine";
- end
- if string.find(strName, "item.mob_ingredient") then
- strName = "Nebulous Heart";
- end
- local itemGroup = "No group";
- if (type(groups) == "table") then
- for l,filter in pairs(groups) do
- if(string.find(string.lower(strName),string.lower(filter)) ~= nil) then
- itemGroup = filter;
- end
- end
- end
- local isWhitelisted = true;
- if (type(whitelist) == "table") then
- isWhitelisted = false;
- for l,filter in pairs(whitelist) do
- if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
- isWhitelisted = true;
- end
- end
- end
- if (isWhitelisted == false) then
- return;
- end
- if (type(blacklist) == "table") then
- for l,filter in pairs(blacklist) do
- if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
- return;
- end
- end
- end
- if (ContentData[id] == nil) then
- ContentData[id] = {};
- end
- if (ContentData[id][timestamp] == nil) then
- ContentData[id][timestamp] = {};
- end
- if (ContentData[id][timestamp][itemGroup] == nil) then
- ContentData[id][timestamp][itemGroup] = {};
- end
- if (ContentData[id][timestamp][itemGroup][strName] == nil) then
- ContentData[id][timestamp][itemGroup][strName] = {};
- end
- if (ContentData[id][timestamp][itemGroup][strName]["count"] == nil) then
- ContentData[id][timestamp][itemGroup][strName]["legend"] = strLegend;
- ContentData[id][timestamp][itemGroup][strName]["count"] = strAmount;
- else
- ContentData[id][timestamp][itemGroup][strName]["legend"] = strLegend;
- if not (ContentData[id][timestamp][itemGroup][strName]["count"] == strAmount) then
- ContentData[id][timestamp][itemGroup][strName]["count"] = strAmount;
- ContentData[id][timestamp][itemGroup][strName]["legend"] = "?";
- end
- end
- end
- --[[
- local titleTextColor = colors.red;
- local titleBackColor = colors.white;
- local tankTextColor = colors.white; +? (double)
- local tankBackColor = colors.red; +? (double)
- local chestTextColor = colors.white; (ME)
- local chestBackColor = colors.purple; (ME)
- local cacheTextColor = colors.white; $ (Black Hole)
- local cacheBackColor = colors.cyan; $ (Black Hole)
- local powerTextColor = colors.black; * (Capacitor)
- local powerBackColor = colors.orange; * (Capacitor)
- ]]--
- function printmon(strName,strAmount,strMax,strLegend)
- if(strAmount<50000) then
- return false;
- end
- local textColor;
- local backColor;
- local FullPercentAt = ItemsFullPercentAt;
- if (strLegend == "#") then
- textColor = chestTextColor;
- backColor = chestBackColor;
- strLegend = "";
- end
- if (strLegend == "+") then
- textColor = tankTextColor;
- backColor = tankBackColor;
- FullPercentAt = FluidFullPercentAt;
- strLegend = "";
- end
- if (strLegend == "*") then
- textColor = powerTextColor;
- backColor = powerBackColor;
- strLegend = "";
- end
- if (strLegend == "$") then
- textColor = cacheTextColor;
- backColor = cacheBackColor;
- strLegend = "";
- end
- if (strLegend == "?") then
- textColor = tankTextColor;
- backColor = tankBackColor;
- strLegend = "";
- end
- local line = string.format("%s %3i%s",padString(strName,NameLen+1),strAmount,padString(strLegend,1));
- if (strAmount >= 1000000) then
- line = string.format("%s %.2fM%s",padString(strName,NameLen),strAmount/1000000,padString(strLegend,1));
- elseif (strAmount >= 1000) then
- line = string.format("%s %.1fK%s",padString(strName,NameLen),strAmount/1000,padString(strLegend,1));
- end
- local ColPadding = 0;
- if (CurColumn > 0) then
- ColPadding = 1;
- end
- local CurX = math.floor((CurColumn*ColumnWidth))+math.floor(CurColumn*ColPadding)+1;
- if (CurColumn == 0) then
- -- print("CurX:"..CurX);
- end
- mon.setCursorPos(CurX,CurLine);
- --local percent = strAmount / strMax * 100;
- mon.setBackgroundColor(backColor);
- local percent = strAmount / FullPercentAt * 100;
- if (percent > 100) then percent = 100; end
- local barlength = math.floor(percent / 100 * (string.len(line)+2));
- --if (CurColumn == 0) then
- -- barlength = barlength + 1;
- --end
- if (string.len(line) > barlength) then
- local msg = string.sub(line,1,barlength);
- mon.setTextColor(textColor);
- mon.write(msg);
- --[[if (percent == 0) then
- mon.setBackgroundColor();
- else
- mon.setBackgroundColor(colors.black);
- end--]]
- mon.setBackgroundColor(colors.black);
- mon.setTextColor(backColor);
- mon.write(string.sub(line,barlength+1,-2))
- else
- local spaces = barlength - string.len(line);
- mon.write(line);
- mon.write(string.rep(" ",spaces));
- end
- mon.setTextColor(colors.white);
- CurColumn = CurColumn + 1;
- if (CurColumn > MaxColumn) then
- CurColumn = 0;
- CurLine = CurLine + 1;
- end
- return true;
- end
- -- Find a monitor
- function findMonitor()
- for i,name in pairs(peripherals) do
- for j,method in pairs(peripheral.getMethods(name)) do
- if (method == 'getCursorPos') then
- prepmonitor(name);
- end
- end
- end
- end
- -- Find a wireless modem
- function findWirelessModem()
- local foundWireless = false;
- for i,name in pairs(peripherals) do
- for j,method in pairs(peripheral.getMethods(name)) do
- if (method == 'isWireless') then
- wmod = peripheral.wrap(name);
- if (wmod.isWireless()) then
- wmod.closeAll();
- foundWireless = true;
- break;
- else
- wmod = {};
- end
- end
- end
- if (foundWireless) then
- break;
- end
- end
- end
- function collectLocalInfo()
- local timestamp = os.clock();
- for i,name in pairs(peripherals) do
- local p = peripheral.wrap(name);
- local displayNames = {};
- if (p.getTankInfo ~= nil) then
- print("Processing Tank");
- local iteminfo = p.getTankInfo();
- if (iteminfo ~= nil) then
- local displayname = "Empty";
- local amount = 0;
- if (iteminfo[1].contents) then
- displayname = iteminfo[1].contents.rawName;
- amount = iteminfo[1].contents.amount;
- amount = math.floor(amount/1000);
- end
- if (amount ~= 0) then
- updateTable(displayname,amount,timestamp,"+");
- end
- end
- end
- if (p.getStoredItems ~= nil) then
- print("Processing Cache");
- local iteminfo = p.getStoredItems();
- if (iteminfo) then
- local displayname = iteminfo.display_name;
- updateTable(displayname,iteminfo.qty,timestamp,"$");
- end
- end
- if (p.getEnergyStored ~= nil) then
- print("Processing Energy");
- local energy = p.getEnergyStored();
- if (energy ~= nil) then
- updateTable("Energy",energy,timestamp,"*");
- end
- end
- if (p.getInventorySize ~= nil) then
- print("Processing Chest");
- local chestSize = p.getInventorySize();
- if (chestSize ~= nil) then
- local items = {};
- for j=1,chestSize,1 do
- local iteminfo = p.getStackInSlot(j);
- if (iteminfo) then
- displayname = iteminfo.display_name;
- if (displayname) then
- if (not items[displayname]) then
- items[displayname] = iteminfo.qty;
- else
- items[displayname] = items[displayname] + iteminfo.qty;
- end
- end
- end
- end
- local k = 0;
- for key,val in pairs(items) do
- k = k + 1;
- updateTable(key,val,timestamp,"#");
- end
- end
- end
- if (p.getMetadata ~= nil) then
- local metadata = p.getMetadata();
- if (metadata ~= nil) then
- if (metadata.displayName ~= nil) then
- if string.find(metadata.displayName, "Black Hole Controller") then
- local itemarray = p.list();
- if (itemarray ~= nil) then
- print("Processing Black Hole Controller");
- for j=1,table.getn(itemarray),1 do
- if (itemarray[j].count > 0) then
- local amount = itemarray[j].count;
- local fingerprint = itemarray[j].name;
- local displayName = itemarray[j].displayName;
- updateTable(displayName,amount,timestamp,"$");
- end
- end
- end
- end
- end
- end
- end
- if (p.listAvailableItems ~= nil) then
- print("Processing ME System");
- local itemarray = p.listAvailableItems();
- if (itemarray ~= nil) then
- for j=1,table.getn(itemarray),1 do
- if (itemarray[j].count > 0) then
- local amount = itemarray[j].count;
- local fingerprint = itemarray[j].name;
- local displayName = itemarray[j].displayName;
- updateTable(displayName,amount,timestamp,"#");
- end
- end
- end
- end
- if (p.listItems ~= nil) then
- print("Processing ME Bridge");
- local itemarray = p.listItems();
- if (itemarray ~= nil) then
- for j=1,table.getn(itemarray),1 do
- if (itemarray[j].amount > 0) then
- local amount = itemarray[j].amount;
- local fingerprint = itemarray[j].name;
- local displayName = itemarray[j].displayName;
- updateTable(displayName,amount,timestamp,"#");
- end
- end
- end
- end
- print("Done");
- end
- end
- function updateMonitor()
- x,y = mon.getSize();
- ColumnWidth = NameLen + 10;
- MaxColumn = math.floor(x / (ColumnWidth))-1;
- mon.setBackgroundColor(colors.black);
- mon.clear();
- CurColumn = 0;
- CurLine = 1;
- mon.setCursorPos(1,1);
- mon.setTextColor(colors.white);
- mon.setTextScale(0.5);
- mon.write(VersionInfo);
- -- Sort by Base names
- local sortedSources = {};
- for n in pairs(ContentData) do
- table.insert(sortedSources, n);
- end
- table.sort(sortedSources);
- local name = "";
- for i,source in ipairs(sortedSources) do
- if (name ~= source) then
- name = source;
- CurColumn = 0;
- mon.setTextColor(titleTextColor);
- mon.setBackgroundColor(titleBackColor);
- mon.setCursorPos(1,CurLine+1);
- mon.write(padString("Contents for "..name,x-1));
- CurLine = CurLine + 3;
- end
- sortedTimestamps = {};
- for timestamp in pairs(ContentData[source]) do
- table.insert(sortedTimestamps,timestamp);
- end
- table.sort(sortedTimestamps);
- latest = nil;
- for j=table.maxn(sortedTimestamps),1,-1 do
- if (latest == nil) then
- latest = sortedTimestamps[j];
- else
- ContentData[source][sortedTimestamps[j]] = nil;
- end
- end
- for itemgroup in pairs(ContentData[source][latest]) do
- CurColumn = 0;
- mon.setTextColor(titleTextColor);
- mon.setBackgroundColor(titleBackColor);
- mon.setCursorPos(1,CurLine-1);
- mon.write(padString(itemgroup, x-1));
- for itemname in pairs(ContentData[source][latest][itemgroup]) do
- count = ContentData[source][latest][itemgroup][itemname]["count"];
- if(count ~= nil) then
- legend = ContentData[source][latest][itemgroup][itemname]["legend"];
- max = 0;
- printmon(itemname,count,max,legend);
- end
- end
- CurLine = CurLine + 2;
- end
- end
- end
- -- This is the main section of the script
- findMonitor();
- findWirelessModem();
- if (isMain == true) then
- if (type(wmod.isWireless) == "function") then
- wmod.open(mainChannel);
- else
- print("You don't have a wireless modem, and this is set as the main computer");
- end
- end
- ContentData = {};
- local timerUpdate = os.startTimer(UpdateIntervalSeconds);
- local updateCount = 0;
- local wirelessEventCount = 0;
- -- Perform Initial Collection and Update the Monitor
- collectLocalInfo();
- updateMonitor();
- -- Main program loop
- while true do
- local event, param1, param2, param3, param4, param5 = os.pullEvent();
- print("Received event:"..event);
- if (event == "timer") then
- if (param1 == timerUpdate) then
- collectLocalInfo();
- if (wmod) then
- if (isMain == false) then
- wmod.transmit(mainChannel,1,ContentData);
- end
- end
- updateMonitor();
- wirelessEventCount = 0;
- timerUpdate = os.startTimer(UpdateIntervalSeconds);
- end
- end
- if (event == "modem_message") then
- if (isMain == true) then
- wirelessEventCount = wirelessEventCount + 1;
- for source,data in pairs(param4) do
- if (ContentData[source] == nil) then
- ContentData[source] = {};
- end
- ContentData[source] = param4[source];
- end
- if (wirelessEventCount >= 10) then
- timerUpdate = os.startTimer(1);
- end
- end
- end
- if (event == "monitor_touch") or (event == "monitor_resize") then
- print("Updating the Monitor");
- collectLocalInfo();
- updateMonitor();
- end
- if (event == "peripheral") or (event == "peripheral_detach") then
- print("Updating the peripheral list");
- peripherals = peripheral.getNames();
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement