Skip to content

Commit

Permalink
Keep retrying when connection to doorbell fails
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed Sep 30, 2024
1 parent 9a645a9 commit ddf9abe
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions hikvision-doorbell/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from input import InputReader


def signal_handler(task: asyncio.Task):
logger.debug("Received SIGINT, terminating task")
task.cancel()


async def main():
"""Main entrypoint of the application"""

Expand Down Expand Up @@ -85,22 +90,24 @@ async def main():
logger.info("Shutting down")
shutdownSDK(sdk)


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.error("Retrying in 15 seconds...")
await asyncio.sleep(15)
else:
break
except (OSError, ConnectionRefusedError) as e:
logger.error("Error while connecting to MQTT broker: {}", e.strerror)
break
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 ddf9abe

Please sign in to comment.