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
2 changes: 0 additions & 2 deletions apps/device/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.db import transaction
from django_tenants.utils import schema_context

from apps.device_model.services import create_default_device_models
from apps.network_server.services import create_network_servers


Expand All @@ -11,4 +10,3 @@ class NewOrganizationHandler(NewOrganizationHandlerBase):
def handle(self):
with schema_context(self._organization.slug_name):
create_network_servers()
create_default_device_models()
9 changes: 2 additions & 7 deletions apps/device/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
("device_model", "0001_initial"),
("space", "0001_initial"),
]

Expand All @@ -29,12 +28,8 @@ class Migration(migrations.Migration):
),
),
(
"device_model",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="devices",
to="device_model.devicemodel",
),
"device_model_id",
models.CharField(blank=True, null=True),
),
],
options={
Expand Down
35 changes: 35 additions & 0 deletions apps/device/migrations/0013_alter_device_model_to_charfield.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 5.0.6

from django.db import migrations, models


def copy_device_model_id_to_device_model(apps, schema_editor):
schema_editor.execute(
"""
UPDATE device_device
SET device_model = device_model_id
WHERE device_model_id IS NOT NULL
"""
)


class Migration(migrations.Migration):
dependencies = [
("device", "0012_delete_devicetransformeddata"),
]

operations = [
migrations.AddField(
model_name="device",
name="device_model",
field=models.UUIDField(null=True, blank=True),
),
migrations.RunPython(
copy_device_model_id_to_device_model,
reverse_code=migrations.RunPython.noop,
),
migrations.RemoveField(
model_name="device",
name="device_model_id",
),
]
5 changes: 1 addition & 4 deletions apps/device/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.db import models

from apps.device.constants import DeviceStatus
from apps.device_model.models import DeviceModel
from apps.network_server.models import NetworkServer


Expand All @@ -17,9 +16,7 @@ class Device(BaseModel):
blank=True,
null=True,
)
device_model = models.ForeignKey(
DeviceModel, related_name="devices", on_delete=models.CASCADE
)
device_model = models.UUIDField(null=True, blank=True)
status = models.CharField(
choices=DeviceStatus.choices, default=DeviceStatus.IN_INVENTORY
)
Expand Down
23 changes: 17 additions & 6 deletions apps/device/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

from common.utils.custom_fields import HexCharField
from common.utils.telemetry_client import TelemetryServiceClient
from common.utils.tranformer_client import TranformerServiceClient
from django.core.cache import cache
from django.db import transaction
from rest_framework import serializers

from apps.device.models import Device, LorawanDevice, SpaceDevice, Trip
from apps.device_model.serializers import DeviceModelSerializer
from apps.network_server.serializers import NetworkServerSerializer

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -53,31 +53,43 @@ def create(self, validated_data):

class FormatDeviceSerializer(serializers.ModelSerializer):
device_id = serializers.UUIDField(read_only=True, source="lorawan_device.id")
device_profile = DeviceModelSerializer(read_only=True, source="device_model")
space_slug = serializers.CharField()

class Meta:
model = Device
fields = ["id", "device_profile", "device_id", "space_slug", "is_published"]
fields = ["id", "device_id", "device_model", "space_slug", "is_published"]


class DeviceSerializer(serializers.ModelSerializer):
lorawan_device = LorawanDeviceSerializer(many=False, required=False)
device_profile = DeviceModelSerializer(read_only=True, source="device_model")

class Meta:
model = Device
fields = [
"id",
"network_server",
"device_model",
"device_profile",
"status",
"lorawan_device",
"is_published",
]
list_serializer_class = MultiDeviceSerializer

def to_representation(self, instance):
data = super().to_representation(instance)
device_profile = None
if instance.device_model:
try:
client = TranformerServiceClient()
device_profile = client.get_device_model(str(instance.device_model))
except Exception as e:
logger.error(
f"Failed to fetch device model for {instance.id}: {str(e)}",
exc_info=True,
)
data["device_profile"] = device_profile
return data

def create(self, validated_data):
lorawan_data = validated_data.pop("lorawan_device", None)
try:
Expand Down Expand Up @@ -143,7 +155,6 @@ def update(self, instance, validated_data):

class GetDeviceSerializer(DeviceSerializer):
network_server = NetworkServerSerializer(read_only=True)
device_model = DeviceModelSerializer(read_only=True)

class Meta(DeviceSerializer.Meta):
model = Device
Expand Down
3 changes: 2 additions & 1 deletion apps/device/services/filter_processor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging
from typing import List

from common.utils.haversine_distance import haversine_distance

from apps.device.constants import (
DEFAULT_MAX_SPEED_KMH,
DEFAULT_MIN_POINT_DISTANCE,
LocationPoint,
)
from apps.utils.haversine_distance import haversine_distance

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion apps/device/services/trip_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
from typing import List, Tuple

import pytz
from common.utils.haversine_distance import haversine_distance
from common.utils.telemetry_client import LocationPoint, TelemetryServiceClient
from django.conf import settings
from django.db import transaction

from apps.device.models import SpaceDevice, Trip
from apps.device.services.filter_processor import FilterProcessor
from apps.utils.haversine_distance import haversine_distance

logger = logging.getLogger(__name__)

Expand Down
8 changes: 4 additions & 4 deletions apps/device/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ListCreateSpaceDeviceViewSet(generics.ListCreateAPIView):
"id",
"device__lorawan_device__dev_eui",
"device__id",
"device__device_model__name",
"device__device_model",
]

def get_serializer_class(self):
Expand Down Expand Up @@ -283,9 +283,9 @@ def list(self, request, *args, **kwargs):
class DeviceLookupView(UseTenantFromRequestMixin, generics.GenericAPIView):
serializer_class = FormatDeviceSerializer
lookup_field = "lorawan_device__dev_eui"
queryset = Device.objects.select_related(
"device_model", "device_model__manufacture", "lorawan_device"
).prefetch_related("space_devices")
queryset = Device.objects.select_related("lorawan_device").prefetch_related(
"space_devices"
)

def get_queryset(self):
qs = super().get_queryset()
Expand Down
Empty file removed apps/device_model/__init__.py
Empty file.
6 changes: 0 additions & 6 deletions apps/device_model/apps.py

This file was deleted.

6 changes: 0 additions & 6 deletions apps/device_model/constants.py

This file was deleted.

83 changes: 0 additions & 83 deletions apps/device_model/migrations/0001_initial.py

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions apps/device_model/migrations/0003_devicemodel_device_type.py

This file was deleted.

This file was deleted.

Loading