Advertisement
SkyNetCloud

energy

Mar 27th, 2024 (edited)
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.10 KB | Gaming | 0 0
  1. local bat={}
  2. local version = 2
  3.  
  4. local installer = "P324jv87"
  5. local token = '0'
  6. local module_name = ''
  7. local username = ''
  8. local type = ''
  9.  
  10. -- write text to the terminal screen
  11. function draw_text_term(x, y, text, text_color, bg_color)
  12.     term.setTextColor(text_color)
  13.     term.setBackgroundColor(bg_color)
  14.     term.setCursorPos(x,y)
  15.     write(text)
  16. end
  17.  
  18. -- draw a line on the terminal screen
  19. function draw_line_term(x, y, length, color)
  20.     term.setBackgroundColor(color)
  21.     term.setCursorPos(x,y)
  22.     term.write(string.rep(" ", length))
  23. end
  24.  
  25. function bars()
  26.     draw_line_term(1, 1, 51, colors.lime)
  27.     draw_line_term(1, 19, 51, colors.lime)
  28.     draw_text_term(15, 1, 'CraftNanny Energy Module', colors.gray, colors.lime)
  29.     draw_text_term(10, 19, 'craftnanny.org', colors.gray, colors.lime)
  30. end
  31.  
  32. function terminal_screen()
  33.     term.clear()
  34.     bars()
  35.     draw_text_term(1, 2, 'Module: ', colors.lime, colors.black)
  36.     draw_text_term(10, 2, module_name, colors.white, colors.black)
  37.     draw_text_term(1, 3, 'Owner: ', colors.lime, colors.black)
  38.     draw_text_term(8, 3, username, colors.white, colors.black)
  39.     draw_text_term(1, 4 , string.rep("-", 51), colors.lime, colors.black)
  40. end
  41.  
  42. -- retrieves token from local text file
  43. function load_config()
  44.     sr = fs.open("config.txt", "r")
  45.     token = sr.readLine()
  46.     module_name = sr.readLine()
  47.     username = sr.readLine()
  48.     type = sr.readLine()
  49.     sr.close()
  50. end
  51.  
  52. -- called for new installations and when the scanner needs to be updated
  53. function run_installer()
  54.     if fs.exists("install") then
  55.         fs.delete("install")
  56.     end
  57.     shell.run("pastebin get "..installer.." install")
  58.     sleep(1)
  59.     shell.run("install")
  60. end
  61.  
  62.  
  63. ------  Start module specific code ---------
  64.  
  65. function ping_home()
  66.     response = http.post("https://craftnanny.org/code/ping.php",
  67.         "token="..token.."&id="..os.getComputerID())
  68.     current_version = response.readAll()
  69.  
  70.     if tonumber(current_version) > version then
  71.       run_installer()
  72.     end
  73. end
  74.  
  75. function phone_home(bat_name, energy_type, percent)
  76.     response = http.post("https://craftnanny.org/code/energy.php",
  77.         "token="..token.."&id="..os.getComputerID().."&bat_name="..bat_name.."&energy_type="..energy_type.."&percent="..percent)
  78.     --return_string = response.readAll()
  79. end
  80.  
  81. function findSide()
  82.     local faces = {"left", "right", "bottom", "top", "back"}
  83.     for _, face in ipairs(faces) do
  84.         if peripheral.isPresent(face) then
  85.             local peripheralType = peripheral.getType(face)
  86.             if peripheralType == "advancedEnergyCude" then
  87.                 return true, face
  88.             end
  89.         end
  90.     end
  91.     return false, ""
  92. end
  93.  
  94.  
  95. function round(num, idp)
  96.     local mult = 10^(idp or 0)
  97.     return math.floor(num * mult + 0.5) / mult
  98. end
  99.  
  100. function getBat(t,batName)
  101.     bt=peripheral.wrap(t)
  102.  
  103.     okFE,msg = pcall(bt.getMaxEnergy)
  104.     okRF,msg = pcall(bt.getEnergyCapacity)
  105.     okIM,msg = pcall(bt.getMaxEnergyStored)
  106.  
  107.  
  108.     if okIM then
  109.         capacity=bt.getMaxEnergyStored()
  110.         batAmount=bt.getEnergyStored()
  111.         batContentName="FE"
  112.     elseif okRF then
  113.         capacity=bt.getEnergyCapacity()
  114.         batAmount=bt.getEnergy()
  115.         batContentName="RF"
  116.     elseif okFE then
  117.         capacity=bt.getMaxEnergy()
  118.         batAmount=bt.getEnergy()
  119.         batContentName="FE"
  120.     else
  121.         return false
  122.     end
  123.  
  124.     percent=round((batAmount/capacity*100),2)
  125.  
  126.     phone_home(batName, batContentName, percent)
  127.     print(batName," ",batContentName," :")
  128.     powerBar = round(((term.getSize()*percent)/100),0)
  129.     if powerBar < 50 then
  130.         draw_line_term(6, 7, powerBar , colors.green)
  131.         draw_line_term(6+powerBar,7,term.getSize()-powerBar-6,colors.red)
  132.         draw_text_term(1,7,percent.." % ",colors.lime,colors.black)
  133.         term.setBackgroundColor(colors.black)
  134.     else
  135.         draw_line_term(6, 7, powerBar -6 , colors.green)
  136.         draw_text_term(1,7,percent.." % ",colors.lime,colors.black)
  137.         term.setBackgroundColor(colors.black)
  138.     end
  139.     return true
  140. end
  141.  
  142. function nostorage()
  143.     -- relevant error msg
  144. end
  145.  
  146.  
  147. function start_loop()
  148.     ok,side=findSide ()
  149.     if not ok then
  150.         nostorage()
  151.     end
  152.     bats = peripheral.getNames()
  153.     while true do
  154.         terminal_screen()
  155.         ping_home()
  156.         if #bats >2 then
  157.             print("Only one device is supported")
  158.             break
  159.         elseif  #bats == 2 then
  160.             for batNo,bat in pairs(bats) do
  161.                 if bat~=side then
  162.                     ok = getBat(bat,bat)
  163.                 end
  164.             end
  165.         else
  166.             ok = getBat(side,"Battery"..os.getComputerID())
  167.         end
  168.         if not ok then
  169.             print("No power storage found")
  170.             print("Do you have the right module?")
  171.             print("remove all file except install to reset")
  172.             break
  173.         end
  174.         sleep(30)
  175.     end
  176. end
  177.  
  178. function start()
  179.     term.clear()
  180.     term.setCursorPos(1,1)
  181.  
  182.     if fs.exists("config.txt") then
  183.         load_config()
  184.         start_loop()
  185.     else
  186.         run_installer()
  187.     end
  188. end
  189.  
  190. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement