Mercurial > templog
annotate py/params.py @ 639:89818a14648b rust tip
- switch to using anyhow for errors, surf for http
runs but surf has problems
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 28 Nov 2019 23:57:00 +0800 |
parents | ef3a75128116 |
children |
rev | line source |
---|---|
145 | 1 # -*- coding: utf-8 -*- |
2 import collections | |
3 import json | |
157 | 4 import signal |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
5 import tempfile |
259 | 6 import os |
7 import binascii | |
157 | 8 |
145 | 9 import config |
148 | 10 from utils import W,L,E,EX |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
11 import utils |
145 | 12 |
13 _FIELD_DEFAULTS = { | |
14 'fridge_setpoint': 16, | |
15 'fridge_difference': 0.2, | |
16 'overshoot_delay': 720, # 12 minutes | |
17 'overshoot_factor': 1, # ÂșC | |
162
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
18 'disabled': False, |
168 | 19 'nowort': False, |
170
78255c49bf9a
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
168
diff
changeset
|
20 'fridge_range_lower': 3, |
78255c49bf9a
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
168
diff
changeset
|
21 'fridge_range_upper': 3, |
145 | 22 } |
23 | |
24 class Params(dict): | |
25 class Error(Exception): | |
26 pass | |
27 | |
28 def __init__(self): | |
29 self.update(_FIELD_DEFAULTS) | |
259 | 30 self._set_epoch(None) |
145 | 31 |
32 def __getattr__(self, k): | |
33 return self[k] | |
34 | |
35 def __setattr__(self, k, v): | |
36 # fail if we set a bad value | |
37 self[k] | |
38 self[k] = v | |
39 | |
259 | 40 def _set_epoch(self, epoch): |
41 # since __setattr__ is overridden | |
42 object.__setattr__(self, '_epoch', epoch) | |
43 | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
44 def _do_load(self, f): |
157 | 45 try: |
259 | 46 u = utils.json_load_round_float(f.read()) |
230 | 47 except Exception as e: |
157 | 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) | |
259 | 56 # new epoch, 120 random bits |
57 self._set_epoch(binascii.hexlify(os.urandom(15)).decode()) | |
145 | 58 |
160 | 59 L("Loaded parameters") |
60 L(self.save_string()) | |
61 | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
62 def load(self, f = None): |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
63 if f: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
64 return self._do_load(f) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
65 else: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
66 with open(config.PARAMS_FILE, 'r') as f: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
67 try: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
68 return self._do_load(f) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
69 except IOError as e: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
70 W("Missing parameter file, using defaults. %s" % str(e)) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
71 return |
160 | 72 |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
73 def get_epoch(self): |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
74 return self._epoch |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
75 |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
76 def receive(self, params, epoch): |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
77 """ updates parameters from the server. does some validation, |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
78 writes config file to disk. |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
79 Returns True on success, False failure |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
80 """ |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
81 |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
82 if epoch != self._epoch: |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
83 return |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
84 |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
85 def same_type(a, b): |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
86 ta = type(a) |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
87 tb = type(b) |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
88 |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
89 if ta == int: |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
90 ta = float |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
91 if tb == int: |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
92 tb = float |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
93 |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
94 return ta == tb |
157 | 95 |
259 | 96 if self.keys() != params.keys(): |
97 diff = self.keys() ^ params.keys() | |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
98 E("Mismatching params, %s" % str(diff)) |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
99 return False |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
100 |
259 | 101 for k, v in params.items(): |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
102 if not same_type(v, self[k]): |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
103 E("Bad type for %s" % k) |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
104 return False |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
105 |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
106 dir = os.path.dirname(config.PARAMS_FILE) |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
107 try: |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
108 t = tempfile.NamedTemporaryFile(prefix='config', |
262 | 109 mode='w+t', # NamedTemporaryFile is binary by default |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
110 dir = dir, |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
111 delete = False) |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
112 |
259 | 113 out = json.dumps(params, sort_keys=True, indent=4)+'\n' |
114 t.write(out) | |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
115 name = t.name |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
116 t.close() |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
117 |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
118 os.rename(name, config.PARAMS_FILE) |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
119 except Exception as e: |
259 | 120 EX("Problem: %s" % e) |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
121 return False |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
122 |
262 | 123 self.update(params) |
124 L("Received parameters") | |
125 L(self.save_string()) | |
126 return True | |
127 | |
160 | 128 def save_string(self): |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
233
diff
changeset
|
129 return json.dumps(self, sort_keys=True, indent=4) |