-
Notifications
You must be signed in to change notification settings - Fork 0
/
brwry.py
424 lines (386 loc) · 15 KB
/
brwry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from flask import Flask, render_template, url_for, jsonify, request
from livetemp import LiveTemp
from device import Device
from archive import ArchiveData
from pidcontrol import PIDControl
import json
import time
import os
os.chdir("/home/pi/brwry/")
config = json.loads(open('config.dat').read())
t = LiveTemp(config)
t.start()
d = Device(config)
d.allOff()
a = ArchiveData(config,t,d)
a.start()
c = PIDControl(config,t,d)
c.start()
app = Flask(__name__)
if config['archiving'] == "True":
a.resume()
def updateConfig():
with open('config.dat','w') as outfile:
json.dump(config,outfile)
t.updateConfig(config)
a.updateConfig(config)
d.updateDevices(config)
c.updateConfig(config)
@app.route('/_allOff')
def allOff():
d.allOff()
for ind, target in enumerate(config['targets']):
config['targets'].pop(ind)
print "All Off"
return "Success"
@app.route('/')
def brwry_main():
return render_template('main_page.html')
@app.route('/about')
def brwry_about():
return render_template('about.html')
@app.route('/archives')
def brwry_archives():
return render_template('archives.html')
@app.route('/_liveTempRequest')
def liveTempRequest():
return jsonify(result=t.getCurTemp(),
heat=d.getCurStatus('heats'),
pump=d.getCurStatus('pumps'),
valve=d.getCurStatus('valves'),
targets=config['targets'])
@app.route('/_chartRequest')
def chartRequest():
try:
liveData = json.loads(open(config['storage']+'live.dat').read())
except:
liveData = []
liveOut = {}
for data in liveData:
for key in data:
if key != 'timestamp':
try:
liveOut[key]["data"].append({"x":data['timestamp'],"y":data[key]})
except:
liveOut[key] = {"name":key,"data":[{"x":data['timestamp'],"y":data[key]}]}
arcOut = {}
if config['brwInfo']['brwDate'] != '':
brwFile = config['brwInfo']['brwDate']+'-'+config['brwInfo']['brwr']+'-'+config['brwInfo']['brwName']+'.brw'
try:
archiveData = json.loads(open(config['storage']+brwFile).read())
except:
archiveData = []
for data in archiveData['archive']:
for key in data:
if key != 'timestamp':
for sensor in config['sensors']:
if key == sensor['sensorName']:
try:
arcOut[key]["data"].append({"x":data['timestamp'],"y":data[key]})
except:
arcOut[key] = {"name":key,"type":"sensor","data":[{"x":data['timestamp'],"y":data[key]}]}
for heat in config['heats']:
if key == heat['deviceName']:
try:
arcOut[key]["data"].append({"x":data['timestamp'],"y":data[key]})
except:
arcOut[key] = {"name":key,"type":"heat","data":[{"x":data['timestamp'],"y":data[key]}]}
for pump in config['pumps']:
if key == pump['deviceName']:
try:
arcOut[key]["data"].append({"x":data['timestamp'],"y":data[key]})
except:
arcOut[key] = {"name":key,"type":"pump","data":[{"x":data['timestamp'],"y":data[key]}]}
for valve in config['valves']:
if key == valve['deviceName']:
try:
arcOut[key]["data"].append({"x":data['timestamp'],"y":data[key]})
except:
arcOut[key] = {"name":key,"type":"valve","data":[{"x":data['timestamp'],"y":data[key]}]}
return jsonify(live=liveOut,arch=arcOut)
@app.route('/_chartArchive', methods=['POST'])
def chartArchive():
arcOut = {}
print request.json
if request.json['actionType'] == "view":
try:
archiveData = json.loads(open(config['storage']+request.json['fileName']).read())
except:
archiveData = []
print "archiveData: ",archiveData
for data in archiveData['archive']:
for key in data:
if key != 'timestamp':
for sensor in config['sensors']:
if key == sensor['sensorName']:
try:
arcOut[key]["data"].append({"x":data['timestamp'],"y":data[key]})
except:
arcOut[key] = {"name":key,"type":"sensor","data":[{"x":data['timestamp'],"y":data[key]}]}
for heat in config['heats']:
if key == heat['deviceName']:
try:
arcOut[key]["data"].append({"x":data['timestamp'],"y":data[key]})
except:
arcOut[key] = {"name":key,"type":"heat","data":[{"x":data['timestamp'],"y":data[key]}]}
for pump in config['pumps']:
if key == pump['deviceName']:
try:
arcOut[key]["data"].append({"x":data['timestamp'],"y":data[key]})
except:
arcOut[key] = {"name":key,"type":"pump","data":[{"x":data['timestamp'],"y":data[key]}]}
for valve in config['valves']:
if key == valve['deviceName']:
try:
arcOut[key]["data"].append({"x":data['timestamp'],"y":data[key]})
except:
arcOut[key] = {"name":key,"type":"valve","data":[{"x":data['timestamp'],"y":data[key]}]}
elif request.json['actionType'] == "merge":
archiveAll = []
for archFile in request.json['fileNames']:
try:
archiveData = json.loads(open(config['storage']+archFile).read())
except:
archiveData = []
archiveAll.append(archiveData)
firstDate = -1
firstKey = -1
for key,val in enumerate(archiveAll):
print val['brwDate']
if firstDate == -1:
firstDate = int(val['brwDate'])
firstKey = key
elif int(val['brwDate']) < firstDate:
firstDate = int(val['brwDate'])
firstKey = key
brwFile = archiveAll[firstKey]['brwDate']+'-'+archiveAll[firstKey]['brwr']+'-'+archiveAll[firstKey]['brwName']+'.brw'
arcOut = {"brwDate":archiveAll[firstKey]['brwDate'],"brwr":archiveAll[firstKey]['brwr'],"brwName":archiveAll[firstKey]['brwName'],"archive":[]}
for arcDat in archiveAll:
arcOut['archive'].append(arcDat['archive'])
arcOut['archive'].sort()
with open(config['storage']+brwFile,'w') as outfile:
json.dump(brwData,outfile)
elif request.json['actionType'] == "delete":
for archFile in request.json['fileNames']:
try:
os.remove(config['storage']+archFile)
except:
print "Error deleting file: ",archFile
return jsonify(arch=arcOut)
@app.route('/_configRequest')
def configRequest():
return jsonify(heat=config['heats'],pump=config['pumps'],sensor=config['sensors'],valve=config['valves'])
@app.route('/_existingBrw')
def existingBrw():
return jsonify(brwName=config['brwInfo']['brwName'],brwr=config['brwInfo']['brwr'],brwDate=config['brwInfo']['brwDate'],paused=config['paused'])
@app.route('/configure')
def brwry_config():
return render_template('configure.html')
@app.route('/_archivedBrews')
def archivedBrews():
storageList = os.listdir(config['storage'])
brwList = []
for brw in storageList:
if brw.find('.brw') > -1:
brwList.append(brw)
return jsonify(brwList=brwList)
@app.route('/_fullConfig')
def fullConfig():
availSensors = os.listdir('/sys/bus/w1/devices')
for ind, f in enumerate(availSensors):
if f == 'w1_bus_master1':
availSensors.pop(ind)
return jsonify(config=config,availSensors=availSensors)
@app.route('/_startBrw', methods=['POST'])
def startBrw():
config['brwInfo'] = {
"brwName":request.json['brwName'],
"brwr":request.json['brwr'],
"brwDate":str(int(time.time()))
}
config['archiving']="True"
config['paused']
updateConfig()
a.brwChange(config['brwInfo'])
a.resume()
return jsonify(result=config['brwInfo'])
@app.route('/_deviceOnOff', methods=['POST'])
def deviceOnOff():
for heat in config['heats']:
if heat['deviceName'] == request.json['deviceName']:
if request.json['onOff'] == "ON":
d.deviceOn(heat['gpioPIN'])
else:
d.deviceOff(heat['gpioPIN'])
for ind, target in enumerate(Targets):
if target['device'] == request.json['deviceName']:
Targets.pop(ind)
for pump in config['pumps']:
if pump['deviceName'] == request.json['deviceName']:
if request.json['onOff'] == "ON":
d.deviceOn(pump['gpioPIN'])
else:
d.deviceOff(pump['gpioPIN'])
for ind, target in enumerate(Targets):
if target['device'] == request.json['deviceName']:
Targets.pop(ind)
for valve in config['valves']:
if valve['deviceName'] == request.json['deviceName']:
if request.json['onOff'] == "ON":
d.deviceOn(valve['gpioPIN'])
else:
d.deviceOff(valve['gpioPIN'])
return "Success"
@app.route('/_addTarget', methods=['POST'])
def addTarget():
for ind, target in enumerate(config['targets']):
if target['device'] == request.json['device']:
config['targets'].pop(ind)
targetWrite = {
"device":request.json['device'],
"sensor":request.json['sensor'],
"target":request.json['target']
}
config['targets'].append(targetWrite)
c.updateTargets(config['targets'])
return "Success"
@app.route('/_postConfig', methods=['POST'])
def postConfig():
if request.json['btnType'] == "add":
if request.json['updateData']['what'].find('Sensor') > -1:
config['sensors'].append({"sensorName":request.json['updateData']['sensorName'],
"sensorAddress":request.json['updateData']['sensorAddress'],
"correctionFactor":float(request.json['updateData']['correctionFactor'])})
elif request.json['updateData']['what'].find('Heat') > -1:
if request.json['updateData']['sensors'] == None:
config['heats'].append({"deviceName":request.json['updateData']['deviceName'],
"gpioPIN":int(request.json['updateData']['gpioPIN']),
"sensors":["None"]})
else:
config['heats'].append({"deviceName":request.json['updateData']['deviceName'],
"gpioPIN":int(request.json['updateData']['gpioPIN']),
"sensors":request.json['updateData']['sensors']})
for ind, avail in enumerate(config['gpioPINs']['available']):
if avail == int(request.json['updateData']['gpioPIN']):
config['gpioPINs']['available'].pop(ind)
config['gpioPINs']['unavailable'].append(int(request.json['updateData']['gpioPIN']))
elif request.json['updateData']['what'].find('Pump') > -1:
if request.json['updateData']['sensors'] is None:
config['pumps'].append({"deviceName":request.json['updateData']['deviceName'],
"gpioPIN":int(request.json['updateData']['gpioPIN']),
"sensors":["None"]})
else:
config['pumps'].append({"deviceName":request.json['updateData']['deviceName'],
"gpioPIN":int(request.json['updateData']['gpioPIN']),
"sensors":request.json['updateData']['sensors']})
for ind, avail in enumerate(config['gpioPINs']['available']):
if avail == int(request.json['updateData']['gpioPIN']):
config['gpioPINs']['available'].pop(ind)
config['gpioPINs']['unavailable'].append(int(request.json['updateData']['gpioPIN']))
elif request.json['updateData']['what'].find('Valve') > -1:
config['valves'].append({"deviceName":request.json['updateData']['deviceName'],
"gpioPIN":int(request.json['updateData']['gpioPIN'])})
for ind, avail in enumerate(config['gpioPINs']['available']):
if avail == int(request.json['updateData']['gpioPIN']):
config['gpioPINs']['available'].pop(ind)
config['gpioPINs']['unavailable'].append(int(request.json['updateData']['gpioPIN']))
else:
if request.json['updateData']['what'].find('sens') > -1:
for ind, val in enumerate(config['sensors']):
if val['sensorName'] == request.json['updateData']['oldName']:
for device in config['heats']:
for dind, dval in enumerate(device['sensors']):
if dval == request.json['updateData']['oldName']:
if request.json['btnType'] == "delete":
device['sensors'].pop(dind)
else:
device['sensors'][dind] = request.json['updateData']['sensorName']
for device in config['pumps']:
for dind, dval in enumerate(device['sensors']):
if dval == request.json['updateData']['oldName']:
if request.json['btnType'] == "delete":
device['sensors'].pop(dind)
else:
device['sensors'][dind] = request.json['updateData']['sensorName']
if request.json['btnType'] == "delete":
config['sensors'].pop(ind)
else:
config['sensors'][ind]['sensorName'] = request.json['updateData']['sensorName']
config['sensors'][ind]['sensorAddress'] = request.json['updateData']['sensorAddress']
config['sensors'][ind]['correctionFactor'] = float(request.json['updateData']['correctionFactor'])
elif request.json['updateData']['what'].find('heat') > -1:
for ind, val in enumerate(config['heats']):
if val['deviceName'] == request.json['updateData']['oldName']:
if request.json['btnType'] == "delete":
d.deviceOff(int(request.json['updateData']['gpioPIN']))
config['heats'].pop(ind)
for dind, avail in enumerate(config['gpioPINs']['unavailable']):
if avail == int(request.json['updateData']['gpioPIN']):
config['gpioPINs']['unavailable'].pop(ind)
config['gpioPINs']['available'].append(int(request.json['updateData']['gpioPIN']))
else:
config['heats'][ind]['deviceName'] = request.json['updateData']['deviceName']
config['heats'][ind]['gpioPIN'] = int(request.json['updateData']['gpioPIN'])
config['heats'][ind]['sensors'] = request.json['updateData']['sensors']
elif request.json['updateData']['what'].find('pump') > -1:
for ind, val in enumerate(config['pumps']):
if val['deviceName'] == request.json['updateData']['oldName']:
if request.json['btnType'] == "delete":
d.deviceOff(int(request.json['updateData']['gpioPIN']))
config['pumps'].pop(ind)
for dind, avail in enumerate(config['gpioPINs']['unavailable']):
if avail == int(request.json['updateData']['gpioPIN']):
config['gpioPINs']['unavailable'].pop(ind)
config['gpioPINs']['available'].append(int(request.json['updateData']['gpioPIN']))
else:
config['pumps'][ind]['deviceName'] = request.json['updateData']['deviceName']
config['pumps'][ind]['gpioPIN'] = int(request.json['updateData']['gpioPIN'])
config['pumps'][ind]['sensors'] = request.json['updateData']['sensors']
elif request.json['updateData']['what'].find('valve') > -1:
for ind, val in enumerate(config['valves']):
if val['deviceName'] == request.json['updateData']['oldName']:
if request.json['btnType'] == "delete":
d.deviceOff(int(request.json['updateData']['gpioPIN']))
config['valves'].pop(ind)
for dind, avail in enumerate(config['gpioPINs']['unavailable']):
if avail == int(request.json['updateData']['gpioPIN']):
config['gpioPINs']['unavailable'].pop(ind)
config['gpioPINs']['available'].append(int(request.json['updateData']['gpioPIN']))
else:
config['valves'][ind]['deviceName'] = request.json['updateData']['deviceName']
config['valves'][ind]['gpioPIN'] = int(request.json['updateData']['gpioPIN'])
elif request.json['updateData']['what'] == 'storage':
config['storage'] = request.json['updateData']['storage']
config['gpioPINs']['available'] = sorted(config['gpioPINs']['available'])
config['gpioPINs']['unavailable'] = sorted(config['gpioPINs']['unavailable'])
updateConfig()
return render_template('configure.html')
@app.route('/_pauseBrw')
def pauseBrw():
a.pause()
config['paused']="True"
updateConfig()
return "Success"
@app.route('/_endBrw')
def endBrw():
d.allOff()
a.pause()
config['brwInfo'] = {
"brwName":"",
"brwr":"",
"brwDate":""
}
config['archiving']="False"
config['paused']="True"
config['targets'] = []
updateConfig()
c.updateTargets(config['targets'])
return "Success"
@app.route('/_resumeBrw')
def resumeBrw():
config['paused']="False"
updateConfig()
a.resume()
return "Success"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=80)