diff --git a/backend/core/api/views.py b/backend/core/api/views.py index 756a330..1ce6325 100644 --- a/backend/core/api/views.py +++ b/backend/core/api/views.py @@ -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"}) diff --git a/backend/core/migrations/0017_auto_20191231_1437.py b/backend/core/migrations/0017_auto_20191231_1437.py new file mode 100644 index 0000000..99f6281 --- /dev/null +++ b/backend/core/migrations/0017_auto_20191231_1437.py @@ -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'), + ), + ] diff --git a/backend/core/models.py b/backend/core/models.py index a0fbf9c..1e4e25e 100644 --- a/backend/core/models.py +++ b/backend/core/models.py @@ -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): diff --git a/backend/core/tests/test_digita_gw.py b/backend/core/tests/test_digita_gw.py index 417505c..17faa6b 100644 --- a/backend/core/tests/test_digita_gw.py +++ b/backend/core/tests/test_digita_gw.py @@ -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": { @@ -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")