Skip to content

Commit

Permalink
update asset integration actions (#909)
Browse files Browse the repository at this point in the history
* minor bug fix for onvif asset integration

* update asset integration actions

* allow using insecure connections for asset integration
  • Loading branch information
iamsdas authored Jul 7, 2022
1 parent acc53b7 commit 0247b81
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 60 deletions.
7 changes: 6 additions & 1 deletion care/utils/assetintegration/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

import requests
from django.conf import settings
from rest_framework.exceptions import APIException


Expand All @@ -9,12 +10,16 @@ def __init__(self, meta):
self.meta = meta
self.host = self.meta["local_ip_address"]
self.middleware_hostname = self.meta["middleware_hostname"]
self.insecure_connection = self.meta.get("insecure_connection", False)

def handle_action(self, action):
pass

def get_url(self, endpoint):
return "http://{}/{}".format(self.middleware_hostname, endpoint)
protocol = "http"
if not self.insecure_connection or settings.IS_PRODUCTION:
protocol += "s"
return f"{protocol}://{self.middleware_hostname}/{endpoint}"

def api_post(self, url, data=None):
req = requests.post(url, json=data)
Expand Down
32 changes: 0 additions & 32 deletions care/utils/assetintegration/hl7monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,19 @@ 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):
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):
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"})
62 changes: 35 additions & 27 deletions care/utils/assetintegration/onvif.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,49 @@ class OnvifAsset(BaseAssetIntegration):
_name = "onvif"

class OnvifActions(enum.Enum):
MOVE_ABSOLUTE = "move_absolute"
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):
try:
super().__init__(meta)
self.name = self.meta["camera_type"]
self.port = self.meta["camera_port"] or 80
self.port = self.meta["port"]
self.username = self.meta["camera_access_key"].split(":")[0]
self.password = self.meta["access_credentials"].split(":")[1]
self.password = self.meta["camera_access_key"].split(":")[1]
self.access_key = self.meta["camera_access_key"].split(":")[2]
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:
# Make API Call for action
request_data = {
"x": action.data["x"],
"y": action.data["y"],
"z": action.data["z"],
"speed": action.data["speed"],
"meta": self.meta,
}
return self.api_post(self.get_url("absoluteMove"), data=request_data)

elif action["type"] == self.OnvifActions.GOTO_PRESET.value:
# Make API Call for action
request_data = {
"preset": action.preset,
"meta": self.meta,
}
return self.api_post(self.get_url("gotoPreset"), data=request_data)
elif action["type"] == self.OnvifActions.GOTO_PRESET.value:
# Make API Call for action
return self.api_get(self.get_url("status"), data={})
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,
"accessKey": self.access_key,
**action_data
}

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

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

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

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

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

raise ValidationError({"action": "invalid action type"})

0 comments on commit 0247b81

Please sign in to comment.