444
|
1 # -*- coding: utf-8 -*- |
|
2 import collections |
|
3 import json |
456
|
4 import signal |
|
5 |
|
6 import gevent |
|
7 |
444
|
8 import config |
447
|
9 from utils import W,L,E,EX |
444
|
10 |
|
11 _FIELD_DEFAULTS = { |
|
12 'fridge_setpoint': 16, |
|
13 'fridge_difference': 0.2, |
|
14 'overshoot_delay': 720, # 12 minutes |
|
15 'overshoot_factor': 1, # ÂșC |
|
16 } |
|
17 |
|
18 class Params(dict): |
|
19 class Error(Exception): |
|
20 pass |
|
21 |
|
22 def __init__(self): |
|
23 self.update(_FIELD_DEFAULTS) |
456
|
24 gevent.signal(signal.SIGHUP, self.reload_signal) |
444
|
25 |
|
26 def __getattr__(self, k): |
|
27 return self[k] |
|
28 |
|
29 def __setattr__(self, k, v): |
|
30 # fail if we set a bad value |
|
31 self[k] |
|
32 self[k] = v |
|
33 |
|
34 def load(self, f = None): |
|
35 if not f: |
447
|
36 try: |
|
37 f = file(config.PARAMS_FILE, 'r') |
|
38 except IOError, e: |
|
39 W("Missing parameter file, using defaults. %s", e) |
|
40 return |
456
|
41 try: |
|
42 u = json.load(f) |
|
43 except Exception, e: |
|
44 raise self.Error(e) |
|
45 |
444
|
46 for k in u: |
|
47 if k not in self: |
|
48 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) |
|
49 self.update(u) |
|
50 |
|
51 def save(self, f = None): |
|
52 if not f: |
|
53 f = file(config.PARAMS_FILE, 'w') |
|
54 json.dump(self, f, sort_keys=True, indent=4) |
454
|
55 f.write('\n') |
444
|
56 f.flush() |
456
|
57 |
|
58 def reload_signal(self): |
|
59 try: |
|
60 self.load() |
|
61 L("Reloaded params") |
|
62 except self.Error, e: |
|
63 W("Problem reloading: %s" % str(e)) |