|
@@ -9,12 +9,15 @@
|
|
|
import base64
|
|
|
import json
|
|
|
import logging
|
|
|
+import time
|
|
|
+from decimal import Decimal
|
|
|
|
|
|
import requests
|
|
|
from Crypto.Cipher import AES
|
|
|
|
|
|
from Ansjer.config import unicomAppUrl, unicomAppId, unicomAppSecret, unicomTenantId, \
|
|
|
- unicomEncodeKey, unicomIvKey, unicomUserName, unicomPassword, unicomPushKey, SERVER_DOMAIN_SSL
|
|
|
+ unicomEncodeKey, unicomIvKey, unicomUserName, unicomPassword, unicomPushKey, SERVER_DOMAIN_SSL, UNICOM_KEY
|
|
|
+from Model.models import UnicomDeviceInfo
|
|
|
from Object.RedisObject import RedisObject
|
|
|
from Object.utils import SM3Util
|
|
|
from Object.utils.SymmetricCryptoUtil import AESencrypt
|
|
@@ -87,7 +90,7 @@ class UnicomObjeect:
|
|
|
|
|
|
def get_encode_password(self):
|
|
|
"""
|
|
|
- 获取对称加密
|
|
|
+ 获取对称加密AES
|
|
|
@return: encrypt_pwd
|
|
|
"""
|
|
|
aes = AESencrypt(self.encodeKey.encode('utf-8'), AES.MODE_CBC, self.ivKey.encode('utf-8'),
|
|
@@ -101,6 +104,10 @@ class UnicomObjeect:
|
|
|
生成令牌
|
|
|
@return: token
|
|
|
"""
|
|
|
+ redis = RedisObject()
|
|
|
+ token = redis.get_data(UNICOM_KEY)
|
|
|
+ if token:
|
|
|
+ return token
|
|
|
url = self.appUrl + '/auc/oauth/token'
|
|
|
pwd = self.get_encode_password()
|
|
|
body = {'username': self.username, 'password': pwd, 'grant_type': 'password', 'scope': 'server'}
|
|
@@ -108,7 +115,11 @@ class UnicomObjeect:
|
|
|
headers['Authorization'] = self.get_login_authorization()
|
|
|
response_data = requests.post(url, data=body, headers=headers)
|
|
|
response_data = json.loads(response_data.text)
|
|
|
- return response_data['access_token']
|
|
|
+ token = response_data['access_token']
|
|
|
+ expires_in = response_data['expires_in']
|
|
|
+ redis.CONN.setnx(UNICOM_KEY, token)
|
|
|
+ redis.CONN.expire(UNICOM_KEY, int(expires_in))
|
|
|
+ return token
|
|
|
|
|
|
def refresh_token(self, refresh_token):
|
|
|
"""
|
|
@@ -141,7 +152,8 @@ class UnicomObjeect:
|
|
|
@param re_params:
|
|
|
@return:
|
|
|
"""
|
|
|
- url = self.appUrl + '/platform/api/device/verify-device'
|
|
|
+ re_params['appId'] = self.appId
|
|
|
+ url = self.appUrl + '/cop-platform/api/device/verify-device'
|
|
|
return requests.get(url, params=re_params, headers=self.business_unify_headers())
|
|
|
|
|
|
def query_device_status(self, **re_params):
|
|
@@ -150,8 +162,11 @@ class UnicomObjeect:
|
|
|
@param re_params:
|
|
|
@return:
|
|
|
"""
|
|
|
- url = self.appUrl + '/platform/api/device/device-status'
|
|
|
- return requests.get(url, params=re_params, headers=self.business_unify_headers())
|
|
|
+ url = self.appUrl + '/cop-platform/api/device/detail'
|
|
|
+ re_params['appId'] = self.appId
|
|
|
+ headers = self.business_unify_headers()
|
|
|
+ headers['content-type'] = 'application/json'
|
|
|
+ return requests.get(url, params=re_params, headers=headers)
|
|
|
|
|
|
def update_device_state(self, **re_data):
|
|
|
"""
|
|
@@ -159,9 +174,10 @@ class UnicomObjeect:
|
|
|
@param re_data:
|
|
|
@return:
|
|
|
"""
|
|
|
- url = self.appUrl + '/platform/api/device/async-device-state'
|
|
|
+ url = self.appUrl + '/cop-platform/api/device/async-device-state'
|
|
|
headers = self.business_unify_headers()
|
|
|
headers['content-type'] = 'application/json'
|
|
|
+ re_data['appId'] = self.appId
|
|
|
re_data['callbackUrl'] = SERVER_DOMAIN_SSL + 'unicom/api/device-status-change'
|
|
|
return requests.post(url, data=json.dumps(re_data), headers=headers)
|
|
|
|
|
@@ -171,8 +187,10 @@ class UnicomObjeect:
|
|
|
@param re_params:
|
|
|
@return:
|
|
|
"""
|
|
|
- url = self.appUrl + '/platform/api/usage/device-usage-history'
|
|
|
- return requests.get(url, params=re_params, headers=self.business_unify_headers())
|
|
|
+ headers = self.business_unify_headers()
|
|
|
+ re_params['appId'] = self.appId
|
|
|
+ url = self.appUrl + '/cop-platform/api/usage/device-usage-history'
|
|
|
+ return requests.get(url, params=re_params, headers=headers)
|
|
|
|
|
|
def query_current_renew_list_usage_details(self, **re_params):
|
|
|
"""
|
|
@@ -180,7 +198,8 @@ class UnicomObjeect:
|
|
|
@param re_params:
|
|
|
@return:
|
|
|
"""
|
|
|
- url = self.appUrl + '/platform/api/usage/current-renewlist-usage-details'
|
|
|
+ url = self.appUrl + '/cop-platform/api/device/current-package-usage-details'
|
|
|
+ re_params['appId'] = self.appId
|
|
|
return requests.get(url, params=re_params, headers=self.business_unify_headers())
|
|
|
|
|
|
def get_device_batch_detail(self, **re_data):
|
|
@@ -232,7 +251,7 @@ class UnicomObjeect:
|
|
|
return None
|
|
|
|
|
|
@staticmethod
|
|
|
- def get_flow_total_usage(key, expire=0, **usage_data):
|
|
|
+ def get_flow_total_usage(key, expire=600, **usage_data):
|
|
|
"""
|
|
|
设备当前队列用量详情(实现缓存)
|
|
|
@param key: 缓存key
|
|
@@ -241,14 +260,44 @@ class UnicomObjeect:
|
|
|
@return: 返回结果dict
|
|
|
"""
|
|
|
redis = RedisObject()
|
|
|
- flow_usage_details = redis.get_data(key)
|
|
|
- if flow_usage_details:
|
|
|
- flow_usage_details = json.loads(flow_usage_details)
|
|
|
+ sim_flow_used_total = redis.get_data(key)
|
|
|
+ if sim_flow_used_total:
|
|
|
+ return Decimal(sim_flow_used_total).quantize(Decimal('0.00'))
|
|
|
else:
|
|
|
- flow_usage_details = UnicomObjeect().query_current_renew_list_usage_details(**usage_data)
|
|
|
+ # API查询设备信息其中包含周期流量使用
|
|
|
+ flow_usage_details = UnicomObjeect().query_device_status(**usage_data)
|
|
|
flow_usage_details = UnicomObjeect().get_text_dict(flow_usage_details)
|
|
|
- redis.set_data(key=key, val=json.dumps(flow_usage_details), expire=expire)
|
|
|
- return flow_usage_details
|
|
|
+ if flow_usage_details and flow_usage_details.get('success'):
|
|
|
+ cycle_total = flow_usage_details['data'].get('simCycleUsedFlow', 0)
|
|
|
+ else:
|
|
|
+ cycle_total = 0
|
|
|
+ # 查询SIM卡信息
|
|
|
+ sim_qs = UnicomDeviceInfo.objects.filter(iccid=usage_data['iccid'])
|
|
|
+ if not sim_qs:
|
|
|
+ return cycle_total
|
|
|
+ cycle_total = Decimal(cycle_total).quantize(Decimal('0.00'))
|
|
|
+ sim_vo = sim_qs.first()
|
|
|
+ n_time = int(time.time())
|
|
|
+
|
|
|
+ # 判断数据库周期流量用量 是否大于API查询出来的周期用量 如果是则判定进入了下一个周期
|
|
|
+ if sim_vo.sim_cycle_used_flow != 0 and sim_vo.sim_cycle_used_flow > cycle_total:
|
|
|
+ sim_used_flow = sim_vo.sim_used_flow + sim_vo.sim_cycle_used_flow
|
|
|
+ sim_qs.update(
|
|
|
+ sim_used_flow=sim_used_flow,
|
|
|
+ sim_cycle_used_flow=cycle_total,
|
|
|
+ updated_time=n_time
|
|
|
+ )
|
|
|
+ # 队列用量历史总量 + 上一个周期流量 + 当前周期流量 = 总消耗流量
|
|
|
+ sim_flow_used_total = sim_used_flow + cycle_total
|
|
|
+ elif cycle_total > sim_vo.sim_cycle_used_flow: # API周期用量大于当前数据库用量则更新记录
|
|
|
+ sim_qs.update(sim_cycle_used_flow=cycle_total, updated_time=n_time)
|
|
|
+ # 队列用量历史总量 + 当前周期流量 = 总消耗流量
|
|
|
+ sim_flow_used_total = sim_vo.sim_used_flow + cycle_total
|
|
|
+ else:
|
|
|
+ sim_flow_used_total = sim_vo.sim_used_flow + sim_vo.sim_cycle_used_flow
|
|
|
+ redis.CONN.setnx(key, str(sim_flow_used_total))
|
|
|
+ redis.CONN.expire(key, expire)
|
|
|
+ return sim_flow_used_total
|
|
|
|
|
|
@staticmethod
|
|
|
def get_flow_usage_total(iccid):
|
|
@@ -258,14 +307,9 @@ class UnicomObjeect:
|
|
|
@return: flow_total_usage 当前套餐总已使用流量
|
|
|
"""
|
|
|
flow_key = 'ASJ:UNICOM:FLOW:{}'
|
|
|
- usage_data = {'iccid': iccid, 'realTime': True}
|
|
|
+ usage_data = {'iccid': iccid}
|
|
|
expire_time = 60 * 10 + 60
|
|
|
- flow_usage_details = UnicomObjeect().get_flow_total_usage(flow_key.format(iccid), expire_time, **usage_data)
|
|
|
- # 当月实际总使用流量
|
|
|
- flow_total_usage = 0
|
|
|
- if flow_usage_details and flow_usage_details['success']:
|
|
|
- flow_total_usage = flow_usage_details['data']['flowTotalUsage']
|
|
|
- return flow_total_usage
|
|
|
+ return UnicomObjeect().get_flow_total_usage(flow_key.format(iccid), expire_time, **usage_data)
|
|
|
|
|
|
@staticmethod
|
|
|
def change_device_to_activate(iccid):
|
|
@@ -278,7 +322,7 @@ class UnicomObjeect:
|
|
|
re_data = {'iccid': iccid}
|
|
|
result = UnicomObjeect().query_device_status(**re_data)
|
|
|
res_dict = UnicomObjeect().get_text_dict(result)
|
|
|
- # 状态不等于1(激活)时进行激活 1:激活;2:停用
|
|
|
+ # 状态不等于1(激活)时进行激活 1:激活;3:停用
|
|
|
if res_dict['data']['status'] != 1:
|
|
|
re_data = {"iccid": iccid, "status": 1}
|
|
|
UnicomObjeect().update_device_state(**re_data)
|
|
@@ -293,7 +337,7 @@ class UnicomObjeect:
|
|
|
@return:
|
|
|
"""
|
|
|
if iccid:
|
|
|
- re_data = {"iccid": iccid, "status": 2}
|
|
|
+ re_data = {"iccid": iccid, "status": 3}
|
|
|
response = UnicomObjeect().update_device_state(**re_data)
|
|
|
logger.info('停用iccid响应结果:{}'.format(response.text))
|
|
|
# 查询是否停用成功
|
|
@@ -302,7 +346,7 @@ class UnicomObjeect:
|
|
|
res_dict = UnicomObjeect().get_text_dict(result)
|
|
|
logger.info('查询iccid状态:{}'.format(res_dict))
|
|
|
if res_dict['data']['status'] != 3:
|
|
|
- re_data['status'] = 2
|
|
|
+ re_data['status'] = 3
|
|
|
response = UnicomObjeect().update_device_state(**re_data)
|
|
|
logger.info('再次停卡:{}'.format(response.text))
|
|
|
return True
|
|
@@ -328,5 +372,3 @@ class UnicomObjeect:
|
|
|
return 0
|
|
|
traffic_dict = json.loads(traffic_val)
|
|
|
return traffic_dict['data']['flowTotalUsage']
|
|
|
-
|
|
|
-
|