Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added boundary model and validations #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions .github/workflows/test-plugin-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@v3
with:
repository: DraKen0009/care
ref: adding-camera-plugin
ref: camera-boundary-changes
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the branch name according to the respective branch on core app.

This will be fixed once we have merged the plugin PR in core care



# Update the plug_config.py file with the required content
Expand All @@ -33,14 +33,14 @@ jobs:
cat > ./plug_config.py <<EOL
from plugs.manager import PlugManager
from plugs.plug import Plug

camera_plugin = Plug(
name="camera",
package_name="git+https://github.com/ohcnetwork/care_camera_asset.git",
version="@${BRANCH_NAME}",
configs={},
)

plugs = [camera_plugin]
manager = PlugManager(plugs)
EOL
Expand All @@ -53,4 +53,4 @@ jobs:
# Run Django management command tests
- name: Run Django management command tests
run: |
docker compose exec backend bash -c "python manage.py test camera --keepdb --parallel --shuffle"
docker compose exec backend bash -c "python manage.py test camera --keepdb --parallel --shuffle"
2 changes: 1 addition & 1 deletion camera/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('facility', '0468_alter_asset_asset_class_alter_asset_meta'),
('facility', '0469_alter_asset_asset_class_alter_asset_meta'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

Expand Down
34 changes: 34 additions & 0 deletions camera/migrations/0004_assetbedboundary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 5.1.3 on 2024-12-28 18:51

import django.db.models.deletion
import uuid
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('camera', '0003_alter_positionpreset_created_date_and_more'),
('facility', '0469_alter_asset_asset_class_alter_asset_meta'),
]

operations = [
migrations.CreateModel(
name='AssetBedBoundary',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('external_id', models.UUIDField(db_index=True, default=uuid.uuid4, unique=True)),
('created_date', models.DateTimeField(auto_now_add=True, db_index=True, null=True)),
('modified_date', models.DateTimeField(auto_now=True, db_index=True, null=True)),
('deleted', models.BooleanField(db_index=True, default=False)),
('x0', models.IntegerField()),
('y0', models.IntegerField()),
('x1', models.IntegerField()),
('y1', models.IntegerField()),
('asset_bed', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='assetbed_camera_boundary', to='facility.assetbed')),
],
options={
'abstract': False,
},
),
]
1 change: 1 addition & 0 deletions camera/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from camera.models.asset_bed_boundary import AssetBedBoundary
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

importing it, so that migrations can be created

48 changes: 48 additions & 0 deletions camera/models/asset_bed_boundary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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":
error="AssetBedBoundary is only applicable for AssetBeds with ONVIF assets."
raise ValidationError(error)

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) from e

super().save(*args, **kwargs)


def to_dict(self):
"""Serialize boundary data as a dictionary."""
return {
"x0": self.x0,
"y0": self.y0,
"x1": self.x1,
"y1": self.y1,
}
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,
}
15 changes: 15 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,19 @@ 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 hasattr(asset_bed, "assetbed_camera_boundary"):
boundary = asset_bed.assetbed_camera_boundary.to_dict()
request_body.update({"boundary": boundary})
return self.api_post(self.get_url("relativeMove"), request_body, timeout)

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