فهرست منبع

注册IOT创建线程调用Alexa接口

locky 3 ماه پیش
والد
کامیت
ee0e066a0d
1فایلهای تغییر یافته به همراه26 افزوده شده و 13 حذف شده
  1. 26 13
      Controller/IotCoreController.py

+ 26 - 13
Controller/IotCoreController.py

@@ -2,16 +2,18 @@
 # -*- coding: utf-8 -*-
 import hashlib
 import logging
+import threading
 import time
 import uuid
 from collections import OrderedDict
 
+import requests
 from django.views import View
 
 from Ansjer.config import AWS_IOT_GETS3_PULL_CHINA_ID, AWS_IOT_GETS3_PULL_CHINA_SECRET, \
     AWS_IOT_GETS3_PULL_FOREIGN_ID, AWS_IOT_GETS3_PULL_FOREIGN_SECRET, AWS_ARN, AWS_IOT_SES_ACCESS_CHINA_REGION, \
     AWS_IOT_SES_ACCESS_FOREIGN_REGION_ASIA, AWS_IOT_SES_ACCESS_FOREIGN_REGION_EUROPE, \
-    AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA, REGION_ID_LIST
+    AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA, REGION_ID_LIST, ALEXA_DOMAIN
 from Model.models import Device_Info, iotdeviceInfoModel, SerialNumberModel, UidSetModel
 from Object.IOTCore.IotObject import IOTClient
 from Object.ResponseObject import ResponseObject
@@ -103,7 +105,7 @@ class IotCoreView(View):
                 try:
                     SerialNumberModel.objects.get(serial_number=serial)
                 except:
-                    return response.json(444)
+                    return response.json(173)
 
                 thing_name_suffix = serial_number  # 物品名后缀
                 iot_device_info_qs = iotdeviceInfoModel.objects.filter(serial_number=serial)
@@ -140,17 +142,23 @@ class IotCoreView(View):
                 res = iotClient.register_to_iot_core(thingName, thingGroup, response)
 
                 token_iot_number = hashlib.md5((str(uuid.uuid1()) + str(int(time.time()))).encode('utf-8')).hexdigest()
-                iotdeviceInfoModel.objects.create(uid=uid,
-                                                  serial_number=serial,
-                                                  endpoint=res[0]['endpoint'],
-                                                  certificate_id=res[0]['certificateId'],
-                                                  certificate_pem=res[0]['certificatePem'],
-                                                  public_key=res[0]['publicKey'],
-                                                  private_key=res[0]['privateKey'],
-                                                  thing_name=res[1]['ThingName'],
-                                                  thing_groups=res[1]['thingGroupName'],
-                                                  token_iot_number=token_iot_number
-                                                  )
+                
+                data = {
+                    'uid': uid,
+                    'serial_number': serial,
+                    'endpoint': res[0]['endpoint'],
+                    'certificate_id': res[0]['certificateId'],
+                    'certificate_pem': res[0]['certificatePem'],
+                    'public_key': res[0]['publicKey'],
+                    'private_key': res[0]['privateKey'],
+                    'thing_name': res[1]['ThingName'],
+                    'thing_groups': res[1]['thingGroupName'],
+                    'token_iot_number': token_iot_number
+                }
+                # 创建线程调用Alexa接口
+                threading.Thread(target=call_alexa_api, kwargs=data).start()
+                
+                iotdeviceInfoModel.objects.create(**data)
                 res = {
                     'certificateId': res[0]['certificateId'],
                     'certificatePem': res[0]['certificatePem'],
@@ -389,3 +397,8 @@ class IotCoreView(View):
             return response.json(0, res)
         except Exception as e:
             return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+
+def call_alexa_api(**kwargs):
+    url = ALEXA_DOMAIN + 'device/creatOrUpdateIotInfo'
+    requests.post(url=url, data=kwargs, timeout=30)