1
0
mirror of https://github.com/daniviga/bite.git synced 2024-11-22 21:16:12 +01:00

Improve API endpoints (#10)

* Improve device API endpoints

* Arduino minor optimizations
This commit is contained in:
Daniele Viganò 2020-06-05 13:24:12 +02:00 committed by GitHub
parent 253411c0fd
commit bbb92100f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 18 deletions

View File

@ -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 ;

View File

@ -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)

View File

@ -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),
),
]

View File

@ -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)

View File

@ -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'),
]