From a46e3e15c057f66e19c84ef4d71c5e72e2985d7d Mon Sep 17 00:00:00 2001 From: slespersen Date: Thu, 19 Dec 2024 18:56:27 +0100 Subject: [PATCH] Updated BLEManager.scan to handle UUIDs for newer revisions of the board. --- evseMQTT/ble_manager.py | 30 +++++++++++++++++++++++++----- evseMQTT/constants.py | 5 +++++ main.py | 2 +- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/evseMQTT/ble_manager.py b/evseMQTT/ble_manager.py index ec3dbd9..71933d3 100644 --- a/evseMQTT/ble_manager.py +++ b/evseMQTT/ble_manager.py @@ -16,6 +16,9 @@ def __init__(self, event_handler, logger, callback=None): self.message_timeout = 35 # 35 seconds timeout for message reception self.max_retries = 5 # Maximum number of retries for connection + self.write_uuid = "" + self.read_uuid = "" + # Ensure bleak does not go bananas, if we set logging to DEBUG self.logger_bleak = logging.getLogger("bleak") self.logger_bleak.setLevel(logging.INFO) @@ -23,12 +26,29 @@ def __init__(self, event_handler, logger, callback=None): async def scan(self): self.logger.info("Scanning for evse BLE devices...") try: - devices = await BleakScanner.discover() - self.available_devices = {dev.address: dev for dev in devices if "ACP#" in dev.name} - for address, device in self.available_devices.items(): + devices = await BleakScanner.discover(return_adv=True) + + # Filter devices with "ACP#" in their name + self.available_devices = {dev.address: (dev, adv_data) for dev, adv_data in devices.values() if "ACP#" in dev.name} + for address, (device, adv_data) in self.available_devices.items(): self.logger.info(f"Found device: {device.name} ({address})") self.connectiondata[address] = device + + if any("0000fff0" in uuid for uuid in adv_data.service_uuids): + self.logger.info(f"Device {device.name} ({device.address}) matches UUIDs for Old Board") + + self.write_uuid = Constants.WRITE_UUID + self.read_uuid = Constants.READ_UUID + elif any("0000ffe4" in uuid for uuid in adv_data.service_uuids): + self.logger.info(f"Device {device.name} ({device.address}) matches UUIDs for New Board") + + self.write_uuid = Constants.NEW_BOARD_WRITE_UUID + self.read_uuid = Constants.NEW_BOARD_READ_UUID + else: + self.logger.info(f"No board identified!? Service UUIDs: {adv_data.service_uuids}") + return self.available_devices + except BleakError as e: await self.manager.exit_with_error(f"BleakError during scanning: {e}") @@ -42,7 +62,7 @@ async def connect_device(self, address): self.connected_devices[address] = client self.logger.info(f"Connected to {address}") - await self.start_notifications(address, Constants.READ_UUID) + await self.start_notifications(address, self.read_uuid) self._schedule_reconnect_check() return True except BleakError as e: @@ -77,7 +97,7 @@ async def disconnect_device(self, address): client = self.connected_devices[address] if client.is_connected: - await client.stop_notify(Constants.READ_UUID) + await client.stop_notify(self.read_uuid) await client.disconnect() del self.connected_devices[address] diff --git a/evseMQTT/constants.py b/evseMQTT/constants.py index 9b61762..a36a3c0 100644 --- a/evseMQTT/constants.py +++ b/evseMQTT/constants.py @@ -3,6 +3,11 @@ class Constants: WRITE_UUID = "0000fff2-0000-1000-8000-00805f9b34fb" READ_UUID = "0000fff1-0000-1000-8000-00805f9b34fb" + # NEW BOARD BLE UUIDs + NEW_BOARD_WRITE_UUID = "0000ffe9-0000-1000-8000-00805f9b34fb" + NEW_BOARD_READ_UUID = "0000ffe4-0000-1000-8000-00805f9b34fb" + + ERRORS = { 0: "Relay Stick Error", 1: "Relay Stick Error", diff --git a/main.py b/main.py index 11def8b..618d854 100644 --- a/main.py +++ b/main.py @@ -48,7 +48,7 @@ async def run(self, address): self.logger.info(f"Connected.") # Start the producer and consumer tasks - consumer = asyncio.create_task(self.ble_manager.message_consumer(address, Constants.WRITE_UUID)) + consumer = asyncio.create_task(self.ble_manager.message_consumer(address, self.ble_manager.write_uuid)) try: self.logger.info("Waiting for device initialization...")