From 967b2727f209799621a2c244d4e7356b71386123 Mon Sep 17 00:00:00 2001 From: Jay Huang Date: Mon, 27 Nov 2023 17:48:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86mqtt=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=94=A8=E4=BA=8E=E8=BF=9C=E7=A8=8B=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E7=81=AD=E8=9A=8A=E7=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/counter/api/serializers.py | 1 + apps/mosquito/api/urls.py | 2 ++ apps/mosquito/api/views.py | 23 ++++++++++++++++++++++- mosqkiller/settings.py | 6 +++--- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/apps/counter/api/serializers.py b/apps/counter/api/serializers.py index d07861d..9cb51d3 100644 --- a/apps/counter/api/serializers.py +++ b/apps/counter/api/serializers.py @@ -245,6 +245,7 @@ class DeviceInfoSerializer(serializers.ModelSerializer): fields = [ 'device_id', 'device_name', + 'chip_type', 'status', 'count', 'signal', diff --git a/apps/mosquito/api/urls.py b/apps/mosquito/api/urls.py index 52828d3..5c49ea5 100644 --- a/apps/mosquito/api/urls.py +++ b/apps/mosquito/api/urls.py @@ -6,6 +6,7 @@ from .views import ( WeatherLogListAPIView, DeviceInfoAPIView, WeatherStationInfoAPIView, + remote_control, ) @@ -17,5 +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'), ] \ No newline at end of file diff --git a/apps/mosquito/api/views.py b/apps/mosquito/api/views.py index 1f6b741..26f9aeb 100644 --- a/apps/mosquito/api/views.py +++ b/apps/mosquito/api/views.py @@ -1,5 +1,8 @@ import re import pytz +import json +import paho.mqtt.publish as publish +from rest_framework.decorators import api_view from rest_framework.generics import ( ListAPIView, RetrieveAPIView, @@ -207,4 +210,22 @@ class DeviceInfoAPIView(ListAPIView, RoleMixin): if query.org == self.request.user.org or query.org.id in child] queryset_list = queryset_list.filter(device_id__in=device_ids) - return queryset_list \ No newline at end of file + return queryset_list + +@api_view(['POST']) +def remote_control(request, device_id): + command = request.data.get('command') + + if command == 'ledOn': + message = {'RemoteControl': {'LED': 'ON'}} + elif command == 'ledOff': + message = {'RemoteControl': {'LED': 'OFF'}} + elif command == 'countClear': + message = {'count': 0} + 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 diff --git a/mosqkiller/settings.py b/mosqkiller/settings.py index e3ac92c..81b1651 100644 --- a/mosqkiller/settings.py +++ b/mosqkiller/settings.py @@ -168,9 +168,9 @@ REST_FRAMEWORK = { 'rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer', ), - # 'DEFAULT_PARSER_CLASSES': ( - # 'rest_framework.parsers.JSONParser', - # ), + 'DEFAULT_PARSER_CLASSES': ( + 'rest_framework.parsers.JSONParser', + ), 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.SessionAuthentication',