-
Notifications
You must be signed in to change notification settings - Fork 2
/
flask_app.py
629 lines (568 loc) · 22.3 KB
/
flask_app.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
from __future__ import print_function
import os
import sys
import time
import datetime
from collections import deque
import logging
import logzero
from logzero import logger
# import socket
import subprocess
import zmq
import fridge_machine
import tail
import ruamel
from ruamel.yaml import YAML
import numpy as np
from flask import Flask, render_template, request, send_file
from flask_socketio import SocketIO, emit
import eventlet
import plotly
# import plotly.plotly as py
import plotly.graph_objs as go
import json
import preallocate
import inspect
currentdir = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
print(parentdir)
if 'piServer2' not in parentdir:
parentdir = os.path.join(parentdir, 'piServer2')
sys.path.insert(0, parentdir)
import data_logger
import client
try:
import lttb
except Exception as e:
print('lttb not found')
print(e)
# from eventlet import monkey_patch as monkey_patch
eventlet.monkey_patch()
yaml = YAML()
logzero.loglevel(logging.INFO)
app_logger = logger
# Socket to talk to subscribe to temperatures
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.connect("tcp://localhost:5556")
subscriber_filter = ''
# Python 2 - ascii bytes to unicode str
if isinstance(subscriber_filter, bytes):
subscriber_filter = subscriber_filter.decode('ascii')
subscriber.setsockopt_string(zmq.SUBSCRIBE, subscriber_filter)
state_subscriber = context.socket(zmq.SUB)
state_subscriber.connect("tcp://localhost:5557")
state_subscriber.setsockopt_string(zmq.SUBSCRIBE, subscriber_filter)
poller = zmq.Poller()
poller.register(subscriber, zmq.POLLIN)
poller.register(state_subscriber, zmq.POLLIN)
fridge_client = context.socket(zmq.REQ)
fridge_client.connect("tcp://localhost:5555")
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['TEMPLATES_AUTO_RELOAD'] = True
socketio = SocketIO(app, async_mode=None)
thread = None
TESTING = False
if len(sys.argv) == 3:
TESTING = True
print('TESTING')
if TESTING:
# with open('./logs/2017-10-10-12-19-14.csv', 'r') as f:
with open('./logs/2018-11-03-16-42-33.csv', 'r') as f:
labels = f.readline()
labels = labels.split(',', 1)[-1]
LOG_PATH = './logs/'
table = ['40K', '4K', '1K', 'switch', 'pump', 'hp', 'hs']
LABEL_OFFSET = 1
YAML_FILE = './config.yaml'
else:
labels = client.client('127.0.0.1', 50326, 'getlabels')
LOG_PATH = '../logs/'
table = ['40K', '4K', '1K', 'switch', 'pump', 'hp', 'hs', 'relays']
LABEL_OFFSET = 2
YAML_FILE = '../config.yaml'
with open(YAML_FILE, 'r') as f:
CONFIG = yaml.load(f)
table = CONFIG['show_list']
graph = CONFIG['graph']
labels = labels.split(',')
labels = [label.strip() for label in labels]
for l in labels:
print(len(l), l, type(l))
# graph = ['40K', '4K', '1K', 'switch', 'pump']
DATA = None
SIZE = 100000
HR_date = False
def load_history():
# global data_history, logfile_prefix
# if 'logfile_prefix' in fridge.config['logfile_prefix']:
# prefix = fridge.config['logfile_prefix']
# else:
# prefix = ''
# Find previous log file
global HR_date, logfile_prefix
previous_log_filename = data_logger.getlast.getlast(LOG_PATH, '*.csv')
print('log_filename', previous_log_filename)
# Create logger object
if '-' in previous_log_filename:
logfile_prefix = previous_log_filename.split('-')[:2]
print('logfile_prefix', logfile_prefix)
try:
if len(logfile_prefix[1]) == 4: # Second item is the year
logfile_prefix = logfile_prefix[0]
else:
logfile_prefix = logfile_prefix[0][:-4] # strip year
print('logfile_prefix', logfile_prefix)
except:
pass
else:
logfile_prefix = ''
filename = LOG_PATH + previous_log_filename
if os.name == 'nt':
history = tail.tail(filename, 50000)
data_history = np.genfromtxt(history, invalid_raise=False,
delimiter=",")
else:
tail_out = subprocess.Popen(['tail', '-50000', filename],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, errors = tail_out.communicate()
# with open(filename, 'r') as f:
# history = deque(f, history_start)
history_start = 100000
history = deque(iter(output.decode().splitlines()), history_start)
history = output.decode().splitlines()
print('type history', type(history))
data_history = np.genfromtxt(history, invalid_raise=False,
delimiter=',')
print(type(data_history), data_history.shape)
# Check if Human Readable data is the second column
print(data_history[0, 1])
if np.isnan(data_history[0, 1]):
HR_date = True
else:
HR_date = False
print('HR_date: ' + '%s' % HR_date)
data = preallocate.PreallocatedArray(np.zeros((SIZE, data_history.shape[1])))
data[:data_history.shape[0], :data_history.shape[1]] = data_history
data.length = data_history.shape[0]
try:
last_point = history.pop()
except:
last_point = None
app_logger.info('last_point, %r' % last_point)
return data, filename
DATA, FILENAME = load_history()
def on_new_log_pressed():
global logfile_prefix
head = ''
if len(logfile_prefix) > 0:
print('logfile_prefix:', logfile_prefix)
if logfile_prefix.endswith('-'):
logfile_prefix = logfile_prefix[:-1]
head = logfile_prefix.rstrip('0123456789')
print('head:', head)
if len(head) > 0:
print(logfile_prefix, head, len(head))
tail_idx = int(logfile_prefix[len(head):])
print('new file name', head, tail_idx+1)
logfile_prefix = head + '%d-' % (tail_idx + 1)
print(logfile_prefix)
filename = LOG_PATH + logfile_prefix + \
datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S.csv')
print('new log file name: ' + filename)
with open(filename, 'w') as csvfile:
csvfile.write('')
if False:
create_log(filename, logger=logger_config['logger'])
self.logfile_label.set_text(filename)
self.logfile_label.attributes['download'] = os.path.basename(filename)
self.logfile_label._filename = filename
def background_thread():
global labels, graph, table, DATA, HR_date
"""send server generated events to clients."""
app_logger.info('starting background thread')
socketio.sleep(1)
while True:
app_logger.debug('poll in background')
socketio.sleep(1)
socks = dict(poller.poll(100))
app_logger.debug('finished polling')
# print('poll socks', socks)
if subscriber in socks:
data_string = subscriber.recv_string()
if data_string.startswith('file'):
app_logger.info('data_string:', data_string)
socketio.emit('update_log_name', os.path.basename(data_string))
return
# print('subscriber message:', data_string)
data = data_string.split(',')
# print('len of data', len(data))
app_logger.debug(data_string)
x = datetime.datetime.fromtimestamp(float(data[0]))
# x = datetime.datetime.now()
x = x.strftime('%Y-%m-%d %H:%M:%S')
timestamp = x
y = []
x = []
#for i, name in enumerate(fridge.config['graph']):
# idx = self.sensor_names.index(name) + 1
# value = float(data_string_list[idx])
def get_idx(labels, name):
idx = None
offset = 1
try:
idx = labels.index(name.lower()) + offset
except ValueError:
try:
idx = labels.index(name.upper()) + offset
except Exception as e:
# print(e)
pass
except Exception as e:
print(e)
# if idx is None:
# print('Can not find ', name)
return idx
# print('labels', labels)
for i, name in enumerate(graph):
idx = get_idx(labels, name)
# print(i, name, idx)
value = float(data[idx])
if np.isnan(value):
value = -1
y.append([value])
x.append([timestamp])
# print(x,y)
datastr = {'x': x, 'y': y}
# print('datastr', datastr)
table_dict = {}
for name in table:
idx = get_idx(labels, name)
# idx = labels.index(name.lower()) + 1
if idx is not None:
value = float(data[idx])
if np.isnan(value):
value = -1
else:
value = -2
table_dict[name] = value
alldata = {'graph': datastr, 'table': table_dict}
socketio.emit('new_data', alldata) # ,namespace='/')
new_row = np.fromstring(data_string, sep=',')
# print('len new_row', len(new_row), HR_date)
if HR_date:
new_row2 = np.zeros(len(new_row)+1)
new_row2[0] = new_row[0]
new_row2[1] = float('nan')
new_row2[2:] = new_row[1:]
else:
new_row2 = new_row
DATA.append_row(new_row2)
# print('background DATA.shape', DATA.shape, DATA.length)
elif state_subscriber in socks:
data_string = state_subscriber.recv_string()
app_logger.debug('fridge is in state: %s' % data_string)
socketio.emit('fridge_state', data_string)
@socketio.on('connect', namespace='/')
def test_connect():
global thread, graph, table
if (thread is None): # and (not TESTING):
thread = socketio.start_background_task(target=background_thread)
app_logger.debug("got connect, started background, sending filename" +
"and stuff, connect event")
data = {'graph': graph, 'table': table,
'log_filename': os.path.basename(FILENAME)}
socketio.emit('connect', data) # , namespace='/')
@socketio.on('disconnect') # , namespace='/')
def test_disconnect():
print('Client disconnected', request.sid)
@socketio.on('my_event') # , namespace='/')
def my_event(message):
app_logger.info('my_event' + request.sid)
app_logger.info('message' + message)
# if True:
if not TESTING:
client_message = 'get_recycle_hour'
fridge_client.send_string(client_message)
recycle_hour = int(float(fridge_client.recv_string()))
client_message = 'get_next_recycle_time'
fridge_client.send_string(client_message)
next_recycle_time = eval(fridge_client.recv_string())
next_recycle_time = next_recycle_time.strftime('%Y-%m-%d %H:%M:%S')
message = {'recycle_hour': recycle_hour,
'next_time': next_recycle_time}
socketio.emit('update_recycle', message)
def tweak_display():
alldata = {'elt': 'recycle', 'display': 'none'}
socketio.emit('display', alldata) # ,namespace='/')
alldata = {'elt': 'checkboxes', 'display': 'none'}
socketio.emit('display', alldata) # ,namespace='/')
@socketio.on('new_logfile') # , namespace='/')
def new_logfile():
print('Create new log file')
on_new_log_pressed()
@socketio.on('unzoom') # , namespace='/')
def unzoom(message):
global graph
app_logger.debug('Got unzoom request')
graphJSON = build_graphjson()
app_logger.debug('send command to update graph via zoom_graph')
socketio.emit('zoom_graph', graphJSON)
@socketio.on('zoom') # , namespace='/')
def zoom(message):
global graph
# print('zoom', request.sid)
# print('message', type(message), message)
format_str = '%Y-%m-%d %H:%M:%S.%f'
start = datetime.datetime.strptime(message['xaxis.range[0]'], format_str)
stop = datetime.datetime.strptime(message['xaxis.range[1]'], format_str)
start = time.mktime(start.timetuple())
stop = time.mktime(stop.timetuple())
app_logger.info('zoom in timestamps for range %r %r %r' % (start, stop,
stop-start))
newdata, filename = load_data(graph, data_slice=(start, stop))
data = []
figure = {}
for curve in newdata:
trace = go.Scattergl(x=curve['x'], y=curve['y'],
name=curve['name'], mode=curve['mode'])
data.append(trace)
layout = go.Layout( # title=plotTitle, \
yaxis={'title': 'Temperatures', 'type': 'log'},
xaxis={'title': 'Time'},
margin={'l': 50, 'r': 50, 'b': 50, 't': 5, 'pad': 0},
)
# PlotlyJSONEncoder converts objects to their JSON equivalents
figure=dict(data=data, layout=layout)
graphJSON = json.dumps(figure, cls=plotly.utils.PlotlyJSONEncoder)
socketio.emit('zoom_graph', graphJSON)
@socketio.on('set_recycle_hour') #, namespace='/')
def set_recycle_hour(message):
print('set_recyle_hour', request.sid)
print('recycle_hour message', type(message), message)
fridge_client.send_string('set_recycle_hour '+message)
msg = fridge_client.recv_string()
@socketio.on('switch') #, namespace='/')
def switch_event(message):
print('switch', request.sid)
print('switch message', type(message), message)
client_message = ' '.join(['set_heater', message['name'],
'%s' % message['state']])
print('client_message', client_message)
fridge_client.send_string(client_message)
msg = fridge_client.recv_string()
@socketio.on('state') #, namespace='/')
def switch_event(message):
print('state', request.sid)
print('state message', type(message), message)
# This sends the data... should see if timesout...
fridge_client.send_string('set_state '+message)
msg = fridge_client.recv_string()
@app.route("/logfile/")
def download_logfile():
try:
print('sending', os.path.basename(FILENAME))
return send_file(FILENAME, as_attachment=True, attachment_filename=os.path.basename(FILENAME))
except Exception as e:
return str(e)
@app.route("/", methods=['GET', 'POST'])
def plot():
app_logger.info('plot from route')
global DATA, graph
plotTitle = 'Flask socketio plotly test app'
# graphJSON = build_graphjson()
layout = go.Layout( # title=plotTitle, \
yaxis={'title':'Temperatures', 'type':'log'}, xaxis={'title':'Time'},
margin={ 'l': 50, 'r': 50, 'b': 50, 't': 5, 'pad': 0 },
)
trace= go.Scattergl(x=[0,1], y=[1,0], name='temp')
# PlotlyJSONEncoder converts objects to their JSON equivalents
figure=dict(data=[trace], layout=layout)
graphJSON = json.dumps(figure, cls=plotly.utils.PlotlyJSONEncoder)
# # Updates the data for the table
# newdata, filename = load_data(graph)
# # Create a traces and layout to plot
# print('filename', filename)
# data=[]
# figure={}
# # for j in range(num_curves):
# for curve in newdata:
# trace = go.Scattergl(x=curve['x'], y=curve['y'],
# name=curve['name'], mode=curve['mode'])
# data.append(trace)
# layout = go.Layout( # title=plotTitle, \
# yaxis={'title':'Temperatures', 'type':'log'}, xaxis={'title':'Time'},
# margin={ 'l': 50, 'r': 50, 'b': 50, 't': 5, 'pad': 0 },
# )
#
# # PlotlyJSONEncoder converts objects to their JSON equivalents
# figure=dict(data=data, layout=layout)
# graphJSON = json.dumps(figure, cls=plotly.utils.PlotlyJSONEncoder)
# try to connect to clients
print('try to connect to clients')
test_connect()
tweak_display()
if False:
client_message = 'get_recycle_hour'
fridge_client.send_string(client_message)
recycle_hour = int(float(fridge_client.recv_string()))
client_message = 'get_next_recycle_time'
fridge_client.send_string(client_message)
next_recycle_time = eval(fridge_client.recv_string())
next_recycle_time = next_recycle_time.strftime('%Y-%m-%d %H:%M:%S')
else:
recycle_hour = -1
next_recycle_time = 'fix me'
return render_template('webFridge.html', graphJSON=graphJSON,
states = fridge_machine.Fridge.fridge_states,
recycle_hour = recycle_hour,
next_recycle_time = next_recycle_time,
async_mode=socketio.async_mode,
room=CONFIG['location'],
name=CONFIG['name'],
)
def load_data(graph, data_slice=None):
global DATA, FILENAME
app_logger.info('building graphs')
app_logger.info('load_data DATA.shape %r %r' % (DATA.shape, DATA.length))
# history, filename = load_history()
history = DATA
filename = FILENAME
newdata = []
if data_slice is None:
data_slice = range(DATA.length)
else:
start = np.where(history[:,0]>data_slice[0])[0][0]
stop = np.where(history[:,0]<data_slice[1])[0][-1]
data_slice = range(start, stop)
# print('data_slice:', data_slice, stop-start)
# bool_ = np.logical_and(history[:,0] > data_slice[0], history[:,0] <
# data_slice[1])
# data_slice = np.where(bool_)[0]
x = [datetime.datetime.fromtimestamp(d).strftime('%y-%m-%d %H:%M:%S')
for d in history[data_slice, 0]]
use_lttb = len(x) > 1000
for idx, name in enumerate(graph):
lower_labels = [label.lower() for label in labels]
idx = lower_labels.index(name.lower()) + LABEL_OFFSET # Could be 2 if there is human readable date
# y = history[data_slice, idx]
# print('y', y)
# if len(history[data_slice, 0]) > 1000:
if use_lttb:
app_logger.debug('data_slice len', len(data_slice))
hdata = np.column_stack((history[data_slice, 0], history[data_slice, idx]))
app_logger.debug('hdata.shape', hdata.shape)
downsize = lttb.downsample(hdata, n_out=1000)
assert downsize.shape == (1000, 2)
x = [datetime.datetime.fromtimestamp(d).strftime('%y-%m-%d %H:%M:%S')
for d in downsize[:, 0]]
y = list(downsize[:,1])
else:
y = list(history[data_slice, idx])
# print('load_data:', name, idx)
newdata.append({'x': x, 'y': y, 'type': 'scatter',
'mode':'markers+lines', 'name':'%s' % name})
app_logger.info('done building graphs')
return newdata, filename
def build_graphjson(data_slice=None):
global graph, FILENAME
app_logger.info('build_graphjson, filename: '+FILENAME)
if data_slice is None:
newdata, filename = load_data(graph)
else:
newdata, filename = load_data(graph, data_slice)
# Create a traces and layout to plot
data=[]
figure={}
# for j in range(num_curves):
for curve in newdata:
trace = go.Scattergl(x=curve['x'], y=curve['y'],
name=curve['name'], mode=curve['mode'])
data.append(trace)
layout = go.Layout( # title=plotTitle, \
yaxis={'title':'Temperatures', 'type':'log'}, xaxis={'title':'Time'},
margin={ 'l': 50, 'r': 50, 'b': 50, 't': 5, 'pad': 0 },
)
# PlotlyJSONEncoder converts objects to their JSON equivalents
figure=dict(data=data, layout=layout)
graphJSON = json.dumps(figure, cls=plotly.utils.PlotlyJSONEncoder)
app_logger.debug('return graphJSON')
return graphJSON
def check_change(**kwargs):
""" Check if config.yaml has changed"""
kwargs = kwargs['kwargs']
# print('check_change kwargs', kwargs)
if not hasattr(check_change, 'filename'):
check_change.filename = kwargs['filename']
if not hasattr(check_change, 'mtime'):
check_change.mtime = os.stat(check_change.filename).st_mtime
new_mtime = os.stat(check_change.filename).st_mtime
changed = new_mtime != check_change.mtime
if changed:
print('old, new', '%.2f'%time.time(), check_change.mtime, new_mtime)
check_change.mtime = new_mtime
return changed
def signal_modified_yaml(**kwargs):
print('start to check for modified yaml')
print('kwargs', kwargs)
while True:
socketio.sleep(1)
# print('done sleeping')
changed = check_change(**kwargs)
if changed:
# print('put something on queue')
# QUEUE.put(changed)
print('emit update')
socketio.emit('update', CONFIG, namespace='/config')
yaml_thread = None
@socketio.on('connect', namespace='/config')
def socketio_connect():
global CONFIG, yaml_thread
data = CONFIG
socketio.emit('connect', data, namespace='/config')
if yaml_thread is None:
print('Start background yaml_thread')
thread = socketio.start_background_task(target=signal_modified_yaml,
kwargs={'filename': YAML_FILE} )
@socketio.on('disconnect', namespace='/config')
def socketio_disconnect():
print('Client disconnected', request.sid)
def update(d, new):
updated = False
for k in d.keys():
if isinstance(d[k], ruamel.yaml.comments.CommentedMap):
updated |= update(d[k], new[k])
else:
if d[k] != new[k]:
print('replace', k, d[k], new[k])
d[k] = new[k]
updated = True
return updated
@socketio.on('new_config', namespace='/config')
def socketio_update(msg):
""" handle new_config socketio event, yaml was updated on web page
"""
global yaml, YAML_FILE, CONFIG
print('new_config', request.sid)
updated = update(CONFIG, msg)
if updated:
# print('new CONFIG', CONFIG)
with open(YAML_FILE, 'w') as f:
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.dump(CONFIG, f)
@app.route('/config')
def config_endpoint():
""" /config endpoint of website """
return render_template('jsoneditor.html')
if __name__ == "__main__":
if len(sys.argv) > 1:
ip, port_ = '0.0.0.0', sys.argv[1]
else:
ip, port_ = '0.0.0.0', '50000'
# Start Flask app
# socketio.run(app, host=ip, port=port_, debug=True, use_reloader=True)
socketio.run(app, host=ip, port=port_, debug=True, use_reloader=True)
# socketio.run(app, host=ip, port=port_)