Skip to content

Commit

Permalink
Updated Manager.exit_with_error and all calls, to write the error. 'D…
Browse files Browse the repository at this point in the history
…evice <address> not found' will now cause the script to exit.
  • Loading branch information
slespersen committed Dec 15, 2024
1 parent 7548912 commit 7b2e142
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
11 changes: 4 additions & 7 deletions evseMQTT/ble_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ async def scan(self):
self.connectiondata[address] = device
return self.available_devices
except BleakError as e:
self.logger.error(f"BleakError during scanning: {e}")
await self.manager.exit_with_error()
await self.manager.exit_with_error(f"BleakError during scanning: {e}")

async def connect_device(self, address):
if address in self.available_devices:
Expand All @@ -47,16 +46,14 @@ async def connect_device(self, address):
self._schedule_reconnect_check()
return True
except BleakError as e:
self.logger.error(f"Attempt {attempt + 1} failed with BleakError: {e}")
await self.manager.exit_with_error()
await self.manager.exit_with_error(f"Attempt {attempt + 1} failed with BleakError: {e}")
except Exception as e:
self.logger.error(f"Attempt {attempt + 1} failed with error: {e}")
await asyncio.sleep(2) # Wait a bit before retrying
self.logger.error(f"Failed to connect to {address} after {self.max_retries} attempts")
await self.manager.exit_with_error()
await self.manager.exit_with_error(f"Failed to connect to {address} after {self.max_retries} attempts")
return False
else:
self.logger.error(f"Device {address} not found")
await self.manager.exit_with_error(f"Device {address} not found")
return False

async def start_notifications(self, address, characteristic_uuid):
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ async def restart_run(self, address=None):

await self.run(address)

async def exit_with_error(self):
self.logger.error("Exiting due to repeated connection failures.")
async def exit_with_error(self, error):
self.logger.error(f"Error encountered:\n{error}")
# Perform necessary cleanup actions
if self.mqtt_client:
self.mqtt_client.publish_availability(self.device.info['serial'], "offline")
Expand Down

0 comments on commit 7b2e142

Please sign in to comment.