diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-06-06 19:03:29 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2023-06-06 19:03:29 +0300 |
commit | 5de1896f5be183d600361d70218c6d579f3a5899 (patch) | |
tree | a196b39b3e4cf429e7206ec31c0a471aed72a7f9 | |
parent | 47380c2e680a24be1d4892eb401eace4bce2895d (diff) | |
parent | 5e3605327510f9221d71554291aed4819e8371aa (diff) |
Merge branch 'mqtt-refactoring' of ch1p.io:homekit into mqtt-refactoring
-rwxr-xr-x | src/pump_bot.py | 13 | ||||
-rwxr-xr-x | src/temphum_mqtt_node.py | 77 |
2 files changed, 88 insertions, 2 deletions
diff --git a/src/pump_bot.py b/src/pump_bot.py index fa884ab..48efec4 100755 --- a/src/pump_bot.py +++ b/src/pump_bot.py @@ -13,7 +13,7 @@ from home.api.types import BotType from home.mqtt import MqttNode, MqttModule, MqttPayload, add_mqtt_module from home.mqtt.module.relay import MqttPowerStatusPayload from home.mqtt.module.temphum import MqttTemphumDataPayload -from home.mqtt.module.diagnostics import InitialDiagnosticsPayload +from home.mqtt.module.diagnostics import InitialDiagnosticsPayload, DiagnosticsPayload config.load('pump_bot') @@ -210,7 +210,16 @@ def markup(ctx: Optional[bot.Context]) -> Optional[ReplyKeyboardMarkup]: def mqtt_payload_callback(payload: MqttPayload): global watering_mcu_status - watering_mcu_status['last_time'] = int(time()) + types_the_node_can_send = ( + InitialDiagnosticsPayload, + DiagnosticsPayload, + MqttTemphumDataPayload, + MqttPowerStatusPayload + ) + for cl in types_the_node_can_send: + if isinstance(payload, cl): + watering_mcu_status['last_time'] = int(time()) + break if isinstance(payload, InitialDiagnosticsPayload): watering_mcu_status['last_boot_time'] = int(time()) diff --git a/src/temphum_mqtt_node.py b/src/temphum_mqtt_node.py new file mode 100755 index 0000000..f4d1fca --- /dev/null +++ b/src/temphum_mqtt_node.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +import asyncio +import json +import logging + +from typing import Optional + +from home.config import config +from home.temphum import SensorType, create_sensor, TempHumSensor + +logger = logging.getLogger(__name__) +sensor: Optional[TempHumSensor] = None +lock = asyncio.Lock() +delay = 0.01 + + +async def get_measurements(): + async with lock: + await asyncio.sleep(delay) + + temp = sensor.temperature() + rh = sensor.humidity() + + return rh, temp + + +async def handle_client(reader, writer): + request = None + while request != 'quit': + try: + request = await reader.read(255) + if request == b'\x04': + break + request = request.decode('utf-8').strip() + except Exception: + break + + if request == 'read': + try: + rh, temp = await asyncio.wait_for(get_measurements(), timeout=3) + data = dict(humidity=rh, temp=temp) + except asyncio.TimeoutError as e: + logger.exception(e) + data = dict(error='i2c call timed out') + else: + data = dict(error='invalid request') + + writer.write((json.dumps(data) + '\r\n').encode('utf-8')) + try: + await writer.drain() + except ConnectionResetError: + pass + + writer.close() + + +async def run_server(host, port): + server = await asyncio.start_server(handle_client, host, port) + async with server: + logger.info('Server started.') + await server.serve_forever() + + +if __name__ == '__main__': + config.load() + + if 'measure_delay' in config['sensor']: + delay = float(config['sensor']['measure_delay']) + + sensor = create_sensor(SensorType(config['sensor']['type']), + int(config['sensor']['bus'])) + + try: + host, port = config.get_addr('server.listen') + asyncio.run(run_server(host, port)) + except KeyboardInterrupt: + logging.info('Exiting...') |