Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------
- -- Tank module for caftNanny
- -- by demethan
- -- www.breakfastcraft.com
- -- www.mrjohndowe.com/craftnanny
- -- 2015 08 12 demethan:
- -- -fixed modem support
- -- -did some error magement
- -- -added visual bar
- ---------------------------------------------
- -- variables
- local containers={}
- local version = 1
- local installer = "PTGLduYm"
- local token = '0'
- local module_name = ''
- local username = ''
- local type = ''
- -- write text to the terminal screen
- function draw_text_term(x, y, text, text_color, bg_color)
- term.setTextColor(text_color)
- term.setBackgroundColor(bg_color)
- term.setCursorPos(x,y)
- write(text)
- end
- -- draw a line on the terminal screen
- function draw_line_term(x, y, length, color)
- term.setBackgroundColor(color)
- term.setCursorPos(x,y)
- term.write(string.rep(" ", length))
- end
- function bars()
- draw_line_term(1, 1, 51, colors.lime)
- draw_line_term(1, 19, 51, colors.lime)
- draw_text_term(15, 1, 'CraftNanny Fluid Module', colors.gray, colors.lime)
- draw_text_term(10, 19, 'www.mrjohndowe.com/craftnanny', colors.gray, colors.lime)
- end
- function terminal_screen()
- term.clear()
- bars()
- draw_text_term(1, 2, 'Module: ', colors.lime, colors.black)
- draw_text_term(10, 2, module_name, colors.white, colors.black)
- draw_text_term(1, 3, 'Owner: ', colors.lime, colors.black)
- draw_text_term(8, 3, username, colors.white, colors.black)
- draw_text_term(1, 4 , string.rep("-", 51), colors.lime, colors.black)
- --draw_text_term(2, 8, "I dont know what to put here...", colors.white, colors.black)
- end
- -- retrieves token from local text file
- function load_config()
- sr = fs.open("config.txt", "r")
- token = sr.readLine()
- module_name = sr.readLine()
- username = sr.readLine()
- type = sr.readLine()
- sr.close()
- end
- -- called for new installations and when the scanner needs to be updated
- function run_installer()
- if fs.exists("install") then
- fs.delete("install")
- end
- shell.run("pastebin get "..installer.." install")
- sleep(1)
- shell.run("install")
- end
- ------ Start module specific code ---------
- function phone_home(tank_name, fluid_type, percent)
- response = http.post("http://mrjohndowe.com/craftnanny/code/fluid.php",
- "token="..token.."&id="..os.getComputerID().."&tank_name="..tank_name.."&fluid_type="..fluid_type.."&percent="..percent)
- return_string = response.readAll()
- if tonumber(return_string) > version then
- run_installer()
- end
- end
- --functions
- function findSide()
- local face
- if peripheral.isPresent("left") then
- face="left"
- return true, face
- elseif peripheral.isPresent("right") then
- face="right"
- return true, face
- elseif peripheral.isPresent("bottom") then
- face="bottom"
- return true, face
- elseif peripheral.isPresent("top") then
- face="top"
- return true, face
- elseif peripheral.isPresent("back") then
- face="back"
- return true,face
- else
- face=""
- return false,face
- end
- end
- function round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function getTankInformation(t,tankName)
- tnk=peripheral.wrap(t)
- okLiquid,msg = pcall(tnk.getTankInfo)
- if okLiquid then
- tankTbl=tnk.getTankInfo()
- capacity=tankTbl[1].capacity
- contentsTbl=tankTbl[1].contents or {["rawName"]="nothing",["amount"]=0}
- tankContentName=contentsTbl.rawName
- tankContentAmount=contentsTbl.amount
- percent=round((tankContentAmount/capacity*100),2)
- --print(tankName," ",tankContentName," ",percent," % ")
- phone_home(tankName, tankContentName, percent)
- print(tankContentName," :")
- graphBar= round(((term.getSize()*percent)/100),0)
- if graphBar < 50 then
- draw_line_term(6, 7, graphBar , colors.green)
- draw_line_term(6+graphBar,7,term.getSize()-graphBar-6,colors.red)
- draw_text_term(1,7,percent.." % ",colors.lime,colors.black)
- term.setBackgroundColor(colors.black)
- else
- draw_line_term(6, 7, graphBar -6 , colors.green)
- draw_text_term(1,7,percent.." % ",colors.lime,colors.black)
- term.setBackgroundColor(colors.black)
- end
- return true
- else
- return false
- end
- end
- function notanks()
- -- relavent error msg
- end
- function start_loop()
- ok,side=findSide ()
- if not ok then
- print("No tank storage found")
- end
- tanks = peripheral.getNames()
- while true do
- terminal_screen()
- if #tanks > 2 then
- print("Only one device is supported")
- break
- elseif #tanks == 2 then
- for tankNo,tank in pairs(tanks) do
- if tank~=side then
- ok = getTankInformation(tank,tank)
- end
- end
- else
- ok = getTankInformation(side,"Tank"..os.getComputerID())
- end
- if not ok then
- print("No tank storage found")
- print("Do you have the right module?")
- print("Please check your modems")
- break
- end
- -- main active status with server
- sleep(30)
- end
- end
- function start()
- term.clear()
- term.setCursorPos(1,1)
- if fs.exists("config.txt") then
- load_config()
- start_loop()
- else
- run_installer()
- end
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement