From f29e139cbb7e4a4d539cba6e894ef4a6acd312d6 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 May 2023 09:22:00 +0300 Subject: WIP: big refactoring --- src/temphumd.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/temphumd.py') diff --git a/src/temphumd.py b/src/temphumd.py index f4d1fca..c3d1975 100755 --- a/src/temphumd.py +++ b/src/temphumd.py @@ -6,10 +6,11 @@ import logging from typing import Optional from home.config import config -from home.temphum import SensorType, create_sensor, TempHumSensor +from home.temphum import SensorType, BaseSensor +from home.temphum.i2c import create_sensor logger = logging.getLogger(__name__) -sensor: Optional[TempHumSensor] = None +sensor: Optional[BaseSensor] = None lock = asyncio.Lock() delay = 0.01 @@ -62,7 +63,7 @@ async def run_server(host, port): if __name__ == '__main__': - config.load() + config.load_app() if 'measure_delay' in config['sensor']: delay = float(config['sensor']['measure_delay']) -- cgit v1.2.3 From b0bf43e6a272d42a55158e657bd937cb82fc3d8d Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 23:02:34 +0300 Subject: move files, rename home package to homekit --- src/temphumd.py | 78 --------------------------------------------------------- 1 file changed, 78 deletions(-) delete mode 100755 src/temphumd.py (limited to 'src/temphumd.py') diff --git a/src/temphumd.py b/src/temphumd.py deleted file mode 100755 index c3d1975..0000000 --- a/src/temphumd.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env python3 -import asyncio -import json -import logging - -from typing import Optional - -from home.config import config -from home.temphum import SensorType, BaseSensor -from home.temphum.i2c import create_sensor - -logger = logging.getLogger(__name__) -sensor: Optional[BaseSensor] = 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_app() - - 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...') -- cgit v1.2.3