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