|
@@ -235,8 +235,8 @@ class SerialNumberView(View):
|
|
|
res = self.get_uid_info_by_serial(company_serial.id)
|
|
|
return response.json(0, res)
|
|
|
elif company_serial.status == 3: # 已占用
|
|
|
- res = self.log_and_send_email(request, company_serial.id, serial_number, now_time)
|
|
|
- if not res:
|
|
|
+ sync_success = self.sync_serial_data_and_log(request, company_serial.id, serial_number, now_time)
|
|
|
+ if not sync_success:
|
|
|
return response.json(10042)
|
|
|
return response.json(0, self.get_uid_info_by_serial(company_serial.id))
|
|
|
except Exception as e:
|
|
@@ -351,94 +351,105 @@ class SerialNumberView(View):
|
|
|
LOGGER.info('发送提醒邮件异常: error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
- def log_and_send_email(request, company_serial_id, serial, now_time):
|
|
|
+ def sync_serial_data_and_log(request, company_serial_id, serial, now_time):
|
|
|
"""
|
|
|
- 记录序列号占用操作日志及发送邮件通知,(美服、欧服已占用序列号将执行数据同步)
|
|
|
+ 同步序列号数据和记录日志
|
|
|
@param company_serial_id: 企业关联序列号 ID
|
|
|
@param request: 请求
|
|
|
@param serial: 序列号
|
|
|
@param now_time: 当前时间
|
|
|
+ @return : bool
|
|
|
"""
|
|
|
- send_email = S3Email()
|
|
|
+ ip = CommonService.get_ip_address(request)
|
|
|
+ operation = '{}序列号占用,'.format(serial)
|
|
|
+ log = {
|
|
|
+ 'ip': ip,
|
|
|
+ 'user_id': 1,
|
|
|
+ 'status': 200,
|
|
|
+ 'time': now_time,
|
|
|
+ 'url': 'serialNumber/attachUID',
|
|
|
+ }
|
|
|
+ sync_result = False
|
|
|
+ # 测试服和国内服不同步
|
|
|
+ if CONFIG_INFO == 'test' or CONFIG_INFO == 'cn':
|
|
|
+ return sync_result
|
|
|
try:
|
|
|
- if CONFIG_INFO == 'test':
|
|
|
- return {}
|
|
|
# 判断当前序列号是否绑定UID
|
|
|
uid_serial_qs = UIDCompanySerialModel.objects.filter(company_serial_id=company_serial_id)
|
|
|
if uid_serial_qs.exists():
|
|
|
- email_content = '{}序列号已占用当前数据库已绑定UID: {}'.format(CONFIG_INFO, serial)
|
|
|
- send_email.faEmail(email_content, 'servers@ansjer.com')
|
|
|
- return {}
|
|
|
- if CONFIG_INFO == 'eur' or CONFIG_INFO == 'cn': # 查美服
|
|
|
- response = requests.get("https://www.dvema.com/serialNumber/get-status",
|
|
|
- params={'serial_number': serial}, timeout=15)
|
|
|
- else: # 查国服
|
|
|
- response = requests.get("https://www.zositechc.cn/serialNumber/get-status",
|
|
|
- params={'serial_number': serial}, timeout=15)
|
|
|
- ip = CommonService.get_ip_address(request)
|
|
|
- operation = '{}序列号占用:{}'.format(CONFIG_INFO, serial)
|
|
|
- log = {
|
|
|
- 'ip': ip,
|
|
|
- 'user_id': 1,
|
|
|
- 'status': 200,
|
|
|
- 'time': now_time,
|
|
|
- 'operation': operation,
|
|
|
- 'url': 'serialNumber/attachUID',
|
|
|
- }
|
|
|
- if not response.status_code == 200:
|
|
|
- operation += '查询其它服UID数据异常响应状态:{}'.format(response.status_code)
|
|
|
+ operation += ',已绑定UID,不同步数据'
|
|
|
+ log['operation'] = operation
|
|
|
+ LogModel.objects.create(**log)
|
|
|
+ return sync_result
|
|
|
+
|
|
|
+ region = 'us'
|
|
|
+ # 欧洲服同步美洲服,美洲服同步国内服数据
|
|
|
+ if CONFIG_INFO == 'eur':
|
|
|
+ url = 'https://www.dvema.com/'
|
|
|
+ else:
|
|
|
+ region = 'cn'
|
|
|
+ url = 'https://www.zositechc.cn/'
|
|
|
+
|
|
|
+ url += 'serialNumber/get-status'
|
|
|
+ response = requests.get(url=url, params={'serial_number': serial}, timeout=15)
|
|
|
+
|
|
|
+ if response.status_code != 200:
|
|
|
+ operation += '查询{}服UID数据响应状态码异常:{}'.format(region, response.status_code)
|
|
|
log['operation'] = operation
|
|
|
LogModel.objects.create(**log)
|
|
|
- return {}
|
|
|
+ return sync_result
|
|
|
+
|
|
|
results = json.loads(response.text)
|
|
|
- if not results['result_code'] == 0:
|
|
|
- operation += '其它服绑定UID数据result_code: {}'.format(results['result_code'])
|
|
|
+ if results['result_code'] != 0:
|
|
|
+ operation += '查询{}服UID数据result_code异常:{}'.format(region, results['result_code'])
|
|
|
log['operation'] = operation
|
|
|
LogModel.objects.create(**log)
|
|
|
- return {}
|
|
|
- # 解析从其它服获取的UID数据
|
|
|
+ return sync_result
|
|
|
+
|
|
|
+ # 其它服没有绑定uid
|
|
|
uid_info = results['result']['uidInfo']
|
|
|
- if uid_info:
|
|
|
- if uid_info['p2p_type'] == 1: # 尚云UID不进行同步只进行 邮寄警报
|
|
|
- # 邮件通知
|
|
|
- email_content = '国内uid同步国外服类型为尚云不操作: {}'.format(serial)
|
|
|
- send_email.faEmail(email_content, 'servers@ansjer.com')
|
|
|
- else:
|
|
|
- # 同步UID数据
|
|
|
- uid_id = ''
|
|
|
- uid_qs = UIDModel.objects.filter(uid=uid_info['uid'])
|
|
|
- if uid_qs.exists():
|
|
|
- if uid_qs.first().status == 2: # 判断uid是否被使用
|
|
|
- email_content = '{}序列号已占用且{}已被使用:{}'.format(CONFIG_INFO, uid_info['uid'], serial)
|
|
|
- send_email.faEmail(email_content, 'servers@ansjer.com')
|
|
|
- return {}
|
|
|
- else:
|
|
|
- uid_qs.update(status=2, update_time=now_time)
|
|
|
- uid_id = uid_qs.first().id
|
|
|
- if not uid_id:
|
|
|
- uid_id = UIDModel.objects.create(**uid_info).id
|
|
|
- # 企业序列号关联 uid
|
|
|
- UIDCompanySerialModel.objects.create(uid_id=uid_id, company_serial_id=company_serial_id,
|
|
|
- add_time=now_time, update_time=now_time)
|
|
|
- # 成功后 修改企业序列号状态为2(已分配)
|
|
|
- CompanySerialModel.objects.filter(id=company_serial_id) \
|
|
|
- .update(status=2, update_time=now_time)
|
|
|
- SerialNumberView.sync_iot_core_data(serial)
|
|
|
- email_content = '{}序列号已占用已将其它服UID数据同步完成: {}'.format(CONFIG_INFO, serial)
|
|
|
- send_email.faEmail(email_content, 'servers@ansjer.com')
|
|
|
- operation += '同步成功'
|
|
|
+ if not uid_info:
|
|
|
+ operation += '{}服没有绑定UID'.format(region)
|
|
|
+ log['operation'] = operation
|
|
|
+ LogModel.objects.create(**log)
|
|
|
+ return sync_result
|
|
|
+ # 同步uid数据
|
|
|
+ if uid_info['p2p_type'] == 1:
|
|
|
+ operation += '尚云UID不同步数据'
|
|
|
+ else:
|
|
|
+ uid_qs = UIDModel.objects.filter(uid=uid_info['uid'])
|
|
|
+ if not uid_qs.exists():
|
|
|
+ uid_id = UIDModel.objects.create(**uid_info).id
|
|
|
+ if uid_qs.first().status == 2: # 判断uid是否被使用
|
|
|
+ operation += 'uid{}已被使用'.format(uid_info['uid'])
|
|
|
log['operation'] = operation
|
|
|
LogModel.objects.create(**log)
|
|
|
- return 'success'
|
|
|
- email_content = '{}序列号已占用其它服没有绑定UID: {}'.format(CONFIG_INFO, serial)
|
|
|
- send_email.faEmail(email_content, 'servers@ansjer.com')
|
|
|
- return {}
|
|
|
+ return sync_result
|
|
|
+ else:
|
|
|
+ uid_qs.update(status=2, update_time=now_time)
|
|
|
+ uid_id = uid_qs.first().id
|
|
|
+
|
|
|
+ # 企业序列号关联 uid
|
|
|
+ UIDCompanySerialModel.objects.create(
|
|
|
+ uid_id=uid_id, company_serial_id=company_serial_id, add_time=now_time, update_time=now_time)
|
|
|
+ # 修改企业序列号状态为2(绑定uid)
|
|
|
+ CompanySerialModel.objects.filter(id=company_serial_id) \
|
|
|
+ .update(status=2, update_time=now_time)
|
|
|
+ sync_iot_result = SerialNumberView.sync_iot_core_data(serial)
|
|
|
+
|
|
|
+ operation += '同步{}服uid数据成功,同步iot数据结果:{}'.format(region, sync_iot_result)
|
|
|
+ sync_result = True
|
|
|
+
|
|
|
+ # 记录日志
|
|
|
+ log['operation'] = operation
|
|
|
+ LogModel.objects.create(**log)
|
|
|
+ return sync_result
|
|
|
+
|
|
|
except Exception as e:
|
|
|
- email_content = '序列号占用同步数据异常: {},errLine:{}, errMsg:{}' \
|
|
|
- .format(serial, e.__traceback__.tb_lineno, repr(e))
|
|
|
- send_email.faEmail(email_content, 'servers@ansjer.com')
|
|
|
- send_email.faEmail(email_content, 'antony@ansjer.com')
|
|
|
- return {}
|
|
|
+ operation += '同步数据异常,error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e))
|
|
|
+ log['operation'] = operation
|
|
|
+ LogModel.objects.create(**log)
|
|
|
+ return sync_result
|
|
|
|
|
|
def do_get_uid(self, request_dict, response):
|
|
|
serial_number = request_dict.get('serial_number', None)
|
|
@@ -611,8 +622,8 @@ class SerialNumberView(View):
|
|
|
res = self.get_uid_info_by_serial(company_serial.id)
|
|
|
return response.json(0, res)
|
|
|
elif company_serial.status == 3: # 已占用
|
|
|
- res = self.log_and_send_email(request, company_serial.id, serial_number, now_time)
|
|
|
- if not res:
|
|
|
+ sync_success = self.sync_serial_data_and_log(request, company_serial.id, serial_number, now_time)
|
|
|
+ if not sync_success:
|
|
|
return response.json(10042)
|
|
|
return response.json(0, self.get_uid_info_by_serial(company_serial.id))
|
|
|
except Exception as e:
|