comparison web/log.py @ 551:9499bd2f344b

long polling config updates
author Matt Johnston <matt@ucc.asn.au>
date Mon, 08 Jun 2015 22:29:46 +0800
parents 1f25c9d97111
children 03e540c3ec24
comparison
equal deleted inserted replaced
550:1040946133ea 551:9499bd2f344b
18 import subprocess 18 import subprocess
19 from colorsys import hls_to_rgb 19 from colorsys import hls_to_rgb
20 20
21 import config 21 import config
22 import atomicfile 22 import atomicfile
23 import settings
24
25 fridge_settings = settings.Settings()
23 26
24 def sensor_rrd_path(s): 27 def sensor_rrd_path(s):
25 return '%s/sensor_%s.rrd' % (config.DATA_PATH, str(s)) 28 return '%s/sensor_%s.rrd' % (config.DATA_PATH, str(s))
26 29
27 # returns (path, sensor_name) tuples 30 # returns (path, sensor_name) tuples
243 tick_wake = int(entries['tick_wake']) + 1 246 tick_wake = int(entries['tick_wake']) + 1
244 tick_secs = int(entries['tick_secs']) 247 tick_secs = int(entries['tick_secs'])
245 return val_ticks + float(val_rem) * tick_secs / tick_wake 248 return val_ticks + float(val_rem) * tick_secs / tick_wake
246 249
247 def write_current_params(current_params): 250 def write_current_params(current_params):
248 out = {} 251 fridge_settings.update(current_params)
249 out['params'] = current_params
250 out['time'] = time.time()
251 atomicfile.AtomicFile("%s/current_params.txt" % config.DATA_PATH).write(
252 json.dumps(out, sort_keys=True, indent=4)+'\n')
253 252
254 def read_current_params(): 253 def read_current_params():
255 p = atomicfile.AtomicFile("%s/current_params.txt" % config.DATA_PATH).read() 254 params, epochtag = fridge_settings.get()
256 dat = json.loads(p) 255 return params
257 return dat['params']
258 256
259 def parse(params): 257 def parse(params):
260 258
261 start_time = time.time() 259 start_time = time.time()
262 260
276 for s, v in rs.iteritems(): 274 for s, v in rs.iteritems():
277 measurements.setdefault(s, []).append((real_t, v)) 275 measurements.setdefault(s, []).append((real_t, v))
278 276
279 # one-off measurements here 277 # one-off measurements here
280 current_params = params['current_params'] 278 current_params = params['current_params']
279 current_epoch = params['current_params_epoch']
281 measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ] 280 measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ]
282 measurements['fridge_setpoint'] = [ (time.time(), current_params['fridge_setpoint']) ] 281 measurements['fridge_setpoint'] = [ (time.time(), current_params['fridge_setpoint']) ]
283 282
284 write_current_params(current_params) 283 write_current_params(current_params, current_epoch)
285 284
286 for s, vs in measurements.iteritems(): 285 for s, vs in measurements.iteritems():
287 sensor_update(s, vs) 286 sensor_update(s, vs)
288 287
289 timedelta = time.time() - start_time 288 timedelta = time.time() - start_time
369 368
370 for k, v in params.items(): 369 for k, v in params.items():
371 if not same_type(v, _FIELD_DEFAULTS[k]): 370 if not same_type(v, _FIELD_DEFAULTS[k]):
372 return "Bad type for %s, %s vs %s" % (k , type(v), type(_FIELD_DEFAULTS[k])) 371 return "Bad type for %s, %s vs %s" % (k , type(v), type(_FIELD_DEFAULTS[k]))
373 372
374 ret = send_params(params) 373 fridge_settings.update(params)
375 if ret is not True:
376 return "Failed sending params: %s" % ret
377
378 return True 374 return True
379 375
380 376
381 377