From 99a06d04323e91458cab49e938e69e56f221fc92 Mon Sep 17 00:00:00 2001 From: Sean Kinsey Date: Tue, 26 Nov 2024 20:22:07 -0800 Subject: [PATCH] Bumped the version to 1.1 after adding shutdown handling --- CHANGELOG.md | 7 +++++-- README.md | 4 ---- setup.cfg | 1 + src/avionmqtt/__init__.py | 29 +++++++++++++---------------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd28705..b4ab01d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # Changelog -## 0.1.0 2024-11-26 -Initial commit \ No newline at end of file +## 1.0.0 2024-11-26 +Initial commit + +## 1.1.0 2024-11-26 +Added basic shutdown handling \ No newline at end of file diff --git a/README.md b/README.md index b8261fc..1663808 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,6 @@ pip install avionmqtt python -m avionmqtt -s settings.yaml ``` -> [!WARNING] -> This script currently does not have any graceful handling of SIGINT and so will leave the BLE connection dangling. -> Use the `sudo bluetoothctl power off && sudo bluetoothctl power on` or similar to reset the adapter. - # settings.yaml ```yaml diff --git a/setup.cfg b/setup.cfg index 9f53011..d5e4d3c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,6 +22,7 @@ install_requires = halohome ~=0.7.0 bleak ~=0.22 csrmesh ~=0.10 + aiorun ~=2024.8.1 [options.packages.find] where = src \ No newline at end of file diff --git a/src/avionmqtt/__init__.py b/src/avionmqtt/__init__.py index 5a0c0fa..7de0c05 100644 --- a/src/avionmqtt/__init__.py +++ b/src/avionmqtt/__init__.py @@ -12,14 +12,7 @@ from enum import Enum import signal import sys - - -def signal_handler(sig, frame): - print("You pressed Ctrl+C!") - sys.exit(0) - - -signal.signal(signal.SIGINT, signal_handler) +from aiorun import run MQTT_RETRY_INTERVAL = 5 CHARACTERISTIC_LOW = "c4edc000-9daf-11e3-8003-00025b000b00" @@ -312,8 +305,10 @@ async def main(): username=mqtt_settings["username"], password=mqtt_settings["password"], ) + + running = True # connect to mqtt - while True: + while running: try: print("mqtt: Connecting to broker") async with mqtt: @@ -326,7 +321,7 @@ async def main(): # now connect the mesh print("mesh: Connecting to mesh") - while True: + while running: try: mesh = None print("mesh: Scanning for devices") @@ -355,6 +350,9 @@ async def main(): await mqtt_subscribe(mqtt, mesh, key) print("done?") + except asyncio.CancelledError: + running = False + except BleakError as e: print(f"mesh: Error connecting to {mac}") @@ -364,18 +362,17 @@ async def main(): print(e) finally: + print("mesh done") if mesh and mesh.is_connected: await mesh.disconnect() - print("mesh: Disconnected from {mac}") + print(f"mesh: Disconnected from {mac}") except aiomqtt.MqttError: print(f"mqtt: Connection lost; Reconnecting in {MQTT_RETRY_INTERVAL} seconds ...") await asyncio.sleep(MQTT_RETRY_INTERVAL) + finally: + print("mqtt done") # create a new event loop (low-level api) -loop = asyncio.new_event_loop() -# schedule our coroutine -loop.create_task(main()) -# run the event loop until stopped -loop.run_forever() +run(main())