|
@@ -338,14 +338,21 @@ class AiView(View):
|
|
|
def do_ai_identification(self, request_dict,response):
|
|
|
etk = request_dict.get('etk', None)
|
|
|
now_time = int(time.time())
|
|
|
- if etk:
|
|
|
+ if not etk:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 解密uid及判断长度
|
|
|
eto = ETkObject(etk)
|
|
|
uid = eto.uid
|
|
|
+ if len(uid) != 20 and len(uid) != 14:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
##通过uid查出endTime是否过期,并且ai开关是否打开
|
|
|
AiServiceQuery = AiService.objects.filter(uid=uid, detect_status=0, use_status=1, endTime__gt=now_time).\
|
|
|
values('detect_group')
|
|
|
if not AiServiceQuery.exists():
|
|
|
- return JsonResponse(status=500, data='ai_service_invalid', safe=False)
|
|
|
+ return response.json(173)
|
|
|
detect_group = AiServiceQuery[0]['detect_group']
|
|
|
#{}??
|
|
|
#
|
|
@@ -388,144 +395,117 @@ class AiView(View):
|
|
|
#识别合成图片
|
|
|
maxLabels = 50
|
|
|
minConfidence = 96
|
|
|
- try:
|
|
|
- client = boto3.client(
|
|
|
- 'rekognition',
|
|
|
- aws_access_key_id='AKIA2E67UIMD6JD6TN3J',
|
|
|
- aws_secret_access_key='6YaziO3aodyNUeaayaF8pK9BxHp/GvbbtdrOAI83',
|
|
|
- region_name='us-east-1')
|
|
|
- # doc:
|
|
|
- rekognition_res = client.detect_labels(
|
|
|
- Image={
|
|
|
- 'Bytes': photo.read()},
|
|
|
- MaxLabels=maxLabels,
|
|
|
- MinConfidence=minConfidence)
|
|
|
- if rekognition_res['ResponseMetadata']['HTTPStatusCode'] != 200:
|
|
|
- return response.json(173)
|
|
|
- labels =rekognition_res['Labels']
|
|
|
- label_name = []
|
|
|
- for label in labels:
|
|
|
- label_name.append(label['Name'])
|
|
|
- for Parents in label['Parents']:
|
|
|
- label_name.append(Parents['Name'])
|
|
|
- labels = self.checkLabels(detect_group, label_name) #检查标签是否符合用户选择的识别类型
|
|
|
- if len(labels['label_list']) == 0:
|
|
|
- return JsonResponse(status=500, data='label_list_none')
|
|
|
- event_type = ','.join(labels['label_type'])
|
|
|
-
|
|
|
- #存储消息以及推送
|
|
|
- channel = request_dict.get('channel', '1')
|
|
|
- n_time = now_time
|
|
|
- # event_type = request_dict.get('event_type', None)
|
|
|
-
|
|
|
- is_st = 1 #单图
|
|
|
- region = 1
|
|
|
- # uid = request_dict.get('uid', None) # 调试
|
|
|
- # 判断uid长度
|
|
|
- if len(uid) != 20 and len(uid) != 14:
|
|
|
- return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong uid'})
|
|
|
-
|
|
|
- # pkey = '{uid}_{channel}_{event_type}_ptl'.format(uid=uid, channel=channel, event_type=event_type)
|
|
|
- # redisObj = RedisObject(db=6)
|
|
|
- # have_pkey = redisObj.get_data(key=pkey) # 一分钟限制key
|
|
|
- #
|
|
|
- # # 一分钟内不推送
|
|
|
- # if have_pkey:
|
|
|
- # return JsonResponse(status=200, data={'code': 0, 'msg': 'Push again in one minute'})
|
|
|
- # redisObj.set_data(key=pkey, val=1, expire=60)
|
|
|
-
|
|
|
- # 查询推送数据
|
|
|
- uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
|
|
|
- values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id',
|
|
|
- 'userID__NickName',
|
|
|
- 'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval',
|
|
|
- 'uid_set__detect_group',
|
|
|
- 'uid_set__channel')
|
|
|
- if not uid_push_qs.exists():
|
|
|
- return JsonResponse(status=200, data={'code': 176, 'msg': 'no uid_push data'})
|
|
|
- redis_list = []
|
|
|
- for qs in uid_push_qs:
|
|
|
- redis_list.append(qs)
|
|
|
-
|
|
|
- nickname = redis_list[0]['uid_set__nickname']
|
|
|
-
|
|
|
- if not nickname:
|
|
|
- nickname = uid
|
|
|
-
|
|
|
- kwag_args = {
|
|
|
+
|
|
|
+ client = boto3.client(
|
|
|
+ 'rekognition',
|
|
|
+ aws_access_key_id='AKIA2E67UIMD6JD6TN3J',
|
|
|
+ aws_secret_access_key='6YaziO3aodyNUeaayaF8pK9BxHp/GvbbtdrOAI83',
|
|
|
+ region_name='us-east-1')
|
|
|
+ # doc:
|
|
|
+ rekognition_res = client.detect_labels(
|
|
|
+ Image={'Bytes': photo.read()},
|
|
|
+ MaxLabels=maxLabels,
|
|
|
+ MinConfidence=minConfidence)
|
|
|
+ if rekognition_res['ResponseMetadata']['HTTPStatusCode'] != 200:
|
|
|
+ return response.json(173)
|
|
|
+ labels =rekognition_res['Labels']
|
|
|
+ label_name = []
|
|
|
+ for label in labels:
|
|
|
+ label_name.append(label['Name'])
|
|
|
+ for Parents in label['Parents']:
|
|
|
+ label_name.append(Parents['Name'])
|
|
|
+ labels = self.checkLabels(detect_group, label_name) #检查标签是否符合用户选择的识别类型
|
|
|
+ if len(labels['label_list']) == 0:
|
|
|
+ return JsonResponse(status=500, data='label_list_none')
|
|
|
+ event_type = ','.join(labels['label_type'])
|
|
|
+ label_list = ','.join(labels['label_list'])
|
|
|
+
|
|
|
+ #存储消息以及推送
|
|
|
+ channel = request_dict.get('channel', '1')
|
|
|
+ n_time = now_time
|
|
|
+ is_st = 1 #单图
|
|
|
+
|
|
|
+ # 查询推送数据
|
|
|
+ uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
|
|
|
+ values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id',
|
|
|
+ 'userID__NickName',
|
|
|
+ 'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval',
|
|
|
+ 'uid_set__detect_group',
|
|
|
+ 'uid_set__channel')
|
|
|
+ if not uid_push_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ uid_push_list = []
|
|
|
+ for qs in uid_push_qs:
|
|
|
+ uid_push_list.append(qs)
|
|
|
+
|
|
|
+ nickname = uid_push_list[0]['uid_set__nickname']
|
|
|
+ if not nickname:
|
|
|
+ nickname = uid
|
|
|
+
|
|
|
+ eq_list = []
|
|
|
+ userID_ids = []
|
|
|
+ for up in uid_push_list:
|
|
|
+ push_type = up['push_type']
|
|
|
+ appBundleId = up['appBundleId']
|
|
|
+ token_val = up['token_val']
|
|
|
+ lang = up['lang']
|
|
|
+ tz = up['tz']
|
|
|
+ if tz is None or tz == '':
|
|
|
+ tz = 0
|
|
|
+
|
|
|
+ # 推送标题
|
|
|
+ msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
|
|
|
+ # 推送内容
|
|
|
+ msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz, label_list=label_list)
|
|
|
+ kwargs = {
|
|
|
'uid': uid,
|
|
|
'channel': channel,
|
|
|
'event_type': event_type,
|
|
|
'n_time': n_time,
|
|
|
+ 'appBundleId': appBundleId,
|
|
|
+ 'token_val': token_val,
|
|
|
+ 'msg_title': msg_title,
|
|
|
+ 'msg_text': msg_text,
|
|
|
}
|
|
|
- eq_list = []
|
|
|
- userID_ids = []
|
|
|
- do_apns_code = ''
|
|
|
- do_fcm_code = ''
|
|
|
- do_jpush_code = ''
|
|
|
- for up in redis_list:
|
|
|
- # push_type = up['push_type']
|
|
|
- # appBundleId = up['appBundleId']
|
|
|
- # token_val = up['token_val']
|
|
|
- # lang = up['lang']
|
|
|
- # tz = up['tz']
|
|
|
- # if tz is None or tz == '':
|
|
|
- # tz = 0
|
|
|
-
|
|
|
- # 发送标题
|
|
|
- # msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
|
|
|
- # # 发送内容
|
|
|
- # msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
|
|
|
- # event_type=event_type)
|
|
|
- # kwag_args['appBundleId'] = appBundleId
|
|
|
- # kwag_args['token_val'] = token_val
|
|
|
- # kwag_args['msg_title'] = msg_title
|
|
|
- # kwag_args['msg_text'] = msg_text
|
|
|
- # 推送消息
|
|
|
- # if push_type == 0: # ios apns
|
|
|
- # do_apns_code = self.do_apns(**kwag_args)
|
|
|
- # elif push_type == 1: # android gcm
|
|
|
- # do_fcm_code = self.do_fcm(**kwag_args)
|
|
|
- # elif push_type == 2: # android jpush
|
|
|
- # do_jpush_code = self.do_jpush(**kwag_args)
|
|
|
- # 以下是存库
|
|
|
- userID_id = up["userID_id"]
|
|
|
- if userID_id not in userID_ids:
|
|
|
- now_time = int(time.time())
|
|
|
- eq_list.append(Ai_Push_Info(
|
|
|
- userID_id=userID_id,
|
|
|
- eventTime=n_time,
|
|
|
- eventType=event_type,
|
|
|
- devUid=uid,
|
|
|
- devNickName=nickname,
|
|
|
- Channel=channel,
|
|
|
- alarm='检查到{labels} \tChannel:{channel}'.format(labels=','.join(labels['label_list']), channel=channel),
|
|
|
- is_st=is_st,
|
|
|
- receiveTime=n_time,
|
|
|
- addTime=now_time,
|
|
|
- storage_location=2
|
|
|
- ))
|
|
|
- userID_ids.append(userID_id)
|
|
|
- Ai_Push_Info.objects.bulk_create(eq_list)
|
|
|
-
|
|
|
- #上传缩略图到s3
|
|
|
- upload_cover_path = "{uid}/{channel}/cover{n_time}.jpg".format(uid=uid, channel=channel, n_time=n_time) #封面图
|
|
|
- upload_desc_path = "{uid}/{channel}/desc{n_time}.jpg".format(uid=uid, channel=channel, n_time=n_time) #详情内容图
|
|
|
- c_res = self.upload_s3(cover, upload_cover_path)
|
|
|
- d_res = self.upload_s3(desc, upload_desc_path)
|
|
|
- if c_res and d_res:
|
|
|
- return JsonResponse(status=200, data='success', safe=False)
|
|
|
- return JsonResponse(status=500, data='fail', safe=False)
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- print(e)
|
|
|
- return response.json(500, repr(e))
|
|
|
+ # 推送消息
|
|
|
+ if push_type == 0: # ios apns
|
|
|
+ self.do_apns(**kwargs)
|
|
|
+ elif push_type == 1: # android gcm
|
|
|
+ self.do_fcm(**kwargs)
|
|
|
+ elif push_type == 2: # android jpush
|
|
|
+ self.do_jpush(**kwargs)
|
|
|
+ # 以下是存库
|
|
|
+ userID_id = up["userID_id"]
|
|
|
+ if userID_id not in userID_ids:
|
|
|
+ now_time = int(time.time())
|
|
|
+ eq_list.append(Ai_Push_Info(
|
|
|
+ userID_id=userID_id,
|
|
|
+ eventTime=n_time,
|
|
|
+ eventType=event_type,
|
|
|
+ devUid=uid,
|
|
|
+ devNickName=nickname,
|
|
|
+ Channel=channel,
|
|
|
+ alarm='检查到{labels} \tChannel:{channel}'.format(labels=','.join(labels['label_list']), channel=channel),
|
|
|
+ is_st=is_st,
|
|
|
+ receiveTime=n_time,
|
|
|
+ addTime=now_time,
|
|
|
+ storage_location=2
|
|
|
+ ))
|
|
|
+ userID_ids.append(userID_id)
|
|
|
+ Ai_Push_Info.objects.bulk_create(eq_list)
|
|
|
+
|
|
|
+ #上传缩略图到s3
|
|
|
+ upload_cover_path = "{uid}/{channel}/cover{n_time}.jpg".format(uid=uid, channel=channel, n_time=n_time) #封面图
|
|
|
+ upload_desc_path = "{uid}/{channel}/desc{n_time}.jpg".format(uid=uid, channel=channel, n_time=n_time) #详情内容图
|
|
|
+ c_res = self.upload_s3(cover, upload_cover_path)
|
|
|
+ d_res = self.upload_s3(desc, upload_desc_path)
|
|
|
+ if c_res and d_res:
|
|
|
+ return JsonResponse(status=200, data='success', safe=False)
|
|
|
+ return JsonResponse(status=500, data='fail', safe=False)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- else:
|
|
|
- return HttpResponse("fail")
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
|
|
|
|
|
## 检查是否有符合条件的标签
|
|
@@ -585,56 +565,22 @@ class AiView(View):
|
|
|
else:
|
|
|
return nickname
|
|
|
|
|
|
- def is_sys_msg(self, event_type):
|
|
|
- event_type_list = [702, 703, 704]
|
|
|
- if event_type in event_type_list:
|
|
|
- return True
|
|
|
- return False
|
|
|
-
|
|
|
- def get_msg_text(self, channel, n_time, lang, tz, event_type, is_sys=0):
|
|
|
+ def get_msg_text(self, channel, n_time, lang, tz, label_list):
|
|
|
n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz,lang=lang)
|
|
|
- etype = int(event_type)
|
|
|
if lang == 'cn':
|
|
|
- if etype == 704:
|
|
|
- msg_type = '电量过低'
|
|
|
- elif etype == 702:
|
|
|
- msg_type = '摄像头休眠'
|
|
|
- elif etype == 703:
|
|
|
- msg_type = '摄像头唤醒'
|
|
|
- else:
|
|
|
- msg_type = ''
|
|
|
- if is_sys:
|
|
|
- send_text = '{msg_type} 通道:{channel}'.format(msg_type=msg_type, channel=channel)
|
|
|
- else:
|
|
|
- send_text = '{msg_type} 通道:{channel} 日期:{date}'.format(msg_type=msg_type, channel=channel, date=n_date)
|
|
|
- # send_text = '{msg_type} 通道:{channel} 日期:{date}'.format(msg_type=msg_type, channel=channel, date=n_date)
|
|
|
+ msg = '摄像头AI识别到了{}'.format(label_list)
|
|
|
+ send_text = '{msg} 通道:{channel} 日期:{date}'.format(msg=msg, channel=channel, date=n_date)
|
|
|
else:
|
|
|
- if etype == 704:
|
|
|
- msg_type = 'Low battery'
|
|
|
- elif etype == 702:
|
|
|
- msg_type = 'Camera sleep'
|
|
|
- elif etype == 703:
|
|
|
- msg_type = 'Camera wake'
|
|
|
- else:
|
|
|
- msg_type = ''
|
|
|
- if is_sys:
|
|
|
- send_text = '{msg_type} channel:{channel}'. \
|
|
|
- format(msg_type=msg_type, channel=channel)
|
|
|
- else:
|
|
|
- send_text = '{msg_type} channel:{channel} date:{date}'. \
|
|
|
- format(msg_type=msg_type, channel=channel, date=n_date)
|
|
|
+ msg = 'Camera AI recognizes{}'.format(label_list)
|
|
|
+ send_text = '{msg} channel:{channel} date:{date}'.format(msg=msg, channel=channel, date=n_date)
|
|
|
return send_text
|
|
|
|
|
|
- def do_jpush(self, uid, channel, appBundleId, token_val, event_type, n_time,
|
|
|
- msg_title, msg_text):
|
|
|
+ def do_jpush(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
|
|
|
app_key = JPUSH_CONFIG[appBundleId]['Key']
|
|
|
master_secret = JPUSH_CONFIG[appBundleId]['Secret']
|
|
|
# 此处换成各自的app_key和master_secre
|
|
|
_jpush = jpush.JPush(app_key, master_secret)
|
|
|
push = _jpush.create_push()
|
|
|
- # if you set the logging level to "DEBUG",it will show the debug logging.
|
|
|
- # _jpush.set_logging("DEBUG")
|
|
|
- # push.audience = jpush.all_
|
|
|
push.audience = jpush.registration_id(token_val)
|
|
|
push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
|
|
|
"received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
|
|
@@ -646,17 +592,6 @@ class AiView(View):
|
|
|
res = push.send()
|
|
|
print(res)
|
|
|
return res.status_code
|
|
|
- # try:
|
|
|
- # res = push.send()
|
|
|
- # print(res)
|
|
|
- # except Exception as e:
|
|
|
- # print("jpush fail")
|
|
|
- # print("Exception")
|
|
|
- # print(repr(e))
|
|
|
- # return
|
|
|
- # else:
|
|
|
- # print("jpush success")
|
|
|
- # return
|
|
|
|
|
|
def do_fcm(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
|
|
|
try:
|
|
@@ -677,39 +612,24 @@ class AiView(View):
|
|
|
print(result)
|
|
|
return result
|
|
|
|
|
|
- def do_apns(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title,
|
|
|
- msg_text):
|
|
|
+ def do_apns(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
|
|
|
logger = logging.getLogger('info')
|
|
|
logger.info("进来do_apns函数了")
|
|
|
logger.info(token_val)
|
|
|
logger.info(APNS_MODE)
|
|
|
logger.info(os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
|
|
|
try:
|
|
|
- cli = apns2.APNSClient(mode=APNS_MODE,
|
|
|
- client_cert=os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
|
|
|
-
|
|
|
+ cli = apns2.APNSClient(mode=APNS_MODE, client_cert=os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
|
|
|
push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
|
|
|
"received_at": n_time, "sound": "", "uid": uid, "zpush": "1", "channel": channel}
|
|
|
alert = apns2.PayloadAlert(body=msg_text, title=msg_title)
|
|
|
payload = apns2.Payload(alert=alert, custom=push_data, sound="default")
|
|
|
-
|
|
|
- # return uid, channel, appBundleId, str(token_val), event_type, n_time, msg_title,msg_text
|
|
|
n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
|
|
|
res = cli.push(n=n, device_token=token_val, topic=appBundleId)
|
|
|
- print(res.status_code)
|
|
|
- logger.info("推送状态:")
|
|
|
- logger.info(res.status_code)
|
|
|
-
|
|
|
- # 200, 推送成功。
|
|
|
- # 400, 请求有问题。
|
|
|
- # 403, 证书或Token有问题。
|
|
|
- # 405, 请求方式不正确, 只支持POST请求
|
|
|
- # 410, 设备的Token与证书不一致
|
|
|
+
|
|
|
if res.status_code == 200:
|
|
|
return res.status_code
|
|
|
else:
|
|
|
- print('apns push fail')
|
|
|
- print(res.reason)
|
|
|
logger.info('apns push fail')
|
|
|
logger.info(res.reason)
|
|
|
return res.status_code
|
|
@@ -719,4 +639,3 @@ class AiView(View):
|
|
|
print(repr(e))
|
|
|
logger.info(repr(e))
|
|
|
return repr(e)
|
|
|
-
|