forked from thomaseichhorn/bacdevice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_live.py
268 lines (213 loc) · 8.51 KB
/
main_live.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
#!/usr/bin/env python3
# Main program to broadcast data points to the DESY BACNet
# --------------------------------------------------------
# running bokeh server:
# bokeh serve --address fhlcleangate.desy.de --port 5001 --allow-websocket-origin=fhlcleangate.desy.de:5001 main_live.py
import logging
logger = logging.getLogger ( 'mylivelog' )
logger.setLevel ( logging.DEBUG )
fh = logging.FileHandler ( 'output_live.log' )
fh.setLevel ( logging.DEBUG )
logger.addHandler ( fh )
import configparser
from sys import exit, version_info
import threading
import time
from uuid import getnode
from os import path
import csv
import os
import sys
from datetime import datetime
import time
from collections import OrderedDict
from math import pi
from bokeh.plotting import figure, curdoc
from bokeh.driving import linear
from bokeh.models import DatetimeTickFormatter
from bokeh.models.widgets import Div,PreText
from bokeh.layouts import column,row
import random
import thermorasp
METERS = { "thermorasps": thermorasp }
#unixtime = time.time()
#data = -273.
def readout(meters):
global unixtime
global data
# time.sleep ( 10 )
measurements = {}
for meter in meters :
section = meter.getSection()
if not section in measurements:
measurements[section] = [None]*4
fname = meter.name
outputvar = meter.getPresentValue ( )
measurements[section][0] = datetime.strptime(meter.getPresentDate ( ),'%Y-%m-%d %H:%M:%S.%f') .replace ( microsecond = 0 )
if 'temp' in fname:
measurements[section][1] = outputvar
elif 'pres' in fname:
measurements[section][2] = outputvar
elif 'hum' in fname:
measurements[section][3] = outputvar
else:
print('Invalid measurement for ',fname)
continue
var_date = datetime.strptime(meter.getPresentDate ( ),'%Y-%m-%d %H:%M:%S.%f') .replace ( microsecond = 0 )
unixtime = int(time.time())
data = measurements['raspberry3-bus1-ch1'][1]
point = [var_date,data]
# print(measurements)
return measurements
@linear()
def update(step):
measurements = readout(mymeters)
# datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
date1 = measurements['raspberry3-bus1-ch1'][0]
temp1 = measurements['raspberry3-bus1-ch1'][1]
ds1.data['x'].append(date1)
ds1.data['y'].append(temp1)
ds1.trigger('data', ds1.data, ds1.data)
date2 = measurements['raspberry3-bus4-ch1'][0]
temp2 = measurements['raspberry3-bus4-ch1'][1]
ds2.data['x'].append(date2)
ds2.data['y'].append(temp2)
ds2.trigger('data', ds2.data, ds2.data)
date3 = measurements['raspberry3-bus4-ch0'][0]
temp3 = measurements['raspberry3-bus4-ch0'][1]
ds3.data['x'].append(date3)
ds3.data['y'].append(temp3)
ds3.trigger('data', ds3.data, ds3.data)
pre_head2.update(text="Present values")
pre_temp_top.update(text="Top sensor tempearture : {} C".format(temp1))
pre_temp_mid.update(text="Middle sensor tempearture: {} C".format(temp2))
pre_temp_bot.update(text="Bottom sensor tempearture: {} C".format(temp3))
deltaT = (ds1.data['x'][-1]-ds1.data['x'][0]).seconds
if deltaT > 1800:
del ds1.data['x'][0]
del ds1.data['y'][0]
del ds2.data['x'][0]
del ds2.data['y'][0]
del ds3.data['x'][0]
del ds3.data['y'][0]
class DataThread ( threading.Thread ) :
def __init__ ( self, meters ) :
threading.Thread.__init__ ( self )
threading.Thread.setName ( self, "dataThread" )
self.meters = meters
self.flag_stop = False
def run ( self ) :
while not self.flag_stop :
time.sleep ( 10 )
measurements = {}
for meter in self.meters :
section = meter.getSection()
if not section in measurements:
measurements[section] = [None]*3
fname = meter.name
outputvar = meter.getPresentValue ( )
if 'temp' in fname:
measurements[section][0] = outputvar
elif 'pres' in fname:
measurements[section][1] = outputvar
elif 'hum' in fname:
measurements[section][2] = outputvar
else:
print('Invalid measurement for ',fname)
continue
var_date = datetime.datetime.strptime(meter.getPresentDate ( ),'%Y-%m-%d %H:%M:%S.%f') .replace ( microsecond = 0 )
unixtime = int(time.time())
data = measurements['raspberry3-bus1-ch1'][0]
print(unixtime,data)
print('---')
def stop ( self ) :
self.flag_stop = True
def main ( ) :
global mymeters
if not path.exists ( "server.cfg" ) :
logger.error ( "Error: File server.cfg not found." )
exit ( 1 )
cparser = configparser.ConfigParser ( )
cparser.read ( "server.cfg" )
if not "server" in cparser :
logger.error ( "Invalid config: No server section" )
exit ( 1 )
required_keys = { "ip", "port", "objectname", "vendoridentifier", "location", "vendorname", "modelname", "description" }
missing_keys = required_keys - set ( cparser["server"].keys ( ) )
if len ( missing_keys ) != 0 :
logger.error ( "Missing config keys in server section: " + ( " ".join ( missing_keys ) ) )
exit ( 1 )
meters_active = []
ai_objs = []
idx = 1
logger.info ( "Initializing meters..." )
for key, metermodule in sorted(METERS.items(),reverse=True) :
if not key in cparser["server"] :
logger.warning ( "No key '{}' in config server section. Skipping" .format ( key ) )
continue
metersections = cparser["server"][key].split ( )
missing_metersections = set ( metersections ) - set ( cparser.keys ( ) )
if len ( missing_metersections ) != 0 :
logger.error ( "Missing config sections for meters: " + "" .join ( missing_metersections ) )
exit ( 1 )
for metersection in metersections :
info = cparser[metersection]
# for a sensor there is three meters: temp, hum, pres
# code is for one sensor in one raspberry
ms = metermodule.getMeters ( info )
logger.info ( "Got {} meter(s) from {}" .format ( len ( ms ), metersection ) )
meters_active.extend ( ms )
for m in ms :
m.name = "{}_{}" .format ( metersection, m.name )
m.section = metersection
idx += 1
fname = m.name
mymeters = meters_active
for m in meters_active :
m.start ( )
# datathread = DataThread ( meters_active )
# datathread.start ( )
# while True:
# pass
# datathread.stop ( )
# datathread.join ( )
# for m in meters_active :
# m.stop ( )
# m.join ( )
if __name__ == "__main__" :
main ( )
else:
# div = Div(text="<img src='bacdevice/img/daf_25c_cabinet_sensors_test.jpg'>")
p = figure(plot_width=800, plot_height=500,x_axis_type="datetime")
date_format = ['%d %b %Y %H:%M:%S']
p.xaxis.formatter=DatetimeTickFormatter(
microseconds=date_format,
milliseconds=date_format,
seconds=date_format,
minsec=date_format,
minutes=date_format,
hourmin=date_format,
hours=date_format,
days=date_format,
months=date_format,
years=date_format
)
p.xaxis.major_label_orientation = pi/3
p.xaxis.axis_label = "Local time"
p.yaxis.axis_label = "Temperature (C)"
r1 = p.line([], [], color="firebrick", line_width=2,legend_label='top')
r2 = p.line([], [], color="navy", line_width=2,legend_label='middle')
r3 = p.line([], [], color="green", line_width=2,legend_label='bottom')
p.legend.location = "top_left"
ds1 = r1.data_source
ds2 = r2.data_source
ds3 = r3.data_source
pre_head = PreText(text="N.B.: Readout every 10 seconds. Be patient!",width=500, height=50)
pre_head2 = PreText(text="",width=500, height=25)
pre_temp_top = PreText(text="",width=500, height=20)
pre_temp_mid = PreText(text="",width=500, height=20)
pre_temp_bot = PreText(text="",width=500, height=20)
curdoc().add_root(column(pre_head,row(column(pre_head2,pre_temp_top,pre_temp_mid,pre_temp_bot),p,)))
main()
# time.sleep ( 10 )
curdoc().add_periodic_callback(update, 10000)