tommy2805

Monitor batterie V2

Feb 26th, 2023 (edited)
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.55 KB | None | 0 0
  1. os.loadAPI("ocs/apis/sensor");
  2. lastCount = 0;
  3. for a, b in pairs(rs.getSides()) do
  4.     if peripheral.getType(b) == "sensor" then
  5.         prox = sensor.wrap(b);
  6.         break
  7.     end
  8. end
  9. for a, b in pairs(rs.getSides()) do
  10.     if peripheral.getType(b) == "monitor" then
  11.         mon = peripheral.wrap(b);
  12.         break
  13.     end
  14. end
  15. term.redirect(mon);
  16.  
  17. -- stati batterie
  18. function ChangeState(x,y,color)
  19.     term.setCursorPos(x, y);
  20.     term.setTextColor(color);
  21.     print("||");
  22.     term.setTextColor(colors.white);
  23. end
  24. function ClearState(x,y)
  25.     term.setCursorPos(x,y);
  26.     term.setTextColor(colors.black);
  27.     print("||");
  28.     term.setTextColor(colors.white);
  29. end
  30.  
  31. -- azioni a seconda della carica della batteria
  32. function battery(l, b)
  33.     local red = colors.red;
  34.     local yellow = colors.yellow;
  35.     local green = colors.lime;
  36.     if percent < 33 then
  37.         ChangeState(l, b,red);
  38.         ClearState(l,b-1);
  39.         ClearState(l,b-2);
  40.     elseif percent >= 33 and (percent <= 66) then
  41.         ChangeState(l, b,red);
  42.         ChangeState(l, b-1,yellow);
  43.         ClearState(l,b-2);
  44.     elseif percent >= 66 then
  45.         ChangeState(l, b,red);
  46.         ChangeState(l, b-1,yellow);
  47.         ChangeState(l, b-2,green);
  48.     end
  49. end
  50.  
  51. function show(device, Nslot, Xstart, Ystart)
  52.     local details = prox.getTargetDetails(device);
  53.     term.setCursorPos(Xstart, Ystart);
  54.     if (details.Name == "Adjustable Energy Storage Unit") then
  55.         print("AESU - ",Nslot);
  56.     elseif (details.Name == "LiFePO4 battery") then
  57.         print("LiFePO4 - ",Nslot);
  58.     end
  59.     term.setCursorPos(Xstart+1, Ystart+1);
  60.     term.setTextColor(colors.black);
  61.     print("--------");
  62.     term.setCursorPos(Xstart+1, Ystart+1);
  63.     term.setTextColor(colors.white);
  64.     print(math.floor(details.Stored));
  65.     term.setCursorPos(Xstart+2, Ystart+2);
  66.     term.setTextColor(colors.black);
  67.     print("-------");
  68.     term.setCursorPos(Xstart+2, Ystart+2);
  69.     term.setTextColor(colors.white);
  70.     percent = math.floor(details.StoredPercentage);
  71.     print(percent, "%");
  72.     battery(Xstart+11, Ystart+2);
  73. end
  74.  
  75. function NoInput(x,y)
  76.     term.setCursorPos(x, y);
  77.     print("No Input");
  78. end
  79.  
  80. -- controllo batterie
  81. while true do
  82.     shell.run("ui");
  83.     t = {};
  84.     tar = prox.getTargets();
  85.     i = 1;
  86.     count = 0;
  87.     for name, basicDetails in pairs(tar) do
  88.         local moreDetails = prox.getTargetDetails(name);
  89.         if moreDetails.Name == "Adjustable Energy Storage Unit" or moreDetails.Name == "LiFePO4 battery" then
  90.             t[i] = name;
  91.             i = i + 1;
  92.             count = count + 1;
  93.         end
  94.     end
  95.     if(count ~= lastCount) then
  96.         term.clear()
  97.     end
  98.     if count > 0 then
  99.         show(t[1],1,3,3);
  100.     else
  101.         NoInput(4,4);
  102.     end
  103.     if count > 1 then
  104.         show(t[2],2,20,3);
  105.     else
  106.         NoInput(21,4);
  107.     end
  108.     if count > 2 then
  109.         show(t[3],3,36,3);
  110.     else
  111.         NoInput(37,4);
  112.     end
  113.     if count > 3 then
  114.         show(t[4],4,3,9);
  115.     else
  116.         NoInput(4,10);
  117.     end
  118.     if count > 4 then
  119.         show(t[5],5,20,9);
  120.     else
  121.         NoInput(21,10);
  122.     end
  123.     if count > 5 then
  124.         show(t[6],6,36,9);
  125.     else
  126.         NoInput(37, 10);
  127.     end
  128.     if count > 6 then
  129.         show(t[7],7,3,15);
  130.     else
  131.         NoInput(4, 16);
  132.     end
  133.     if count > 7 then
  134.         show(t[8],8,20,15);
  135.     else
  136.         NoInput(21, 16);
  137.     end
  138.     if count > 8 then
  139.         show(t[9],9,36,15);
  140.     else
  141.         NoInput(37, 16);
  142.     end
  143.     lastCount = count;
  144.     sleep(1);
  145. end
Advertisement
Add Comment
Please, Sign In to add comment