annotate py/params.py @ 230:185621f47040

run 2to3
author Matt Johnston <matt@ucc.asn.au>
date Fri, 10 Apr 2015 23:59:16 +0800
parents d9e81a563923
children e39ed85d87a5
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
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
5 import io
157
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
6
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 import config
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
8 from utils import W,L,E,EX
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 _FIELD_DEFAULTS = {
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 'fridge_setpoint': 16,
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 'fridge_difference': 0.2,
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 'overshoot_delay': 720, # 12 minutes
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 'overshoot_factor': 1, # ÂșC
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
15 'disabled': False,
168
bfc3213edee4 add nowort mode
Matt Johnston <matt@ucc.asn.au>
parents: 166
diff changeset
16 'nowort': False,
170
78255c49bf9a make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents: 168
diff changeset
17 'fridge_range_lower': 3,
78255c49bf9a make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents: 168
diff changeset
18 'fridge_range_upper': 3,
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 }
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 class Params(dict):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 class Error(Exception):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 pass
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 def __init__(self):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 self.update(_FIELD_DEFAULTS)
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 __getattr__(self, k):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 return self[k]
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 def __setattr__(self, k, v):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 # fail if we set a bad value
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 self[k]
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 self[k] = v
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 def load(self, f = None):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 if not f:
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
38 try:
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
39 f = file(config.PARAMS_FILE, 'r')
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
40 except IOError as e:
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
41 W("Missing parameter file, using defaults. %s", e)
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
42 return
157
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
43 try:
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
44 u = json.load(f)
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
45 except Exception as e:
157
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
46 raise self.Error(e)
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
47
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 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
49 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
50 continue
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 if k not in self:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 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
53 self.update(u)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
55 L("Loaded parameters")
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
56 L(self.save_string())
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
57
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
58
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 def save(self, f = None):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 if not f:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 f = file(config.PARAMS_FILE, 'w')
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 json.dump(self, f, sort_keys=True, indent=4)
155
d03157c7ad60 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
63 f.write('\n')
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 f.flush()
157
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
65
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
66 def save_string(self):
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
67 s = io.StringIO()
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
68 self.save(s)
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
69 return s.getvalue()