/
core.py
300 lines (231 loc) · 8.47 KB
/
core.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
# -*- 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
from binascii import hexlify
from machine import Pin, I2C
from terkin import logging
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
class SensorManager:
"""
Manages all busses and sensors.
"""
def __init__(self):
self.sensors = []
self.busses = {}
def register_sensor(self, sensor):
self.sensors.append(sensor)
def register_bus(self, bus):
bus_name = bus.name
log.info('Registering bus "%s"', bus_name)
self.busses[bus_name] = bus
def get_bus_by_name(self, name):
log.info('Trying to find bus by name "%s"', name)
bus = self.busses.get(name)
log.info('Found bus by name "%s": %s', name, bus)
return bus
def get_sensor_by_name(self, name):
raise NotImplementedError('"get_sensor_by_name" not implemented yet')
def setup_busses(self, busses_settings):
"""
Register configured I2C, OneWire and SPI busses.
"""
log.info("Starting all busses %s", busses_settings)
for bus_settings in busses_settings:
if bus_settings.get("enabled") is False:
log.info('Bus with id "{}" and family "{}" is disabled, '
'skipping registration'.format(bus_settings.get('id'), bus_settings.get('family')))
continue
try:
self.setup_bus(bus_settings)
except:
log.exception('Registering bus failed. settings={}'.format(bus_settings))
def setup_bus(self, bus_settings):
bus_family = bus_settings.get('family')
if bus_family == BusType.OneWire:
owb = OneWireBus(bus_settings)
owb.register_pin("data", bus_settings['pin_data'])
owb.start()
self.register_bus(owb)
elif bus_family == BusType.I2C:
i2c = I2CBus(bus_settings)
i2c.register_pin("sda", bus_settings['pin_sda'])
i2c.register_pin("scl", bus_settings['pin_scl'])
i2c.start()
self.register_bus(i2c)
else:
log.error("Invalid bus configuration: %s", bus_settings)
def power_on(self):
self.power_toggle_busses('power_on')
self.power_toggle_sensors('power_on')
def power_off(self):
self.power_toggle_sensors('power_off')
self.power_toggle_busses('power_off')
def power_toggle_sensors(self, action):
for sensor in self.sensors:
sensorname = sensor.__class__.__name__
if hasattr(sensor, action):
log.info('Sending {} to sensor {}'.format(action, sensorname))
try:
getattr(sensor, action)()
except:
log.exception('Sending {} to sensor {} failed'.format(action, sensorname))
def power_toggle_busses(self, action):
for busname, bus in self.busses.items():
if hasattr(bus, action):
log.info('Sending {} to bus {}'.format(action, busname))
try:
getattr(bus, action)()
except:
log.exception('Sending {} to sensor {} failed'.format(action, busname))
class AbstractSensor:
"""
Abstract sensor container, containing meta data as readings.
"""
SENSOR_NOT_INITIALIZED = object()
def __init__(self, settings=None):
self.settings = settings or {}
self.type = self.settings.get('type')
self.name = None
self.family = None
self.driver = None
"""
e.g. Multiple onewire sensors are address indexed on a bus.
"""
self.address = None
self.bus = None
self.parameter = {}
self.pins = {}
def start(self):
raise NotImplementedError("Must be implemented in sensor driver")
def set_address(self, address):
self.address = address
def register_pin(self, name, pin):
self.pins[name] = pin
def register_parameter(self, name, parameter):
self.parameter[name] = parameter
def acquire_bus(self, bus):
self.bus = bus
def read(self):
raise NotImplementedError()
def format_fieldname(self, name, address):
fieldname = '{name}.{address}.{bus}'.format(name=name, address=address, bus=self.bus.name)
return fieldname
def serialize(self):
return dict(serialize_som(self.__dict__, stringify=['bus']))
class BusType:
I2C = 'i2c'
OneWire = 'onewire'
class AbstractBus:
"""
A blueprint for all bus objects.
"""
type = None
def __init__(self, settings):
"""
convention <type>:<index>
"""
self.settings = settings
self.number = self.settings['number']
self.adapter = None
# TODO: Publish found 1-Wire devices to MQTT bus and HTTP API.
self.devices = []
self.pins = {}
@property
def name(self):
return str(self.type) + ":" + str(self.number)
def register_pin(self, name, pin):
self.pins[name] = pin
def serialize(self):
info = dict(serialize_som(self.__dict__))
# FIXME: Why is that?
info.update({'name': self.name, 'type': self.type})
return info
class OneWireBus(AbstractBus):
"""
Initialize the 1-Wire hardware driver and represent as bus object.
"""
type = BusType.OneWire
def start(self):
# Todo: Improve error handling.
try:
from onewire.onewire import OneWire
self.adapter = OneWire(Pin(self.pins['data']))
self.scan_devices()
except Exception as ex:
log.exception('1-Wire hardware driver failed')
def scan_devices(self):
"""
Resetting the 1-Wire device in case of leftovers
"""
self.adapter.reset()
time.sleep(1)
# Scan for 1-Wire devices and populate `devices`.
# TODO: Refactor things specific to DS18x20 devices elsewhere.
self.devices = [rom for rom in self.adapter.scan() if rom[0] == 0x10 or rom[0] == 0x28]
log.info("Found {} 1-Wire (DS18x20) devices: {}".format(len(self.devices), self.get_devices_ascii()))
def get_devices_ascii(self):
return list(map(self.device_address_ascii, self.devices))
def serialize(self):
info = super().serialize()
if 'devices' in info:
info['devices'] = self.get_devices_ascii()
return info
@staticmethod
def device_address_ascii(address):
# Compute ASCII representation of device address.
if isinstance(address, (bytearray, bytes)):
address = hexlify(address).decode()
return address
class I2CBus(AbstractBus):
"""
Initialize the I2C hardware driver and represent as bus object.
"""
type = BusType.I2C
def start(self):
# Todo: Improve error handling.
try:
self.adapter = I2C(self.number, mode=I2C.MASTER, pins=(self.pins['sda'], self.pins['scl']), baudrate=100000)
self.scan_devices()
except Exception as ex:
log.exception('I2C hardware driver failed')
def scan_devices(self):
self.devices = self.adapter.scan()
# i2c.readfrom(0x76, 5)
log.info("Found {} I2C devices: {}.".format(len(self.devices), self.devices))
def power_off(self):
"""
Turn off the I2C peripheral.
https://docs.pycom.io/firmwareapi/pycom/machine/i2c.html
"""
log.info('Turning off I2C bus {}'.format(self.name))
self.adapter.deinit()
def serialize_som(thing, stringify=None):
"""
Serialize the sensor object model to a representation
suitable to be served for the device API.
"""
stringify = stringify or []
if isinstance(thing, list):
hm = []
for item in thing:
hm.append(serialize_som(item))
return hm
elif isinstance(thing, dict):
newthing = {}
for key, value in thing.items():
if key in stringify:
newthing[key] = str(value)
else:
newthing[key] = serialize_som(value)
return newthing
elif isinstance(thing, (str, int, float, bool, set)) or type(thing) is type(None):
return thing
else:
if hasattr(thing, 'serialize'):
thing = thing.serialize()
else:
thing = str(thing)
return thing