Skip to content

Commit

Permalink
Handle error 7 retrying to connect when device is offline
Browse files Browse the repository at this point in the history
  • Loading branch information
pergolafabio committed Oct 1, 2024
1 parent 9a645a9 commit 3bec185
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions hikvision-doorbell/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,22 @@ def signal_handler(task: asyncio.Task):
logger.debug("Received SIGINT, terminating task")
task.cancel()

async def main_loop():
while True:
try:
await main()
break
except SDKError as e:
user_message, sdk_code, sdk_message = e.args
logger.error("{}: {} Error code: {}", user_message, sdk_message, sdk_code)
if sdk_code == 7:
logger.info("Failed to connect to the device, retrying again in 15 seconds...")
await asyncio.sleep(5)
else:
sys.exit(1)
except (OSError, ConnectionRefusedError) as e:
logger.error("Error while connecting to MQTT broker: {}", e.strerror)
sys.exit(1)

if __name__ == "__main__":
try:
asyncio.run(main())
except SDKError as e:
# Define a global error handler for SDKErrors, to print them out in a user-friendly manner:
# <user_message> <sdk_message> <sdk_code>
user_message, sdk_code, sdk_message = e.args
logger.error("{}: {} Error code:{}", user_message, sdk_message, sdk_code)
sys.exit(1)
except (OSError, ConnectionRefusedError) as e:
# Connection to MQTT broker failed
logger.error("Error while connecting to MQTT broker: {}", e.strerror)
sys.exit(1)
asyncio.run(main_loop())

0 comments on commit 3bec185

Please sign in to comment.