Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework import filters as drf_filters
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework.exceptions import ValidationError, APIException
from rest_framework.mixins import (
CreateModelMixin,
ListModelMixin,
Expand Down Expand Up @@ -185,17 +185,24 @@ def operate_assets(self, request, *args, **kwargs):
This API is used to operate assets. API accepts the asset_id and action as parameters.
"""
try:
if "action" not in request.data:
raise ValidationError({"action": "is required"})
action = request.data["action"]
if "type" not in action:
raise ValidationError({"type": "missing action type"})
asset: Asset = self.get_object()
asset_class: BaseAssetIntegration = AssetClasses[asset.asset_class].value(asset.meta)
asset_class: BaseAssetIntegration = AssetClasses[asset.asset_class].value(
asset.meta)
result = asset_class.handle_action(action)
return Response({"result": result}, status=status.HTTP_200_OK)

except ValidationError as e:
return Response({"message": e.detail}, status=status.HTTP_400_BAD_REQUEST)

except KeyError as e:
return Response({
"message": dict((key, "is required") for key in e.args)
}, status=status.HTTP_400_BAD_REQUEST)

except APIException as e:
return Response(e.detail, e.status_code)

except Exception as e:
print(f"error: {e}")
return Response(
Expand Down
17 changes: 12 additions & 5 deletions care/utils/assetintegration/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json

import requests
from rest_framework.exceptions import APIException


class BaseAssetIntegration:
Expand All @@ -17,14 +19,19 @@ def get_url(self, endpoint):
def api_post(self, url, data=None):
req = requests.post(url, json=data)
try:
return req.json()
except json.decoder.JSONDecodeError:
response = req.json()
if req.status_code >= 400:
raise APIException(response, req.status_code)
return response
except json.decoder.JSONDecodeError as e:
return {"error": "Invalid Response"}

def api_get(self, url, data=None):
req = requests.get(url, params=data)
try:
return req.json()
except Exception as e:
response = req.json()
if req.status_code >= 400:
raise APIException(response, req.status_code)
return response
except json.decoder.JSONDecodeError as e:
return {"error": "Invalid Response"}

49 changes: 43 additions & 6 deletions care/utils/assetintegration/hl7monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,51 @@ class HL7MonitorAsset(BaseAssetIntegration):

class HL7MonitorActions(enum.Enum):
GET_VITALS = "get_vitals"
GET_CAMERA_STATUS = "get_status"
GET_PRESETS = "get_presets"
GOTO_PRESET = "goto_preset"
ABSOLUTE_MOVE = "absolute_move"
RELATIVE_MOVE = "relative_move"

def __init__(self, meta):
super().__init__(meta)
try:
super().__init__(meta)
self.port = self.meta["port"]
self.username = self.meta["username"]
self.password = self.meta["password"]
except KeyError as e:
raise ValidationError(
dict((key, f"{key} not found in asset metadata") for key in e.args))

def handle_action(self, action):
if action["type"] == self.HL7MonitorActions.GET_VITALS.value:
request_params = { "device_id": self.host }
return self.api_get(self.get_url("vitals"), data=request_params)
else:
raise ValidationError({"action": "invalid action type"})
action_type = action["type"]
action_data = action.get("data", {})

request_body = {
"hostname": self.host,
"port": self.port,
"username": self.username,
"password": self.password,
**action_data
}

if action_type == self.HL7MonitorActions.GET_VITALS.value:
request_params = {"device_id": self.host}
return self.api_get(self.get_url("vitals"), request_params)

if action_type == self.HL7MonitorActions.GET_CAMERA_STATUS.value:
return self.api_get(self.get_url("status"), request_body)

if action_type == self.HL7MonitorActions.GET_PRESETS.value:
return self.api_get(self.get_url("presets"), request_body)

if action_type == self.HL7MonitorActions.GOTO_PRESET.value:
return self.api_post(self.get_url("gotoPreset"), request_body)

if action_type == self.HL7MonitorActions.ABSOLUTE_MOVE.value:
return self.api_post(self.get_url("absoluteMove"), request_body)

if action_type == self.HL7MonitorActions.RELATIVE_MOVE.value:
return self.api_post(self.get_url("relativeMove"), request_body)

raise ValidationError({"action": "invalid action type"})
14 changes: 9 additions & 5 deletions care/utils/assetintegration/onvif.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ class OnvifActions(enum.Enum):
GOTO_PRESET = "goto_preset"

def __init__(self, meta):
super().__init__(meta)
self.name = self.meta["camera_type"]
self.port = self.meta["camera_port"] or 80
self.username = self.meta["camera_access_key"].split(":")[0]
self.password = self.meta["access_credentials"].split(":")[1]
try:
super().__init__(meta)
self.name = self.meta["camera_type"]
self.port = self.meta["camera_port"] or 80
self.username = self.meta["camera_access_key"].split(":")[0]
self.password = self.meta["access_credentials"].split(":")[1]
except KeyError as e:
raise ValidationError(
dict((key, f"{key} not found in asset metadata") for key in e.args))

def handle_action(self, action):
if action["type"] == self.OnvifActions.MOVE_ABSOLUTE.value:
Expand Down