Skip to content
Permalink
Browse files

introduce SensorManager

  • Loading branch information...
einsiedlerkrebs committed Mar 23, 2019
1 parent b49bd43 commit 4e622b8a6c1ec5b411df2cc5c5b9c788b00f1512
@@ -9,6 +9,7 @@
#
import settings
from terkin.datalogger import TerkinDatalogger
from terkin.sensor import register_sensor


class DummySensor:
@@ -40,7 +41,7 @@ def register_sensors(self):

# Add a new sensor. This is just an example sensor.
sensor = DummySensor()
self.register_sensor(sensor)
register_sensor(self.sensors, sensor)


def main():
@@ -9,6 +9,7 @@
#
import settings
from terkin.datalogger import TerkinDatalogger
from terkin.sensor import register_sensor


class DummySensor:
@@ -40,7 +41,7 @@ def register_sensors(self):

# Add a new sensor. This is just an example sensor.
sensor = DummySensor()
self.register_sensor(sensor)
register_sensor(self.sensors, sensor)

def loop(self):
"""
@@ -54,14 +54,20 @@ def add_hx711_sensor(self):
offset=hx711_settings['offset'],
)

# Select driver module. Use "gerber" (vanilla) or "heisenberg" (extended).
hx711_sensor = HX711Sensor()
hx711_sensor.register_pin('dout', hx711_settings['pin_dout'])
hx711_sensor.register_pin('dsck', hx711_settings['pin_dout'])
hx711_sensor.register_parameter('scale', hx711_settings['scale'])
hx711_sensor.register_parameter('offset', hx711_settings['offset'])
hx711_sensor.select_driver('heisenberg')

# Select driver module. Use "gerber" (vanilla) or "heisenberg" (extended).

# Start sensor.
hx711_sensor.start()

# Register with framework.
self.register_sensor(hx711_sensor)
self.sensor_manager.register_sensor(hx711_sensor)

def loop(self):
"""
@@ -3,8 +3,9 @@
# (c) 2019 Andreas Motl <andreas@hiveeyes.org>
# License: GNU General Public License, Version 3

from terkin.sensor import AbstractSensor

class HX711Sensor:
class HX711Sensor(AbstractSensor):
"""
A generic HX711 sensor component wrapping possibly
different hardware driver variants.
@@ -20,12 +21,17 @@ def __init__(self, pin_dout=None, pin_pdsck=None, gain=None, scale=None, offset=
'DOUT={}, PD_SCK={}, GAIN={}, scale={}, offset={}'.format(pin_dout, pin_pdsck, gain, scale, offset))

# Hardware parameters and configuration settings.


# this is done by AbstactSensor
self.pin_dout = pin_dout
self.pin_pdsck = pin_pdsck
self.gain = gain or 128
self.scale = scale
self.offset = offset



# The driver class.
self.driver_class = None

@@ -53,7 +59,8 @@ def start(self):

# Initialize the HX711 hardware driver.
try:
self.loadcell = self.driver_class(self.pin_dout, self.pin_pdsck, self.gain)
#self.loadcell = self.driver_class(self.pin_dout, self.pin_pdsck, self.gain)
self.loadcell = self.driver_class(self.pins['dout'], self.pins['dsck'], self.parameters['gain'])
except Exception as ex:
print('ERROR: HX711 hardware driver failed. {}'.format(ex))
raise
@@ -8,7 +8,7 @@
from terkin import __version__
from terkin.configuration import TerkinConfiguration
from terkin.device import TerkinDevice
from terkin.sensor import MemoryFree
from terkin.sensor import MemoryFree, SensorManager


# Maybe refactor to TerkinCore.
@@ -23,7 +23,7 @@ def __init__(self, settings):
self.settings.add(settings)
self.settings.dump()
self.device = None
self.sensors = []
self.sensor_manager = SensorManager()

def start(self):

@@ -59,10 +59,7 @@ def register_sensors(self):
self.device.tlog('Registering Terkin sensors')

memfree = MemoryFree()
self.register_sensor(memfree)

def register_sensor(self, sensor):
self.sensors.append(sensor)
self.sensor_manager.register_sensor(memfree)

def start_mainloop(self):
# TODO: Refactor by using timers.
@@ -89,7 +86,7 @@ def start_mainloop(self):
def read_sensors(self):
"""Read sensors"""
data = {}
for sensor in self.sensors:
for sensor in self.sensor_manager.sensors:
sensor_name = sensor.__class__.__name__
print('INFO: Reading sensor "{}"'.format(sensor_name))
try:
@@ -66,6 +66,7 @@ def start_wdt(self):
# Enable it with a specified timeout.
# TODO: Use values from configuration settings here.
self.wdt = WDT(timeout=5000)

self.wdt.feed()

def feed_wdt(self):
@@ -4,8 +4,69 @@
# License: GNU General Public License, Version 3


class SensorManager:
def __init__(self):
self.sensors = []
pass

def register_sensor(self, sensor):
self.sensors.append(sensor)


def get_sensor_by_name(self, name):
pass


class AbstractSensor:
"""
Abstract sensor container, containing meta data as readings
"""
def __init__(self):
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.pins = {}
self.parameter = {}

def start(self):
raise NotImplementedError()
pass

def register_pin(self, name, pin):
self.pins[name] = pin

raise NotImplementedError()
pass

def register_parameter(self, name, parameter):
self.parameter[name] = parameter

raise NotImplementedError()
pass


def power_off(self):
pass

def power_on(self):
pass


def read(self):
raise NotImplementedError()
pass



class MemoryFree:

def read(self):
import gc
return {'memfree': gc.mem_free()}

@@ -22,9 +22,12 @@
"""
import json
from copy import copy
import sys
print(sys.path)
from urllib.parse import urlsplit, urlencode



class TelemetryManager:

def __init__(self):

0 comments on commit 4e622b8

Please sign in to comment.
You can’t perform that action at this time.