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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import collections
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import json
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
4 import signal
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
5 import StringIO
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
6
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
7 import gevent
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
8
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 import config
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
10 from utils import W,L,E,EX
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 _FIELD_DEFAULTS = {
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 'fridge_setpoint': 16,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 'fridge_difference': 0.2,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 'overshoot_delay': 720, # 12 minutes
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
fa79666fb375 add nowort mode
Matt Johnston <matt@ucc.asn.au>
parents: 463
diff changeset
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
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 }
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 class Params(dict):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 class Error(Exception):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 pass
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 def __init__(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 self.update(_FIELD_DEFAULTS)
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
29 gevent.signal(signal.SIGHUP, self.reload_signal)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 def __getattr__(self, k):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return self[k]
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 def __setattr__(self, k, v):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 # fail if we set a bad value
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 self[k]
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 self[k] = v
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 def load(self, f = None):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 if not f:
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
41 try:
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
42 f = file(config.PARAMS_FILE, 'r')
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
43 except IOError, e:
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
44 W("Missing parameter file, using defaults. %s", e)
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
45 return
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
46 try:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
47 u = json.load(f)
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
48 except Exception, e:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
49 raise self.Error(e)
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
50
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 if k not in self:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???')))
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 self.update(u)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
58 L("Loaded parameters")
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
59 L(self.save_string())
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
60
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
61
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 def save(self, f = None):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 if not f:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 f = file(config.PARAMS_FILE, 'w')
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 json.dump(self, f, sort_keys=True, indent=4)
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
66 f.write('\n')
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 f.flush()
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
68
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
69 def save_string(self):
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
70 s = StringIO.StringIO()
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
71 self.save(s)
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
72 return s.getvalue()
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
73
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
74 def reload_signal(self):
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
75 try:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
76 self.load()
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
77 L("Reloaded.")
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
78 except self.Error, e:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
79 W("Problem reloading: %s" % str(e))