From 2b87aec8f9d84f27f2eaf4f1f1a3be679f6d9749 Mon Sep 17 00:00:00 2001 From: fxxian Date: Sun, 10 Mar 2024 14:39:15 +0800 Subject: [PATCH] feat: TODO device remote ctl api --- apps/mosquito/api/urls.py | 2 +- apps/mosquito/api/views.py | 48 +++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/apps/mosquito/api/urls.py b/apps/mosquito/api/urls.py index 5c49ea5..8c283b6 100644 --- a/apps/mosquito/api/urls.py +++ b/apps/mosquito/api/urls.py @@ -18,6 +18,6 @@ urlpatterns = [ path('weather/', WeatherLogListAPIView.as_view(), name='weather-log'), path('deviceinfo/', DeviceInfoAPIView.as_view(), name='device-info'), path('weatherstationinfo/', WeatherStationInfoAPIView.as_view(), name='weather-station-info'), - path('devicemqtt/remotecontrol//', remote_control, name='remote_control'), + path('device/remote/', remote_control, name='device-remote'), ] \ No newline at end of file diff --git a/apps/mosquito/api/views.py b/apps/mosquito/api/views.py index 4df72e7..88a3512 100644 --- a/apps/mosquito/api/views.py +++ b/apps/mosquito/api/views.py @@ -2,11 +2,12 @@ import re import pytz import json 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.generics import ( ListAPIView, - RetrieveAPIView, - CreateAPIView ) from django.utils.timezone import timedelta, datetime from rest_framework.response import Response @@ -34,6 +35,7 @@ from .serializers import ( WeatherLogWithInfoSerializer, DeviceInfoSerializer, WeatherStationInfoSerializer, + DeviceRemoteSerializer, ) @@ -212,23 +214,31 @@ class DeviceInfoAPIView(ListAPIView, RoleMixin): return queryset_list -@api_view(['POST']) -def remote_control(request, device_id): - command = request.data.get('command') - speed = request.data.get('speed') - if command == 'ledOn': - message = {'RemoteControl': {'LED': 'ON'}} - elif command == 'ledOff': - message = {'RemoteControl': {'LED': 'OFF'}} - elif command == 'countClear': - message = {'count': 0} - elif command == 'mosqClean': - message = {'MosqDeviceClean': speed} - else: - return Response({'error': 'Invalid command'}, status=400) +class DeviceRemote(mixins.ListModelMixin, + mixins.RetrieveModelMixin, + RoleMixin, + GenericViewSet): + serializer_class = DeviceRemoteSerializer + permission_classes = [IsAuthenticated] + filter_backends = [SearchFilter, OrderingFilter] - topic = f'solarmosquitolamp/devices/{device_id}/control' - publish.single(topic, payload=json.dumps(message), hostname='8.217.112.255', port=1883) + def remote_control(self, request, device_id): + command = request.data.get('command') + speed = request.data.get('speed') - return Response({'success': True}) \ No newline at end of file + if command == 'ledOn': + message = {'RemoteControl': {'LED': 'ON'}} + elif command == 'ledOff': + message = {'RemoteControl': {'LED': 'OFF'}} + elif command == 'countClear': + message = {'count': 0} + elif command == 'mosqClean': + message = {'MosqDeviceClean': speed} + else: + return Response({'error': 'Invalid command'}, status=400) + + topic = f'solarmosquitolamp/devices/{device_id}/control' + publish.single(topic, payload=json.dumps(message), hostname='8.217.112.255', port=1883) + + return Response({'success': True}) \ No newline at end of file