Skip to content

Commit

Permalink
Improved verification of repair
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed Sep 18, 2022
1 parent c585e9f commit 2bb42ca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
44 changes: 36 additions & 8 deletions custom_components/shinobi/component/api/shinobi_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ async def _async_get(self, endpoint, resource_available_check: bool = False):
return result

async def async_update(self):
_LOGGER.info(f"Updating data from Shinobi Video Server ({self.config_data.host})")
_LOGGER.debug(f"Updating data from Shinobi Video Server ({self.config_data.host})")

if self.status == ConnectivityStatus.Failed:
await self.initialize()
Expand Down Expand Up @@ -413,19 +413,28 @@ async def _async_repair_monitor(self, monitor_id: str):

await self.async_set_monitor_mode(monitor_id, MONITOR_MODE_STOP)

await sleep(5)
await sleep(REPAIR_REPAIR_RECORD_INTERVAL)

await self.async_set_monitor_mode(monitor_id, MONITOR_MODE_RECORD)

await sleep(15)
for index in range(REPAIR_UPDATE_STATUS_ATTEMPTS):
await sleep(REPAIR_UPDATE_STATUS_INTERVAL)

monitor = self.monitors.get(monitor_id)
await self._async_update_monitor_details(monitor_id)

if monitor.should_repair:
_LOGGER.warning(f"Unable to repair monitor: {monitor_id}")
monitor = self.monitors.get(monitor_id)

else:
_LOGGER.info(f"Monitor {monitor_id} is repaired")
if self.status != ConnectivityStatus.Connected:
status_message = ConnectivityStatus.get_log_level(self.status)
_LOGGER.warning(f"Stopped sampling status for {monitor_id}, Reason: {status_message}")
break

if not monitor.should_repair:
_LOGGER.info(f"Monitor {monitor_id} is repaired, Attempt #{index + 1}")
break

if monitor.should_repair and self.status == ConnectivityStatus.Connected:
_LOGGER.warning(f"Unable to repair monitor {monitor_id}, Attempts: {REPAIR_UPDATE_STATUS_ATTEMPTS}")

except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
Expand Down Expand Up @@ -490,3 +499,22 @@ async def _async_set_detection_mode(self, monitor_id: str, detector: str, enable
_LOGGER.info(f"{response_message} for {monitor_id}")
else:
_LOGGER.warning(f"{response_message} for {monitor_id}")

async def _async_update_monitor_details(self, monitor_id: str):
_LOGGER.debug(f"Updating monitor details for {monitor_id}")

if self.status == ConnectivityStatus.Connected:
url = f"{URL_MONITORS}/{monitor_id}"

response: dict = await self._async_get(url)
monitor_data = response[0]

monitor_details_str = monitor_data.get(ATTR_MONITOR_DETAILS)
details = json.loads(monitor_details_str)

monitor_data[ATTR_MONITOR_DETAILS] = details

monitor_data = MonitorData(monitor_data)

if monitor_data is not None:
self.monitors[monitor_data.id] = monitor_data
21 changes: 12 additions & 9 deletions custom_components/shinobi/component/helpers/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
MAX_MSG_SIZE = 0
DISCONNECT_INTERVAL = 5
RECONNECT_INTERVAL = 30
REPAIR_REPAIR_RECORD_INTERVAL = 5
REPAIR_UPDATE_STATUS_INTERVAL = 10
REPAIR_UPDATE_STATUS_ATTEMPTS = 12 # Up to 2 minutes of retries

DISCOVERY = f"{DOMAIN}_discovery"

Expand Down Expand Up @@ -71,15 +74,15 @@
LOGIN_PASSWORD = "pass"

DEFAULT_ACCESS_DETAILS = {
"auth_socket": False,
"get_monitors": True,
"control_monitors": False,
"get_logs": False,
"watch_stream": True,
"watch_snapshot": True,
"watch_videos": True,
"delete_videos": False
}
"auth_socket": False,
"get_monitors": True,
"control_monitors": False,
"get_logs": False,
"watch_stream": True,
"watch_snapshot": True,
"watch_videos": True,
"delete_videos": False
}

ATTR_MONITOR_ID = "mid"
ATTR_MONITOR_GROUP_ID = "ke"
Expand Down

0 comments on commit 2bb42ca

Please sign in to comment.