From fa67d9e21a666796fa33e4598880e61c77a75f0a Mon Sep 17 00:00:00 2001 From: Danny Staple Date: Thu, 19 Jan 2023 23:12:21 +0000 Subject: [PATCH] Make a nicer error when the robot isn't found. --- ch-13/2.3-bluetooth/find_devices.py | 3 +-- ch-13/2.3-bluetooth/robot_ble_connection.py | 7 ++++--- ch-13/3.1-displaying-a-pose/robot_ble_connection.py | 11 +++++++---- ch-13/3.2-collision-avoidance/robot_ble_connection.py | 11 +++++++---- .../3.3-moving-with-encoders/robot_ble_connection.py | 11 +++++++---- ch-13/4.1-generating-weights/robot_ble_connection.py | 11 +++++++---- 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/ch-13/2.3-bluetooth/find_devices.py b/ch-13/2.3-bluetooth/find_devices.py index 316c65b..44c2468 100644 --- a/ch-13/2.3-bluetooth/find_devices.py +++ b/ch-13/2.3-bluetooth/find_devices.py @@ -9,8 +9,7 @@ async def run(): print([device.name for device in devices]) matching_devices = [device for device in devices if device.name==ble_name] if len(matching_devices) == 0: - print(f"Could not find robot at {ble_name}") - return + raise RuntimeError("Could not find robot") ble_device_info = matching_devices[0] print(f"Found robot {ble_device_info.name}...") diff --git a/ch-13/2.3-bluetooth/robot_ble_connection.py b/ch-13/2.3-bluetooth/robot_ble_connection.py index 59953bd..91d37b1 100644 --- a/ch-13/2.3-bluetooth/robot_ble_connection.py +++ b/ch-13/2.3-bluetooth/robot_ble_connection.py @@ -22,9 +22,10 @@ class BleConnection: devices = await bleak.BleakScanner.discover( service_uuids=[self.ble_uuid] ) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect() diff --git a/ch-13/3.1-displaying-a-pose/robot_ble_connection.py b/ch-13/3.1-displaying-a-pose/robot_ble_connection.py index 66dd100..91d37b1 100644 --- a/ch-13/3.1-displaying-a-pose/robot_ble_connection.py +++ b/ch-13/3.1-displaying-a-pose/robot_ble_connection.py @@ -19,10 +19,13 @@ class BleConnection: async def connect(self): print("Scanning for devices...") - devices = await bleak.BleakScanner.discover(service_uuids=[self.ble_uuid]) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + devices = await bleak.BleakScanner.discover( + service_uuids=[self.ble_uuid] + ) + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect() diff --git a/ch-13/3.2-collision-avoidance/robot_ble_connection.py b/ch-13/3.2-collision-avoidance/robot_ble_connection.py index 66dd100..91d37b1 100644 --- a/ch-13/3.2-collision-avoidance/robot_ble_connection.py +++ b/ch-13/3.2-collision-avoidance/robot_ble_connection.py @@ -19,10 +19,13 @@ class BleConnection: async def connect(self): print("Scanning for devices...") - devices = await bleak.BleakScanner.discover(service_uuids=[self.ble_uuid]) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + devices = await bleak.BleakScanner.discover( + service_uuids=[self.ble_uuid] + ) + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect() diff --git a/ch-13/3.3-moving-with-encoders/robot_ble_connection.py b/ch-13/3.3-moving-with-encoders/robot_ble_connection.py index 66dd100..91d37b1 100644 --- a/ch-13/3.3-moving-with-encoders/robot_ble_connection.py +++ b/ch-13/3.3-moving-with-encoders/robot_ble_connection.py @@ -19,10 +19,13 @@ class BleConnection: async def connect(self): print("Scanning for devices...") - devices = await bleak.BleakScanner.discover(service_uuids=[self.ble_uuid]) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + devices = await bleak.BleakScanner.discover( + service_uuids=[self.ble_uuid] + ) + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect() diff --git a/ch-13/4.1-generating-weights/robot_ble_connection.py b/ch-13/4.1-generating-weights/robot_ble_connection.py index 66dd100..91d37b1 100644 --- a/ch-13/4.1-generating-weights/robot_ble_connection.py +++ b/ch-13/4.1-generating-weights/robot_ble_connection.py @@ -19,10 +19,13 @@ class BleConnection: async def connect(self): print("Scanning for devices...") - devices = await bleak.BleakScanner.discover(service_uuids=[self.ble_uuid]) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + devices = await bleak.BleakScanner.discover( + service_uuids=[self.ble_uuid] + ) + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect()