Skip to content

Commit

Permalink
Refs #3 - Integrate the app with Lora thingpark, change logic so new …
Browse files Browse the repository at this point in the history
…ApartmentSensors, attributes and values are generated automatically when data is received
  • Loading branch information
johan-fvh committed Dec 31, 2019
1 parent 5f26f4c commit 7f2a65e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
32 changes: 6 additions & 26 deletions backend/core/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,36 +183,16 @@ def digita_gw(request):
Digita GW endpoint implementation
"""
identifier = request.data['DevEUI_uplink']['DevEUI']
try:
apsen = models.ApartmentSensor.objects.get(identifier=identifier)
except models.ApartmentSensor.DoesNotExist:
return Response(
{
"message": f"ApartmentSensor does not exists with given identifier {identifier}"
},
status=status.HTTP_404_NOT_FOUND,
)

apsen = models.ApartmentSensor.objects.get_or_create(identifier=identifier)[0]
payload = binascii.unhexlify(request.data['DevEUI_uplink']['payload_hex'])
decoded_payload = decode_elsys_payload(payload)
mapping = settings.DIGITA_GW_PAYLOAD_TO_ATTRIBUTES # type: dict

for key, value in decoded_payload.items():
try:
uri = mapping[key]
apsenval = apsen.apartment_sensor_values.get(
attribute__uri=uri
) # type: models.ApartmentSensorValue
except models.ApartmentSensorValue.DoesNotExist:
log.debug(
"ApartmentSensorValue does not exists with given URI %s (%s)", uri, key
)
except KeyError:
log.debug('No configured mapping to attribute for %s', key)
continue
uri = mapping.get(key, '')
if uri:
attr = models.SensorAttribute.objects.get_or_create(uri=uri, defaults={'description': key})[0]
else:
apsenval.value = value
apsenval.save()
log.debug('Updated %s', apsenval)

attr = models.SensorAttribute.objects.get_or_create(description=key)[0]
apsen.apartment_sensor_values.create(value=value, attribute=attr)
return Response({"message": "Updated successfully"})
24 changes: 24 additions & 0 deletions backend/core/migrations/0017_auto_20191231_1437.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.2.8 on 2019-12-31 14:37

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


class Migration(migrations.Migration):

dependencies = [
('core', '0016_auto_20190130_0833'),
]

operations = [
migrations.AlterField(
model_name='apartmentsensor',
name='apartment',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='apartment_sensors', to='core.Apartment'),
),
migrations.AlterField(
model_name='apartmentsensor',
name='sensor',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='core.Sensor'),
),
]
5 changes: 2 additions & 3 deletions backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ class ApartmentSensor(models.Model):
"""

apartment = models.ForeignKey(
Apartment, related_name='apartment_sensors', on_delete=models.CASCADE
)
sensor = models.ForeignKey('Sensor', on_delete=models.DO_NOTHING)
Apartment, null=True, related_name='apartment_sensors', on_delete=models.SET_NULL)
sensor = models.ForeignKey('Sensor', null=True, on_delete=models.SET_NULL)
identifier = models.CharField(max_length=255, unique=True)

def __str__(self):
Expand Down
5 changes: 3 additions & 2 deletions backend/core/tests/test_digita_gw.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setUp(self):
User.objects.create_user(username="nkha", password="123456")
self.client.login(username="nkha", password="123456")

def test_invalid_gw_data(self):
def test_new_sensor_gw_data(self):
# No sensor
data = {
"DevEUI_uplink": {
Expand Down Expand Up @@ -57,7 +57,8 @@ def test_invalid_gw_data(self):
}

response = self.client.post(self.url, data, format='json')
self.assertEqual(404, response.status_code)
self.assertEqual(200, response.status_code)
self.assertTrue(ApartmentSensor.objects.filter(identifier="A81758FFFE030CF6").exists())

def test_valid_gw_data(self):
sensor = Sensor.objects.create(name="T-800")
Expand Down

0 comments on commit 7f2a65e

Please sign in to comment.