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('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'),
|
||||||
|
|
||||||
]
|
]
|
|
@ -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,23 +214,31 @@ class DeviceInfoAPIView(ListAPIView, RoleMixin):
|
||||||
|
|
||||||
return queryset_list
|
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':
|
class DeviceRemote(mixins.ListModelMixin,
|
||||||
message = {'RemoteControl': {'LED': 'ON'}}
|
mixins.RetrieveModelMixin,
|
||||||
elif command == 'ledOff':
|
RoleMixin,
|
||||||
message = {'RemoteControl': {'LED': 'OFF'}}
|
GenericViewSet):
|
||||||
elif command == 'countClear':
|
serializer_class = DeviceRemoteSerializer
|
||||||
message = {'count': 0}
|
permission_classes = [IsAuthenticated]
|
||||||
elif command == 'mosqClean':
|
filter_backends = [SearchFilter, OrderingFilter]
|
||||||
message = {'MosqDeviceClean': speed}
|
|
||||||
else:
|
|
||||||
return Response({'error': 'Invalid command'}, status=400)
|
|
||||||
|
|
||||||
topic = f'solarmosquitolamp/devices/{device_id}/control'
|
def remote_control(self, request, device_id):
|
||||||
publish.single(topic, payload=json.dumps(message), hostname='8.217.112.255', port=1883)
|
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