Permalink
Browse files

/v1/now endpoint added

  • Loading branch information...
mfa committed Apr 23, 2016
1 parent 6b0a1d2 commit 3ebbce1b70d6454ff1371112fe14385c3d475b4b
Showing with 49 additions and 0 deletions.
  1. +30 −0 feinstaub/sensors/serializers.py
  2. +2 −0 feinstaub/sensors/urls.py
  3. +17 −0 feinstaub/sensors/views.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')
@@ -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(
View
@@ -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):

0 comments on commit 3ebbce1

Please sign in to comment.