From bbb92100f8c4748dc2bf1fa6cc45948179e372ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Fri, 5 Jun 2020 13:24:12 +0200 Subject: [PATCH] Improve API endpoints (#10) * Improve device API endpoints * Arduino minor optimizations --- arduino/tempLightSensor/tempLightSensor.ino | 11 +++++------ docker/simulators/simulator_http.py | 19 ++++++++++--------- freedcs/api/migrations/0004_device_uuid.py | 19 +++++++++++++++++++ freedcs/api/models.py | 4 ++++ freedcs/api/urls.py | 9 ++++++--- 5 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 freedcs/api/migrations/0004_device_uuid.py diff --git a/arduino/tempLightSensor/tempLightSensor.ino b/arduino/tempLightSensor/tempLightSensor.ino index 6a98bc9..3956ccd 100644 --- a/arduino/tempLightSensor/tempLightSensor.ino +++ b/arduino/tempLightSensor/tempLightSensor.ino @@ -16,8 +16,7 @@ JsonObject payload = telemetry.createNestedObject("payload"); JsonObject temp = payload.createNestedObject("temperature"); unsigned int counter = 0; -int tempPin = A0; -int photocellPin = A1; + EthernetUDP ntpUDP; NTPClient timeClient(ntpUDP); @@ -29,9 +28,8 @@ struct netConfig { }; netConfig config; -const String apiURL = "/api/subscribe/"; +const String apiURL = "/api/device/subscribe/"; const String telemetryURL = "/telemetry/"; -const int postDelay = 10 * 1000; void setup(void) { Serial.begin(9600); @@ -90,9 +88,10 @@ void setup(void) { } void loop(void) { + const int postDelay = 10 * 1000; - unsigned int photocellReading = analogRead(photocellPin); - unsigned int tempReading = analogRead(tempPin); + unsigned int tempReading = analogRead(A0); + unsigned int photocellReading = analogRead(A1); float tempVoltage = tempReading * AREF_VOLTAGE / 1024.0; float tempC = (tempVoltage - 0.5) * 100 ; diff --git a/docker/simulators/simulator_http.py b/docker/simulators/simulator_http.py index 73e3eec..bcbfd29 100644 --- a/docker/simulators/simulator_http.py +++ b/docker/simulators/simulator_http.py @@ -31,10 +31,12 @@ def post_json(host, url, data): except urllib3.exceptions.MaxRetryError: pass + sleep(10) # retry in 10 seconds + def main(): host = os.environ.get('IOT_HOST', 'http://127.0.0.1:8000') - subscribe = '/api/subscribe/' + subscribe = '/api/device/subscribe/' telemetry = '/telemetry/' delay = int(os.environ.get('IOT_DELAY', 10)) @@ -49,17 +51,16 @@ def main(): data = { 'device': serial, 'clock': int(datetime.datetime.now().timestamp()), - 'payload': { - 'id': 'device_http_simulator', - 'light': random.randint(300, 500), - "temperature": { - "celsius": random.uniform(20, 28) - } - } } while True: - post_json(host, telemetry, data) + payload = { + 'id': 'device_http_simulator', + 'light': random.randint(300, 500), + 'temperature': { + 'celsius': round(random.uniform(20, 28), 1)} + } + post_json(host, telemetry, {**data, 'payload': payload}) sleep(delay) diff --git a/freedcs/api/migrations/0004_device_uuid.py b/freedcs/api/migrations/0004_device_uuid.py new file mode 100644 index 0000000..e627420 --- /dev/null +++ b/freedcs/api/migrations/0004_device_uuid.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.6 on 2020-06-05 09:19 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0003_auto_20200602_2128'), + ] + + operations = [ + migrations.AddField( + model_name='device', + name='uuid', + field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True), + ), + ] diff --git a/freedcs/api/models.py b/freedcs/api/models.py index aaab60e..80047b7 100644 --- a/freedcs/api/models.py +++ b/freedcs/api/models.py @@ -1,3 +1,5 @@ +import uuid + from django.db import models from django.conf import settings from django.core.exceptions import ValidationError @@ -31,6 +33,8 @@ class WhiteList(models.Model): class Device(models.Model): serial = models.CharField(max_length=128, unique=True, validators=[device_validation]) + uuid = models.UUIDField(unique=True, default=uuid.uuid4, + editable=False) creation_time = models.DateTimeField(auto_now_add=True) updated_time = models.DateTimeField(auto_now=True) diff --git a/freedcs/api/urls.py b/freedcs/api/urls.py index ad50dae..256d548 100644 --- a/freedcs/api/urls.py +++ b/freedcs/api/urls.py @@ -17,7 +17,10 @@ from django.urls import path from api.views import APISubscribe urlpatterns = [ - path('subscribe/', - APISubscribe.as_view({'get': 'list', 'post': 'create'}), - name='api_subscribe'), + path('device/subscribe/', + APISubscribe.as_view({'post': 'create'}), + name='device-subscribe'), + path('device/list/', + APISubscribe.as_view({'get': 'list'}), + name='device-list'), ]