-
Notifications
You must be signed in to change notification settings - Fork 10
/
bridge.py
95 lines (73 loc) · 2.31 KB
/
bridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import logging
import urllib
log = logging.getLogger(__name__)
def parse_basic_info(x):
integers = ['port', 'err', 'pv']
booleans = ['pow', 'led']
parse_data(x, integers=integers, booleans=booleans)
x['name'] = urllib.parse.unquote(x['name'])
return x
def parse_sensor_info(x):
integers = ['err']
temps = ['hhum', 'htemp', 'otemp']
parse_data(x, integers=integers, temps=temps)
return x
ctrl_integers = ['alert', 'mode', 'b_mode']
ctrl_temps = ['shum', 'stemp', 'b_shum']
ctrl_booleans = ['pow']
def parse_control_info(x):
parse_data(x, integers=ctrl_integers, temps=ctrl_temps, booleans=ctrl_booleans)
return x
def format_control_info(x):
format_data(x, integers=ctrl_integers, temps=ctrl_temps, booleans=ctrl_booleans)
return x
def parse_data(x, integers=[],
booleans=[],
temps=[]):
for field in integers:
try:
x[field] = int(x[field])
except ValueError as e:
log.exception("failed to parse field '{}': {}".format(field, e.message))
for field in booleans:
try:
x[field] = bool(int(x[field]))
except ValueError as e:
log.exception("Failed to parse field '{}': {}".format(field, e.message))
for field in temps:
try:
x[field] = parse_temperature(x[field])
except ValueError:
log.exception(("Failed to parse field {{'{}':'{}'}}."
"A temperature was expected").format(field, x[field]))
pass
def format_data(x, strict=True,
integers=[],
booleans=[],
temps=[]):
for field in integers:
try:
x[field] = str(int(x[field]))
except KeyError:
if not strict:
pass
for field in booleans:
try:
x[field] = str(int(bool(x[field])))
except KeyError:
if not strict:
pass
for field in temps:
try:
x[field] = str(float(x[field]))
except KeyError:
if not strict:
pass
def parse_temperature(temp):
try:
return float(temp)
except ValueError:
if temp == '-' or temp == '--':
return None
else:
raise