|
@@ -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'],
|
|
@@ -161,7 +169,7 @@ class IotCoreView(View):
|
|
|
return response.json(0, {'res': res})
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
def thing_regroup(request_dict, response):
|
|
@@ -245,7 +253,7 @@ class IotCoreView(View):
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
def clear_Iot_Cerm(self, request_dict, response):
|
|
|
serial_number = request_dict.get('serial_number', None)
|
|
@@ -282,7 +290,7 @@ class IotCoreView(View):
|
|
|
return response.json(10044)
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
def get_s3_pull_key(self, request_dict, response, request):
|
|
|
# 通用发布主题通知
|
|
@@ -311,7 +319,7 @@ class IotCoreView(View):
|
|
|
return response.json(0, MSG)
|
|
|
except Exception as e:
|
|
|
# print(e)
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
def get_s3_key_return_msg(self, endpoint):
|
|
|
MSG = {}
|
|
@@ -362,7 +370,7 @@ class IotCoreView(View):
|
|
|
res = {'endpoint': endpoint, 'token_iot_number': token_iot_number}
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(e))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
def pcGetIotInfo(self, request_dict, response):
|
|
|
# PC工具获取IoT数据
|
|
@@ -388,4 +396,9 @@ class IotCoreView(View):
|
|
|
res = {'endpoint': endpoint, 'token_iot_number': token_iot_number}
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
- return response.json(500, repr(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)
|