feat: TODO device remote ctl api

This commit is contained in:
fxxian 2024-03-10 14:39:15 +08:00
parent 409aa8a441
commit 2b87aec8f9
2 changed files with 30 additions and 20 deletions

View File

@ -18,6 +18,6 @@ urlpatterns = [
path('weather/', WeatherLogListAPIView.as_view(), name='weather-log'), path('weather/', WeatherLogListAPIView.as_view(), name='weather-log'),
path('deviceinfo/', DeviceInfoAPIView.as_view(), name='device-info'), path('deviceinfo/', DeviceInfoAPIView.as_view(), name='device-info'),
path('weatherstationinfo/', WeatherStationInfoAPIView.as_view(), name='weather-station-info'), path('weatherstationinfo/', WeatherStationInfoAPIView.as_view(), name='weather-station-info'),
path('devicemqtt/remotecontrol/<str:device_id>/', remote_control, name='remote_control'), path('device/remote/', remote_control, name='device-remote'),
] ]

View File

@ -2,11 +2,12 @@ import re
import pytz import pytz
import json import json
import paho.mqtt.publish as publish import paho.mqtt.publish as publish
# import paho.mqtt.subscribe as subscribe
from rest_framework import mixins
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
from rest_framework.generics import ( from rest_framework.generics import (
ListAPIView, ListAPIView,
RetrieveAPIView,
CreateAPIView
) )
from django.utils.timezone import timedelta, datetime from django.utils.timezone import timedelta, datetime
from rest_framework.response import Response from rest_framework.response import Response
@ -34,6 +35,7 @@ from .serializers import (
WeatherLogWithInfoSerializer, WeatherLogWithInfoSerializer,
DeviceInfoSerializer, DeviceInfoSerializer,
WeatherStationInfoSerializer, WeatherStationInfoSerializer,
DeviceRemoteSerializer,
) )
@ -212,8 +214,16 @@ class DeviceInfoAPIView(ListAPIView, RoleMixin):
return queryset_list return queryset_list
@api_view(['POST'])
def remote_control(request, device_id): class DeviceRemote(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
RoleMixin,
GenericViewSet):
serializer_class = DeviceRemoteSerializer
permission_classes = [IsAuthenticated]
filter_backends = [SearchFilter, OrderingFilter]
def remote_control(self, request, device_id):
command = request.data.get('command') command = request.data.get('command')
speed = request.data.get('speed') speed = request.data.get('speed')