feat: device mqtt remote
This commit is contained in:
parent
2b87aec8f9
commit
0bfb2ee9af
|
@ -252,3 +252,8 @@ class DeviceInfoSerializer(serializers.ModelSerializer):
|
|||
'point_y',
|
||||
]
|
||||
|
||||
|
||||
class DeviceRemoteSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = DeviceInfo
|
||||
fields = []
|
||||
|
|
|
@ -6,7 +6,7 @@ from .views import (
|
|||
WeatherLogListAPIView,
|
||||
DeviceInfoAPIView,
|
||||
WeatherStationInfoAPIView,
|
||||
remote_control,
|
||||
DeviceRemoteViewSet,
|
||||
)
|
||||
|
||||
|
||||
|
@ -18,6 +18,5 @@ 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('device/remote/', remote_control, name='device-remote'),
|
||||
|
||||
]
|
||||
path('device/<device_id>/remote/', DeviceRemoteViewSet.as_view({'post': 'remote'}), name='device-remote'),
|
||||
]
|
||||
|
|
|
@ -3,7 +3,8 @@ import pytz
|
|||
import json
|
||||
import paho.mqtt.publish as publish
|
||||
# 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.decorators import api_view
|
||||
from rest_framework.generics import (
|
||||
|
@ -215,15 +216,16 @@ class DeviceInfoAPIView(ListAPIView, RoleMixin):
|
|||
return queryset_list
|
||||
|
||||
|
||||
class DeviceRemote(mixins.ListModelMixin,
|
||||
mixins.RetrieveModelMixin,
|
||||
RoleMixin,
|
||||
GenericViewSet):
|
||||
class DeviceRemoteViewSet(mixins.ListModelMixin,
|
||||
mixins.RetrieveModelMixin,
|
||||
RoleMixin,
|
||||
GenericViewSet):
|
||||
serializer_class = DeviceRemoteSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
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')
|
||||
speed = request.data.get('speed')
|
||||
|
||||
|
@ -241,4 +243,11 @@ class DeviceRemote(mixins.ListModelMixin,
|
|||
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})
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue