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