Bimgo

CC-Gestion stock

Dec 28th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[Gestion base 1.0 : Ce script permet de faire un suivis des stocks d'une base (tank, chest et barrels) avec une station principale et des secondaires. Pour les chest et barrels il faut utiliser "peripheral proxy".--]]
  2. -- Below are values you can change
  3. local isMain = true; -- base principale (qui va recevoire les information sdes autres bases
  4. local myid = os.computerLabel()
  5. local id = myid -- "Base principale"; -- nom de la base
  6. local UpdateIntervalSeconds = 10; -- reduire le temps si server trop charge
  7. local ItemsFullPercentAt = 256; -- stock minimum d'objet (barre %)
  8. local FluidFullPercentAt = 64000; -- stock minimum de liquide (barre %)
  9.  
  10. local VersionInfo = "Gestion stock"; -- nom de la version
  11.  
  12. local titleTextColor = colors.blue;
  13. local titleBackColor = colors.white;
  14. local tankTextColor = colors.black;
  15. local tankBackColor = colors.lime;
  16. local chestTextColor = colors.white; -- couleur text (chest)
  17. local chestBackColor = colors.purple; -- couleur fond (chest)
  18. local cacheTextColor = colors.white;
  19. local cacheBackColor = colors.cyan;
  20. local powerTextColor = colors.black;
  21. local powerBackColor = colors.orange;
  22.  
  23. local blacklist = {"xxxx"}; -- element a exclure de l'affichage ex: "iron" n'affichera pas Iron ore, Iron Block, Iron Helmet, ...
  24. local whitelist = {""}; -- ajouter les therme a afficher. si whitelist = "" pour desactiver
  25.  
  26. local NameLen = 18;
  27. local mainChannel = 2;
  28.  
  29. -- Above are values you can change
  30. print("Demmarrage "..VersionInfo);
  31. local peripherals = peripheral.getNames();
  32. local mon;
  33. local wmod;
  34. local x,y;
  35. local CurColumn = 0;
  36. local MaxColumn;
  37. local ColumnWidth;
  38. local CurLine = 2;
  39. local ContentData = {};
  40.  
  41. function padString (sText, iLen) -- longueur des text
  42.     local iTextLen = string.len(sText);
  43.     -- Too short, pad
  44.     if (iTextLen < iLen) then
  45.         local iDiff = iLen - iTextLen;
  46.         return(sText..string.rep(" ",iDiff));
  47.     end
  48.     -- Too long, trim
  49.     if (iTextLen > iLen) then
  50.         return(string.sub(sText,1,iLen));
  51.     end
  52.     -- Exact length
  53.     return(sText);
  54. end
  55.  
  56. function prepmonitor(objectmon) -- preparation du moniteur
  57.     mon = peripheral.wrap(objectmon);
  58.     if (mon.isColor() == false) then
  59.         titleTextColor = colors.black;
  60.         titleBackColor = colors.white;
  61.         tankTextColor = colors.black;
  62.         tankBackColor = colors.white;
  63.         chestTextColor = colors.black;
  64.         chestBackColor = colors.white;
  65.         cacheTextColor = colors.black;
  66.         cacheBackColor = colors.white;
  67.         powerTextColor = colors.black;
  68.         powerBackColor = colors.white;
  69.     end
  70. end
  71.  
  72. function updateTable(strSource,strName,strAmount,timestamp,strLegend) -- gestion des la table et affichage information
  73.     local isWhitelisted = true;
  74.     if (type(whitelist) == "table") then
  75.         isWhitelisted = false;
  76.         for l,filter in pairs(whitelist) do
  77.             if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
  78.                 isWhitelisted = true;
  79.             end
  80.         end
  81.     end
  82.     if (isWhitelisted == false) then
  83.         return;
  84.     end
  85.     if (type(blacklist) == "table") then
  86.         for l,filter in pairs(blacklist) do
  87.             if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
  88.                 return;
  89.             end
  90.         end
  91.     end
  92.     if (ContentData[strSource] == nil) then
  93.         ContentData[strSource] = {};
  94.     end
  95.     if (ContentData[strSource][timestamp] == nil) then
  96.         ContentData[strSource][timestamp] = {};
  97.     end
  98.     if (ContentData[strSource][timestamp][strName] == nil) then
  99.         ContentData[strSource][timestamp][strName] = {};
  100.     end
  101.     if (ContentData[strSource][timestamp][strName]["count"] == nil) then
  102.         ContentData[strSource][timestamp][strName]["count"] = strAmount;
  103.     else
  104.         ContentData[strSource][timestamp][strName]["count"] = ContentData[strSource][timestamp][strName]["count"] + strAmount;
  105.     end
  106.     ContentData[strSource][timestamp][strName]["legend"] = strLegend;
  107. end
  108.  
  109. function printmon(strName,strAmount,strMax,strLegend)
  110.     local textColor;
  111.     local backColor;
  112.     local FullPercentAt = ItemsFullPercentAt;
  113.     if (strLegend == "#") then
  114.         textColor = chestTextColor;
  115.         backColor = chestBackColor;
  116.         strLegend = "";
  117.     end
  118.     if (strLegend == "+") then
  119.         textColor = tankTextColor;
  120.         backColor = tankBackColor;
  121.         FullPercentAt = FluidFullPercentAt;
  122.         strLegend = "";
  123.     end
  124.     if (strLegend == "*") then
  125.         textColor = powerTextColor;
  126.         backColor = powerBackColor;
  127.         strLegend = "";
  128.     end
  129.     if (strLegend == "$") then
  130.         textColor = cacheTextColor;
  131.         backColor = cacheBackColor;
  132.         strLegend = "";
  133.     end
  134.     local line = string.format("%s  %3i%s",padString(strName,NameLen+1),strAmount,padString(strLegend,1));
  135.     if (strAmount >= 1000000) then
  136.         line = string.format("%s  %3iM%s",padString(strName,NameLen),math.floor(strAmount/1000000),padString(strLegend,1));
  137.     elseif (strAmount >= 1000) then
  138.         line = string.format("%s  %3iK%s",padString(strName,NameLen),math.floor(strAmount/1000),padString(strLegend,1));
  139.     end
  140.  
  141.     local ColPadding = 0;
  142.     if (CurColumn > 0) then
  143.         ColPadding = 1;
  144.     end
  145.     local CurX = math.floor((CurColumn*ColumnWidth))+math.floor(CurColumn*ColPadding)+1;
  146.     if (CurColumn == 0) then
  147.         --  print("CurX:"..CurX);
  148.     end
  149.     mon.setCursorPos(CurX,CurLine);
  150.     --local percent = strAmount / strMax * 100;
  151.     mon.setBackgroundColor(backColor);
  152.     local percent = strAmount / FullPercentAt * 100;
  153.     if (percent > 100) then percent = 100; end
  154.     local barlength = math.floor(percent / 100 * (string.len(line)-1));
  155.     --if (CurColumn == 0) then
  156.     --  barlength = barlength + 1;
  157.     --end
  158.  
  159.     if (string.len(line) > barlength) then
  160.         local msg = string.sub(line,1,barlength);
  161.         mon.setTextColor(textColor);
  162.         mon.write(msg);
  163.         --[[if (percent == 0) then
  164.             mon.setBackgroundColor();
  165.         else
  166.             mon.setBackgroundColor(colors.black);
  167.         end--]]
  168.         mon.setBackgroundColor(colors.black);
  169.         mon.setTextColor(backColor);
  170.         mon.write(string.sub(line,barlength+1,-2))
  171.     else
  172.         local spaces = barlength - string.len(line);
  173.         mon.write(line);
  174.         mon.write(string.rep(" ",spaces));
  175.     end
  176.  
  177.     mon.setTextColor(colors.white);
  178.     CurColumn = CurColumn + 1;
  179.     if (CurColumn > MaxColumn) then
  180.         CurColumn = 0;
  181.         CurLine = CurLine + 1;
  182.     end
  183.     return true;
  184. end
  185.  
  186. function findMonitor() -- recherche moniteur
  187.     for i,name in pairs(peripherals) do
  188.         for j,method in pairs(peripheral.getMethods(name)) do
  189.             if (method == 'getCursorPos') then
  190.                 prepmonitor(name);
  191.             end
  192.         end
  193.     end
  194. end
  195.  
  196. function findWirelessModem() -- recherche modem
  197.     local foundWireless = false;
  198.     for i,name in pairs(peripherals) do
  199.         for j,method in pairs(peripheral.getMethods(name)) do
  200.             if (method == 'isWireless') then
  201.                 wmod = peripheral.wrap(name);
  202.                 if (wmod.isWireless()) then
  203.                     wmod.closeAll();
  204.                     foundWireless = true;
  205.                     break;
  206.                 else
  207.                     wmod = {};
  208.                 end
  209.             end
  210.         end
  211.         if (foundWireless) then
  212.             break;
  213.         end
  214.     end
  215. end
  216.  
  217. function collectLocalInfo()
  218.     local timestamp = os.clock();
  219.     for i,name in pairs(peripherals) do
  220.         local p = peripheral.wrap(name);
  221.         local displayNames = {};
  222.         if (p.getTankInfo ~= nil) then -- RECHERCHE TANK
  223.             print("Processing Tank");
  224.             local iteminfo = p.getTankInfo();
  225.             if (iteminfo ~= nil) then
  226.                 local displayname = "Empty";
  227.                 local amount = 0;
  228.                 if (iteminfo[1].contents) then
  229.                     displayname = iteminfo[1].contents.rawName;
  230.                     amount = iteminfo[1].contents.amount;
  231.                     amount = math.floor(amount/1000);
  232.                 end
  233.                 if (amount ~= 0) then
  234.                     updateTable(id,displayname,amount,timestamp,"+");
  235.                 end
  236.             end
  237.         end
  238.         if (p.getStoredItems ~= nil) then -- RECHERCHE STOCKAGE ITEM (barrel)
  239.             print("Processing Cache");
  240.             local iteminfo = p.getStoredItems();
  241.             if (iteminfo) then
  242.                 local displayname = iteminfo.display_name;
  243.                 updateTable(id,displayname,iteminfo.qty,timestamp,"$");
  244.             end
  245.         end
  246.         if (p.getEnergyStored ~= nil) then -- RECHERCHE STOCAGE ENERGY
  247.             print("Processing Energy");
  248.             local energy = p.getEnergyStored();
  249.             if (energy ~= nil) then
  250.                 updateTable(id,"Energy",energy,timestamp,"*");
  251.             end
  252.         end
  253.         if (p.getInventorySize ~= nil) then -- RECHERCHE STOCKAGE ITEM (chest)
  254.             print("Processing Chest");
  255.             local chestSize = p.getInventorySize();
  256.             if (chestSize ~= nil) then
  257.                 local items = {};
  258.                 for j=1,chestSize,1 do
  259.                     local iteminfo = p.getStackInSlot(j);
  260.                     if (iteminfo) then
  261.                         displayname = iteminfo.display_name;
  262.                         if (displayname) then
  263.                             if (not items[displayname]) then
  264.                                 items[displayname] = iteminfo.qty;
  265.                             else
  266.                                 items[displayname] = items[displayname] + iteminfo.qty;
  267.                             end
  268.                         end
  269.                     end
  270.                 end
  271.                 local k = 0;
  272.                 for key,val in pairs(items) do
  273.                     k = k + 1;
  274.                     updateTable(id,key,val,timestamp,"#");
  275.                 end
  276.             end
  277.         end
  278.         if (p.getAvailableItems ~= nil) then -- RECHERCHE DONNEES ME / APPLIED ENERGETIC
  279.             print("Processing ME System");
  280.             local itemarray = p.getAvailableItems();
  281.             if (itemarray ~= nil) then
  282.                 for j=1,table.getn(itemarray),1 do
  283.                     if (itemarray[j].size > 0) then
  284.                         local fingerprint = itemarray[j].fingerprint;
  285.                         if ( (displayNames[fingerprint.id] == nil) or (displayNames[fingerprint.id] == fingerprint.id)) then
  286.                             local ItemDetail = p.getItemDetail(fingerprint);
  287.                             if (ItemDetail ~= nil) then
  288.                                 displayNames[fingerprint.id] = ItemDetail.basic().display_name;
  289.                             else
  290.                                 displayNames[fingerprint.id] = fingerprint.id;
  291.                             end
  292.                         end
  293.                         updateTable(id,displayNames[fingerprint.id],itemarray[j].size,timestamp,"#");
  294.                     end
  295.                 end
  296.             end
  297.         end
  298.         if (p.listItems ~= nil) then -- RECHERCHE BRIDGE ME
  299.             print("Processing ME Bridge");
  300.             local itemarray = p.listItems();
  301.             if (itemarray ~= nil) then
  302.                 for j=1,table.getn(itemarray),1 do
  303.                     if (itemarray[j].amount > 0) then
  304.                         local fingerprint = itemarray[j].name;
  305.                         local displayName = itemarray[j].displayName;
  306.                         updateTable(id,displayName,itemarray[j].amount,timestamp,"#");
  307.                     end
  308.                 end
  309.             end
  310.         end
  311.         print("Done");
  312.     end
  313. end
  314.  
  315. function updateMonitor() -- mise a jour moniteur
  316.     x,y = mon.getSize();
  317.     ColumnWidth = NameLen + 7;
  318.     MaxColumn = math.floor(x / (ColumnWidth))-1;
  319.     mon.setBackgroundColor(colors.black);
  320.     mon.clear();
  321.     CurColumn = 0;
  322.     CurLine = 1;
  323.     mon.setCursorPos(1,1);
  324.     mon.setTextColor(colors.white);
  325.     mon.setTextScale(0.5);
  326.     mon.write(VersionInfo);
  327.     -- Tri par nom de base
  328.     local sortedSources = {};
  329.     for n in pairs(ContentData) do
  330.         table.insert(sortedSources, n);
  331.     end
  332.     table.sort(sortedSources);
  333.     local name = "";
  334.     for i,source in ipairs(sortedSources) do
  335.         if (name ~= source) then
  336.             name = source;
  337.             CurColumn = 0;
  338.             mon.setTextColor(titleTextColor);
  339.             mon.setBackgroundColor(titleBackColor);
  340.             mon.setCursorPos(1,CurLine+1);
  341.             mon.write(padString("Stock de "..name,x-1));
  342.             CurLine = CurLine + 2;
  343.         end
  344.         sortedTimestamps = {};
  345.         for timestamp in pairs(ContentData[source]) do
  346.             table.insert(sortedTimestamps,timestamp);
  347.         end
  348.         table.sort(sortedTimestamps);
  349.         latest = nil;
  350.         for j=table.maxn(sortedTimestamps),1,-1 do
  351.             if (latest == nil) then
  352.                 latest = sortedTimestamps[j];
  353.             else
  354.                 ContentData[source][sortedTimestamps[j]] = nil;
  355.             end
  356.         end
  357.         for itemname in pairs(ContentData[source][latest]) do
  358.             displayname = itemname;
  359.             count = ContentData[source][latest][itemname]["count"];
  360.             legend = ContentData[source][latest][itemname]["legend"];
  361.             max = 0;
  362.             printmon(displayname,count,max,legend);
  363.         end
  364.     end
  365. end
  366.  
  367. -----------------------------------------------PROGRAM---------------------------------------------------
  368. findMonitor();
  369. findWirelessModem();
  370.  
  371. if (isMain == true) then
  372.     if (type(wmod.isWireless) == "function") then
  373.         wmod.open(mainChannel);
  374.     else
  375.         print("Pas de modem sur le PC principal");
  376.     end
  377. end
  378.  
  379. ContentData = {};
  380.  
  381. local timerUpdate = os.startTimer(UpdateIntervalSeconds);
  382. local updateCount = 0;
  383. local wirelessEventCount = 0;
  384. -- Perform Initial Collection and Update the Monitor
  385. collectLocalInfo();
  386. updateMonitor();
  387.  
  388. -- Main program loop
  389. while true do  
  390.     local event, param1, param2, param3, param4, param5 = os.pullEvent();
  391.     print("Event recus :"..event);
  392.     if (event == "timer") then
  393.         if (param1 == timerUpdate) then
  394.             collectLocalInfo();
  395.             if (wmod) then
  396.                 if (isMain == false) then
  397.                     wmod.transmit(mainChannel,1,ContentData);
  398.                 end
  399.             end
  400.             updateMonitor();
  401.             wirelessEventCount = 0;
  402.             timerUpdate = os.startTimer(UpdateIntervalSeconds);
  403.         end
  404.     end
  405.     if (event == "modem_message") then
  406.         if (isMain == true) then
  407.             wirelessEventCount = wirelessEventCount + 1;
  408.             for source,data in pairs(param4) do
  409.                 if (ContentData[source] == nil) then
  410.                     ContentData[source] = {};
  411.                 end
  412.                 ContentData[source] = param4[source];
  413.             end
  414.             if (wirelessEventCount >= 10) then
  415.                 timerUpdate = os.startTimer(1);
  416.             end
  417.         end
  418.     end
  419.     if (event == "monitor_touch") or (event == "monitor_resize") then
  420.         print("Updating the Monitor");
  421.         updateMonitor();
  422.     end
  423.     if (event == "peripheral") or (event == "peripheral_detach") then
  424.         print("Updating the peripheral list");
  425.         peripherals = peripheral.getNames();
  426.     end
  427. end
Add Comment
Please, Sign In to add comment