|
|
@@ -5,6 +5,7 @@ import struct
|
|
|
import sqlite3
|
|
|
from dotenv import load_dotenv
|
|
|
import os
|
|
|
+import signal
|
|
|
|
|
|
import smbus2
|
|
|
import bme280
|
|
|
@@ -46,43 +47,32 @@ def notification_handler(characteristic: BleakGATTCharacteristic, data: bytearra
|
|
|
|
|
|
async def ble_task():
|
|
|
global outdoorConnected
|
|
|
- client = None
|
|
|
|
|
|
- try:
|
|
|
- while True:
|
|
|
- device = await BleakScanner.find_device_by_name(outdoorName)
|
|
|
-
|
|
|
- if device is None:
|
|
|
- outdoorConnected = 0
|
|
|
- print("No device found, wait then scan again")
|
|
|
- await asyncio.sleep(10)
|
|
|
- continue
|
|
|
-
|
|
|
- try:
|
|
|
- async with BleakClient(device) as client:
|
|
|
- outdoorConnected = 1
|
|
|
- print(f"Connected to {outdoorName}")
|
|
|
-
|
|
|
- await client.start_notify(TEMP_UUID, notification_handler)
|
|
|
- await client.start_notify(PRESSURE_UUID, notification_handler)
|
|
|
- await client.start_notify(HUMIDITY_UUID, notification_handler)
|
|
|
- await client.start_notify(ALTITUDE_UUID, notification_handler)
|
|
|
-
|
|
|
- while client.is_connected:
|
|
|
- await asyncio.sleep(1)
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- print(f"Connection lost or error: {e}")
|
|
|
- finally:
|
|
|
- outdoorConnected = 0
|
|
|
- print("Cleaning up connection state...")
|
|
|
+ while True:
|
|
|
+ device = await BleakScanner.find_device_by_name(outdoorName)
|
|
|
|
|
|
- except asyncio.CancelledError:
|
|
|
- print("BLE task is being cancelled...")
|
|
|
- if client and client.is_connected:
|
|
|
- await client.disconnect()
|
|
|
- outdoorConnected = 0
|
|
|
- raise
|
|
|
+ if device is None:
|
|
|
+ outdoorConnected = 0
|
|
|
+
|
|
|
+ print("no device found, wait then scan again")
|
|
|
+ await asyncio.sleep(10)
|
|
|
+ continue
|
|
|
+
|
|
|
+ try:
|
|
|
+ async with BleakClient(device) as client:
|
|
|
+ outdoorConnected = 1
|
|
|
+ print(f"connected to {outdoorName}")
|
|
|
+
|
|
|
+ await client.start_notify(TEMP_UUID, notification_handler)
|
|
|
+ await client.start_notify(PRESSURE_UUID, notification_handler)
|
|
|
+ await client.start_notify(HUMIDITY_UUID, notification_handler)
|
|
|
+ await client.start_notify(ALTITUDE_UUID, notification_handler)
|
|
|
+
|
|
|
+ while (client.is_connected):
|
|
|
+ await asyncio.sleep(1)
|
|
|
+ except Exception:
|
|
|
+ outdoorConnected = 0
|
|
|
+ print("exception while connecting or getting data")
|
|
|
|
|
|
async def sensor_task():
|
|
|
global indoorValues
|
|
|
@@ -145,12 +135,26 @@ async def write_task():
|
|
|
await asyncio.sleep(10)
|
|
|
|
|
|
async def main():
|
|
|
+ t1 = asyncio.create_task(ble_task())
|
|
|
+ t2 = asyncio.create_task(sensor_task())
|
|
|
+ t3 = asyncio.create_task(write_task())
|
|
|
+ tasks = [t1, t2, t3]
|
|
|
+
|
|
|
+ def handle_stop_signal():
|
|
|
+ print("Received stop signal, cancelling tasks...")
|
|
|
+ for t in tasks:
|
|
|
+ t.cancel()
|
|
|
+
|
|
|
+ loop = asyncio.get_running_loop()
|
|
|
+ for sig in (signal.SIGINT, signal.SIGTERM):
|
|
|
+ loop.add_signal_handler(sig, handle_stop_signal)
|
|
|
+
|
|
|
try:
|
|
|
- t1 = asyncio.create_task(ble_task())
|
|
|
- t2 = asyncio.create_task(sensor_task())
|
|
|
- t3 = asyncio.create_task(write_task())
|
|
|
- await asyncio.gather(t1, t2, t3)
|
|
|
- except asyncio.exceptions.CancelledError:
|
|
|
- pass
|
|
|
-
|
|
|
-asyncio.run(main())
|
|
|
+ await asyncio.gather(*tasks)
|
|
|
+ except asyncio.CancelledError:
|
|
|
+ print("Tasks were cancelled gracefully.")
|
|
|
+ finally:
|
|
|
+ print("Shutdown complete.")
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ asyncio.run(main())
|