Skip to content

Commit

Permalink
django: initial data: maintain lwm2m resouce model
Browse files Browse the repository at this point in the history
The lwm2m resource model is defined in the specification. As of not
there was a .py mapping table that was mapping object/resource ids to a
name and data type. This can be made more elegant by directly adding it
to the database during first Django start. That way we can always
reference the database and no code changes are required for adding new
resources.

Signed-off-by: Jonas Remmert <jremmert@gmx.net>
  • Loading branch information
jonas-rem authored and Kappuccino111 committed Jun 4, 2024
1 parent 516c6fd commit 36b0254
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 41 deletions.
290 changes: 290 additions & 0 deletions server/django/db_initial_resource_types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
[
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3303,
"resource_id": 5700,
"name": "temperature",
"data_type": "float"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3304,
"resource_id": 5700,
"name": "humidity",
"data_type": "float"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 0,
"name": "manufacturer",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 1,
"name": "model_number",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 2,
"name": "serial_number",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 3,
"name": "firmware_version",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 4,
"name": "reboot",
"data_type": ""
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 5,
"name": "factory_reset",
"data_type": ""
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 6,
"name": "power_source",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 7,
"name": "power_source_v",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 8,
"name": "power_source_i",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 9,
"name": "battery_level",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 10,
"name": "memory_free",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 11,
"name": "error_code",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 12,
"name": "reset_error_code",
"data_type": ""
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 13,
"name": "current_time",
"data_type": "time"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 14,
"name": "utc_offset",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 15,
"name": "timezone",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 16,
"name": "binding_mode",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 17,
"name": "device_type",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 18,
"name": "hardware_version",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 19,
"name": "software_version",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 20,
"name": "battery_status",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 21,
"name": "memory_total",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 3,
"resource_id": 22,
"name": "ext_dev_info",
"data_type": "objlnk"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 5,
"resource_id": 0,
"name": "package",
"data_type": "opaque"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 5,
"resource_id": 1,
"name": "package_uri",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 5,
"resource_id": 2,
"name": "update",
"data_type": ""
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 5,
"resource_id": 3,
"name": "state",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 5,
"resource_id": 4,
"name": "update_result",
"data_type": "integer"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 5,
"resource_id": 5,
"name": "package_name",
"data_type": "string"
}
},
{
"model": "sensordata.resourcetype",
"fields": {
"object_id": 5,
"resource_id": 6,
"name": "package_version",
"data_type": "string"
}
}
]
2 changes: 2 additions & 0 deletions server/django/django_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ echo "Running migrate..."
python manage.py migrate 2>&1 | tee -a $logfile
echo "Loading initial data..."
python manage.py loaddata db_initial_data.json 2>&1 | tee -a $logfile
echo "Loading lwm2m resource types data..."
python manage.py loaddata db_initial_resource_types.json 2>&1 | tee -a $logfile
echo "Starting the server..."
python manage.py runserver 0.0.0.0:8000 2>&1 | tee -a "$logfile"
11 changes: 7 additions & 4 deletions server/django/sensordata/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,23 @@ def save_model(self, request, obj, form, change):

# Determine the value based on the type of resource value
value = None
if resource_type.data_type == 'int':
if resource_type.data_type == 'integer':
value = resource.int_value
elif resource_type.data_type == 'float':
value = resource.float_value
elif resource_type.data_type == 'string':
value = resource.str_value
elif resource_type.data_type == 'bool':
value = resource.bool_value
value = resource.int_value
elif resource_type.data_type == 'time':
value = resource.int_value
# Execute operation
elif resource_type.data_type == '':
pass
else:
# TODO: maybe an execute operation?
log.error('Resource value not found')
return


# Construct the URL based on the device, object_id, and resource_id
url = (
f'{LESHAN_URI}/clients/{device.device_id}/'
Expand Down
23 changes: 0 additions & 23 deletions server/django/sensordata/lwm2m_mappings.py

This file was deleted.

Loading

0 comments on commit 36b0254

Please sign in to comment.