comparison web/log.py @ 492:23c6cf01d237

working kinda
author Matt Johnston <matt@ucc.asn.au>
date Tue, 11 Feb 2014 23:47:53 +0800
parents 46e327c00246
children 1d8a8bd2ea78
comparison
equal deleted inserted replaced
491:f2e990b99637 492:23c6cf01d237
13 import traceback 13 import traceback
14 import datetime 14 import datetime
15 import struct 15 import struct
16 import binascii 16 import binascii
17 import json 17 import json
18 import subprocess
18 from colorsys import hls_to_rgb 19 from colorsys import hls_to_rgb
19 20
20 import config 21 import config
21 import atomicfile 22 import atomicfile
22 23
267 268
268 timedelta = time.time() - start_time 269 timedelta = time.time() - start_time
269 debugf.write("Updated sensors in %.2f secs\n" % timedelta) 270 debugf.write("Updated sensors in %.2f secs\n" % timedelta)
270 debugf.flush() 271 debugf.flush()
271 272
273 _FIELD_DEFAULTS = {
274 'fridge_setpoint': 16,
275 'fridge_difference': 0.2,
276 'overshoot_delay': 720, # 12 minutes
277 'overshoot_factor': 1, # ºC
278 'disabled': False,
279 'nowort': True,
280 'fridge_range_lower': 3,
281 'fridge_range_upper': 3,
282 }
283
272 def get_params(): 284 def get_params():
273 _FIELD_DEFAULTS = {
274 'fridge_setpoint': 16,
275 'fridge_difference': 0.2,
276 'overshoot_delay': 720, # 12 minutes
277 'overshoot_factor': 1, # ºC
278 'disabled': False,
279 'nowort': True,
280 'fridge_range_lower': 3,
281 'fridge_range_upper': 3,
282 }
283 285
284 r = [] 286 r = []
285 287
286 vals = read_current_params() 288 vals = read_current_params()
287 289
288 for k, v in _FIELD_DEFAULTS.iteritems(): 290 for k, v in _FIELD_DEFAULTS.iteritems():
289 n = {'name': k, 'value': vals[k]} 291 n = {'name': k, 'value': type(v)(vals[k])}
290 if type(v) is bool: 292 if type(v) is bool:
291 kind = 'yesno' 293 kind = 'yesno'
292 else: 294 else:
293 kind = 'number' 295 kind = 'number'
294 if k == 'overshoot_delay': 296 if k == 'overshoot_delay':
303 n['title'] = k 305 n['title'] = k
304 r.append(n) 306 r.append(n)
305 307
306 return json.dumps(r, sort_keys=True, indent=4) 308 return json.dumps(r, sort_keys=True, indent=4)
307 309
308 310 def send_params(params):
309 def get_csrf_blob(user_ident): 311 # 'templog_receive' is ignored due to authorized_keys
310 return "aaa" 312 # restrictions
313 args = [config.SSH_PROG, '-i', config.SSH_KEYFILE,
314 config.SSH_HOST, 'templog_receive']
315 try:
316 p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
317 (out, err) = p.communicate(json.dumps(params))
318 except OSError, e:
319 print>>sys.stderr, e
320 return "Failed update"
321
322 if 'Good Update' in out:
323 return True
324
325 print>>sys.stderr, "Strange return from update:"
326 print>>sys.stderr, out
327 return "Unexpected update result"
328
329 def same_type(a, b):
330 ta = type(a)
331 tb = type(b)
332
333 if ta == int:
334 ta = float
335 if tb == int:
336 tb = float
337
338 return (ta == tb)
339
340 def update_params(p):
341 params = {}
342 for i in p:
343 params[i['name']] = i['value']
344
345 if params.viewkeys() != _FIELD_DEFAULTS.viewkeys():
346 diff = params.viewkeys() ^ _FIELD_DEFAULTS.viewkeys()
347 return "Key mismatch, difference %s" % str(diff)
348
349 for k, v in params.items():
350 if not same_type(v, _FIELD_DEFAULTS[k]):
351 return "Bad type for %s, %s vs %s" % (k , type(v), type(_FIELD_DEFAULTS[k]))
352
353 ret = send_params(params)
354 if ret is not True:
355 return "Failed sending params: %s" % ret
356
357 return True
358
359
360