Quellcode durchsuchen

优化获取,修改AI开关状态接口

locky vor 2 Jahren
Ursprung
Commit
c7c00944e6
1 geänderte Dateien mit 85 neuen und 267 gelöschten Zeilen
  1. 85 267
      Controller/AiController.py

+ 85 - 267
Controller/AiController.py

@@ -1,37 +1,17 @@
-#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
-"""
-@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
-@AUTHOR: ASJRD018
-@NAME: AnsjerFormal
-@software: PyCharm
-@DATE: 2018/12/5 9:30
-@Version: python3.6
-@MODIFY DECORD:ansjer dev
-@file: cloudstorage.py
-@Contact: chanjunkai@163.com
-"""
-import logging
-import os
 import time
-from urllib.parse import quote, parse_qs, unquote
-
-import apns2
 import boto3
+import logging
 import botocore
-import jpush
 import paypalrestsdk
-from boto3.session import Session
+
 from botocore import client
+from boto3.session import Session
 from django.db import transaction
 from django.db.models import Q, F, Sum
-from django.http import HttpResponseRedirect, HttpResponse
 from django.views.generic.base import View
-from pyfcm import FCMNotification
+from django.http import HttpResponseRedirect, HttpResponse
 
-from Ansjer.config import PAYPAL_CRD, \
-    SERVER_DOMAIN_SSL, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, APNS_MODE, APNS_CONFIG, BASE_DIR, \
-    JPUSH_CONFIG, FCM_CONFIG, DETECT_PUSH_DOMAINS
+from Ansjer.config import PAYPAL_CRD, SERVER_DOMAIN_SSL, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, DETECT_PUSH_DOMAINS
 from Model.models import Device_Info, Order_Model, ExperienceAiModel, Pay_Type, CDKcontextModel, UidPushModel, \
     AiStoreMeal, AiService, UidSetModel, Ai_Push_Info
 from Object.AliPayObject import AliPayObject
@@ -42,10 +22,15 @@ from Object.TokenObject import TokenObject
 from Object.WechatPayObject import WechatPayObject
 from Service.CommonService import CommonService
 from Service.ModelService import ModelService
+from urllib.parse import quote, parse_qs, unquote
 
 
-# AI服务
 class AiView(View):
+    """
+    AI业务功能
+    AI套餐,开关,消息列表
+    """
+
     def get(self, request, *args, **kwargs):
         request.encoding = 'utf-8'
         operation = kwargs.get('operation')
@@ -68,7 +53,6 @@ class AiView(View):
             return self.do_wechat_callback(request, response)
         else:
             token = request_dict.get('token', None)
-            # 设备主键uid
             tko = TokenObject(token)
             response.lang = tko.lang
             if tko.code != 0:
@@ -76,10 +60,10 @@ class AiView(View):
             userID = tko.userID
             if operation == 'createpayorder':  # 创建支付订单
                 return self.do_create_pay_order(request_dict, request, userID, response)
+            elif operation == 'getAiStatus':  # 获取AI开关状态
+                return self.get_ai_status(request_dict, response)
             elif operation == 'changeaistatus':  # 修改AI开关状态
                 return self.do_change_ai_status(userID, request_dict, response)
-            elif operation == 'getAiStatus':  # 获取AI开关状态
-                return self.getAiStatus(userID, request_dict, response)
             elif operation == 'commoditylist':  # 获取AI套餐列表
                 return self.do_commodity_list(request_dict, response)
             elif operation == 'queryInfo':  # 查询消息列表
@@ -97,7 +81,50 @@ class AiView(View):
             else:
                 return response.json(414)
 
-    def do_change_ai_status(self, userID, request_dict, response):
+    @staticmethod
+    def get_ai_status(request_dict, response):
+        """
+        获取AI开关状态
+        @param request_dict: 请求数据
+        @request_dict uid: uid
+        @param response: 响应
+        @return: response
+        """
+        uid = request_dict.get('uid', None)
+        if not uid:
+            return response.json(444)
+        try:
+            ai_server_qs = AiService.objects.filter(uid=uid, use_status=1).values('detect_status', 'detect_group')
+            if not ai_server_qs.exists():
+                return response.json(173)
+            res = {
+                'detect_status': ai_server_qs[0]['detect_status'],
+                'detect_group': ai_server_qs[0]['detect_group'],
+            }
+            return response.json(0, {'data': res})
+        except Exception as e:
+            return response.json(500, repr(e))
+
+    @staticmethod
+    def do_change_ai_status(userID, request_dict, response):
+        """
+        修改AI开关状态
+        @param userID: 用户id
+        @param request_dict: 请求数据
+        @request_dict token_val: 设备验证令牌
+        @request_dict appBundleId: app包id
+        @request_dict app_type: app类型
+        @request_dict push_type: 推送类型
+        @request_dict status: 开关状态, 0: 关, 1: 开
+        @request_dict m_code: 手机唯一标识
+        @request_dict uid: uid
+        @request_dict lang: 语言
+        @request_dict tz: 时区
+        @request_dict detect_group: 检测类型
+        @request_dict interval: 推送间隔
+        @param response: 响应
+        @return: response
+        """
         token_val = request_dict.get('token_val', None)
         appBundleId = request_dict.get('appBundleId', None)
         app_type = request_dict.get('app_type', None)
@@ -110,26 +137,11 @@ class AiView(View):
         detect_group = request_dict.get('detect_group', None)
         interval = request_dict.get('interval', None)
 
-        if not status:
-            return response.json(444, 'status')
-        # 关闭推送
-        if not all([appBundleId, app_type, token_val, uid, m_code]):
-            return response.json(444, 'appBundleId,app_type,token_val,uid,m_code')
+        if not all([appBundleId, app_type, token_val, uid, m_code, status]):
+            return response.json(444, 'appBundleId, app_type, token_val, uid,m_code, status')
 
         # 如果传空上来,就默认为0
-        tz = 0 if tz == '' else tz.replace("GMT", "")
-        # 判断推送类型对应key是否存在
-        if push_type == '0':
-            if appBundleId not in APNS_CONFIG.keys():
-                return response.json(904)
-        elif push_type == '1':
-            if appBundleId not in FCM_CONFIG.keys():
-                return response.json(904)
-        elif push_type == '2':
-            if appBundleId not in JPUSH_CONFIG.keys():
-                return response.json(904)
-        else:
-            return response.json(173)
+        tz = '0' if tz == '' else tz.replace('GMT', '')
 
         try:
             ai_service_qs = AiService.objects.filter(uid=uid, use_status=1)
@@ -176,6 +188,7 @@ class AiView(View):
             ai_service_qs.update(**qs_data)
             thing_name = CommonService.query_serial_with_uid(uid)  # 存在序列号则为使用序列号作为物品名
             topic_name = 'ansjer/generic/{}'.format(thing_name)
+
             if status == 0:  # 关闭
                 # mqtt通知设备关闭AI识别功能
                 msg = {'commandType': 'AIDisable'}
@@ -184,73 +197,44 @@ class AiView(View):
                     return response.json(10044)
                 return response.json(0)
             elif status == 1:  # 开启
+                # 更新或创建uid_push数据
                 uid_push_qs = UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid)
+                uid_push_data = {
+                    'appBundleId': appBundleId,
+                    'app_type': app_type,
+                    'push_type': push_type,
+                    'token_val': token_val,
+                    'updTime': nowTime,
+                    'lang': lang,
+                    'tz': tz
+                }
 
                 if uid_push_qs.exists():
-                    uid_push_update_dict = {
-                        'appBundleId': appBundleId,
-                        'app_type': app_type,
-                        'push_type': push_type,
-                        'token_val': token_val,
-                        'updTime': nowTime,
-                        'lang': lang,
-                        'tz': tz
-                    }
-                    uid_push_qs.update(**uid_push_update_dict)
+                    uid_push_qs.update(**uid_push_data)
                 else:
-                    uid_push_create_dict = {
-                        'uid_set_id': uid_set_id,
-                        'userID_id': userID,
-                        'appBundleId': appBundleId,
-                        'app_type': app_type,
-                        'push_type': push_type,
-                        'token_val': token_val,
-                        'm_code': m_code,
-                        'addTime': nowTime,
-                        'updTime': nowTime,
-                        'lang': lang,
-                        'tz': tz
-                    }
-                    # 绑定设备推送
-                    UidPushModel.objects.create(**uid_push_create_dict)
+                    uid_push_data['uid_set_id'] = uid_set_id
+                    uid_push_data['userID_id'] = userID
+                    uid_push_data['m_code'] = m_code
+                    uid_push_data['addTime'] = nowTime
+                    UidPushModel.objects.create(**uid_push_data)
 
                 etkObj = ETkObject(etk='')
                 etk = etkObj.encrypt(uid)
 
-                aiIdentificationUrl = "{DETECT_PUSH_DOMAIN}AiService/identification".format(
-                    DETECT_PUSH_DOMAIN=DETECT_PUSH_DOMAINS)
-
                 # mqtt通知设备开启AI识别功能
+                aiIdentificationUrl = '{}AiService/identification'.format(DETECT_PUSH_DOMAINS)
                 msg = {
-                          'commandType': 'AIEnable',
-                          'payload': {
-                              'etk': etk,
-                              'endTime': endTime,
-                              'aiIdentificationUrl': aiIdentificationUrl,
-                          }
-                      },
+                    'commandType': 'AIEnable',
+                    'payload': {
+                        'etk': etk,
+                        'endTime': endTime,
+                        'aiIdentificationUrl': aiIdentificationUrl,
+                    }
+                }
                 req_success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
                 if not req_success:
                     return response.json(10044)
                 return response.json(0, {'aiIdentificationUrl': aiIdentificationUrl, 'endTime': endTime, 'etk': etk})
-        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)
-
-        if not uid:
-            return response.json(444)
-        try:
-            ai_server_qs = AiService.objects.filter(uid=uid, use_status=1).values('detect_status', 'detect_group')
-            if not ai_server_qs.exists():
-                return response.json(173)
-            res = {
-                'detect_status': ai_server_qs[0]['detect_status'],
-                'detect_group': ai_server_qs[0]['detect_group'],
-            }
-            return response.json(0, {'data': res})
         except Exception as e:
             return response.json(500, repr(e))
 
@@ -765,76 +749,6 @@ class AiView(View):
         else:
             return HttpResponseRedirect(pay_success_url)
 
-    ## 检查是否有符合条件的标签,并且返回标签坐标位置信息
-    def labelsCoords(self, user_detect_group, rekognition_res, image_size, n_time):
-        logger = logging.getLogger('info')
-        labels = rekognition_res['Labels']
-        label_name = []
-        logger.info('--------识别到的标签-------')
-        logger.info(labels)
-        labels_type = {
-            '1': ['Person', 'Human'],  # 人
-            '2': ['Dog', 'Pet', 'Canine', 'Animal', 'Puppy'],  # 动物
-            '3': ['Car', 'Vehicle', 'Transportation', 'Automobile']  # 车
-        }
-        # 找出识别的所有标签
-        for label in labels:
-            label_name.append(label['Name'])
-            for Parents in label['Parents']:
-                label_name.append(Parents['Name'])
-
-        # 删除用户没有选择的ai识别类型, 并且得出最终识别结果
-        user_detect_list = user_detect_group.split(',')
-        user_detect_list = [i.strip() for i in user_detect_list]
-        label_result_list = []
-        new_labels_type = labels_type
-        for key, label_type in labels_type.items():
-            if key in user_detect_list:
-                new_labels_type[key] = label_type
-                for label in label_type:
-                    if label in label_name:
-                        label_result_list.append(label)
-
-        # 找出标签边框线位置信息
-        boundingBoxList = []
-        for label in labels:
-            if label['Name'] in label_result_list:
-                for boundingBox in label['Instances']:
-                    boundingBoxList.append(boundingBox['BoundingBox'])
-
-        # 找出边框位置信息对应的单图位置并重新计算位置比
-        merge_image_height = image_size['height']
-        merge_image_width = image_size['width']
-        single_height = merge_image_height // image_size['num']
-        new_bounding_box_dict = {}
-        new_bounding_box_dict[n_time + '_0'] = []
-        new_bounding_box_dict[n_time + '_1'] = []
-        new_bounding_box_dict[n_time + '_2'] = []
-        new_bounding_box_dict[n_time + '_3'] = []
-        for k, val in enumerate(boundingBoxList):
-            boundingBoxTop = merge_image_height * val['Top']
-            # 找出当前边框属于哪张图片范围
-            boxDict = {}
-            for i in range(image_size['num']):
-                min = i * single_height  # 第n张图
-                max = (i + 1) * single_height
-                if boundingBoxTop >= min and boundingBoxTop <= max:
-                    # print("属于第{i}张图".format(i=i+1))
-                    boxDict['Width'] = val['Width']
-                    boxDict['Height'] = merge_image_height * val['Height'] / single_height
-                    boxDict['Top'] = ((merge_image_height * val['Top']) - (
-                            i * single_height)) / single_height  # 减去前i张图片的高度
-                    boxDict['Left'] = val['Left']
-                    boxDict['picName'] = "{n_time}_{i}".format(n_time=n_time, i=i)
-                    # new_bounding_box_list.append(boxDict)
-                    # new_bounding_box_list.append(boxDict)
-                    new_bounding_box_dict["{n_time}_{i}".format(n_time=n_time, i=i)].append(boxDict)
-        # exit(new_bounding_box_list)
-        user_labels_list = list(new_labels_type.keys())
-        user_labels_list.sort()
-        return {'label_type': user_labels_list, 'label_list': label_result_list,
-                'new_bounding_box_dict': new_bounding_box_dict}
-
     def upload_s3(self, file_path, upload_path):
         try:
             aws_key = "AKIA2E67UIMD45Y3HL53"  # 【你的 aws_access_key】
@@ -853,102 +767,6 @@ class AiView(View):
             print(repr(e))
             return False
 
-    def get_msg_title(self, appBundleId, nickname):
-        package_title_config = {
-            'com.ansjer.customizedd_a': 'DVS',
-            'com.ansjer.zccloud_a': 'ZosiSmart',
-            'com.ansjer.zccloud_ab': '周视',
-            'com.ansjer.adcloud_a': 'ADCloud',
-            'com.ansjer.adcloud_ab': 'ADCloud',
-            'com.ansjer.accloud_a': 'ACCloud',
-            'com.ansjer.loocamccloud_a': 'Loocam',
-            'com.ansjer.loocamdcloud_a': 'Anlapus',
-            'com.ansjer.customizedb_a': 'COCOONHD',
-            'com.ansjer.customizeda_a': 'Guardian365',
-            'com.ansjer.customizedc_a': 'PatrolSecure',
-        }
-        if appBundleId in package_title_config.keys():
-            return package_title_config[appBundleId] + '(' + nickname + ')'
-        else:
-            return nickname
-
-    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)
-        if lang == 'cn':
-            msg = '摄像头AI识别到了{}'.format(label_list)
-            send_text = '{msg} 通道:{channel} 日期:{date}'.format(msg=msg, channel=channel, date=n_date)
-        else:
-            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):
-        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()
-        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}
-        android = jpush.android(alert=msg_text, priority=1, style=1, alert_type=7,
-                                big_text=msg_text, title=msg_title,
-                                extras=push_data)
-        push.notification = jpush.notification(android=android)
-        push.platform = jpush.all_
-        res = push.send()
-        print(res)
-        return res.status_code
-
-    def do_fcm(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
-        try:
-
-            serverKey = FCM_CONFIG[appBundleId]
-            push_service = FCMNotification(api_key=serverKey)
-            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}
-            result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
-                                                       message_body=msg_text, data_message=data,
-                                                       extra_kwargs={
-                                                           'default_vibrate_timings': True,
-                                                           'default_sound': True,
-                                                           'default_light_settings': True
-                                                       })
-            print('fcm push ing')
-            print(result)
-            return result
-        except Exception as e:
-            return 'serverKey abnormal'
-
-    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']))
-            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")
-            n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
-            res = cli.push(n=n, device_token=token_val, topic=appBundleId)
-
-            if res.status_code == 200:
-                return res.status_code
-            else:
-                logger.info('apns push fail')
-                logger.info(res.reason)
-                return res.status_code
-        except (ValueError, ArithmeticError):
-            return 'The program has a numeric format exception, one of the arithmetic exceptions'
-        except Exception as e:
-            print(repr(e))
-            logger.info(repr(e))
-            return repr(e)
-
     def queryInfo(self, userID, request_dict, response):
         page = int(request_dict.get('page', None))
         line = int(request_dict.get('line', None))