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:
parent
253411c0fd
commit
bbb92100f8
|
@ -16,8 +16,7 @@ JsonObject payload = telemetry.createNestedObject("payload");
|
||||||
JsonObject temp = payload.createNestedObject("temperature");
|
JsonObject temp = payload.createNestedObject("temperature");
|
||||||
|
|
||||||
unsigned int counter = 0;
|
unsigned int counter = 0;
|
||||||
int tempPin = A0;
|
|
||||||
int photocellPin = A1;
|
|
||||||
|
|
||||||
EthernetUDP ntpUDP;
|
EthernetUDP ntpUDP;
|
||||||
NTPClient timeClient(ntpUDP);
|
NTPClient timeClient(ntpUDP);
|
||||||
|
@ -29,9 +28,8 @@ struct netConfig {
|
||||||
};
|
};
|
||||||
netConfig config;
|
netConfig config;
|
||||||
|
|
||||||
const String apiURL = "/api/subscribe/";
|
const String apiURL = "/api/device/subscribe/";
|
||||||
const String telemetryURL = "/telemetry/";
|
const String telemetryURL = "/telemetry/";
|
||||||
const int postDelay = 10 * 1000;
|
|
||||||
|
|
||||||
void setup(void) {
|
void setup(void) {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
@ -90,9 +88,10 @@ void setup(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
|
const int postDelay = 10 * 1000;
|
||||||
|
|
||||||
unsigned int photocellReading = analogRead(photocellPin);
|
unsigned int tempReading = analogRead(A0);
|
||||||
unsigned int tempReading = analogRead(tempPin);
|
unsigned int photocellReading = analogRead(A1);
|
||||||
|
|
||||||
float tempVoltage = tempReading * AREF_VOLTAGE / 1024.0;
|
float tempVoltage = tempReading * AREF_VOLTAGE / 1024.0;
|
||||||
float tempC = (tempVoltage - 0.5) * 100 ;
|
float tempC = (tempVoltage - 0.5) * 100 ;
|
||||||
|
|
|
@ -31,10 +31,12 @@ def post_json(host, url, data):
|
||||||
except urllib3.exceptions.MaxRetryError:
|
except urllib3.exceptions.MaxRetryError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
sleep(10) # retry in 10 seconds
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
host = os.environ.get('IOT_HOST', 'http://127.0.0.1:8000')
|
host = os.environ.get('IOT_HOST', 'http://127.0.0.1:8000')
|
||||||
subscribe = '/api/subscribe/'
|
subscribe = '/api/device/subscribe/'
|
||||||
telemetry = '/telemetry/'
|
telemetry = '/telemetry/'
|
||||||
delay = int(os.environ.get('IOT_DELAY', 10))
|
delay = int(os.environ.get('IOT_DELAY', 10))
|
||||||
|
|
||||||
|
@ -49,17 +51,16 @@ def main():
|
||||||
data = {
|
data = {
|
||||||
'device': serial,
|
'device': serial,
|
||||||
'clock': int(datetime.datetime.now().timestamp()),
|
'clock': int(datetime.datetime.now().timestamp()),
|
||||||
'payload': {
|
|
||||||
'id': 'device_http_simulator',
|
|
||||||
'light': random.randint(300, 500),
|
|
||||||
"temperature": {
|
|
||||||
"celsius": random.uniform(20, 28)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while True:
|
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)
|
sleep(delay)
|
||||||
|
|
||||||
|
|
||||||
|
|
19
freedcs/api/migrations/0004_device_uuid.py
Normal file
19
freedcs/api/migrations/0004_device_uuid.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,3 +1,5 @@
|
||||||
|
import uuid
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
@ -31,6 +33,8 @@ class WhiteList(models.Model):
|
||||||
class Device(models.Model):
|
class Device(models.Model):
|
||||||
serial = models.CharField(max_length=128, unique=True,
|
serial = models.CharField(max_length=128, unique=True,
|
||||||
validators=[device_validation])
|
validators=[device_validation])
|
||||||
|
uuid = models.UUIDField(unique=True, default=uuid.uuid4,
|
||||||
|
editable=False)
|
||||||
creation_time = models.DateTimeField(auto_now_add=True)
|
creation_time = models.DateTimeField(auto_now_add=True)
|
||||||
updated_time = models.DateTimeField(auto_now=True)
|
updated_time = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,10 @@ from django.urls import path
|
||||||
from api.views import APISubscribe
|
from api.views import APISubscribe
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('subscribe/',
|
path('device/subscribe/',
|
||||||
APISubscribe.as_view({'get': 'list', 'post': 'create'}),
|
APISubscribe.as_view({'post': 'create'}),
|
||||||
name='api_subscribe'),
|
name='device-subscribe'),
|
||||||
|
path('device/list/',
|
||||||
|
APISubscribe.as_view({'get': 'list'}),
|
||||||
|
name='device-list'),
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user