Skip to content

Commit

Permalink
django: Adapt to the new database model
Browse files Browse the repository at this point in the history
- Rewrite the serializer for single and multi resource
- Adapt admin dashboard
- Remove listView for now, admin dashboard is sufficient for testing
  currently

Signed-off-by: Jonas Remmert <jremmert@gmx.net>
  • Loading branch information
jonas-rem committed May 15, 2024
1 parent 28024c7 commit 6a5cec8
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 238 deletions.
50 changes: 49 additions & 1 deletion server/django/sensordata/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
from django.contrib import admin
from .models import (
Device,
ResourceType,
Resource,
Event,
EventResource,
DeviceOperation,
Firmware
)

# Register your models here.
@admin.register(Device)
class DeviceAdmin(admin.ModelAdmin):
list_display = ('device_id', 'name')
search_fields = ('device_id', 'name')

@admin.register(ResourceType)
class ResourceTypeAdmin(admin.ModelAdmin):
list_display = ('object_id', 'resource_id', 'name', 'data_type')
search_fields = ('object_id', 'resource_id', 'name')

@admin.register(Resource)
class ResourceAdmin(admin.ModelAdmin):
list_display = ('device', 'resource_type', 'timestamp')
search_fields = ('device__device_id', 'resource_type__name')
list_filter = ('device', 'resource_type', 'timestamp')

@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
list_display = ('device', 'event_type', 'start_time', 'end_time')
search_fields = ('device__device_id', 'event_type')
list_filter = ('device', 'event_type')

@admin.register(EventResource)
class EventResourceAdmin(admin.ModelAdmin):
list_display = ('event', 'resource')
search_fields = ('event__event_type', 'resource__resource_type__name')
list_filter = ('event', 'resource')

@admin.register(DeviceOperation)
class DeviceOperationAdmin(admin.ModelAdmin):
list_display = ('resource', 'operation_type', 'status', 'timestamp_sent',
'retransmit_counter', 'last_attempt')
search_fields = ('resource__device__device_id', 'operation_type', 'status')
list_filter = ('resource', 'operation_type', 'status', 'timestamp_sent')

@admin.register(Firmware)
class FirmwareAdmin(admin.ModelAdmin):
list_display = ('version', 'file_name', 'download_url', 'created_at')
search_fields = ('version', 'file_name')
list_filter = ('created_at',)
23 changes: 23 additions & 0 deletions server/django/sensordata/lwm2m_mappings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
LWM2M_RESOURCE_MAP = {
(3303, 5700): {"name": "temperature", "data_type": "float"},
(3303, 5701): {"name": "humidity", "data_type": "float"},
(3, 0): {"name": "manufacturer", "data_type": "string"},
(3, 1): {"name": "model_number", "data_type": "string"},
(3, 2): {"name": "serial_number", "data_type": "string"},
(3, 3): {"name": "firmware_version", "data_type": "string"},
(3, 6): {"name": "power_source", "data_type": "int"},
(3, 7): {"name": "power_source_v", "data_type": "int"},
(3, 8): {"name": "power_source_i", "data_type": "int"},
(3, 9): {"name": "battery_level", "data_type": "int"},
(3, 10): {"name": "memory_free", "data_type": "int"},
(3, 11): {"name": "error_code", "data_type": "int"},
(3, 13): {"name": "current_time", "data_type": "time"},
(3, 14): {"name": "utc_offset", "data_type": "string"},
(3, 15): {"name": "timezone", "data_type": "string"},
(3, 16): {"name": "binding_mode", "data_type": "string"},
(3, 17): {"name": "device_type", "data_type": "string"},
(3, 18): {"name": "hardware_version", "data_type": "string"},
(3, 19): {"name": "software_version", "data_type": "string"},
(3, 20): {"name": "battery_status", "data_type": "int"},
(3, 21): {"name": "memory_total", "data_type": "int"},
}
42 changes: 6 additions & 36 deletions server/django/sensordata/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.contrib import admin
from django.db import models

class Device(models.Model):
Expand All @@ -16,6 +15,9 @@ class ResourceType(models.Model):
class Meta:
unique_together = ('object_id', 'resource_id')

def __str__(self):
return f"{self.object_id}/{self.resource_id} - {self.name}"

class Resource(models.Model):
"""Stores individual resource data, such as sensor readings, from a device."""
device = models.ForeignKey(Device, on_delete=models.PROTECT)
Expand All @@ -29,6 +31,9 @@ class Resource(models.Model):
class Meta:
unique_together = ('device', 'resource_type', 'timestamp')

def __str__(self):
return f"{self.device} - {self.resource_type} - {self.timestamp}"

class Event(models.Model):
"""
Represents a significant event in the system that is associated with a
Expand Down Expand Up @@ -65,38 +70,3 @@ class Firmware(models.Model):
file_name = models.CharField(max_length=255)
download_url = models.URLField()
created_at = models.DateTimeField(auto_now_add=True)

#class Endpoint(models.Model):
# endpoint = models.CharField(max_length=100, default='', unique=True)
# manufacturer = models.CharField(max_length=100, default='')
# model_number = models.CharField(max_length=100, default='')
# serial_number = models.CharField(max_length=100, default='')
# firmware_version = models.CharField(max_length=100, default='')
# reboot = models.IntegerField(default=0)
# factory_reset = models.IntegerField(default=0)
# battery_level = models.IntegerField(default=0)
# last_updated = models.DateTimeField(auto_now=True)
#
#class SensorData(models.Model):
# endpoint = models.CharField(max_length=100, default='')
# time = models.DateTimeField(auto_now_add=True)
# temperature = models.FloatField()
#
#@admin.register(SensorData)
#class SensorDataAdmin(admin.ModelAdmin):
# list_display = [
# 'time',
# 'endpoint',
# 'temperature',
# ]
#
#@admin.register(Endpoint)
#class EndpointAdmin(admin.ModelAdmin):
# list_display = [
# 'endpoint',
# 'serial_number',
# 'model_number',
# 'firmware_version',
# 'reboot',
# 'last_updated',
# ]
Loading

0 comments on commit 6a5cec8

Please sign in to comment.