-
Notifications
You must be signed in to change notification settings - Fork 0
/
webMonitor.py
478 lines (413 loc) · 16.5 KB
/
webMonitor.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
from flexx import flx
import os
import serial
#needed for thingspeak
thingspeak_key='B9JGO2DYZM4ONN38'
#class Main(flx.PyComponent):
# def init(self):
# self.sercon = SerialConnection()
# self.widget = SlowControlGUI(self.sercon)
#
# self.sercon.sendCommand('o')
BASE_DIR = os.getcwd()
with open(BASE_DIR + '/static/css/style.css') as f:
style = f.read()
with open(BASE_DIR + '/static/js/script.js') as f:
script = f.read()
flx.assets.associate_asset(__name__, 'style.css', style)
flx.assets.associate_asset(__name__, 'script.js', script)
class LedPulser(flx.PyComponent):
def ledSwitch(self, status):
ret=0
if (status==1):
ret+=os.system("echo 'C1:OUTP ON'>/dev/usbtmc0")
ret+=os.system("echo 'C2:OUTP ON'>/dev/usbtmc0")
elif (status==0):
ret+=os.system("echo 'C1:OUTP OFF'>/dev/usbtmc0")
ret+=os.system("echo 'C2:OUTP OFF'>/dev/usbtmc0")
return ret
import time
class SerialConnection(flx.PyComponent):
def init(self,port):
self.con = serial.Serial(port,115200,timeout=2)
# self.con.timeout = 5
def waitAnswer(self,response):
reply = ''
time.sleep(0.5)
# while True:
x = self.con.readline().decode('UTF-8')
reply += x.strip()
if reply == response:
return reply
def returnAnswer(self):
reply = ''
x = self.con.readline().decode('UTF-8')
reply += x.strip()
return reply
def sendCommand(self,command):
if (command.lower()) == 'i' :
self.con.write("i".encode())
return self.waitAnswer('INH')
elif (command.lower()) == 'o' :
self.con.write("o".encode())
return self.waitAnswer('ON')
elif command == '1':
self.con.write("1".encode())
return self.waitAnswer('V1')
elif command == '0':
self.con.write("0".encode())
return self.waitAnswer('V0')
elif (command.lower()) == 't':
self.con.write("t".encode())
time.sleep(1)
return self.returnAnswer()
else:
return 'Command not supported'
class SerialConnectionChiller(flx.PyComponent):
def init(self,port):
self.con = serial.Serial(port,9600,timeout=2)
# self.con.timeout = 5
def waitAnswer(self,response):
reply = ''
# while True:
time.sleep(0.5)
x = self.con.readline().decode('UTF-8')
reply += x.strip()
if reply == response:
return reply
def returnAnswer(self):
reply = ''
# while True:
x = self.con.readline().decode('UTF-8')
reply += x.strip()
return reply
def sendCommand(self,command):
if (command.lower()) == 't' :
self.con.write('IN_PV_00\r\n'.encode())
time.sleep(0.5)
return self.returnAnswer()
if (command.lower()) == 'g' :
self.con.write('IN_SP_00\r\n'.encode())
time.sleep(0.5)
return self.returnAnswer()
if (command.lower()) == 'p' :
self.con.write('IN_SP_01\r\n'.encode())
time.sleep(0.5)
return self.returnAnswer()
elif (command.lower()) == 's' :
self.con.write('STATUS\r\n'.encode())
time.sleep(0.5)
return self.returnAnswer()
elif (command.lower()) == 'o' :
self.con.write('START\r\n'.encode())
if (self.waitAnswer('OK') == 'OK'):
self.con.write('OUT_SP_01_3\r\n'.encode())
return self.waitAnswer('OK')
else:
return 'ERR'
elif (command.lower()) == 'k' :
self.con.write('STOP\r\n'.encode())
return self.waitAnswer('OK')
else:
return 'Command not supported'
class SerialConnectionHV(flx.PyComponent):
def init(self,port):
self.con = serial.Serial(port,9600,timeout=2)
# self.con.timeout = 5
def waitAnswer(self,response):
reply = ''
# while True:
time.sleep(0.5)
x = self.con.readline().decode('UTF-8')
reply += x.strip()
if response in reply:
return reply
def returnAnswer(self):
reply = ''
# while True:
x = self.con.readline().decode('UTF-8')
reply += x.strip()
return reply
def sendCommand(self,command):
if (command.lower()) == 'm' :
self.con.write('$BD:00,CMD:MON,CH:0,PAR:VMON\r\n'.encode())
time.sleep(0.5)
r=self.waitAnswer('CMD:OK,VAL:')
return r.split('VAL:')[1].strip()
elif (command.lower()) == 's' :
self.con.write('$BD:00,CMD:MON,CH:0,PAR:VSET\r\n'.encode())
time.sleep(0.5)
r=self.waitAnswer('CMD:OK,VAL:')
return r.split('VAL:')[1].strip()
elif (command.lower()) == 'i' :
self.con.write('$BD:00,CMD:MON,CH:0,PAR:STAT\r\n'.encode())
time.sleep(0.5)
r=self.waitAnswer('CMD:OK,VAL:')
return r.split('VAL:')[1].strip()
elif (command.lower()) == 'o' :
self.con.write('$BD:00,CMD:SET,CH:0,PAR:ON\r\n'.encode())
if(self.waitAnswer('CMD:OK')):
return 'OK'
else:
return 'ERR'
elif (command.lower()) == 'k' :
self.con.write('$BD:00,CMD:SET,CH:0,PAR:OFF\r\n'.encode())
if(self.waitAnswer('CMD:OK')):
return 'OK'
else:
return 'ERR'
else:
return 'Command not supported'
class SerialConnectionSensirion(flx.PyComponent):
def init(self,port):
self.con = serial.Serial(port,9600,timeout=2)
# self.con.timeout = 5
def waitAnswer(self,response):
reply = ''
time.sleep(0.5)
# while True:
x = self.con.readline().decode('UTF-8')
reply += x.strip()
if reply == response:
return reply
def returnAnswer(self):
reply = ''
# while True:
x = self.con.readline().decode('UTF-8')
reply += x.strip()
return reply
def sendCommand(self,command):
if (command.lower()) == 't' :
self.con.write('t'.encode())
time.sleep(0.5)
return self.returnAnswer()
else:
return 'Command not supported'
from datetime import datetime
import asyncio
import logging
import requests
def logThingSpeak(message):
try:
conn = requests.post("http://api.thingspeak.com/update",data=message)
except:
print("Thingspeak connection failed")
class SlowControlGUI(flx.PyComponent):
led = flx.IntProp(0, settable=True)
hv = flx.IntProp(0, settable=True)
vsel = flx.IntProp(0, settable=True)
chillerStat = flx.IntProp(0, settable=True)
chillerSwitch = flx.IntProp(0, settable=True)
tempchiller = flx.FloatProp(0, settable=True)
tempbench = flx.FloatProp(0, settable=True)
tempext = flx.FloatProp(0, settable=True)
humbench = flx.FloatProp(0, settable=True)
humext = flx.FloatProp(0, settable=True)
# sercon = flx.ComponentProp()
# serconChiller = flx.ComponentProp()
# serconSensirion = flx.ComponentProp()
# ledcon = flx.ComponentProp()
initialised = 0
def init(self):
super().init()
# self._mutate_sercon(SerialConnection('/dev/ttyACM1'))
self.sercon = SerialConnection('/dev/sensor_0')
# self._mutate_serconChiller(SerialConnectionChiller('/dev/ttyUSB0'))
self.serconChiller = SerialConnectionChiller('/dev/ttyUSB0')
# self._mutate_serconSensirion(SerialConnectionSensirion('/dev/ttyACM0'))
self.serconSensirion = SerialConnectionSensirion('/dev/sensor_1')
# self._mutate_ledcon(LedPulser())
self.ledcon = LedPulser()
self.hvcon = SerialConnectionHV('/dev/ttyUSB4')
self.t0 = datetime.now()
#Always start in inhbit mode
reply=self.sercon.sendCommand('i')
if (reply!='INH'):
print('Please check serial connection')
reply=self.sercon.sendCommand('0')
if (reply!='V0'):
print('Please check serial connection')
ret=self.ledcon.ledSwitch(0)
if (ret!=0):
print('Please check led pulser connection')
# with flx.HBox(flex=0, spacing=10):
with flx.PinboardLayout():
self.tempText = flx.Label(text='T: XX.XX H: YY.YY',style='left:10px; top:120px; width:300px;height:20px;')
self.sensirionText = flx.Label(text='T_EXT:XXX.XX H_EXT:YYY.YY DEW_EXT:ZZZ.ZZ',style='left:10px; top:140px; width:400px;height:20px;')
self.hvText = flx.Label(text='VSET:XXX.XX VMON:XXX.XX',style='left:10px; top:180px; width:300px;height:20px;')
self.chillerText = flx.Label(text='CHILLER TBATH:XXX.XX TSET:YYY.YY PUMP:ZZ',style='left:10px; top:160px; width:400px;height:20px;')
self.but1 = flx.Button(text='Led Pulser',css_class="border-black",style='left:10px; top:10px; width:180px;height:100px;')
self.but2 = flx.Button(text='HV',css_class="border-black",style='left:200px; top:10px; width:150px;height:100px;')
self.but3 = flx.Button(text='VSEL',css_class="border-black",style='left:360px; top:10px; width:100px;height:100px;')
self.chillerStatus = flx.Button(text='COOLING OK',css_class="border-black",style='left:470px; top:10px; width:250px;height:100px;')
# self.label = flx.Label(text='', flex=1) # take all remaining space
self.refreshTemperature()
self.refreshChiller()
self.refreshSensirion()
self.refreshHV()
#upload to things speak (every 60s)
self.refreshThingSpeak()
self.initialised=1
@flx.action
def refreshTemperature(self):
if (self.initialised != 0):
reply=self.sercon.sendCommand('t')
self.tempText.set_text(reply)
logging.info(reply)
temp=reply.split(' ')[1]
self._mutate_tempbench(temp)
hum=reply.split(' ')[3]
self._mutate_humbench(hum)
asyncio.get_event_loop().call_later(5, self.refreshTemperature)
@flx.action
def refreshSensirion(self):
if (self.initialised != 0):
reply=self.serconSensirion.sendCommand('t')
if (len(reply.split(' ')) == 10):
temp=reply.split(' ')[1]
self._mutate_tempext(temp)
hum=reply.split(' ')[7]
self._mutate_humext(hum)
dew=reply.split(' ')[9]
self.sensirionText.set_text('T_EXT:'+temp+' H_EXT:'+hum+' DEW_EXT:'+dew)
logging.info('T_EXT:'+temp+' H_EXT:'+hum+' DEW_EXT:'+dew)
asyncio.get_event_loop().call_later(6, self.refreshSensirion)
@flx.action
def refreshHV(self):
if (self.initialised != 0):
vmon=self.hvcon.sendCommand('m')
vset=self.hvcon.sendCommand('s')
stat=self.hvcon.sendCommand('i')
self.hvText.set_text('VSET:'+vset+' VMON:'+vmon)
logging.info('VSET:'+vset+' VMON:'+vmon+' STATUS:'+stat)
if ( (int(stat) & 1) == 1):
self._mutate_hv(1)
elif ( (int(stat) & 1) == 0):
self._mutate_hv(0)
asyncio.get_event_loop().call_later(7, self.refreshHV)
@flx.action
def refreshChiller(self):
try:
if (self.initialised != 0):
tbath=self.serconChiller.sendCommand('t')
# print(tbath)
tset=self.serconChiller.sendCommand('g')
pump=self.serconChiller.sendCommand('p')
status=self.serconChiller.sendCommand('s')
self.chillerText.set_text('CHILLER TBATH:'+tbath+' TSET:'+tset+' PUMP:'+pump)
if (pump== '000.00' and status == '0'):
self.chillerStatus.set_text('COOLING OFF')
self.chillerStatus.apply_style('background:yellow;')
self._mutate_chillerSwitch(0)
elif (status == '0' and pump== '003.00'):
self.chillerStatus.set_text('COOLING OK')
self.chillerStatus.apply_style('background:green;')
self._mutate_chillerStat(1)
self._mutate_chillerSwitch(1)
else:
self.chillerStatus.set_text('COOLING KO')
self.chillerStatus.apply_style('background:red;')
self._mutate_chillerStat(0)
if not 'ERR' in tbath:
self._mutate_tempchiller(float(tbath))
logging.info('CHILLER TBATH: '+tbath+' STATUS: '+status+' TSET:'+tset+' PUMP:'+pump)
except:
logging.info('Got an exception')
asyncio.get_event_loop().call_later(10, self.refreshChiller)
def refreshThingSpeak(self):
if (self.initialised != 0):
params = {'field1': self.tempbench, 'field2': self.humbench, 'field3': self.tempchiller, 'field4': self.chillerStat, 'field5': self.tempext, 'field6': self.humext, 'key': thingspeak_key}
logThingSpeak(params)
asyncio.get_event_loop().call_later(60, self.refreshThingSpeak)
@flx.action
def hv_switch(self):
if (self.hv==0):
reply=self.hvcon.sendCommand('o')
if (reply=='OK'):
self._mutate_hv(1)
elif (self.hv==1):
reply=self.hvcon.sendCommand('k')
if (reply=='OK'):
self._mutate_hv(0)
@flx.action
def led_switch(self):
if (self.led==0):
ret=self.ledcon.ledSwitch(1)
if (ret==0):
self._mutate_led(1)
elif (self.led==1):
ret=self.ledcon.ledSwitch(0)
if (ret==0):
self._mutate_led(0)
@flx.action
def chiller_switch(self):
if (self.chillerSwitch==0):
reply=self.serconChiller.sendCommand('o')
if (reply=='OK'):
self._mutate_chillerSwitch(1)
elif (self.chillerSwitch==1):
reply=self.serconChiller.sendCommand('k')
if (reply=='OK'):
self._mutate_chillerSwitch(0)
@flx.action
def vsel_switch(self):
if (self.vsel==0):
reply=self.sercon.sendCommand('1')
if (reply=='V1'):
self._mutate_vsel(1)
elif (self.vsel==1):
reply=self.sercon.sendCommand('0')
if (reply=='V0'):
self._mutate_vsel(0)
@flx.reaction('but1.pointer_click')
def but1_clicked(self, *events):
self.led_switch()
@flx.reaction('but2.pointer_click')
def but2_clicked(self, *events):
self.hv_switch()
@flx.reaction('but3.pointer_click')
def but3_clicked(self, *events):
self.vsel_switch()
@flx.reaction('chillerStatus.pointer_click')
def chillerButton_clicked(self, *events):
self.chiller_switch()
# @flx.reaction
# def update_label(self, *events):
@flx.reaction
def update_buttons(self, *events):
if (self.hv==1):
self.but2.set_text('HV ON')
self.but2.apply_style('background:red;')
if (self.hv==0):
self.but2.set_text('HV OFF')
self.but2.apply_style('background:yellow;')
if (self.led==1):
self.but1.set_text('LED ON')
self.but1.apply_style('background:red;')
if (self.led==0):
self.but1.set_text('LED OFF')
self.but1.apply_style('background:yellow;')
if (self.chillerSwitch==0):
self.chillerStatus.set_text('COOLING OFF')
self.chillerStatus.apply_style('background:yellow;')
if (self.chillerSwitch==1 and self.chillerStat==0):
self.chillerStatus.set_text('COOLING ON')
self.chillerStatus.apply_style('background:orange;')
if (self.chillerSwitch==1 and self.chillerStat==1):
self.chillerStatus.set_text('COOLING OK')
self.chillerStatus.apply_style('background:green;')
if (self.vsel==1):
self.but3.set_text('V1')
if (self.vsel==0):
self.but3.set_text('V0')
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l","--log")
parser.add_option("-p","--port")
(options,args)=parser.parse_args()
flx.config.port = options.port
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S',filename=options.log,level=logging.DEBUG)
app = flx.App(SlowControlGUI)
#app.launch('browser') # show it now in a browser
app.serve('')
flx.start() # enter the mainloop