Mercurial > templog
annotate py/params.py @ 484:4a51e2bf689b
a few fixes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 06 Feb 2014 20:35:43 +0800 |
parents | de4abcbe8f46 |
children | 16a83e2c97a0 |
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, |
467 | 18 'nowort': False, |
469
de4abcbe8f46
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
467
diff
changeset
|
19 'fridge_range_lower': 3, |
de4abcbe8f46
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
467
diff
changeset
|
20 'fridge_range_upper': 3, |
444 | 21 } |
22 | |
23 class Params(dict): | |
24 class Error(Exception): | |
25 pass | |
26 | |
27 def __init__(self): | |
28 self.update(_FIELD_DEFAULTS) | |
456 | 29 gevent.signal(signal.SIGHUP, self.reload_signal) |
444 | 30 |
31 def __getattr__(self, k): | |
32 return self[k] | |
33 | |
34 def __setattr__(self, k, v): | |
35 # fail if we set a bad value | |
36 self[k] | |
37 self[k] = v | |
38 | |
39 def load(self, f = None): | |
40 if not f: | |
447 | 41 try: |
42 f = file(config.PARAMS_FILE, 'r') | |
43 except IOError, e: | |
44 W("Missing parameter file, using defaults. %s", e) | |
45 return | |
456 | 46 try: |
47 u = json.load(f) | |
48 except Exception, e: | |
49 raise self.Error(e) | |
50 | |
444 | 51 for k in u: |
463
f2cca8062128
- use the fridge temperature for control too, keep it in a 6deg band
Matt Johnston <matt@ucc.asn.au>
parents:
461
diff
changeset
|
52 if k.startswith('_'): |
f2cca8062128
- use the fridge temperature for control too, keep it in a 6deg band
Matt Johnston <matt@ucc.asn.au>
parents:
461
diff
changeset
|
53 continue |
444 | 54 if k not in self: |
55 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) | |
56 self.update(u) | |
57 | |
459 | 58 L("Loaded parameters") |
59 L(self.save_string()) | |
60 | |
61 | |
444 | 62 def save(self, f = None): |
63 if not f: | |
64 f = file(config.PARAMS_FILE, 'w') | |
65 json.dump(self, f, sort_keys=True, indent=4) | |
454 | 66 f.write('\n') |
444 | 67 f.flush() |
456 | 68 |
459 | 69 def save_string(self): |
70 s = StringIO.StringIO() | |
71 self.save(s) | |
72 return s.getvalue() | |
73 | |
456 | 74 def reload_signal(self): |
75 try: | |
76 self.load() | |
459 | 77 L("Reloaded.") |
456 | 78 except self.Error, e: |
79 W("Problem reloading: %s" % str(e)) |