SkyNetCloud

Tank module

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