فهرست منبع

Alexa请求IoT Core下发MQTT消息添加唤醒设备主题

locky 4 سال پیش
والد
کامیت
1fcf6059b9
1فایلهای تغییر یافته به همراه8 افزوده شده و 8 حذف شده
  1. 8 8
      Controller/IotCoreController.py

+ 8 - 8
Controller/IotCoreController.py

@@ -229,7 +229,7 @@ class IotCoreView(View):
             return response.json(444)
 
     def request_publish_message(self, request_dict, response, request):
-        # Alexa请求IoT Core下发MQTT消息通知设备开始或停止推流
+        # Alexa请求IoT Core下发MQTT消息通知设备开始或停止推流,或唤醒设备
         UID = request_dict.get('UID', None)
         MSG = request_dict.get('MSG', None)
 
@@ -244,19 +244,19 @@ class IotCoreView(View):
             uid = device_info_qs[0]['UID']
             serial_number = device_info_qs[0]['serial_number']
             # 如果device_info表的serial_number不为空,物品名为'Ansjer_Device_序列号'
-            ThingNameSuffix = serial_number if serial_number != '' else uid
-
-            thing_name = ThingNameSuffix
+            thing_name_suffix = serial_number if serial_number != '' else uid
             # 获取数据组织将要请求的url
-            iot = iotdeviceInfoModel.objects.filter(thing_name__contains=thing_name).values('thing_name', 'endpoint',
-                                                                                            'token_iot_number')
+            iot = iotdeviceInfoModel.objects.filter(thing_name__contains=thing_name_suffix).values('thing_name',
+                                                                                                   'endpoint',
+                                                                                                   'token_iot_number')
             if not iot.exists():
                 return response.json(10043)
             thing_name = iot[0]['thing_name'][14:]  # IoT core上的物品名: Ansjer_Device_ + 序列号+企业编码/uid
             endpoint = iot[0]['endpoint']
             Token = iot[0]['token_iot_number']
             # Token = '297a601b3925e04daab5a60280650e09'
-            topic_name = thing_name + '_rtsp_topic'     # MQTT主题
+            topic_suffix = '_power_topic' if 'Turn' in MSG else '_rtsp_topic'
+            topic_name = thing_name + topic_suffix     # MQTT主题
 
             # api doc: https://docs.aws.amazon.com/zh_cn/iot/latest/developerguide/http.html
             # url: https://IoT_data_endpoint/topics/url_encoded_topic_name?qos=1
@@ -266,7 +266,7 @@ class IotCoreView(View):
             signature = self.rsa_sign(Token)  # Token签名
             headers = {'x-amz-customauthorizer-name': authorizer_name, 'Token': Token,
                        'x-amz-customauthorizer-signature': signature}
-            params = {'rtsp_command': MSG}
+            params = {'command': MSG}
             r = requests.post(url=url, headers=headers, json=params, timeout=2)
             if r.status_code == 200:
                 res = r.json()