|
@@ -1,30 +1,24 @@
|
|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
-import logging
|
|
|
-import os
|
|
|
import hashlib
|
|
|
-import json
|
|
|
+import logging
|
|
|
import time
|
|
|
import uuid
|
|
|
-import boto3
|
|
|
-import requests
|
|
|
|
|
|
+import requests
|
|
|
from django.views import View
|
|
|
-from Ansjer.config import BASE_DIR, AWS_IOT_GETS3_PULL_CHINA_ID, AWS_IOT_GETS3_PULL_CHINA_SECRET, \
|
|
|
+
|
|
|
+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, SERVER_TYPE
|
|
|
-from base64 import b64encode, encodebytes
|
|
|
+ AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA
|
|
|
from Controller.DeviceConfirmRegion import Device_Region
|
|
|
-from Model.models import Device_User, Device_Info, iotdeviceInfoModel, UIDCompanySerialModel, \
|
|
|
- SerialNumberModel
|
|
|
+from Model.models import Device_Info, iotdeviceInfoModel, SerialNumberModel, UidSetModel
|
|
|
from Object.IOTCore.IotObject import IOTClient
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Service.CommonService import CommonService
|
|
|
|
|
|
-import OpenSSL.crypto as ct
|
|
|
-
|
|
|
|
|
|
class IotCoreView(View):
|
|
|
|
|
@@ -51,7 +45,7 @@ class IotCoreView(View):
|
|
|
elif operation == 'getS3PullKey':
|
|
|
return self.get_s3_pull_key(request_dict, response, request)
|
|
|
elif operation == 'thingRegroup':
|
|
|
- return self.thing_regroup(request_dict, response, request)
|
|
|
+ return self.thing_regroup(request_dict, response)
|
|
|
elif operation == 'pcGetIotInfo':
|
|
|
return self.pcGetIotInfo(request_dict, response)
|
|
|
else:
|
|
@@ -92,8 +86,8 @@ class IotCoreView(View):
|
|
|
|
|
|
uid = request_dict.get('uid', '')
|
|
|
uid_code = request_dict.get('uid_code', None)
|
|
|
- companyMark = '11A'
|
|
|
- if not uid: # 传序列号
|
|
|
+ company_mark = '11A'
|
|
|
+ if not uid: # 传序列号
|
|
|
serial_number = request_dict.get('serial_number', None)
|
|
|
serial_number_code = request_dict.get('serial_number_code', None)
|
|
|
if not all([serial_number, serial_number_code]):
|
|
@@ -105,22 +99,22 @@ class IotCoreView(View):
|
|
|
return response.json(404)
|
|
|
|
|
|
serial = serial_number[0:6]
|
|
|
- companyMark = serial_number[-3:]
|
|
|
+ company_mark = serial_number[-3:]
|
|
|
try:
|
|
|
SerialNumberModel.objects.get(serial_number=serial)
|
|
|
except:
|
|
|
return response.json(444)
|
|
|
|
|
|
- ThingNameSuffix = serial_number # 物品名后缀
|
|
|
+ thing_name_suffix = serial_number # 物品名后缀
|
|
|
iot_device_info_qs = iotdeviceInfoModel.objects.filter(serial_number=serial)
|
|
|
- else: # 传uid
|
|
|
+ else: # 传uid
|
|
|
# uid编码解码校验
|
|
|
uid_code = CommonService.decode_data(uid_code)
|
|
|
if uid != uid_code:
|
|
|
return response.json(404)
|
|
|
|
|
|
- serial = '' # iot_deviceInfo表写入serial_number为''
|
|
|
- ThingNameSuffix = uid # 物品名后缀
|
|
|
+ serial = '' # iot_deviceInfo表写入serial_number为''
|
|
|
+ thing_name_suffix = uid # 物品名后缀
|
|
|
iot_device_info_qs = iotdeviceInfoModel.objects.filter(uid=uid)
|
|
|
# 判断设备是否已注册过
|
|
|
if iot_device_info_qs.exists():
|
|
@@ -144,14 +138,9 @@ class IotCoreView(View):
|
|
|
iotClient = IOTClient(region_id)
|
|
|
|
|
|
# 拼接物品名
|
|
|
- if companyMark == '11A':
|
|
|
- ThingName = 'Ansjer_Device_' + ThingNameSuffix
|
|
|
- elif companyMark == '11L':
|
|
|
- ThingName = 'Loocam_Device_' + ThingNameSuffix
|
|
|
- else:
|
|
|
- ThingName = ThingNameSuffix
|
|
|
+ thingName = CommonService.get_thing_name(company_mark, thing_name_suffix)
|
|
|
thingGroup = device_version + '_' + language
|
|
|
- res = iotClient.register_to_iot_core(ThingName, thingGroup, response)
|
|
|
+ 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,
|
|
@@ -177,36 +166,40 @@ class IotCoreView(View):
|
|
|
print(e)
|
|
|
return response.json(500, repr(e))
|
|
|
|
|
|
- def thing_regroup(self, request_dict, response, request):
|
|
|
+ @staticmethod
|
|
|
+ def thing_regroup(request_dict, response):
|
|
|
# 物品重新分组
|
|
|
uid = request_dict.get('uid', '')
|
|
|
token = request_dict.get('token', None)
|
|
|
language = request_dict.get('language', None)
|
|
|
+ region_id = request_dict.get('region_id', None)
|
|
|
time_stamp = request_dict.get('time_stamp', None)
|
|
|
device_version = request_dict.get('device_version', None)
|
|
|
|
|
|
- if not all([token, language, time_stamp, device_version]):
|
|
|
- return response.json(444, {'param: token, language, time_stamp, device_version'})
|
|
|
+ if not all([token, language, region_id, time_stamp, device_version]):
|
|
|
+ return response.json(444)
|
|
|
|
|
|
# 时间戳token校验
|
|
|
if not CommonService.check_time_stamp_token(token, time_stamp):
|
|
|
return response.json(13)
|
|
|
|
|
|
- ip = CommonService.get_ip_address(request)
|
|
|
- region_id = Device_Region().get_device_region(ip)
|
|
|
- iotClient = IOTClient(region_id)
|
|
|
-
|
|
|
- ThingNameSuffix = uid
|
|
|
+ company_mark = '11A'
|
|
|
+ thing_name_suffix = uid
|
|
|
if not uid:
|
|
|
# 使用序列号
|
|
|
serial_number = request_dict.get('serial_number', None)
|
|
|
if not serial_number:
|
|
|
return response.json(444)
|
|
|
- ThingNameSuffix = serial_number
|
|
|
- thingName = 'Ansjer_Device_' + ThingNameSuffix
|
|
|
+ company_mark = serial_number[-3:]
|
|
|
+ thing_name_suffix = serial_number
|
|
|
+ uid = CommonService.query_uid_with_serial(serial_number)
|
|
|
+
|
|
|
+ uid_set_qs = UidSetModel.objects.filter(uid=uid)
|
|
|
+ thingName = CommonService.get_thing_name(company_mark, thing_name_suffix)
|
|
|
new_thingGroupName = (device_version + '_' + language).replace('.', '_') # 物品组命名不能包含'.'
|
|
|
|
|
|
try:
|
|
|
+ iotClient = IOTClient(int(region_id))
|
|
|
# 获取旧物品组
|
|
|
list_groups_res = iotClient.client.list_thing_groups_for_thing(thingName=thingName, maxResults=1)
|
|
|
old_thingGroupName = list_groups_res['thingGroups'][0]['groupName']
|
|
@@ -231,10 +224,7 @@ class IotCoreView(View):
|
|
|
, thingGroupsToRemove=[old_thingGroupName])
|
|
|
|
|
|
# 更新设备版本信息
|
|
|
- if uid:
|
|
|
- Device_Info.objects.filter(UID=uid).update(version=device_version)
|
|
|
- else:
|
|
|
- Device_Info.objects.filter(serial_number=serial_number).update(version=device_version)
|
|
|
+ uid_set_qs.update(version=device_version)
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
@@ -281,7 +271,7 @@ class IotCoreView(View):
|
|
|
Token = iot[0]['token_iot_number']
|
|
|
# Token = '297a601b3925e04daab5a60280650e09'
|
|
|
topic_suffix = '_power_topic' if 'Turn' in MSG else '_rtsp_topic'
|
|
|
- topic_name = thing_name + topic_suffix # MQTT主题
|
|
|
+ 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
|
|
@@ -329,14 +319,14 @@ class IotCoreView(View):
|
|
|
endpoint = iot[0]['endpoint']
|
|
|
MSG = self.get_s3_key_return_msg(endpoint)
|
|
|
|
|
|
- return response.json(0,MSG)
|
|
|
+ return response.json(0, MSG)
|
|
|
except Exception as e:
|
|
|
# print(e)
|
|
|
return response.json(500, repr(e))
|
|
|
|
|
|
- def get_s3_key_return_msg(self,endpoint):
|
|
|
+ def get_s3_key_return_msg(self, endpoint):
|
|
|
MSG = {}
|
|
|
- if 'cn-northwest-1' in endpoint :
|
|
|
+ if 'cn-northwest-1' in endpoint:
|
|
|
key = AWS_IOT_GETS3_PULL_CHINA_ID
|
|
|
secret = AWS_IOT_GETS3_PULL_CHINA_SECRET
|
|
|
arn = AWS_ARN[0]
|
|
@@ -345,11 +335,11 @@ class IotCoreView(View):
|
|
|
key = AWS_IOT_GETS3_PULL_FOREIGN_ID
|
|
|
secret = AWS_IOT_GETS3_PULL_FOREIGN_SECRET
|
|
|
arn = AWS_ARN[1]
|
|
|
- if 'ap-southeast-1' in endpoint :
|
|
|
+ if 'ap-southeast-1' in endpoint:
|
|
|
region_name = AWS_IOT_SES_ACCESS_FOREIGN_REGION_ASIA
|
|
|
- if 'eu-west-1' in endpoint :
|
|
|
+ if 'eu-west-1' in endpoint:
|
|
|
region_name = AWS_IOT_SES_ACCESS_FOREIGN_REGION_EUROPE
|
|
|
- if 'us-east-1' in endpoint :
|
|
|
+ if 'us-east-1' in endpoint:
|
|
|
region_name = AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA
|
|
|
|
|
|
MSG['AccessKeyId'] = key
|
|
@@ -369,10 +359,10 @@ class IotCoreView(View):
|
|
|
try:
|
|
|
if serial_number:
|
|
|
serial_number = serial_number[0:6]
|
|
|
- iot_info_qs = iotdeviceInfoModel.objects.filter(serial_number=serial_number).\
|
|
|
+ iot_info_qs = iotdeviceInfoModel.objects.filter(serial_number=serial_number). \
|
|
|
values('endpoint', 'token_iot_number')
|
|
|
else:
|
|
|
- iot_info_qs = iotdeviceInfoModel.objects.filter(uid=uid).\
|
|
|
+ iot_info_qs = iotdeviceInfoModel.objects.filter(uid=uid). \
|
|
|
values('endpoint', 'token_iot_number')
|
|
|
|
|
|
if not iot_info_qs.exists():
|
|
@@ -395,10 +385,10 @@ class IotCoreView(View):
|
|
|
try:
|
|
|
if serial_number:
|
|
|
serial_number = serial_number[0:6]
|
|
|
- iot_info_qs = iotdeviceInfoModel.objects.filter(serial_number=serial_number).\
|
|
|
+ iot_info_qs = iotdeviceInfoModel.objects.filter(serial_number=serial_number). \
|
|
|
values('endpoint', 'token_iot_number')
|
|
|
else:
|
|
|
- iot_info_qs = iotdeviceInfoModel.objects.filter(uid=uid).\
|
|
|
+ iot_info_qs = iotdeviceInfoModel.objects.filter(uid=uid). \
|
|
|
values('endpoint', 'token_iot_number')
|
|
|
|
|
|
if not iot_info_qs.exists():
|