From 25f8c16bce463889c8726f3162cfe1c0a56c098b Mon Sep 17 00:00:00 2001 From: RenierM26 <66512715+RenierM26@users.noreply.github.com> Date: Mon, 10 Jul 2023 07:52:14 +0200 Subject: [PATCH] Add retry to set camera defence, improve logging messages. --- pyezviz/camera.py | 4 +--- pyezviz/client.py | 40 ++++++++++++++++++++++++++++++++-------- setup.py | 2 +- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/pyezviz/camera.py b/pyezviz/camera.py index 2de12eb..c7ad02a 100644 --- a/pyezviz/camera.py +++ b/pyezviz/camera.py @@ -131,9 +131,7 @@ def status(self) -> dict[Any, Any]: "PIR_Status": self.fetch_key(["STATUS", "pirStatus"]), "Motion_Trigger": self._alarmmotiontrigger["alarm_trigger_active"], "Seconds_Last_Trigger": self._alarmmotiontrigger["timepassed"], - "last_alarm_time": self._last_alarm.get( - "alarmStartTimeStr", "2000-01-01 00:00:00" - ), + "last_alarm_time": self._last_alarm.get("alarmStartTimeStr"), "last_alarm_pic": self._last_alarm.get( "picUrl", "https://eustatics.ezvizlife.com/ovs_mall/web/img/index/EZVIZ_logo.png?ver=3007907502", diff --git a/pyezviz/client.py b/pyezviz/client.py index 5a15b18..96d5afd 100644 --- a/pyezviz/client.py +++ b/pyezviz/client.py @@ -567,7 +567,18 @@ def set_camera_defence( ) from err if json_output["meta"]["code"] != 200: - raise PyEzvizError(f"Could not arm or disarm Camera: Got {json_output})") + if json_output["meta"]["code"] == 504: + _LOGGER.warning( + "Arm or disarm for camera %s timed out. Retrying %s of %s", + serial, + max_retries, + MAX_RETRIES, + ) + return self.set_camera_defence(serial, enable, max_retries + 1) + + raise PyEzvizError( + f"Could not arm or disarm Camera {serial}: Got {json_output})" + ) return True @@ -735,9 +746,10 @@ def get_storage_status(self, serial: str, max_retries: int = 0) -> Any: if json_output["resultCode"] != "0": if json_output["resultCode"] == "-1": _LOGGER.warning( - "Can't get storage status from device %s, retrying %s", + "Can't get storage status from device %s, retrying %s of %s", serial, max_retries, + MAX_RETRIES, ) return self.get_storage_status(serial, max_retries + 1) raise PyEzvizError( @@ -954,9 +966,10 @@ def reboot_camera( if json_output["resultCode"] != "0": if json_output["resultCode"] == "-1": _LOGGER.warning( - "Unable to reboot camera, camera %s is unreachable, retrying %s", + "Unable to reboot camera, camera %s is unreachable, retrying %s of %s", serial, max_retries, + MAX_RETRIES, ) return self.reboot_camera(serial, delay, operation, max_retries + 1) raise PyEzvizError(f"Could not reboot device {json_output})") @@ -1225,9 +1238,10 @@ def get_cam_key( if json_output["resultCode"] != "0": if json_output["resultCode"] == "-1": _LOGGER.warning( - "Camera %s encryption key not found, retrying %s", + "Camera %s encryption key not found, retrying %s of %s", serial, max_retries, + MAX_RETRIES, ) return self.get_cam_key(serial, smscode, max_retries + 1) raise PyEzvizError( @@ -1321,7 +1335,10 @@ def return_panoramic(self, serial: str, max_retries: int = 0) -> Any: if json_output["resultCode"] != "0": if json_output["resultCode"] == "-1": _LOGGER.warning( - "Camera %s busy or unreachable, retrying %s", serial, max_retries + "Camera %s busy or unreachable, retrying %s of %s", + serial, + max_retries, + MAX_RETRIES, ) return self.return_panoramic(serial, max_retries + 1) raise PyEzvizError(f"Could retrieve panoramic photo: Got {json_output})") @@ -1528,7 +1545,10 @@ def api_set_defence_schedule( if json_output["resultCode"] != "0": if json_output["resultCode"] == "-1": _LOGGER.warning( - "Camara %s offline or unreachable, retrying %s", serial, max_retries + "Camara %s offline or unreachable, retrying %s of %s", + serial, + max_retries, + MAX_RETRIES, ) return self.api_set_defence_schedule( serial, schedule, enable, max_retries + 1 @@ -1727,9 +1747,10 @@ def detection_sensibility( if response_json["resultCode"] != "0": if response_json["resultCode"] == "-1": _LOGGER.warning( - "Camera %s is offline, can't set sensitivity, retrying %s", + "Camera %s is offline or unreachable, can't set sensitivity, retrying %s of %s", serial, max_retries, + MAX_RETRIES, ) return self.detection_sensibility( serial, sensibility, type_value, max_retries + 1 @@ -1779,7 +1800,10 @@ def get_detection_sensibility( if response_json["resultCode"] != "0": if response_json["resultCode"] == "-1": _LOGGER.warning( - "Camera %s is offline, retrying %s", serial, max_retries + "Camera %s is offline or unreachable, retrying %s of %s", + serial, + max_retries, + MAX_RETRIES, ) return self.get_detection_sensibility( serial, type_value, max_retries + 1 diff --git a/setup.py b/setup.py index eb75735..6fd7185 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='pyezviz', - version="0.2.2.1", + version="0.2.2.2", license='Apache Software License 2.0', author='Pierre Ourdouille', author_email='baqs@users.github.com',