Skip to content

Commit

Permalink
Updated BLEManager.scan to handle UUIDs for newer revisions of the bo…
Browse files Browse the repository at this point in the history
…ard.
  • Loading branch information
slespersen committed Dec 19, 2024
1 parent e6027fa commit a46e3e1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
30 changes: 25 additions & 5 deletions evseMQTT/ble_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,39 @@ 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)

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}")

Expand All @@ -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:
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions evseMQTT/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down

0 comments on commit a46e3e1

Please sign in to comment.