Skip to content

Commit

Permalink
added boundary model and validations
Browse files Browse the repository at this point in the history
  • Loading branch information
DraKen0009 committed Dec 28, 2024
1 parent 01c66c3 commit 228dbe6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
37 changes: 37 additions & 0 deletions camera/models/asset_bed_boundary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.db import models
from django.core.exceptions import ValidationError
from jsonschema import validate
from jsonschema.exceptions import ValidationError as JSONSchemaValidationError
from camera.models.json_schema.boundary import ASSET_BED_BOUNDARY_SCHEMA
from care.facility.models import AssetBed
from care.utils.models.base import BaseModel



class AssetBedBoundary(BaseModel):
asset_bed = models.OneToOneField(
AssetBed, on_delete=models.PROTECT, related_name="assetbed_camera_boundary"
)
x0 = models.IntegerField()
y0 = models.IntegerField()
x1 = models.IntegerField()
y1 = models.IntegerField()

def save(self, *args, **kwargs):
if self.asset_bed.asset.asset_class != "ONVIF":
raise ValidationError("AssetBedBoundary is only applicable for AssetBeds with ONVIF assets.")

data = {
"x0": self.x0,
"y0": self.y0,
"x1": self.x1,
"y1": self.y1,
}

try:
validate(instance=data, schema=ASSET_BED_BOUNDARY_SCHEMA)
except JSONSchemaValidationError as e:
error=f"Invalid data: {str(e)}"
raise ValidationError(error)

super().save(*args, **kwargs)
12 changes: 12 additions & 0 deletions camera/models/json_schema/boundary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ASSET_BED_BOUNDARY_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"x0": {"type": "number"},
"y0": {"type": "number"},
"x1": {"type": "number"},
"y1": {"type": "number"},
},
"required": ["x0", "y0", "x1", "y1"],
"additionalProperties": False,
}
14 changes: 14 additions & 0 deletions camera/utils/onvif.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from rest_framework.exceptions import ValidationError

from care.facility.models.bed import AssetBed
from care.utils.assetintegration.base import ActionParams, BaseAssetIntegration


Expand Down Expand Up @@ -30,6 +31,7 @@ def __init__(self, meta):
def handle_action(self, **kwargs: ActionParams):
action_type = kwargs["type"]
action_data = kwargs.get("data", {})
action_options = kwargs.get("options", {})
timeout = kwargs.get("timeout")

request_body = {
Expand All @@ -54,6 +56,18 @@ def handle_action(self, **kwargs: ActionParams):
return self.api_post(self.get_url("absoluteMove"), request_body, timeout)

if action_type == self.OnvifActions.RELATIVE_MOVE.value:
action_asset_bed_id = action_options.get("asset_bed_id")

if action_asset_bed_id:
asset_bed = AssetBed.objects.filter(
external_id=action_asset_bed_id
).first()

if not asset_bed:
raise ValidationError({"asset_bed_id": "Invalid Asset Bed ID"})

if asset_bed.boundary:
request_body.update({"boundary": asset_bed.boundary})
return self.api_post(self.get_url("relativeMove"), request_body, timeout)

if action_type == self.OnvifActions.GET_STREAM_TOKEN.value:
Expand Down

0 comments on commit 228dbe6

Please sign in to comment.