annotate py/params.py @ 558:9f659ece4e63

a few more parameters
author Matt Johnston <matt@ucc.asn.au>
date Tue, 09 Jun 2015 21:22:10 +0800
parents 9499bd2f344b
children 26eee8591f61
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
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
5 import io
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
6 import tempfile
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
7
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 import config
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
9 from utils import W,L,E,EX
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
10 import utils
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)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
29 self._epoch = None
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
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
39 def _do_load(self, f):
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
40 try:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
41 u = json.load(f)
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
42 except Exception as e:
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
43 raise self.Error(e)
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
44
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 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
46 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
47 continue
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 if k not in self:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 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
50 self.update(u)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
51 self._epoch = utils.hexnonce()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
53 L("Loaded parameters")
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
54 L(self.save_string())
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
55
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
56 def load(self, f = None):
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
57 if f:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
58 return self._do_load(f)
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
59 else:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
60 with open(config.PARAMS_FILE, 'r') as f:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
61 try:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
62 return self._do_load(f)
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
63 except IOError as e:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
64 W("Missing parameter file, using defaults. %s" % str(e))
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
65 return
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
66
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
67 def get_epoch(self):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
68 return self._epoch
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
69
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
70 def receive(self, params, epoch):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
71 """ updates parameters from the server. does some validation,
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
72 writes config file to disk.
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
73 Returns True on success, False failure
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
74 """
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
75
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
76 if epoch != self._epoch:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
77 return
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
78
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
79 def same_type(a, b):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
80 ta = type(a)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
81 tb = type(b)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
82
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
83 if ta == int:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
84 ta = float
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
85 if tb == int:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
86 tb = float
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
87
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
88 return ta == tb
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
89
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
90 if self.keys() != new_params.keys():
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
91 diff = self.keys() ^ new_params.keys()
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
92 E("Mismatching params, %s" % str(diff))
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
93 return False
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
94
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
95 for k, v in new_params.items():
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
96 if not same_type(v, self[k]):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
97 E("Bad type for %s" % k)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
98 return False
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
99
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
100 dir = os.path.dirname(config.PARAMS_FILE)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
101 try:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
102 t = tempfile.NamedTemporaryFile(prefix='config',
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
103 dir = dir,
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
104 delete = False)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
105
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
106 t.write(json.dumps(new_params, sort_keys=True, indent=4)+'\n')
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
107 name = t.name
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
108 t.close()
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
109
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
110 os.rename(name, config.PARAMS_FILE)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
111 return True
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
112 except Exception as e:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
113 E("Problem: %s" % e)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
114 return False
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
115
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
116 def save_string(self):
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
117 return json.dumps(self, sort_keys=True, indent=4)