1
0
mirror of https://github.com/daniviga/bite.git synced 2024-12-23 08:41:24 +01:00

Add time filtering to Telemetry (#12)

This commit is contained in:
Daniele Viganò 2020-06-10 23:17:30 +02:00 committed by GitHub
parent a7cebefbf5
commit 0ee0b51078
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 6 deletions

View File

@ -90,12 +90,11 @@ def main():
data = {'serial': args.serial}
post_json(args.endpoint, subscribe, data)
data = {
'device': args.serial,
'clock': int(datetime.datetime.now().timestamp()),
}
while True:
data = {
'device': args.serial,
'clock': int(datetime.datetime.now().timestamp()),
}
payload = {
'id': 'device_simulator',
'light': random.randint(300, 500),

View File

@ -14,7 +14,7 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path
from telemetry.views import TelemetryView
from telemetry.views import TelemetryView, TelemetryLatest, TelemetryRange
urlpatterns = [
path('',
@ -23,4 +23,13 @@ urlpatterns = [
path('<str:device>/',
TelemetryView.as_view({'get': 'list'}),
name='device-telemetry'),
path('<str:device>/latest/',
TelemetryLatest.as_view({'get': 'retrieve'}),
name='device-telemetry-latest'),
path('<str:device>/<str:time_from>/',
TelemetryRange.as_view({'get': 'list'}),
name='device-telemetry-single'),
path('<str:device>/<str:time_from>/<str:time_to>/',
TelemetryRange.as_view({'get': 'list'}),
name='device-telemetry-range'),
]

View File

@ -1,3 +1,4 @@
from datetime import datetime
from django.http import Http404
from rest_framework.viewsets import ModelViewSet
@ -9,6 +10,7 @@ from rest_framework.response import Response
class TelemetryView(ModelViewSet):
queryset = Telemetry.objects.all()
serializer_class = TelemetrySerializer
lookup_field = 'device'
def list(self, request, device=None):
queryset = Telemetry.objects.filter(device__serial=device)
@ -16,3 +18,32 @@ class TelemetryView(ModelViewSet):
raise Http404
serializer = TelemetrySerializer(queryset, many=True)
return Response(serializer.data)
class TelemetryRange(ModelViewSet):
queryset = Telemetry.objects.all()
serializer_class = TelemetrySerializer
lookup_field = 'device'
def list(self, request, device, time_from, time_to=None):
queryset = Telemetry.objects.filter(
device__serial=device,
time__range=[time_from, datetime.now()])
if not queryset:
raise Http404
serializer = TelemetrySerializer(queryset, many=True)
return Response(serializer.data)
class TelemetryLatest(ModelViewSet):
queryset = Telemetry.objects.all()
serializer_class = TelemetrySerializer
lookup_field = 'device'
def retrieve(self, request, device=None):
queryset = Telemetry.objects.filter(
device__serial=device).order_by('-time')[0]
if not queryset:
raise Http404
serializer = TelemetrySerializer(queryset)
return Response(serializer.data)