Skip to content

Commit

Permalink
Add monitoring for Cameras (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashesh3 authored Jul 28, 2023
1 parent 56471da commit 7dfbef0
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions care/facility/tasks/asset_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any

from celery import shared_task
from django.db.models import Q
from django.utils import timezone

from care.facility.models.asset import (
Expand All @@ -22,6 +23,7 @@ def check_asset_status():

assets = Asset.objects.all()
middleware_status_cache = {}
middleware_camera_status_cache = {}

for asset in assets:
# Skipping if asset class or local IP address is not present
Expand All @@ -36,7 +38,12 @@ def check_asset_status():
result: Any = None

# Checking if middleware status is already cached
if hostname in middleware_status_cache:
if (
hostname in middleware_camera_status_cache
and asset.asset_class == "ONVIF"
):
result = middleware_camera_status_cache[hostname]
elif hostname in middleware_status_cache and asset.asset_class != "ONVIF":
result = middleware_status_cache[hostname]
else:
try:
Expand All @@ -50,15 +57,47 @@ def check_asset_status():
}
)
# Fetching the status of the device
result = asset_class.api_get(asset_class.get_url("devices/status"))
if asset.asset_class == "ONVIF":
similar_assets = Asset.objects.filter(
asset_class="ONVIF"
).filter(
Q(meta__middleware_hostname=hostname)
| Q(current_location__facility__middleware_address=hostname)
)
assets_config = []
for asset in similar_assets:
try:
asset_config = asset.meta["camera_access_key"].split(
":"
)
assets_config.append(
{
"hostname": asset.meta.get("local_ip_address"),
"port": 80,
"username": asset_config[0],
"password": asset_config[1],
}
)
except Exception:
pass
result = asset_class.api_post(
asset_class.get_url("cameras/status"), data=assets_config
)
else:
result = asset_class.api_get(
asset_class.get_url("devices/status")
)
except Exception:
logger.warn(f"Middleware {hostname} is down", exc_info=True)

# If no status is returned, setting default status as down
if not result:
result = [{"time": timezone.now().isoformat(), "status": []}]

middleware_status_cache[hostname] = result
if asset.asset_class == "ONVIF":
middleware_camera_status_cache[hostname] = result
else:
middleware_status_cache[hostname] = result

# Setting new status as down by default
new_status = AvailabilityStatus.DOWN
Expand Down

0 comments on commit 7dfbef0

Please sign in to comment.