tommy2805

monitor batterie v3

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