| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991 | #!/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 base64import jsonimport osimport timeimport globimport urllibfrom urllib.parse import quote, parse_qs, unquoteimport apns2import boto3import jpushimport oss2import paypalrestsdkimport threadingimport calendarimport datetimeimport loggingimport sysimport requestsfrom aliyunsdkcore import clientfrom aliyunsdksts.request.v20150401 import AssumeRoleRequestfrom boto3.session import Sessionfrom django.http import JsonResponse, HttpResponseRedirect, HttpResponsefrom django.db import transactionfrom django.views.generic.base import Viewimport jwtfrom Object.ETkObject import ETkObjectfrom pyfcm import FCMNotificationfrom Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, OSS_ROLE_ARN, SERVER_DOMAIN, PAYPAL_CRD, \    SERVER_DOMAIN_SSL, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ARN, APNS_MODE, APNS_CONFIG, BASE_DIR, \    JPUSH_CONFIG, FCM_CONFIG, OAUTH_ACCESS_TOKEN_SECRETfrom Controller.CheckUserData import DataValidfrom Model.models import Device_Info, Order_Model, Store_Meal, VodHlsModel, OssCrdModel, UID_Bucket, StsCrdModel, \    ExperienceContextModel, Pay_Type, CDKcontextModel, Device_User, SysMassModel, SysMsgModel, UidPushModel, \    Unused_Uid_Meal, UIDMainUser, UserModel, PromotionRuleModel, VideoPlaybackTimeModel, CloudLogModel, CouponModel, \    AiStoreMeal, AiService, UidSetModel, Ai_Push_Info, iotdeviceInfoModel, AiProcessTimefrom Object.AWS.S3Email import S3Emailfrom Object.AliPayObject import AliPayObjectfrom Object.AliSmsObject import AliSmsObjectfrom Object.RedisObject import RedisObjectfrom Object.ResponseObject import ResponseObjectfrom Object.TokenObject import TokenObjectfrom Object.UidTokenObject import UidTokenObjectfrom Service.CommonService import CommonServicefrom Object.m3u8generate import PlaylistGeneratorfrom Object.WechatPayObject import WechatPayObjectfrom django.db.models import Q, F, Countfrom Controller.PaymentCycle import Paypalfrom decimal import Decimalfrom Ansjer.config import SERVER_TYPEfrom Service.ModelService import ModelServicefrom Object import MergePicimport boto3import botocorefrom botocore import client# AI服务class AiView(View):    def get(self, request, *args, **kwargs):        request.encoding = 'utf-8'        operation = kwargs.get('operation')        return self.validation(request.GET, request, operation)    def post(self, request, *args, **kwargs):        request.encoding = 'utf-8'        operation = kwargs.get('operation')        return self.validation(request.POST, request, operation)    def validation(self, request_dict, request, operation):        response = ResponseObject()        if operation is None:            return response.json(444, 'error path')        elif operation == 'identification':  # ai识别            return self.do_ai_identification(request.POST, response)        else:            token = request_dict.get('token', None)            # 设备主键uid            tko = TokenObject(token)            response.lang = tko.lang            if tko.code != 0:                return response.json(tko.code)            userID = tko.userID            if operation == 'createpayorder':  # 创建支付订单                ip = CommonService.get_ip_address(request)                return self.do_create_pay_order(request_dict, userID, ip, 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':  # 修改云存状态,传送两个url,即getsignsts接口和storeplaylist接口                return self.do_commodity_list(userID, request_dict, response)            elif operation == 'identification':  # ai识别                return self.do_ai_identification(request_dict, response)            elif operation == 'queryInfo':  # 查询消息列表                return self.queryInfo(userID, request_dict, response)            elif operation == 'readInfo':  # 消息已读                return self.readInfo(userID, request_dict, response)            elif operation == 'deleteInfo':  # 删除消息                return self.deleteInfo(userID, request_dict, response)            elif operation == 'queryorderlist':  # 查询订单                return self.do_querylist(userID, request_dict, response)            elif operation == 'updateJpushTime':  # 查询订单                return self.update_jpush_time(userID, request_dict, response)            else:                return response.json(414)    def do_change_ai_status(self, userID, request_dict, response):        token_val = request_dict.get('token_val', None)        appBundleId = request_dict.get('appBundleId', None)        app_type = request_dict.get('app_type', None)        push_type = request_dict.get('push_type', None)        status = request_dict.get('status', None)        m_code = request_dict.get('m_code', None)        uid = request_dict.get('uid', None)        # 设备语言        lang = request_dict.get('lang', 'en')        tz = request_dict.get('tz', '0')        # 消息提醒功能新增        # 如果传空上来,就默认为0        if tz == '':            tz = 0        else:            tz = tz.replace("GMT", "")        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')        # 判断推送类型对应key是否存在        print('push_type:', push_type)        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)        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配置信息是否存在            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)                if detect_group:                    qs_data['detect_group'] = detect_group                uid_set_qs.update(**qs_data)            else:                qs_data = {                    'uid': uid,                    'addTime': nowTime,                    'updTime': nowTime,                }                if interval:                    qs_data['detect_interval'] = int(interval)                if detect_group:                    qs_data['detect_group'] = detect_group                # 添加设备配置                uid_set_qs = UidSetModel.objects.create(**qs_data)                uid_set_id = uid_set_qs.id            topic_name = 'AiServer/{}'.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 = {'AiStatus': 'inactive'},                req_success = self.requestPublishMqtt(uid, topic_name, msg)                if not req_success:                    return response.json(10044)                return response.json(0)            elif status == 1:                hasAiService.update(**qs_data)                uid_push_qs = UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid)                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)                else:                    # uid_set_id = uid_set_qs[0].id                    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)                # 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识别功能                msg = {                          'AiStatus': 'active',                          'etk': etk,                          'endTime': endTime,                          'aiIdentificationUrl': aiIdentificationUrl,                      },                req_success = self.requestPublishMqtt(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)    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).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))    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')        nowTime = int(time.time())        # DVR/NVR设备暂不返回云存套餐列表        device_info_qs = Device_Info.objects.filter(Q(UID=uid), Q(Type__lte=4) | Q(Type=10001))        if device_info_qs.exists():            return response.json(0)        qs = AiStoreMeal.objects        qs = qs.filter(is_show=1)  #过滤隐藏套餐        qs = qs.annotate(ai_meal_id=F('id'))        qs = qs.values("ai_meal_id", "title", "content", "price", "effective_day", "currency",                       "virtual_price", "symbol", "pay_type")        if qs.exists():            res = list(qs)            for key, val in enumerate(res):                pay_types = Pay_Type.objects.filter(aistoremeal=res[key]['ai_meal_id']).values("id", "payment")                res[key]['pay_type'] = list(pay_types)            result = {                'meals': res,            }            return response.json(0, result)        else:            return response.json(0)    def do_querylist(self, userID, request_dict, response):        page = request_dict.get('page', None)        line = request_dict.get('line', None)        uid = request_dict.get('uid', None)        lang = request_dict.get('lang', 'en')        if not page or not line:            return response.json(444, 'page,line')        page = int(page)        line = int(line)        omqs = Order_Model.objects.filter(userID_id=userID, status=1, order_type=1)        # 筛选指定设备id的订单        if uid:            omqs.filter(UID=uid)        if not omqs.exists():            return response.json(173)            # return response.json(10, '订单不存在')        count = omqs.count()        omqs = omqs.annotate(rank__title=F('ai_rank__title'), rank__content=F('ai_rank__content'),                             rank__day=F('ai_rank__effective_day'), rank__price=F('ai_rank__price'),                             rank__expire=F('ai_rank__effective_day'), rank__id=F('ai_rank_id'), rank__currency=F('ai_rank__currency'))        order_ql = omqs[(page - 1) * line:page * line].values("orderID", "UID", "channel", "desc", "price", "currency",                                                              "addTime",                                                              "updTime", "paypal", "rank__day", "payType",                                                              "rank__price", "status",                                                              "rank__content", "rank__title", "rank__currency",                                                              "rank__expire", "ai_rank_id")        order_list = list(order_ql)        data = []        nowTime = int(time.time())        # 这里需要进行优化        uid_list = []        for od in order_list:            uid_list.append(od['UID'])        didqs = Device_Info.objects.filter(userID_id=userID, UID__in=uid_list).values('id', 'UID', 'Type')        for d in order_list:            if d['status'] == 0:                if d['addTime'] + 3600 < nowTime:                    d['status'] = 3            for did in didqs:                if d['UID'] == did['UID']:                    d['did'] = did['id']                    d['Type'] = did['Type']                    data.append(d)            # d['rank__lang__content'] = '月' if lang == 'cn' else 'month'        return response.json(0, {'data': data, 'count': count})    def do_create_pay_order(self, request_dict, userID, ip, response):        uid = request_dict.get('uid', None)        channel = request_dict.get('channel', None)        pay_type = int(request_dict.get('pay_type', 1))        ai_meal_id = request_dict.get('ai_meal_id', None)        lang = request_dict.get('lang', 'en')        if not uid or not channel or not pay_type or not ai_meal_id:            return response.json(444)        # dv_qs = Device_Info.objects.filter(userID_id=userID, UID=uid, isShare=False, isExist=1).values(        #     'vodPrimaryUserID',        #     'vodPrimaryMaster')        # if not dv_qs.exists():        #     return response.json(12)        # dvq = Device_Info.objects.filter(UID=uid)        # dvq = dvq.filter(~Q(vodPrimaryUserID='')).values('vodPrimaryUserID')        # if dvq.exists():        #     if dvq[0]['vodPrimaryUserID'] != userID:        #         return response.json(10033)        nowTime = int(time.time())        smqs = AiStoreMeal.objects.filter(id=ai_meal_id, pay_type=pay_type, is_show=1). \            values('currency', 'price', 'content', 'effective_day', 'title')        if not smqs.exists():            return response.json(173)        currency = smqs[0]['currency']        price = smqs[0]['price']        content = smqs[0]['content']        day = smqs[0]['effective_day']        orderID = CommonService.createOrderID()        price = float(price)        price = round(price, 2)        if pay_type == 1:            #正常扣款            cal_url = "{SERVER_DOMAIN_SSL}web/paid2/fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)            if lang != 'cn':                cal_url = "{SERVER_DOMAIN_SSL}web/paid2/en_fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)            call_sub_url = "{SERVER_DOMAIN_SSL}cloudstorage/dopaypalcallback?orderID={orderID}&lang={lang}". \                format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL, orderID=orderID, lang=lang)            # call_sub_url = "http://binbin.uicp.vip/cloudstorage/dopaypalcallback?orderID={orderID}".format(            # SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL, orderID=orderID)            Order_Model.objects.create(orderID=orderID, UID=uid, channel=channel, userID_id=userID,                                       desc=content, payType=pay_type, payTime=nowTime,                                       price=price, currency=currency, addTime=nowTime, updTime=nowTime,                                       pay_url='', commodity_code='', commodity_type=1,                                       ai_rank_id=ai_meal_id, rank_id=1, order_type=1, status=1)            has = AiService.objects.filter(uid=uid, channel=channel, use_status=1)            if has.exists():                use_status = 0            else:                use_status = 1            # return response.json(0)            AiService.objects.create(orders_id=orderID, uid=uid, channel=channel, detect_status=1,                                       endTime=nowTime+(day*86400), addTime=nowTime, updTime=nowTime,                                     use_status=use_status, detect_group='1')            return response.json(0, {"orderID": orderID})        return response.json(10, 'generate_order_false')    def update_jpush_time(self,userID, request_dict, response):        logger = logging.getLogger('info')        msg_id = request_dict.get('msg_id', None)        apns_push_time = request_dict.get('apns_push_time', None)        logger.info("----init--apns----push_time={apns_push_time}".format(apns_push_time=apns_push_time))        apns_push_time = int(apns_push_time)        logger.info("---msg--id={msg_id}".format(msg_id=msg_id))        logger.info("---apns----push_time={apns_push_time}".format(apns_push_time=apns_push_time))        try:            aiRes = AiProcessTime.objects.filter(msg_id=msg_id).update(appPushTime=apns_push_time)        except Exception as e:            logger.info(repr(e))            return response.json(902,repr(e))        return response.json(0,aiRes)    def do_ai_identification(self, request_dict,response):        msg_id = CommonService.createOrderID()        AiProcessTime.objects.create(detectTime=0, receiveTime=0, aiTime=0,                                     aiEndTime=0, pushTime=0,                                     pushEndTime=0, serverCountTime=0, msg_id=msg_id)        etk = request_dict.get('etk', None)        n_time = request_dict.get('n_time', None)        receiveTime = int(time.time())        logger = logging.getLogger('info')        logger.info('-----------into----ai--api')        logger.info("etk={etk}".format(etk=etk))        if not etk:            return response.json(444)        try:            # 解密uid及判断长度            eto = ETkObject(etk)            uid = eto.uid            logger.info("uid={uid}".format(uid=uid))            if len(uid) != 20 and len(uid) != 14:                return response.json(444)            ##通过uid查出endTime是否过期,并且ai开关是否打开            AiServiceQuery = AiService.objects.filter(uid=uid, detect_status=1, use_status=1, endTime__gt=receiveTime).\                values('detect_group')            if not AiServiceQuery.exists():                logger.info('none-----aiService')                return response.json(173)            detect_group = AiServiceQuery[0]['detect_group']            #{}??            #            file_post_one = request_dict.get('fileOne', None)            file_post_two = request_dict.get('fileTwo', None)            file_post_three = request_dict.get('fileThree', None)            file_post_one = file_post_one.replace(' ', '+')            file_post_two = file_post_two.replace(' ', '+')            file_post_three = file_post_three.replace(' ', '+')            file_post_one = base64.b64decode(file_post_one)            file_post_two = base64.b64decode(file_post_two)            file_post_three = base64.b64decode(file_post_three)            file_list = [file_post_one, file_post_two, file_post_three]            del file_post_one, file_post_two, file_post_three            dir_path = os.path.join(BASE_DIR, 'static/ai/' + uid + '/' + str(receiveTime))            if not os.path.exists(dir_path):                os.makedirs(dir_path)            file_path_list = []            i = 1            for index in file_list:                file_path = dir_path + '/' + str(i) + '.jpg'                file_path_list.append(file_path)                with open(file_path, 'wb') as f:                    f.write(index)                    f.close()                i += 1            image_size = 500  # 每张小图片的大小            image_colnum = 1  # 合并成一张图后,一行有几个小图            MergePic.merge_images(dir_path, image_size, image_colnum)            photo = open(dir_path + '.jpg', 'rb')  #打开合成图            cover = dir_path + '/' + str(i-1) + '.jpg'            desc = dir_path + '.jpg'            logger.info('----------------cover')            logger.info(cover)            logger.info(desc)            # photo = open(r'E:\test---------------\test\snipaste20220121_215952.jpg', 'rb')            #识别合成图片            maxLabels = 50            minConfidence = 96            ai_start_time = int(time.time())            client = boto3.client(                'rekognition',                aws_access_key_id='AKIA2E67UIMD6JD6TN3J',                aws_secret_access_key='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',                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)            ai_end_time = int(time.time())            labels =rekognition_res['Labels']            label_name = []            logger.info('--------识别到的标签-------')            logger.info(labels)            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:                logger.info('没有识别到任何标签-----------------')                return response.json(10055)            event_type = ','.join(labels['label_type'])            label_list = ','.join(labels['label_list'])            logger.info(event_type)            logger.info(label_list)            #存储消息以及推送            channel = request_dict.get('channel', '1')            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 = []            apns_start_time = 0            apns_end_time = 0            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,                     'msg_id' : msg_id,                }                # 推送消息                # if push_type == 0:  # ios apns                #     logger.info('into-------apns')                #     apns_start_time = int(time.time())                #     res = self.do_apns(**kwargs)                #     apns_end_time = int(time.time())                #     logger.info(res)                # # elif push_type == 1:  # android gcm                # #     self.do_fcm(**kwargs)                # elif push_type == 2:  # android jpush                #     logger.info('into-------jpush')                #     jpush_start_time = int(time.time())                #     res = self.do_jpush(**kwargs)                #     jpush_end_time = int(time.time())                #     logger.info(res)                if push_type == 1:  # android gcm                    logger.info('into-------gcm')                    apns_start_time = int(time.time())                    res = self.do_fcm(**kwargs)                    apns_end_time = int(time.time())                # 以下是存库                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=receiveTime,                        addTime=now_time,                        storage_location=2                    ))                    userID_ids.append(userID_id)            Ai_Push_Info.objects.bulk_create(eq_list)            #上传缩略图到s3            cover_start_time = int(time.time())            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:                logger.info('upload-----S3----success')                endTime = int(time.time())                count_time = endTime - receiveTime                AiProcessTime.objects.filter(msg_id=msg_id).update(detectTime=n_time, receiveTime=receiveTime, aiTime=ai_start_time,                                             aiEndTime=ai_end_time, pushTime=apns_start_time,                                             pushEndTime=apns_end_time, serverCountTime=count_time,picUploadTime=cover_start_time,                                                                   picUploadEndTime = endTime, endTime=endTime)                #删除临时文件                delf = os.path.join(BASE_DIR, 'static/ai')                self.delfile(path=delf)                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))    def delfile(self, path):        #   read all the files under the folder        fileNames = glob.glob(path + r'\*')        for fileName in fileNames:            try:                #           delete file                os.remove(fileName)            except:                try:                    #               delete empty folders                    os.rmdir(fileName)                except:                    #               Not empty, delete files under folders                    delfile(fileName)                    #               now, folders are empty, delete it                    os.rmdir(fileName)    ## 检查是否有符合条件的标签    def checkLabels(self, user_detect_group, labels):        labels_type = {            '1': ['Person', 'Human'],    #人            '2': ['Dog', 'Pet', 'Canine', 'Animal'],   #动物            '3': ['Car', '', 'Vehicle', 'Transportation', 'Automobile']   #车        }        user_detect_list = user_detect_group.split(',')        user_labels_type = {}        for user_detect in user_detect_list:            if user_detect in labels_type.keys():                user_labels_type[user_detect] = labels_type[user_detect]        label_list = []        for k, labels_type in user_labels_type.items():            for label in labels_type:                if label in labels:                    label_list.append(label)        user_labels_list = list(user_labels_type.keys())        user_labels_list.sort()        return {'label_type': user_labels_list, 'label_list': label_list}    def upload_s3(self, file_path, upload_path):        try:            aws_key = "AKIA2MMWBR4DSFG67DTG" #【你的 aws_access_key】            aws_secret = "aI9gxcAKPmiGgPy9axrtFKzjYGbvpuytEX4xWweL" # 【你的 aws_secret_key】            session = Session(aws_access_key_id=aws_key,                              aws_secret_access_key=aws_secret,                              region_name="cn-northwest-1")            s3 = session.resource("s3")            # client = session.client("s3")            bucket = "aipush" # 【你 bucket 的名字】 # 首先需要保.证 s3 上已经存在该存储桶,否则报错            upload_data = open(file_path, "rb")            # upload_key = "test"            s3.Bucket(bucket).put_object(Key=upload_path, Body=upload_data)            return True        except Exception as e:            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, msg_id=0):        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, msg_id=0):        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,                    "msg_id": msg_id}            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, msg_id=0):        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, "msg_id": msg_id}            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))        if not page or not line:            return response.json(444, 'page,line')        startTime = request_dict.get('startTime', None)        endTime = request_dict.get('endTime', None)        eventType = request_dict.get('eventType', None)        now_time = int(time.time())        seven_days_ago = now_time - 7 * 24 * 3600   # 查询7天内的数据        qs = Ai_Push_Info.objects.filter(userID_id=userID, eventTime__gt=seven_days_ago).order_by('-eventTime')        if startTime and endTime:            qs = qs.filter(eventTime__range=(startTime, endTime))        # if eventType:        #     qs = qs.filter(eventType__contains=eventType)        uids = request_dict.get('uids', None)        if uids:            uid_list = uids.split(',')            qs = qs.filter(devUid__in=uid_list)            dvqs = Device_Info.objects.filter(UID__in=uid_list, userID_id=userID).values('UID', 'Type', 'NickName')            uid_type_dict = {}            for dv in dvqs:                uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}        else:            dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName')            uid_type_dict = {}            for dv in dvqs:                uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}        if not qs.exists():            return response.json(0, {'datas': [], 'count': 0})        count = qs.count()        qs = qs.values('id', 'devUid', 'devNickName', 'Channel', 'eventType', 'status', 'alarm', 'eventTime',                       'receiveTime', 'is_st', 'addTime', 'storage_location')        qs = qs[(page - 1) * line:page * line]        res = []        aws_s3_client = boto3.client(            's3',            aws_access_key_id=AWS_ACCESS_KEY_ID[0],            aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],            config=botocore.client.Config(signature_version='s3v4'),            region_name='cn-northwest-1'        )        for p in qs:            p['eventType'] = int(p['eventType'][0])            devUid = p['devUid']            eventTime = p['eventTime']            channel = p['Channel']            storage_location = p['storage_location']            if p['is_st'] == 1:                s3_img_cover = '{uid}/{channel}/cover{time}.jpg'.format(uid=devUid, channel=channel, time=eventTime)                s3_img_desc = '{uid}/{channel}/desc{time}.jpg'.format(uid=devUid, channel=channel, time=eventTime)                response_url_cover = aws_s3_client.generate_presigned_url('get_object',                                                                    ExpiresIn=300,                                                                    Params={                                                                        'Bucket': 'aipush', 'Key': s3_img_cover                                                                    },                                                                    )                response_url_desc = aws_s3_client.generate_presigned_url('get_object',                                                                          ExpiresIn=300,                                                                          Params={                                                                              'Bucket': 'aipush', 'Key': s3_img_desc                                                                          },                                                                          )                p['img'] = response_url_cover                p['img_list'] = [response_url_desc]            elif p['is_st'] == 2:                pass            if devUid in uid_type_dict.keys():                p['uid_type'] = uid_type_dict[devUid]['type']                p['devNickName'] = uid_type_dict[devUid]['NickName']            else:                p['uid_type'] = ''            res.append(p)        return response.json(0, {'datas': res, 'count': count})    def readInfo(self, userID, request_dict, response):        is_update_all = request_dict.get('is_update_all', 0)        try:            if int(is_update_all) == 1:     # 全部已读                is_update = Ai_Push_Info.objects.filter(userID_id=userID).update(status=1)                return response.json(0, {'update_count': is_update})            else:                id_list = request_dict.get('id_list', None)                if not id_list:                    request_dict.getlist('id_list[]', None)     # 获取IOS数组传参                logger = logging.getLogger('info')                logger.info('已读ai消息id_list:{}'.format(id_list))                if not id_list:                    return response.json(444)                id_list = eval(id_list)     # 字符串转列表                param_flag = CommonService.get_param_flag(data=id_list)                if not param_flag:                    return response.json(444)                count = 0                for id in id_list:                    ai_push_qs = Ai_Push_Info.objects.filter(id=int(id))                    if ai_push_qs.exists():                        own_dev = ModelService.check_own_device(userID, ai_push_qs[0].devUid)                        if own_dev:                            count += 1                            ai_push_qs.update(status=1)                return response.json(0, {'update_success': count})        except Exception as e:            print(e)            return response.json(500, repr(e))    def deleteInfo(self, userID, request_dict, response):        id_list = request_dict.get('id_list', None)        if not id_list:            request_dict.getlist('id_list[]', None)  # 获取IOS数组传参        logger = logging.getLogger('info')        logger.info('删除ai消息id_list:{}'.format(id_list))        if not id_list:            return response.json(444)        try:            id_list = eval(id_list)  # 字符串转列表            param_flag = CommonService.get_param_flag(data=id_list)            if not param_flag:                return response.json(444)            for id in id_list:                ai_push_qs = Ai_Push_Info.objects.filter(id=id)                if ai_push_qs.exists():                    own_dev = ModelService.check_own_device(userID, ai_push_qs[0].devUid)                    if own_dev:                        ai_push_qs.delete()            return response.json(0)        except Exception as e:            print(e)            return response.json(500, repr(e))
 |