From 3ebbce1b70d6454ff1371112fe14385c3d475b4b Mon Sep 17 00:00:00 2001 From: Andreas Madsack Date: Sat, 23 Apr 2016 16:39:22 +0200 Subject: [PATCH] /v1/now endpoint added --- feinstaub/sensors/serializers.py | 30 ++++++++++++++++++++++++++++++ feinstaub/sensors/urls.py | 2 ++ feinstaub/sensors/views.py | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/feinstaub/sensors/serializers.py b/feinstaub/sensors/serializers.py index 18c6939..536e933 100644 --- a/feinstaub/sensors/serializers.py +++ b/feinstaub/sensors/serializers.py @@ -131,3 +131,33 @@ class VerboseSensorDataSerializer(serializers.ModelSerializer): class Meta: model = SensorData fields = ('id', 'sampling_rate', 'timestamp', 'sensordatavalues', 'location', 'sensor', 'software_version') + + +# ################################################## + + +class NowSensorSerializer(serializers.ModelSerializer): + sensor_type = NestedSensorTypeSerializer() + + class Meta: + model = Sensor + fields = ('id', 'pin', 'sensor_type') + + +class NowSensorLocationSerializer(serializers.ModelSerializer): + latitude = serializers.DecimalField(max_digits=6, decimal_places=3) + longitude = serializers.DecimalField(max_digits=6, decimal_places=3) + + class Meta: + model = SensorLocation + fields = ('id', 'latitude', 'longitude') + + +class NowSerializer(serializers.ModelSerializer): + location = NowSensorLocationSerializer() + sensor = NowSensorSerializer() + sensordatavalues = NestedSensorDataValueSerializer(many=True) + + class Meta: + model = SensorData + fields = ('id', 'sampling_rate', 'timestamp', 'sensordatavalues', 'location', 'sensor') diff --git a/feinstaub/sensors/urls.py b/feinstaub/sensors/urls.py index f18a4e6..a15ae3a 100644 --- a/feinstaub/sensors/urls.py +++ b/feinstaub/sensors/urls.py @@ -7,6 +7,7 @@ SensorDataView, SensorView, StatisticsView, + NowView, ) from main.views import UsersView @@ -17,6 +18,7 @@ router.register(r'data', SensorDataView) router.register(r'statistics', StatisticsView, base_name='statistics') router.register(r'user', UsersView) +router.register(r'now', NowView) urlpatterns = patterns( diff --git a/feinstaub/sensors/views.py b/feinstaub/sensors/views.py index a5162dd..69d8864 100644 --- a/feinstaub/sensors/views.py +++ b/feinstaub/sensors/views.py @@ -1,6 +1,8 @@ +import datetime import django_filters from django.contrib.auth.models import User from django.db.models import Q +from django.utils import timezone from rest_framework import mixins, viewsets, filters, pagination from rest_framework.response import Response @@ -11,6 +13,7 @@ NodeSerializer, SensorSerializer, VerboseSensorDataSerializer, + NowSerializer, ) from .models import ( @@ -105,6 +108,20 @@ def get_queryset(self): return Node.objects.none() +class NowView(mixins.ListModelMixin, viewsets.GenericViewSet): + """ Show all sensors active in the last 5 minutes with newest value + """ + permission_classes = [] + serializer_class = NowSerializer + queryset = SensorData.objects.none() + + def get_queryset(self): +# now = timezone.now() + now = datetime.datetime(2016, 1, 1, 1, 1) + startdate = now - datetime.timedelta(minutes=5) + return SensorData.objects.filter(modified__range=[startdate, now]) + + class StatisticsView(viewsets.ViewSet): def list(self, request):