|
@@ -149,9 +149,8 @@ class AiView(View):
|
|
|
# 关闭推送
|
|
|
if not all([appBundleId, app_type, token_val, uid, m_code]):
|
|
|
return response.json(444, 'appBundleId,app_type,token_val,uid,m_code')
|
|
|
- # 判断推送类型对应key是否存在
|
|
|
- print('push_type:', push_type)
|
|
|
|
|
|
+ # 判断推送类型对应key是否存在
|
|
|
if push_type == '0':
|
|
|
if appBundleId not in APNS_CONFIG.keys():
|
|
|
return response.json(904)
|
|
@@ -163,27 +162,26 @@ class AiView(View):
|
|
|
return response.json(904)
|
|
|
else:
|
|
|
return response.json(173)
|
|
|
- hasAiService = AiService.objects.filter(uid=uid,use_status=1)
|
|
|
- if not hasAiService.exists():
|
|
|
- return response.json(10053)
|
|
|
- nowTime = int(time.time())
|
|
|
- endTime = hasAiService.values('endTime')[0]['endTime']
|
|
|
- if nowTime >endTime:
|
|
|
- return response.json(10054)
|
|
|
- dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid)
|
|
|
- status = int(status)
|
|
|
- nowTime = int(time.time())
|
|
|
- if dvqs.exists():
|
|
|
- # 修改状态
|
|
|
- # dvqs.update(NotificationMode=status)
|
|
|
- uid_set_qs = UidSetModel.objects.filter(uid=uid)
|
|
|
- # uid配置信息是否存在
|
|
|
|
|
|
+ try:
|
|
|
+ hasAiService = AiService.objects.filter(uid=uid, use_status=1)
|
|
|
+ if not hasAiService.exists():
|
|
|
+ return response.json(10053)
|
|
|
+ nowTime = int(time.time())
|
|
|
+ endTime = hasAiService.values('endTime')[0]['endTime']
|
|
|
+ if nowTime > endTime:
|
|
|
+ return response.json(10054)
|
|
|
+
|
|
|
+ dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid)
|
|
|
+ status = int(status)
|
|
|
+ nowTime = int(time.time())
|
|
|
+ if not dvqs.exists():
|
|
|
+ return response.json(14)
|
|
|
+ uid_set_qs = UidSetModel.objects.filter(uid=uid)
|
|
|
if uid_set_qs.exists():
|
|
|
uid_set_id = uid_set_qs[0].id
|
|
|
qs_data = {
|
|
|
'updTime': nowTime,
|
|
|
-
|
|
|
}
|
|
|
if interval:
|
|
|
qs_data['detect_interval'] = int(interval)
|
|
@@ -207,13 +205,9 @@ class AiView(View):
|
|
|
topic_name = 'ansjer/generic/{}'.format(uid)
|
|
|
if status == 0: # 关闭
|
|
|
hasAiService.update(**qs_data)
|
|
|
- # UidPushModel.objects.filter(uid_set__uid=uid).delete()
|
|
|
- # 状态为0的时候删除redis缓存数据
|
|
|
- # self.do_delete_redis(uid)
|
|
|
-
|
|
|
# mqtt通知设备关闭AI识别功能
|
|
|
- msg = {'commandType': 'AIDisable'},
|
|
|
- req_success = self.requestPublishMqtt(uid, topic_name, msg)
|
|
|
+ msg = {'commandType': 'AIDisable'}
|
|
|
+ req_success = CommonService.req_publish_mqtt_msg(uid, topic_name, msg)
|
|
|
if not req_success:
|
|
|
return response.json(10044)
|
|
|
return response.json(0)
|
|
@@ -250,19 +244,9 @@ class AiView(View):
|
|
|
# 绑定设备推送
|
|
|
UidPushModel.objects.create(**uid_push_create_dict)
|
|
|
|
|
|
- # if interval:
|
|
|
- # self.do_delete_redis(uid, int(interval))
|
|
|
- # else:
|
|
|
- # self.do_delete_redis(uid)
|
|
|
- # utko = UidTokenObject()
|
|
|
- # # right
|
|
|
- # utko.generate(data={'uid': uid})
|
|
|
etkObj = ETkObject(etk='')
|
|
|
etk = etkObj.encrypt(uid)
|
|
|
|
|
|
- #只返回一个接口就行
|
|
|
- # detectUrl = "{DETECT_PUSH_DOMAIN}AiService/push?etk={etk}&endTime={endTime}". \
|
|
|
- # format(etk=etk, DETECT_PUSH_DOMAIN=SERVER_DOMAIN_SSL, endTime=endTime)
|
|
|
aiIdentificationUrl = "{DETECT_PUSH_DOMAIN}AiService/identification".format(DETECT_PUSH_DOMAIN=SERVER_DOMAIN_SSL)
|
|
|
|
|
|
# mqtt通知设备开启AI识别功能
|
|
@@ -274,12 +258,13 @@ class AiView(View):
|
|
|
'aiIdentificationUrl': aiIdentificationUrl,
|
|
|
}
|
|
|
},
|
|
|
- req_success = self.requestPublishMqtt(uid, topic_name, msg)
|
|
|
+ req_success = CommonService.req_publish_mqtt_msg(uid, topic_name, msg)
|
|
|
if not req_success:
|
|
|
return response.json(10044)
|
|
|
return response.json(0, {'aiIdentificationUrl': aiIdentificationUrl, 'endTime': endTime, 'etk': etk})
|
|
|
- else:
|
|
|
- return response.json(14)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
|
|
def getAiStatus(self, userID, request_dict, response):
|
|
|
uid = request_dict.get('uid', None)
|
|
@@ -289,7 +274,7 @@ class AiView(View):
|
|
|
try:
|
|
|
ai_server_qs = AiService.objects.filter(uid=uid).values('detect_status', 'detect_group')
|
|
|
if not ai_server_qs.exists():
|
|
|
- return response,json(173)
|
|
|
+ return response.json(173)
|
|
|
res = {
|
|
|
'detect_status': ai_server_qs[0]['detect_status'],
|
|
|
'detect_group': ai_server_qs[0]['detect_group'],
|
|
@@ -298,42 +283,6 @@ class AiView(View):
|
|
|
except Exception as e:
|
|
|
return response.json(500, repr(e))
|
|
|
|
|
|
- def requestPublishMqtt(self, thing_name, topic_name, msg):
|
|
|
- # 通用发布MQTT主题通知
|
|
|
- if not all([msg, thing_name, topic_name]):
|
|
|
- return False
|
|
|
-
|
|
|
- try:
|
|
|
- # 获取数据组织将要请求的url
|
|
|
- iot = iotdeviceInfoModel.objects.filter(
|
|
|
- thing_name__icontains=thing_name).values(
|
|
|
- 'endpoint', 'token_iot_number')
|
|
|
- if not iot.exists():
|
|
|
- return False
|
|
|
- endpoint = iot[0]['endpoint']
|
|
|
- Token = iot[0]['token_iot_number']
|
|
|
-
|
|
|
- # 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
|
|
|
- # post请求url发布MQTT消息
|
|
|
- url = 'https://{}/topics/{}'.format(endpoint, topic_name)
|
|
|
- authorizer_name = 'Ansjer_Iot_Auth'
|
|
|
- signature = CommonService.rsa_sign(Token) # Token签名
|
|
|
- headers = {
|
|
|
- 'x-amz-customauthorizer-name': authorizer_name,
|
|
|
- 'Token': Token,
|
|
|
- 'x-amz-customauthorizer-signature': signature}
|
|
|
- r = requests.post(url=url, headers=headers, json=msg, timeout=2)
|
|
|
- if r.status_code == 200:
|
|
|
- res = r.json()
|
|
|
- if res['message'] == 'OK':
|
|
|
- return True
|
|
|
- return False
|
|
|
- else:
|
|
|
- return False
|
|
|
- except Exception as e:
|
|
|
- return False
|
|
|
-
|
|
|
def do_commodity_list(self, userID, request_dict, response):
|
|
|
uid = request_dict.get('uid', None)
|
|
|
lang = request_dict.get('lang', 'en')
|