Mercurial > templog
annotate py/params.py @ 292:28eb733cb803
60 second timeout
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 19 Mar 2015 21:50:52 +0800 |
parents | 16a83e2c97a0 |
children | d9e81a563923 |
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, |
170
78255c49bf9a
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
168
diff
changeset
|
19 'fridge_range_lower': 3, |
78255c49bf9a
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
168
diff
changeset
|
20 'fridge_range_upper': 3, |
145 | 21 } |
22 | |
23 class Params(dict): | |
24 class Error(Exception): | |
25 pass | |
26 | |
27 def __init__(self): | |
28 self.update(_FIELD_DEFAULTS) | |
29 | |
30 def __getattr__(self, k): | |
31 return self[k] | |
32 | |
33 def __setattr__(self, k, v): | |
34 # fail if we set a bad value | |
35 self[k] | |
36 self[k] = v | |
37 | |
38 def load(self, f = None): | |
39 if not f: | |
148 | 40 try: |
41 f = file(config.PARAMS_FILE, 'r') | |
42 except IOError, e: | |
43 W("Missing parameter file, using defaults. %s", e) | |
44 return | |
157 | 45 try: |
46 u = json.load(f) | |
47 except Exception, e: | |
48 raise self.Error(e) | |
49 | |
145 | 50 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
|
51 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
|
52 continue |
145 | 53 if k not in self: |
54 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) | |
55 self.update(u) | |
56 | |
160 | 57 L("Loaded parameters") |
58 L(self.save_string()) | |
59 | |
60 | |
145 | 61 def save(self, f = None): |
62 if not f: | |
63 f = file(config.PARAMS_FILE, 'w') | |
64 json.dump(self, f, sort_keys=True, indent=4) | |
155 | 65 f.write('\n') |
145 | 66 f.flush() |
157 | 67 |
160 | 68 def save_string(self): |
69 s = StringIO.StringIO() | |
70 self.save(s) | |
71 return s.getvalue() |