mirror of
https://github.com/daniviga/bite.git
synced 2024-11-26 23:06:14 +01:00
Add time filtering to Telemetry (#12)
This commit is contained in:
parent
a7cebefbf5
commit
0ee0b51078
|
@ -90,12 +90,11 @@ def main():
|
||||||
data = {'serial': args.serial}
|
data = {'serial': args.serial}
|
||||||
post_json(args.endpoint, subscribe, data)
|
post_json(args.endpoint, subscribe, data)
|
||||||
|
|
||||||
|
while True:
|
||||||
data = {
|
data = {
|
||||||
'device': args.serial,
|
'device': args.serial,
|
||||||
'clock': int(datetime.datetime.now().timestamp()),
|
'clock': int(datetime.datetime.now().timestamp()),
|
||||||
}
|
}
|
||||||
|
|
||||||
while True:
|
|
||||||
payload = {
|
payload = {
|
||||||
'id': 'device_simulator',
|
'id': 'device_simulator',
|
||||||
'light': random.randint(300, 500),
|
'light': random.randint(300, 500),
|
||||||
|
|
|
@ -14,7 +14,7 @@ Including another URLconf
|
||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from telemetry.views import TelemetryView
|
from telemetry.views import TelemetryView, TelemetryLatest, TelemetryRange
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('',
|
path('',
|
||||||
|
@ -23,4 +23,13 @@ urlpatterns = [
|
||||||
path('<str:device>/',
|
path('<str:device>/',
|
||||||
TelemetryView.as_view({'get': 'list'}),
|
TelemetryView.as_view({'get': 'list'}),
|
||||||
name='device-telemetry'),
|
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'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from datetime import datetime
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from rest_framework.viewsets import ModelViewSet
|
from rest_framework.viewsets import ModelViewSet
|
||||||
|
|
||||||
|
@ -9,6 +10,7 @@ from rest_framework.response import Response
|
||||||
class TelemetryView(ModelViewSet):
|
class TelemetryView(ModelViewSet):
|
||||||
queryset = Telemetry.objects.all()
|
queryset = Telemetry.objects.all()
|
||||||
serializer_class = TelemetrySerializer
|
serializer_class = TelemetrySerializer
|
||||||
|
lookup_field = 'device'
|
||||||
|
|
||||||
def list(self, request, device=None):
|
def list(self, request, device=None):
|
||||||
queryset = Telemetry.objects.filter(device__serial=device)
|
queryset = Telemetry.objects.filter(device__serial=device)
|
||||||
|
@ -16,3 +18,32 @@ class TelemetryView(ModelViewSet):
|
||||||
raise Http404
|
raise Http404
|
||||||
serializer = TelemetrySerializer(queryset, many=True)
|
serializer = TelemetrySerializer(queryset, many=True)
|
||||||
return Response(serializer.data)
|
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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user