Skip to content

Commit

Permalink
Bumped the version to 1.1 after adding shutdown handling
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindkinsey committed Nov 27, 2024
1 parent 6a70de3 commit 99a06d0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog

## 0.1.0 2024-11-26
Initial commit
## 1.0.0 2024-11-26
Initial commit

## 1.1.0 2024-11-26
Added basic shutdown handling
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 13 additions & 16 deletions src/avionmqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand All @@ -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")
Expand Down Expand Up @@ -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}")

Expand All @@ -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())

0 comments on commit 99a06d0

Please sign in to comment.