View difference between Paste ID: 0w3ays8x and x7K3zUAC
SHOW: | | - or go back to the newest paste.
1
---------------------------------------------
2
--	Tank module for caftNanny
3
--	by demethan
4
--	www.breakfastcraft.com
5
--  www.craftnanny.org
6
--  2015 08 12  demethan: 
7
--		-fixed modem support
8
--		-did some error magement
9
-- 		-added visual bar	
10
---------------------------------------------
11
12
-- variables
13
14
local containers={}
15
local version = 1
16
17
local installer = "Q8ah3K9S"
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 Fluid Module', colors.gray, colors.lime)
42
	draw_text_term(10, 19, 'www.craftnanny.org', 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
	--draw_text_term(2, 8, "I dont know what to put here...", colors.white, colors.black)
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(tank_name, fluid_type, percent)
83
    response = http.post("http://craftnanny.org/code/fluid.php",
84
    			"token="..token.."&id="..os.getComputerID().."&tank_name="..tank_name.."&fluid_type="..fluid_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
93
--functions
94
function findSide()
95
	local face
96
	if peripheral.isPresent("left") then 
97
		face="left"
98
		return true, face
99
	elseif peripheral.isPresent("right") then 
100
		face="right"
101
		return true, face
102
	elseif peripheral.isPresent("bottom") then 
103
		face="bottom"
104
		return true, face
105
	elseif peripheral.isPresent("top") then 
106
		face="top"
107
		return true, face
108
	elseif peripheral.isPresent("back") then 
109
		face="back"
110
		return true,face
111
	else
112
		face=""
113
		return false,face
114
	end
115
end
116
117
function round(num, idp)
118
  local mult = 10^(idp or 0)
119
  return math.floor(num * mult + 0.5) / mult
120
end
121
122
function getTankInformation(t,tankName)
123
				tnk=peripheral.wrap(t)
124
				okLiquid,msg = pcall(tnk.getTankInfo)
125
				if okLiquid then 
126
					tankTbl=tnk.getTankInfo()
127
					capacity=tankTbl[1].capacity
128
					contentsTbl=tankTbl[1].contents or {["rawName"]="nothing",["amount"]=0}
129
					tankContentName=contentsTbl.rawName
130
					tankContentAmount=contentsTbl.amount
131
					percent=round((tankContentAmount/capacity*100),2)
132
					
133
					--print(tankName," ",tankContentName," ",percent," %        ")
134
					phone_home(tankName, tankContentName, percent)
135
					
136
					print(tankContentName," :")
137
					graphBar= round(((term.getSize()*percent)/100),0)
138
					if graphBar < 50 then 
139
						draw_line_term(6, 7, graphBar , colors.green)
140
						draw_line_term(6+graphBar,7,term.getSize()-graphBar-6,colors.red)
141
						draw_text_term(1,7,percent.." % ",colors.lime,colors.black)
142
						term.setBackgroundColor(colors.black)
143
					else
144
						draw_line_term(6, 7, graphBar -6 , colors.green)
145
						draw_text_term(1,7,percent.." % ",colors.lime,colors.black)
146
						term.setBackgroundColor(colors.black)
147
					end
148
					return true
149
				else
150
					return false
151
				end
152
end
153
154
function notanks()
155
	-- relavent error msg
156
end
157
158
159
function start_loop()
160
	ok,side=findSide ()
161
	if not ok then 
162
		print("No tank storage found")
163
	end
164
	
165
	tanks = peripheral.getNames()
166
167
	while true do
168
		terminal_screen()
169
		
170
		if #tanks > 2 then
171
			print("Only one device is supported")
172
			break
173
		elseif #tanks == 2 then
174
			 for tankNo,tank in pairs(tanks) do
175
			 	if tank~=side then
176
			 	ok = getTankInformation(tank,tank)			
177
			 	end
178
	
179
			 end
180
			
181
		else
182
			ok = getTankInformation(side,"Tank"..os.getComputerID())
183
		end
184
			if not ok then 
185
					print("No tank storage found")
186
					print("Do you have the right module?")
187
					print("Please check your modems")
188
					break
189
				end
190
		-- main active status with server
191
		sleep(30)
192
	end
193
end
194
195
function start()
196
	term.clear()
197
	term.setCursorPos(1,1)
198
	
199
  if fs.exists("config.txt") then
200
      load_config()
201
	  start_loop()
202
  else
203
  	  run_installer()
204
  end
205
end
206
207
start()