comparison web/log.py @ 586:87c20b8c5472 default master

port to python3
author Matt Johnston <matt@ucc.asn.au>
date Mon, 09 Sep 2019 22:24:10 +0800
parents f7261dd970da
children
comparison
equal deleted inserted replaced
585:1c484dab4e83 586:87c20b8c5472
56 *args) 56 *args)
57 57
58 # stolen from viewmtn, stolen from monotone-viz 58 # stolen from viewmtn, stolen from monotone-viz
59 def colour_from_string(str): 59 def colour_from_string(str):
60 def f(off): 60 def f(off):
61 return ord(hashval[off]) / 256.0 61 return hashval[off] / 256.0
62 hashval = hashlib.sha1(str).digest() 62 hashval = hashlib.sha1(str.encode()).digest()
63 hue = f(5) 63 hue = f(5)
64 li = f(1) * 0.15 + 0.55 64 li = f(1) * 0.15 + 0.55
65 sat = f(2) * 0.5 + .5 65 sat = f(2) * 0.5 + .5
66 return ''.join(["%.2x" % int(x * 256) for x in hls_to_rgb(hue, li, sat)]) 66 return ''.join(["%.2x" % int(x * 256) for x in hls_to_rgb(hue, li, sat)])
67 67
136 graph_args.append('CDEF:trendfermheat=fermheat,7200,TRENDNAN' % locals()) 136 graph_args.append('CDEF:trendfermheat=fermheat,7200,TRENDNAN' % locals())
137 graph_args.append('CDEF:limitfermheat=trendfermheat,5,+,11,MIN,2,MAX' % locals()) 137 graph_args.append('CDEF:limitfermheat=trendfermheat,5,+,11,MIN,2,MAX' % locals())
138 graph_args.append('LINE0.5:limitfermheat#%(colour)s' % locals()) 138 graph_args.append('LINE0.5:limitfermheat#%(colour)s' % locals())
139 139
140 # lines are done afterwards so they can be layered 140 # lines are done afterwards so they can be layered
141 sensor_lines.sort(key = lambda (legend, line): "Wort" in legend) 141 sensor_lines.sort(key = lambda legend_line: "Wort" in legend_line[0])
142 graph_args += (line for (legend, line) in sensor_lines) 142 graph_args += (line for (legend, line) in sensor_lines)
143 143
144 #print>>sys.stderr, '\n'.join(graph_args) 144 #print>>sys.stderr, '\n'.join(graph_args)
145 145
146 end = int(start+length) 146 end = int(start+length)
195 return '%f' % m 195 return '%f' % m
196 196
197 def sensor_update(sensor_id, measurements): 197 def sensor_update(sensor_id, measurements):
198 try: 198 try:
199 open(sensor_rrd_path(sensor_id)) 199 open(sensor_rrd_path(sensor_id))
200 except IOError, e: 200 except IOError as e:
201 create_rrd(sensor_id) 201 create_rrd(sensor_id)
202 202
203 if measurements: 203 if measurements:
204 values = ['%d:%s' % (t, validate_value(m)) for (t, m) in measurements] 204 values = ['%d:%s' % (t, validate_value(m)) for (t, m) in measurements]
205 205
206 rrdfile = sensor_rrd_path(sensor_id) 206 rrdfile = sensor_rrd_path(sensor_id)
207 # XXX what to do here when it fails... 207 # XXX what to do here when it fails...
208 for v in values: 208 for v in values:
209 try: 209 try:
210 rrdtool.update(rrdfile, v) 210 rrdtool.update(rrdfile, v)
211 except rrdtool.error, e: 211 except rrdtool.error as e:
212 print>>sys.stderr, "Bad rrdtool update '%s': %s" % (v, str(e)) 212 print("Bad rrdtool update '%s': %s" % (v, str(e)), file=sys.stderr)
213 traceback.print_exc(file=sys.stderr) 213 traceback.print_exc(file=sys.stderr)
214 214
215 # be paranoid 215 # be paranoid
216 #f = file(rrdfile) 216 #f = file(rrdfile)
217 #os.fsync(f.fileno()) 217 #os.fsync(f.fileno())
232 f.seek(0, 2) 232 f.seek(0, 2)
233 size = f.tell() 233 size = f.tell()
234 f.seek(max(0, size-30000)) 234 f.seek(max(0, size-30000))
235 return '\n'.join(l.strip() for l in f.readlines()[-400:]) 235 return '\n'.join(l.strip() for l in f.readlines()[-400:])
236 236
237 def convert_ds18b20_12bit(reading):
238 value = struct.unpack('>h', binascii.unhexlify(reading))[0]
239 return value * 0.0625
240
241 def time_rem(name, entries): 237 def time_rem(name, entries):
242 val_ticks = int(entries[name]) 238 val_ticks = int(entries[name])
243 val_rem = int(entries['%s_rem' % name]) 239 val_rem = int(entries['%s_rem' % name])
244 tick_wake = int(entries['tick_wake']) + 1 240 tick_wake = int(entries['tick_wake']) + 1
245 tick_secs = int(entries['tick_secs']) 241 tick_secs = int(entries['tick_secs'])
267 263
268 # measurements is {sensorname: [(time, value), ...], ...} 264 # measurements is {sensorname: [(time, value), ...], ...}
269 measurements = {} 265 measurements = {}
270 for rs, t in readings: 266 for rs, t in readings:
271 real_t = t + time_diff 267 real_t = t + time_diff
272 for s, v in rs.iteritems(): 268 for s, v in rs.items():
273 measurements.setdefault(s, []).append((real_t, v)) 269 measurements.setdefault(s, []).append((real_t, v))
274 270
275 # one-off measurements here 271 # one-off measurements here
276 current_params = params['current_params'] 272 current_params = params['current_params']
277 current_epoch = params['current_params_epoch'] 273 current_epoch = params['current_params_epoch']
278 measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ] 274 measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ]
279 measurements['fridge_setpoint'] = [ (time.time(), current_params['fridge_setpoint']) ] 275 measurements['fridge_setpoint'] = [ (time.time(), current_params['fridge_setpoint']) ]
280 276
281 write_current_params(current_params, current_epoch) 277 write_current_params(current_params, current_epoch)
282 278
283 for s, vs in measurements.iteritems(): 279 for s, vs in measurements.items():
284 sensor_update(s, vs) 280 sensor_update(s, vs)
285 281
286 timedelta = time.time() - start_time 282 timedelta = time.time() - start_time
287 debugf.write("Updated sensors in %.2f secs\n" % timedelta) 283 debugf.write("Updated sensors in %.2f secs\n" % timedelta)
288 debugf.flush() 284 debugf.flush()
314 310
315 vals = read_current_params() 311 vals = read_current_params()
316 if not vals: 312 if not vals:
317 return None 313 return None
318 314
319 for k, v in _FIELD_DEFAULTS.iteritems(): 315 for k, v in _FIELD_DEFAULTS.items():
320 n = {'name': k, 'value': type(v)(vals[k])} 316 n = {'name': k, 'value': type(v)(vals[k])}
321 if type(v) is bool: 317 if type(v) is bool:
322 kind = 'yesno' 318 kind = 'yesno'
323 else: 319 else:
324 kind = 'number' 320 kind = 'number'
350 def update_params(p): 346 def update_params(p):
351 params = {} 347 params = {}
352 for i in p: 348 for i in p:
353 params[i['name']] = i['value'] 349 params[i['name']] = i['value']
354 350
355 if params.viewkeys() != _FIELD_DEFAULTS.viewkeys(): 351 if params.keys() != _FIELD_DEFAULTS.keys():
356 diff = params.viewkeys() ^ _FIELD_DEFAULTS.viewkeys() 352 diff = params.keys() ^ _FIELD_DEFAULTS.keys()
357 return "Key mismatch, difference %s" % str(diff) 353 return "Key mismatch, difference %s" % str(diff)
358 354
359 for k, v in params.items(): 355 for k, v in params.items():
360 if not same_type(v, _FIELD_DEFAULTS[k]): 356 if not same_type(v, _FIELD_DEFAULTS[k]):
361 return "Bad type for %s, %s vs %s" % (k , type(v), type(_FIELD_DEFAULTS[k])) 357 return "Bad type for %s, %s vs %s" % (k , type(v), type(_FIELD_DEFAULTS[k]))