feat: TODO device remote ctl api
This commit is contained in:
parent
409aa8a441
commit
2b87aec8f9
|
@ -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/<str:device_id>/', remote_control, name='remote_control'),
|
||||
path('device/remote/', remote_control, name='device-remote'),
|
||||
|
||||
]
|
|
@ -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})
|
||||
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})
|
Loading…
Reference in New Issue