Skip to content

Commit

Permalink
Merge pull request #25 from iRobotEducation/shamlian/catch_and_kill
Browse files Browse the repository at this point in the history
Catch all exit-related exceptions in order to safely shut down the BLE connection
  • Loading branch information
shamlian authored Sep 5, 2023
2 parents 807419c + ceb3d9d commit f1b701a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions irobot_edu_sdk/backend/bluetooth_desktop.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2022 iRobot Corporation. All rights reserved.
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2023 iRobot Corporation. All rights reserved.
#

"""
Expand Down Expand Up @@ -49,10 +49,11 @@ async def connect(self):
self._address = device.address
self._device = device
break
print(f'Connecting to {device.name} ({device.address})')
if self._device:
print(f'Connecting to {device.name} ({device.address})')
self._client = BleakClient(self._device)
else:
print(f'Connecting to {self._address}')
self._client = BleakClient(self._address)

if await self._client.connect():
Expand Down
7 changes: 6 additions & 1 deletion irobot_edu_sdk/robot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2022 iRobot Corporation. All rights reserved.
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2023 iRobot Corporation. All rights reserved.
#

try:
Expand Down Expand Up @@ -92,7 +92,12 @@ def __init__(self, backend: Backend):

self.sound_enabled = True

# Catch all signals and direct to the _exit_handler
signal.signal(signal.SIGINT, _exit_handler)
signal.signal(signal.SIGTSTP, _exit_handler)
signal.signal(signal.SIGQUIT, _exit_handler)
signal.signal(signal.SIGHUP, _exit_handler)
signal.signal(signal.SIGTERM, _exit_handler)

def __enter__(self):
return self
Expand Down

0 comments on commit f1b701a

Please sign in to comment.