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
|
#!/usr/bin/env python3
import logging
import urllib.request
from typing import Optional
logger = logging.getLogger()
class ThermometerRegistry(object):
def __init__(self):
self.thermometers = {
'house_outside': ('10.0.0.75', 'outside_temp'),
'house_inside_downstairs': ('10.0.0.75', 'inside_downstairs_temp'),
'house_inside_upstairs': ('10.0.0.75', 'inside_upstairs_temp'),
'house_computer_closet': ('10.0.0.75', 'computer_closet_temp'),
'house_crawlspace': ('10.0.0.75', 'crawlspace_temp'),
'cabin_outside': ('192.168.0.107', 'outside_temp'),
'cabin_inside': ('192.168.0.107', 'inside_temp'),
'cabin_crawlspace': ('192.168.0.107', 'crawlspace_temp'),
'cabin_hottub': ('192.168.0.107', 'hottub_temp'),
}
def read_temperature(self, location: str, *, convert_to_fahrenheit=False) -> Optional[float]:
record = self.thermometers.get(location, None)
if record is None:
logger.error(
f'Location {location} is not known. Valid locations are {self.thermometers.keys()}.'
)
return None
url = f'http://{record[0]}/~pi/{record[1]}'
logger.debug(f'Constructed URL: {url}')
try:
www = urllib.request.urlopen(url, timeout=3)
temp = www.read().decode('utf-8')
temp = float(temp)
if convert_to_fahrenheit:
temp *= 9 / 5
temp += 32.0
temp = round(temp)
except Exception as e:
logger.exception(e)
logger.error(f'Failed to read temperature at URL: {url}')
temp = None
finally:
if www is not None:
www.close()
return temp
|