Advertisement
SkyNetCloud

Energy Module 2

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