-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
128 lines (105 loc) · 4.67 KB
/
run.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from influx.influx_adapter import *
from influx.datapoint_utils import DatapointType
from location.weather_adapter import WeatherAdapter
from location.wunderground_adapter import WundergroundAdapter
from time import sleep
from threading import Thread, Event
from config import *
from sensors.sensor import Sensor
from sensors.file_sensor import FileSensor
from sensors.serial_sensor import SerialSensor
from logging import basicConfig, getLogger
# Test classes
from test.mock.mock_sensor import MockSensor
from test.mock.mock_weather_adapter import MockWeatherAdapater
# Set up logger
basicConfig(level=log_level)
logger = getLogger(__name__)
class MainThread(Thread):
def __init__(self, current_vals: dict):
"""
Initializes a new program thread, responsible for collecting and storing data.
Current data will be stored in the current_vals reference passed into this constructor.
Pass in an empty dict for current_vals.
"""
super(MainThread, self).__init__()
self.current_vals = current_vals
self.stoprequest = Event()
self.daemon = True
# Sensor obojects
self.temp_sensor: Sensor = None
self.humid_sensor: Sensor = None
# Current readings
self.current_vals = current_vals
self.current_vals['current_temp'] = 0.0
self.current_vals['current_humidity'] = 0.0
self.current_vals['current_location_temp'] = 0.0
self.current_vals['current_location_humidity'] = 0.0
# For Travis
if is_testing:
logger.info("In testing mode. Initing mock sensors.")
self.temp_sensor = MockSensor()
self.humid_sensor = MockSensor()
elif sensor_settings['type'] is SensorType.FILE:
self.temp_sensor = FileSensor(sensor_settings['temperature'])
self.humid_sensor = FileSensor(sensor_settings['humidity'])
elif sensor_settings['type'] is SensorType.SERIAL:
self.temp_sensor = SerialSensor(
sensor_settings['temperature'],
sensor_settings['baud'],
sensor_settings['timeout']
)
def run(self):
"""
Starts the program thread.
"""
while not self.stoprequest.is_set():
temp_val = self.temp_sensor.read_data().strip()
humid_val = self.humid_sensor.read_data().strip()
if len(temp_val) > 0:
logger.debug('Got a new temperature value! %s', temp_val)
self.current_vals['current_temp'] = float(temp_val)
if len(humid_val) > 0:
logger.debug('Got a new humidity value! %s', humid_val)
self.current_vals['current_humidity'] = float(humid_val)
if location_settings['enabled']:
remote_querier: WeatherAdapter = WundergroundAdapter() if not is_testing else MockWeatherAdapater()
logger.debug('Collecting location temp/humiditiy from remote')
self.current_vals['current_location_temp'] = remote_querier.get_outside_temp()
self.current_vals['current_location_humidity'] = remote_querier.get_outside_humidity()
logger.debug('Got the following remote temp/humidity: (%s, %s)',
str(self.current_vals['current_location_temp']),
str(self.current_vals['current_location_humidity'])
)
if influx_settings['enabled']:
influx_push_data(
self.current_vals['current_temp'],
self.current_vals['current_humidity'],
DatapointType.SENSOR
)
if location_settings['enabled']:
influx_push_data(
self.current_vals['current_location_temp'],
self.current_vals['current_location_humidity'],
DatapointType.LOCATION
)
# Wait poll_rate before reading vals again
sleep(poll_rate)
def join(self, timeout=None):
"""
Cleanly terminates the program thread.
"""
self.stoprequest.set()
logger.info("Received join signal - Closing streams and shutting down.")
self.__shutdown_streams()
super(MainThread, self).join(timeout)
def __shutdown_streams(self):
"""
Closes data streams for sensors.
"""
if self.temp_sensor is not None:
logger.debug('Closing temp sensor.')
self.temp_sensor.close()
if self.humid_sensor is not None:
logger.debug('Closing humid sensor.')
self.humid_sensor.close()