Mercurial > templog
annotate py/params.py @ 478:fc8ddea46987
sort sensor lines
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 30 Jan 2014 22:56:05 +0800 |
parents | 1eb68df9f8ab |
children | 5d5424acfed0 |
rev | line source |
---|---|
444 | 1 # -*- coding: utf-8 -*- |
2 import collections | |
3 import json | |
456 | 4 import signal |
459 | 5 import StringIO |
456 | 6 |
7 import gevent | |
8 | |
444 | 9 import config |
447 | 10 from utils import W,L,E,EX |
444 | 11 |
12 _FIELD_DEFAULTS = { | |
13 'fridge_setpoint': 16, | |
14 'fridge_difference': 0.2, | |
15 'overshoot_delay': 720, # 12 minutes | |
16 'overshoot_factor': 1, # ÂșC | |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
17 'disabled': False, |
444 | 18 } |
19 | |
20 class Params(dict): | |
21 class Error(Exception): | |
22 pass | |
23 | |
24 def __init__(self): | |
25 self.update(_FIELD_DEFAULTS) | |
456 | 26 gevent.signal(signal.SIGHUP, self.reload_signal) |
444 | 27 |
28 def __getattr__(self, k): | |
29 return self[k] | |
30 | |
31 def __setattr__(self, k, v): | |
32 # fail if we set a bad value | |
33 self[k] | |
34 self[k] = v | |
35 | |
36 def load(self, f = None): | |
37 if not f: | |
447 | 38 try: |
39 f = file(config.PARAMS_FILE, 'r') | |
40 except IOError, e: | |
41 W("Missing parameter file, using defaults. %s", e) | |
42 return | |
456 | 43 try: |
44 u = json.load(f) | |
45 except Exception, e: | |
46 raise self.Error(e) | |
47 | |
444 | 48 for k in u: |
49 if k not in self: | |
50 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) | |
51 self.update(u) | |
52 | |
459 | 53 L("Loaded parameters") |
54 L(self.save_string()) | |
55 | |
56 | |
444 | 57 def save(self, f = None): |
58 if not f: | |
59 f = file(config.PARAMS_FILE, 'w') | |
60 json.dump(self, f, sort_keys=True, indent=4) | |
454 | 61 f.write('\n') |
444 | 62 f.flush() |
456 | 63 |
459 | 64 def save_string(self): |
65 s = StringIO.StringIO() | |
66 self.save(s) | |
67 return s.getvalue() | |
68 | |
456 | 69 def reload_signal(self): |
70 try: | |
71 self.load() | |
459 | 72 L("Reloaded.") |
456 | 73 except self.Error, e: |
74 W("Problem reloading: %s" % str(e)) |