2020-06-10 23:17:30 +02:00
|
|
|
from datetime import datetime
|
2020-06-05 00:15:31 +02:00
|
|
|
from django.http import Http404
|
2020-06-02 00:57:20 +02:00
|
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
|
|
|
|
from telemetry.models import Telemetry
|
|
|
|
from telemetry.serializers import TelemetrySerializer
|
2020-06-05 00:15:31 +02:00
|
|
|
from rest_framework.response import Response
|
2020-06-02 00:57:20 +02:00
|
|
|
|
|
|
|
|
2020-06-05 00:15:31 +02:00
|
|
|
class TelemetryView(ModelViewSet):
|
2020-06-02 00:57:20 +02:00
|
|
|
queryset = Telemetry.objects.all()
|
|
|
|
serializer_class = TelemetrySerializer
|
2020-06-10 23:17:30 +02:00
|
|
|
lookup_field = 'device'
|
2020-06-02 00:57:20 +02:00
|
|
|
|
2020-06-05 00:15:31 +02:00
|
|
|
def list(self, request, device=None):
|
|
|
|
queryset = Telemetry.objects.filter(device__serial=device)
|
|
|
|
if not queryset:
|
|
|
|
raise Http404
|
|
|
|
serializer = TelemetrySerializer(queryset, many=True)
|
|
|
|
return Response(serializer.data)
|
2020-06-10 23:17:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
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(
|
2020-06-16 22:48:58 +02:00
|
|
|
device__serial=device).order_by('-time')
|
2020-06-10 23:17:30 +02:00
|
|
|
if not queryset:
|
|
|
|
raise Http404
|
2020-06-16 22:48:58 +02:00
|
|
|
serializer = TelemetrySerializer(queryset[0])
|
2020-06-10 23:17:30 +02:00
|
|
|
return Response(serializer.data)
|