From 354d218da6ca31adbdf61c3d7c4126f569df5c43 Mon Sep 17 00:00:00 2001 From: Jonas Remmert Date: Wed, 5 Jun 2024 15:22:22 +0200 Subject: [PATCH] django: migrate to new ReST API endpoint - Adapt serializer to new format Signed-off-by: Jonas Remmert --- server/django/sensordata/serializers.py | 39 ++++++++++++------------- server/django/sensordata/urls.py | 4 +-- server/django/sensordata/views.py | 10 +++---- server/django/server/urls.py | 2 +- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/server/django/sensordata/serializers.py b/server/django/sensordata/serializers.py index 303d877e..93fb3440 100644 --- a/server/django/sensordata/serializers.py +++ b/server/django/sensordata/serializers.py @@ -50,46 +50,43 @@ class ValueSerializer(serializers.Serializer): class LwM2MSerializer(serializers.Serializer): ep = serializers.CharField(max_length=255) - res = serializers.CharField(max_length=255) + obj_id = serializers.IntegerField(required=False) val = ValueSerializer() + def create(self, validated_data): ep = validated_data['ep'] - res = validated_data['res'] val = validated_data['val'] # ep maps to Device.endpoint device, _ = Device.objects.get_or_create(endpoint=ep) - # Check if value is an object with instances - if val['kind'] == 'obj' and 'instances' in val: + # Check if value is an object with instances (Composite resource) + if val['kind'] == 'obj': + obj_id = val.get('id') for instance in val['instances']: for resource in instance['resources']: - self.handle_resource(device, res, resource) + pass + self.handle_resource(device, obj_id, resource) else: # Single resource handling - self.handle_resource(device, res, val) + try: + obj_id = validated_data.get('obj_id') + except KeyError as e: + logger.error(f"Missing required fields: {e}") + raise serializers.ValidationError(f"Missing required fields: {e}") + self.handle_resource(device, obj_id, val) return device - def handle_resource(self, device, res, resource): - # Parse resource path to get object_id and resource_id - resource_path_parts = res.strip('/').split('/') - if len(resource_path_parts) == 3: - object_id = int(resource_path_parts[0]) - resource_id = int(resource_path_parts[2]) - elif resource_path_parts[0].isdigit(): - object_id = int(resource_path_parts[0]) - resource_id = resource['id'] - else: - logger.error(f"Invalid resource path: {res}") - raise serializers.ValidationError("Invalid resource path") + def handle_resource(self, device, obj_id, resource): + res_id = resource['id'] # Fetch resource information from Database - resource_type = ResourceType.objects.get(object_id=object_id, - resource_id=resource_id) + resource_type = ResourceType.objects.get(object_id=obj_id, + resource_id=res_id) if not resource_type: - raise serializers.ValidationError(f"Resource type {object_id}/{resource_id} not found") + raise serializers.ValidationError(f"Resource type {obj_id}/{res_id} not found") logger.debug(f"Adding resource_type: {resource_type}") data_type = resource_type.data_type diff --git a/server/django/sensordata/urls.py b/server/django/sensordata/urls.py index 9cd5eb55..74ed813d 100644 --- a/server/django/sensordata/urls.py +++ b/server/django/sensordata/urls.py @@ -1,6 +1,6 @@ from django.urls import path -from .views import CreateSensorDataView +from .views import PostResourceView urlpatterns = [ - path('endpointdata/', CreateSensorDataView.as_view(), name='add_sensor_data'), + path('resource', PostResourceView.as_view(), name='post_resource'), ] diff --git a/server/django/sensordata/views.py b/server/django/sensordata/views.py index e5d7af3b..f327ffb4 100644 --- a/server/django/sensordata/views.py +++ b/server/django/sensordata/views.py @@ -7,14 +7,12 @@ logger = logging.getLogger(__name__) -class CreateSensorDataView(APIView): - def post(self, request): +class PostResourceView(APIView): + def post(self, request): serializer = LwM2MSerializer(data=request.data, many=False) if serializer.is_valid(): serializer.save() return Response(serializer.validated_data, status=status.HTTP_201_CREATED) - else: - print(serializer.errors) - logger.error(f"Errors: {serializer.errors}") - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + logger.error(serializer.errors) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) diff --git a/server/django/server/urls.py b/server/django/server/urls.py index eda1fa41..d0a4d8c7 100644 --- a/server/django/server/urls.py +++ b/server/django/server/urls.py @@ -3,5 +3,5 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('api/', include('sensordata.urls')), + path('leshan_api/', include('sensordata.urls')), ]