Skip to content
Snippets Groups Projects
tempermonitor.py 8.81 KiB
Newer Older
"""
This is the tempermonitoring system v2.

it is based on the work of the old temperature monitoring system and released
under the terms as stated in the LICENSE.md file.

Changelog:

2018-08 jotweh: reimplemented using a micropython-esp32

Open issues:

- Temperature Limits
- Integrate USB Sensors
"""

import asyncio
import configparser
import importlib
from datetime import datetime
from pathlib import Path
import serial

class Sensor:
    """
    One instance as sensor posing as measurementproxy
    """
    def __init__(self, config, owid):
        self.temperature = None
        self.last_update = 0
        self.calibration = 0

        try:
            if owid in config:
                self.name = config[owid]['name']
                self.calibration = config[owid]['calibration']
            else:
                print("Invalid Config: missing section {}".format(owid))
            print("Invalid Config: for {}: {}".format(owid, exc))
            raise

    def update(self, temperature):
        """
        Store a new measurement, and remember the time it was taken
        """
Johannes Walcher's avatar
Johannes Walcher committed
        self.temperature = float(temperature)
        self.last_update = time.time()

class TempMonitor:
    """
    Interact with the esp-one-wire interface that sends:

    one-wire-id1 temperature
    one-wire-id1 temperature
    one-wire-id1 temperature

    followed by an empty line as data packet
    """

    def __init__(self, loop, configfile):
        self.loop = loop or asyncio.get_event_loop()

        self._configname = configfile
        self.config = configparser.ConfigParser()
        self.config.read(configfile)

        self.plugins = []
        self.sensors = {}
        self._reader, self._writer = (None, None)

        # Test if all necessary config fields are set, that are not part of the normal
            self.config['collectd']['hostname'],
            self.config['collectd']['interval'],
            self.config['mail']['from'],
            self.config['mail']['to'],
            self.config['mail']['to_urgent'],
            self.config['mail']['min_delay_between_messages'],
            self.config['serial']['timeout'],
            self.config['serial']['port'],
            self.config['serial']['baudrate'],
        print("connecting to", self.config['serial']['port'])
            # Skip all known and predefined sections
            if owid in ['DEFAULT', 'serial', 'collectd', 'mail', 'warning']:
            self.sensors[owid] = Sensor(self.config, owid)
        self._run_task = loop.create_task(self.run())

    async def reconnect(self):
        Connect to the ESP chip
        self._reader, self._writer = await serial_asyncio.open_serial_connection(
            url=self.config['serial']['port'],
            baudrate=self.config['serial']['baudrate'],
            loop=self.loop)
        # upon startup we only see garbage. (micropython starting up),
        # also it will produce warnings if the recording is started in the middle
        # of a message, so wait until the end of a message block to start the game
        # If the baudrate is wrong during micropython startup - this will also be
        # skiped.
        while True:
            try:
Johannes Walcher's avatar
Johannes Walcher committed
                if (await self._reader.readline()).decode('ascii').strip() == "":
                    break
            except UnicodeError:
                continue
    async def run(self):
        """
        Read the protocol, update the sensors or trigger a collectd update
        """
        await self.reconnect()
        last_valid_data_received = time.time()
        line = ""
            # Wait for the next line

            if time.time() - last_valid_data_received > 1800:
                self.call_plugin("err_no_valid_data", last_line=line)

            try:
                line = await asyncio.wait_for(
                    self._reader.readline(),
Johannes Walcher's avatar
Johannes Walcher committed
                    timeout=int(self.config['serial']['timeout']))
            except asyncio.TimeoutError:
                await self.call_plugin("err_nodata")
                continue
            except serial.SerialException as exc:
                print("Problem with the serial connection - reconnecting: ", exc)
                await self.reconnect()
Johannes Walcher's avatar
Johannes Walcher committed
                line = line.decode('ascii').strip()
            #print("recv:", line)
            if line == '':
                # Block has ended
                await self.store_sensors()
                continue
            # Try to parse the line
            try:
                owid, temp = line.split(' ')
Johannes Walcher's avatar
Johannes Walcher committed
                temp = float(temp)
            except ValueError as exc:
                print("Invaid line received: {}\n{}".format(line, exc))
                continue

            ## we have at least a valid line
            last_valid_data_received = time.time()

            sensor = self.sensors.get(owid, None)
            if not sensor:
                # If the sensor is new - notify the operators
                await self.call_plugin("err_unknown_sensor",
                                       config=self._configname,
                                       owid=owid,
                                       temp=temp)
            elif temp > 1000 or temp < -1000:
                sensor.valid = False
                # if the sensor is giving bullshit data - notify the operators
                await self.call_plugin("err_problem_sensor",
                                       owid=owid,
                                       name=sensor.name,
                                       temp=temp)
                sensor.valid = True
                # in the unlikely event that everyting is fine: log the data
                sensor.update(temp)

    async def teardown(self):
        """ Terminate all started tasks """
        self._run_task.cancel()
        try:
            await self._run_task
        except asyncio.CancelledError:
            pass

    async def call_plugin(self, call, *args, **kwargs):
        """
        Call the given method on all plugins, proxying arguments
        """
        result = {}
        for plugin in self.plugins:
            func = getattr(plugin, call, None)
            if func:
                if asyncio.iscoroutinefunction(func):
                    result[plugin.name] = await func(*args, **kwargs)
                else:
                    result[plugin.name] = func(*args, **kwargs)

    async def store_sensors(self):
        """
        Prepare the sensors to be stored and maybe send an email
        """
        sensorstr = "measurements: "
        for owid, sensor in self.sensors.items():
            if sensor.valid:
                sensorstr += "{}: {}; ".format(sensor.name, sensor.temperature)
                if sensor.last_update <= self._last_store:
                    sensor.valid = False
                    isotime = datetime.utcfromtimestamp(sensor.last_update).isoformat()
                    await self.call_plugin("err_missed_sensor",
                                           owid=owid,
                                           name=sensor.name,
                                           last_update=isotime)
                sensorstr += "{}: INVALID; ".format(sensor.name)
        print(sensorstr)
        await self.call_plugin("sensor_update")
        self._last_store = time.time()


def setup_plugin(filename, plugin):
    """
    Setup and fix plugins
    """
    if not getattr(plugin, "name", None):
        plugin.name = filename

def main():
    """
    Start the tempmonitor
    """
    loop = asyncio.get_event_loop()

    configfile = "/etc/temperature/tempermon.ini"
    if len(sys.argv) == 2:
        configfile = sys.argv[1]

    print("Configuring temperature monitoring system from {}.".format(configfile))
    monitor = TempMonitor(loop, configfile)

    plugin_path = Path(__file__).resolve().parent / "plugins"
    print("Loading plugins from {}".format(plugin_path))
    for filename in plugin_path.glob("*.py"):
        if (plugin_path / filename).exists():
            print("loading {}".format(filename.name))
            modname = "plugins." + filename.name.split('.')[0]
            module = importlib.import_module(modname)
            plugin = module.init(monitor)
            setup_plugin(filename, plugin)
            monitor.plugins.append(plugin)
            print("Loaded: {}".format(plugin.name))

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        loop.run_until_complete(monitor.teardown())

if __name__ == "__main__":