feat: device mqtt remote

This commit is contained in:
xianfx 2024-03-10 20:59:29 +08:00
parent 2b87aec8f9
commit 0bfb2ee9af
3 changed files with 24 additions and 11 deletions

View File

@ -252,3 +252,8 @@ class DeviceInfoSerializer(serializers.ModelSerializer):
'point_y', 'point_y',
] ]
class DeviceRemoteSerializer(serializers.ModelSerializer):
class Meta:
model = DeviceInfo
fields = []

View File

@ -6,7 +6,7 @@ from .views import (
WeatherLogListAPIView, WeatherLogListAPIView,
DeviceInfoAPIView, DeviceInfoAPIView,
WeatherStationInfoAPIView, WeatherStationInfoAPIView,
remote_control, DeviceRemoteViewSet,
) )
@ -18,6 +18,5 @@ 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('device/remote/', remote_control, name='device-remote'), path('device/<device_id>/remote/', DeviceRemoteViewSet.as_view({'post': 'remote'}), name='device-remote'),
] ]

View File

@ -3,7 +3,8 @@ import pytz
import json import json
import paho.mqtt.publish as publish import paho.mqtt.publish as publish
# import paho.mqtt.subscribe as subscribe # import paho.mqtt.subscribe as subscribe
from rest_framework import mixins from rest_framework import decorators, mixins
from rest_framework.viewsets import GenericViewSet 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 (
@ -215,15 +216,16 @@ class DeviceInfoAPIView(ListAPIView, RoleMixin):
return queryset_list return queryset_list
class DeviceRemote(mixins.ListModelMixin, class DeviceRemoteViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin, mixins.RetrieveModelMixin,
RoleMixin, RoleMixin,
GenericViewSet): GenericViewSet):
serializer_class = DeviceRemoteSerializer serializer_class = DeviceRemoteSerializer
permission_classes = [IsAuthenticated] permission_classes = [IsAuthenticated]
filter_backends = [SearchFilter, OrderingFilter] filter_backends = [SearchFilter, OrderingFilter]
def remote_control(self, request, device_id): @decorators.action(methods='POST', detail=True)
def remote(self, request, device_id=None):
command = request.data.get('command') command = request.data.get('command')
speed = request.data.get('speed') speed = request.data.get('speed')
@ -242,3 +244,10 @@ class DeviceRemote(mixins.ListModelMixin,
publish.single(topic, payload=json.dumps(message), hostname='8.217.112.255', port=1883) publish.single(topic, payload=json.dumps(message), hostname='8.217.112.255', port=1883)
return Response({'success': True}) return Response({'success': True})
def get_object(self):
queryset = self.get_queryset()
filter_kwargs = {'device_id': self.kwargs['device_id']}
obj = queryset.filter(**filter_kwargs).first()
self.check_object_permissions(self.request, obj)
return obj