/
datalogger.py
316 lines (233 loc) · 9.79 KB
/
datalogger.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# -*- coding: utf-8 -*-
# (c) 2019 Richard Pobering <richard@hiveeyes.org>
# (c) 2019 Andreas Motl <andreas@hiveeyes.org>
# License: GNU General Public License, Version 3
import time
import machine
from terkin import __version__
from terkin import logging
from terkin.configuration import TerkinConfiguration
from terkin.device import TerkinDevice
from terkin.network import SystemWiFiMetrics
from terkin.sensor import SensorManager, AbstractSensor
from terkin.sensor.system import SystemMemoryFree, SystemTemperature, SystemBatteryLevel, SystemUptime
log = logging.getLogger(__name__)
# Maybe refactor to TerkinCore.
class TerkinDatalogger:
# Application metadata.
name = 'Terkin MicroPython Datalogger'
version = __version__
__instance__ = None
@staticmethod
def getInstance(settings=None):
"""
Singleton factory.
"""
if TerkinDatalogger.__instance__ is None:
if settings is None:
raise Exception("Settings are None but instance wasn't created before.")
else:
TerkinDatalogger(settings)
return TerkinDatalogger.__instance__
def __init__(self, settings):
# Fulfill singleton factory.
TerkinDatalogger.__instance__ = self
# Obtain configuration settings.
self.settings = TerkinConfiguration()
self.settings.add(settings)
# Configure logging.
logging_enabled = self.settings.get('main.logging.enabled', False)
if not logging_enabled:
logging.disable_logging()
# Initialize device.
self.device = TerkinDevice(name=self.name, version=self.version, settings=self.settings)
# Button manager instance (optional).
self.button_manager = None
# Initialize sensor domain.
self.sensor_manager = SensorManager()
@property
def appname(self):
return '{} {}'.format(self.name, self.version)
def start(self):
# Report about wakeup reason and run wakeup tasks.
self.device.resume()
# Turn off LTE modem and Bluetooth as we don't use them yet.
# Todo: Revisit where this should actually go.
self.device.power_off_lte_modem()
self.device.power_off_bluetooth()
log.info('Starting %s', self.appname)
# Start the watchdog for sanity.
self.device.start_watchdog()
# Configure RGB-LED according to settings.
self.device.configure_rgb_led()
# Dump configuration settings.
log_configuration = self.settings.get('main.logging.configuration', False)
if log_configuration:
self.settings.dump()
# Initialize buttons / touch pads.
buttons_enabled = self.settings.get('sensors.system.buttons.enabled', False)
if buttons_enabled:
from terkin.sensor.button import ButtonManager
self.button_manager = ButtonManager()
self.start_buttons()
# Disable this if you don't want serial access.
#self.device.enable_serial()
# Hello world.
self.device.print_bootscreen()
# Bootstrap infrastructure.
self.device.start_networking()
# Conditionally start telemetry if networking is available.
if self.device.status.networking:
self.device.start_telemetry()
# Todo: Signal readyness by publishing information about the device (Microhomie).
# e.g. ``self.device.publish_properties()``
# Setup sensors.
self.device.feed_watchdog()
bus_settings = self.settings.get('sensors.busses')
self.sensor_manager.register_busses(bus_settings)
self.register_sensors()
# Power up sensor peripherals.
self.sensor_manager.power_on()
# Ready.
self.start_mainloop()
def start_mainloop(self):
# Todo: Refactor by using timers.
# Enter the main loop.
while True:
# Feed the watchdog timer to keep the system alive.
self.device.feed_watchdog()
# Indicate activity.
# Todo: Optionally disable this output.
log.info('--- loop ---')
# Run downstream mainloop handlers.
self.loop()
# Yup.
machine.idle()
def loop(self):
"""
Main duty cycle loop.
"""
#log.info('Terkin loop')
# Read sensors.
readings = self.read_sensors()
# Transmit data.
self.transmit_readings(readings)
# Run the garbage collector.
self.device.run_gc()
# Sleep how ever.
self.sleep()
def sleep(self):
"""
Sleep until the next measurement cycle.
"""
deepsleep = self.settings.get('main.deepsleep', False)
interval = self.get_sleep_time()
# Amend deep sleep intent when masked through maintenance mode.
if self.device.status.maintenance is True:
deepsleep = False
log.info('Device is in maintenance mode. Skipping deep sleep and decreasing '
'interval to {} seconds'.format(interval))
# Use deep sleep if requested.
try:
if deepsleep:
# Shut down sensor peripherals.
self.sensor_manager.power_off()
# Shut down device peripherals.
self.device.power_off()
# Send device to deep sleep.
self.device.hibernate(interval, deepsleep=deepsleep)
# When hibernation fails, fall back to regular "time.sleep".
except:
log.exception('Failed to hibernate, falling back to regular sleep')
# Todo: Emit error message here.
log.info('Sleeping for {} seconds'.format(interval))
time.sleep(interval)
def get_sleep_time(self):
interval = self.settings.get('main.interval', 60.0)
# Configuration switchover backward compatibility / defaults.
if isinstance(interval, (float, int)):
self.settings.set('main.interval', {})
self.settings.setdefault('main.interval.field', interval)
self.settings.setdefault('main.interval.maintenance', 5.0)
# Compute interval.
interval = self.settings.get('main.interval.field')
# Amend deep sleep intent when masked through maintenance mode.
if self.device.status.maintenance is True:
interval = self.settings.get('main.interval.maintenance')
# FIXME
sleep_time = interval
return sleep_time
def register_sensors(self):
"""
Add system sensors.
"""
log.info('Registering Terkin sensors')
system_sensors = [
SystemMemoryFree,
SystemTemperature,
SystemBatteryLevel,
SystemUptime,
]
# Create environmental sensor adapters.
for sensor_factory in system_sensors:
sensor = sensor_factory()
if hasattr(sensor, 'setup') and callable(sensor.setup):
sensor.setup(self.settings)
self.sensor_manager.register_sensor(sensor)
# Add WiFi metrics.
try:
self.sensor_manager.register_sensor(SystemWiFiMetrics(self.device.networking.wifi_manager.station))
except:
log.exception('Enabling SystemWiFiMetrics sensor failed')
def read_sensors(self):
"""Read sensors"""
data = {}
sensors = self.sensor_manager.sensors
log.info('Reading %s sensor ports', len(sensors))
for sensor in sensors:
sensorname = sensor.__class__.__name__
log.info('Reading sensor port "%s"', sensorname)
try:
reading = sensor.read()
if reading is None or reading is AbstractSensor.SENSOR_NOT_INITIALIZED:
continue
data.update(reading)
except:
log.exception('Reading sensor "%s" failed', sensorname)
self.device.feed_watchdog()
# Debugging: Print sensor data before running telemetry.
log.info('Sensor data: %s', data)
return data
def transmit_readings(self, data):
"""Transmit data"""
# TODO: Optionally disable telemetry.
if self.device.telemetry is None:
log.warning('Telemetry disabled')
return False
telemetry_status = self.device.telemetry.transmit(data)
count_total = len(telemetry_status)
success = all(telemetry_status.values())
# Evaluate telemetry status outcome.
if success:
log.info('Telemetry status: SUCCESS ({}/{})'.format(count_total, count_total))
else:
count_failed = len([item for item in telemetry_status.values() if item is not True])
log.warning('Telemetry status: FAILURE. {} out of {} targets failed. '
'Status: {}'.format(count_failed, count_total, telemetry_status))
return success
def start_buttons(self):
# RGB-LED: 2
# POWER-ENABLE: 3
# SD-Card: 4, 8
# LTE 19, 20
# Misc: 13, 14, 9, 23
# Physical location when looking at the board with the RGB-LED oriented to the top.
# Location: Left side, 6th pin from top.
self.button_manager.setup_touchpad('P4', name='Touch3', location='Module-Left-Top-6th')
# Location: Left side, 5th pin from bottom.
self.button_manager.setup_touchpad('P8', name='Touch2', location='Module-Left-Bottom-5th')
# Location: Right side.
self.button_manager.setup_touchpad('P23', name='Touch6', location='Module-Right-Top-4th')
# Will yield ``ValueError: Touch pad error``.
#self.button_manager.setup_touchpad('P20', name='Touch8', location='Module-Right-Top-7th')
#self.button_manager.setup_touchpad('P19', name='Touch9', location='Module-Right-Top-8th')