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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import collections
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import json
157
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
4 import signal
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
5 import tempfile
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
6 import os
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
7 import binascii
157
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
8
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 import config
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
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
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 _FIELD_DEFAULTS = {
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 'fridge_setpoint': 16,
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 'fridge_difference': 0.2,
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 'overshoot_delay': 720, # 12 minutes
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
bfc3213edee4 add nowort mode
Matt Johnston <matt@ucc.asn.au>
parents: 166
diff changeset
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
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 }
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 class Params(dict):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 class Error(Exception):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 pass
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 def __init__(self):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 self.update(_FIELD_DEFAULTS)
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
30 self._set_epoch(None)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 def __getattr__(self, k):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 return self[k]
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 def __setattr__(self, k, v):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 # fail if we set a bad value
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 self[k]
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 self[k] = v
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
40 def _set_epoch(self, epoch):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
41 # since __setattr__ is overridden
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
42 object.__setattr__(self, '_epoch', epoch)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
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
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
45 try:
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
46 u = utils.json_load_round_float(f.read())
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
47 except Exception as e:
157
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
48 raise self.Error(e)
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
49
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 if k not in self:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???')))
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 self.update(u)
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
56 # new epoch, 120 random bits
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
57 self._set_epoch(binascii.hexlify(os.urandom(15)).decode())
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
59 L("Loaded parameters")
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
60 L(self.save_string())
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
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
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
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
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
95
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
96 if self.keys() != params.keys():
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
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
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
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
ef3a75128116 actually update values
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
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
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
113 out = json.dumps(params, sort_keys=True, indent=4)+'\n'
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
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
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
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
ef3a75128116 actually update values
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
123 self.update(params)
ef3a75128116 actually update values
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
124 L("Received parameters")
ef3a75128116 actually update values
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
125 L(self.save_string())
ef3a75128116 actually update values
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
126 return True
ef3a75128116 actually update values
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
127
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
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)