#!/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: 2019/1/14 15:57
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: DetectController.py
@Contact: chanjunkai@163.com
"""
import json
import logging
import os
import threading
import time

import apns2
import boto3
import botocore
import jpush as jpush
from botocore import client
from django.http import JsonResponse
from django.views.generic.base import View
from pyfcm import FCMNotification

from AnsjerPush.config import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
from AnsjerPush.config import JPUSH_CONFIG, FCM_CONFIG, \
    APNS_CONFIG, BASE_DIR, APNS_MODE
from AnsjerPush.config import SERVER_TYPE
from Model.models import Equipment_Info, UidPushModel, SysMsgModel
from Object.ETkObject import ETkObject
from Object.RedisObject import RedisObject
from Object.UidTokenObject import UidTokenObject
from Object.utils import LocalDateTimeUtil
from Service.CommonService import CommonService
from Service.EquipmentInfoService import EquipmentInfoService
from Service.GatewayService import GatewayPushService

'''
http://push.dvema.com/notify/push?etk=Y2lTRXhMTjBWS01sWlpURTVJU0ZWTlJ6RXhNVUU9T3o=&n_time=1526845794&channel=1&event_type=704&is_st=0
http://push.dvema.com/deviceShadow/generateUTK?username=debug_user&password=debug_password&uid=VVDHCVBYDKFMJRWA111A
'''


# 移动侦测接口
class NotificationView(View):

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        return self.validation(request.GET)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        if operation == 'test_apns':
            return self.test_apns(request.POST)
        return self.validation(request.POST)

    def validation(self, request_dict):
        logger = logging.getLogger('info')
        logger.info("进来推送接口了")
        logger.info(request_dict)
        logger.info('使用配置: {}'.format(SERVER_TYPE))
        uidToken = request_dict.get('uidToken', None)
        etk = request_dict.get('etk', None)
        channel = request_dict.get('channel', '1')
        n_time = request_dict.get('n_time', None)
        event_type = request_dict.get('event_type', None)
        is_st = request_dict.get('is_st', None)
        company_secrete = request_dict.get('company_secrete', None)
        region = request_dict.get('region', None)
        electricity = request_dict.get('electricity', '')

        if not all([channel, n_time]):
            return JsonResponse(status=200, data={
                'code': 444,
                'msg': 'param is wrong'})
        if not region or not is_st:
            return JsonResponse(status=200, data={'code': 404, 'msg': 'no region or is_st'})

        try:
            is_st = int(is_st)
            region = int(region)

            # 解密获取uid
            if etk:
                eto = ETkObject(etk)
                uid = eto.uid
            else:
                uto = UidTokenObject(uidToken)
                uid = uto.UID
            # 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'})
            logger.info('调用推送接口的uid：{}'.format(uid))

            pkey = '{uid}_{channel}_{event_type}_ptl'.format(uid=uid, channel=channel, event_type=event_type)
            ykey = '{uid}_redis_qs'.format(uid=uid)
            is_sys_msg = self.is_sys_msg(int(event_type))
            if is_sys_msg:
                dkey = '{uid}_{channel}_{event_type}_flag'.format(uid=uid, channel=channel, event_type=event_type)
            else:
                dkey = '{uid}_{channel}_flag'.format(uid=uid, channel=channel)

            redisObj = RedisObject(db=6)
            have_ykey = redisObj.get_data(key=ykey)  # uid_set 数据库缓存
            have_pkey = redisObj.get_data(key=pkey)  # 一分钟限制key
            have_dkey = redisObj.get_data(key=dkey)  # 推送消息时间间隔
            logger.info('ykey：{}, pkey: {}, dkey: {},'.format(have_ykey, have_pkey, have_dkey))

            # 一分钟内不推送
            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, uid_set__detect_status=1). \
                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():
                logger.info('uid_push 数据不存在')
                return JsonResponse(status=200, data={'code': 176, 'msg': 'no uid_push data'})
            redis_list = []
            for qs in uid_push_qs:
                redis_list.append(qs)
            # 修改redis数据，并设置过期时间为10分钟
            redisObj.set_data(key=ykey, val=str(redis_list), expire=600)

            nickname = redis_list[0]['uid_set__nickname']
            detect_interval = redis_list[0]['uid_set__detect_interval']
            detect_group = redis_list[0]['uid_set__detect_group']

            if not nickname:
                nickname = uid

            if not have_dkey:
                # 设置推送消息的时间间隔
                if detect_group == '0' or detect_group == '':
                    redisObj.set_data(key=dkey, val=1, expire=detect_interval)
                else:
                    detect_group_list = detect_group.split(',')
                    if event_type in detect_group_list:
                        if detect_interval < 60:
                            detect_interval = 60
                        redisObj.set_data(key=dkey, val=1, expire=detect_interval)

            if is_st == 1 or is_st == 3:  # 使用aws s3
                aws_s3_client = s3_client(region=region)
                bucket = 'foreignpush' if region == 1 else 'push'
            kwag_args = {
                'uid': uid,
                'channel': channel,
                'event_type': event_type,
                'n_time': n_time,
            }
            eq_list = []
            sys_msg_list = []
            userID_ids = []
            do_apns_code = ''
            do_fcm_code = ''
            do_jpush_code = ''
            logger.info('进入手机推送------')
            logger.info('uid={}'.format(uid))
            logger.info(redis_list)
            new_device_info_list = []
            local_date_time = ''
            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, electricity=electricity)
                kwag_args['appBundleId'] = appBundleId
                kwag_args['token_val'] = token_val
                kwag_args['msg_title'] = msg_title
                kwag_args['msg_text'] = msg_text
                logger.info('推送要的数据: {}'.format(kwag_args))
                local_date_time = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang='cn')
                logger.info('<<<<<根据时区计算后日期={}，时区={}'.format(local_date_time, tz))
                local_date_time = local_date_time[0:10]
                logger.info('<<<<<切片后的日期={}'.format(local_date_time))
                # 以下是存库
                userID_id = up["userID_id"]
                if userID_id not in userID_ids:
                    now_time = int(time.time())
                    if is_sys_msg:
                        sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
                                                         event_type=event_type, electricity=electricity, is_sys=1)
                        sys_msg_list.append(SysMsgModel(
                            userID_id=userID_id,
                            msg=sys_msg_text,
                            addTime=now_time,
                            updTime=now_time,
                            uid=uid,
                            eventType=event_type))
                    else:
                        # start 根据设备侦测时间为准进行分表存储数据
                        logger.info('分表存数据start------')
                        new_device_info_list.append(EquipmentInfoService.get_equipment_info_obj(
                            local_date_time,
                            device_user_id=userID_id,
                            event_time=n_time,
                            event_type=event_type,
                            device_uid=uid,
                            device_nick_name=nickname,
                            channel=channel,
                            alarm='Motion \tChannel:{channel}'.format(channel=channel),
                            is_st=is_st,
                            receive_time=n_time,
                            add_time=now_time,
                            storage_location=2,
                            border_coords='',
                        ))
                        # end
                    userID_ids.append(userID_id)
                try:
                    # 推送消息
                    if not have_dkey:
                        logger.info('准备推送:{}, {}'.format(uid, request_dict))
                        if (is_st == 1 or is_st == 3) and (push_type == 0 or push_type == 1):   # 推送显示图片
                            if is_st == 1:
                                key = '{}/{}/{}.jpeg'.format(uid, channel, n_time)
                            else:
                                key = '{}/{}/{}_0.jpeg'.format(uid, channel, n_time)
                            push_thread = threading.Thread(target=self.push_thread_test, args=(
                                push_type, aws_s3_client, bucket, key, uid, appBundleId, token_val, event_type, n_time,
                                msg_title, msg_text, channel))
                            push_thread.start()
                        else:
                            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)
                except Exception as e:
                    logger.info(
                        "errLine={errLine}, errMsg={errMsg}".format(errLine=e.__traceback__.tb_lineno, errMsg=repr(e)))
                    continue
            if is_sys_msg:
                SysMsgModel.objects.bulk_create(sys_msg_list)
            else:
                # Equipment_Info.objects.bulk_create(eq_list)
                # new 分表批量存储 设备信息
                if new_device_info_list and len(new_device_info_list) > 0:
                    # 根据日期获得星期几
                    week = LocalDateTimeUtil.date_to_week(local_date_time)
                    EquipmentInfoService.equipment_info_bulk_create(week, new_device_info_list)
                    logger.info('设备信息分表批量保存end------')

            if is_st == 0 or is_st == 2:
                for up in redis_list:
                    if up['push_type'] == 0:  # ios apns
                        up['do_apns_code'] = do_apns_code
                    elif up['push_type'] == 1:  # android gcm
                        up['do_fcm_code'] = do_fcm_code
                    elif up['push_type'] == 2:  # android jpush
                        up['do_jpush_code'] = do_jpush_code
                    up['test_or_www'] = SERVER_TYPE
                    del up['push_type']
                    del up['userID_id']
                    del up['userID__NickName']
                    del up['lang']
                    del up['tz']
                    del up['uid_set__nickname']
                    del up['uid_set__detect_interval']
                    del up['uid_set__detect_group']
                return JsonResponse(status=200, data={'code': 0, 'msg': 'success 0 or 2', 're_list': redis_list})

            elif is_st == 1:
                thumbspng = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
                Params = {'Key': thumbspng}
                if region == 2:  # 2:国内
                    Params['Bucket'] = 'push'
                else:  # 1：国外
                    Params['Bucket'] = 'foreignpush'
                response_url = generate_s3_url(aws_s3_client, Params)
                for up in redis_list:
                    up['do_apns_code'] = do_apns_code
                    up['do_fcm_code'] = do_fcm_code
                    up['do_jpush_code'] = do_jpush_code
                    up['test_or_www'] = SERVER_TYPE
                    del up['push_type']
                    del up['userID_id']
                    del up['userID__NickName']
                    del up['lang']
                    del up['tz']
                    del up['uid_set__nickname']
                    del up['uid_set__detect_interval']
                    del up['uid_set__detect_group']
                res_data = {'code': 0, 'img_push': response_url, 'msg': 'success'}
                return JsonResponse(status=200, data=res_data)

            elif is_st == 3:
                img_url_list = []
                if region == 2:  # 2:国内
                    Params = {'Bucket': 'push'}
                else:  # 1：国外
                    Params = {'Bucket': 'foreignpush'}
                for i in range(is_st):
                    thumbspng = '{uid}/{channel}/{filename}_{st}.jpeg'. \
                        format(uid=uid, channel=channel, filename=n_time, st=i)
                    Params['Key'] = thumbspng
                    response_url = generate_s3_url(aws_s3_client, Params)
                    img_url_list.append(response_url)

                for up in redis_list:
                    up['do_apns_code'] = do_apns_code
                    up['do_fcm_code'] = do_fcm_code
                    up['do_jpush_code'] = do_jpush_code
                    up['test_or_www'] = SERVER_TYPE
                    del up['push_type']
                    del up['userID_id']
                    del up['userID__NickName']
                    del up['lang']
                    del up['tz']
                    del up['uid_set__nickname']
                    del up['uid_set__detect_interval']
                    del up['uid_set__detect_group']
                res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success 3'}
                return JsonResponse(status=200, data=res_data)
        except Exception as e:
            logger.info('移动侦测接口异常: {}'.format(e))
            logger.info('错误文件', e.__traceback__.tb_frame.f_globals['__file__'])
            logger.info('错误行号', e.__traceback__.tb_lineno)
            data = {
                'errLine': e.__traceback__.tb_lineno,
                'errMsg': repr(e),
            }
            return JsonResponse(status=200, data=json.dumps(data), safe=False)

    def push_thread_test(self, push_type, aws_s3_client, bucket, key, uid, appBundleId, token_val, event_type, n_time,
                         msg_title, msg_text, channel):
        logger = logging.getLogger('info')
        logger.info('推送图片测试:{} {} {} {} {} {} {} {}'.format(push_type, uid, appBundleId, token_val, event_type, n_time,
                                                            msg_title, msg_text))
        try:
            image_url = aws_s3_client.generate_presigned_url('get_object', Params={'Bucket': bucket, 'Key': key},
                                                             ExpiresIn=300)
            logger.info('推送图片url:{}'.format(image_url))
            if push_type == 0:
                GatewayPushService.ios_apns_push(uid, appBundleId, token_val, n_time, event_type, msg_title, msg_text,
                                                 uid, channel, image_url)
            elif push_type == 1:
                GatewayPushService.android_fcm_push(uid, appBundleId, token_val, n_time, event_type, msg_title,
                                                    msg_text, uid, channel, image_url)
        except Exception as e:
            logger.info('推送图片测试异常:{}'.format(e))

    def test_apns(self, request_dict):
        kwag_args = {
            'uid': request_dict.get('uid', None),
            'channel': request_dict.get('channel', None),
            'event_type': request_dict.get('event_type', None),
            'n_time': request_dict.get('n_time', None),
            'appBundleId': request_dict.get('appBundleId', None),
            'token_val': request_dict.get('token_val', None),
            'msg_title': request_dict.get('msg_title', None),
            'msg_text': request_dict.get('msg_text', None),
        }
        do_apns_code = self.do_apns(**kwag_args)
        return JsonResponse(status=500, data={'do_apns_code': do_apns_code})

    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 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, electricity='', is_sys=0):
        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 = '剩余电量:' + electricity
            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)
        else:
            if etype == 704:
                msg_type = 'Battery remaining:' + electricity
            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)
        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()
        # 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}
        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
        # 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):
        logger = logging.getLogger('info')
        try:
            serverKey = FCM_CONFIG[appBundleId]
        except Exception as e:
            logger.info('------fcm_error:{}'.format(repr(e)))
            return 'serverKey abnormal'
        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
                                                   })
        logger.info('------fcm_status:')
        logger.info(result)
        print('fcm push ing')
        print(result)
        return result

    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")

            # 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("apns_推送状态：")
            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
        except (ValueError, ArithmeticError):
            return 'The program has a numeric format exception, one of the arithmetic exceptions'
        except Exception as e:
            print(repr(e))
            print('do_apns函数错误行号', e.__traceback__.tb_lineno)
            logger.info('do_apns错误：{}'.format(repr(e)))
            return repr(e)

    def do_update_detect_interval(self, uid, channel, redisObject, detect_interval):
        if channel == 0:
            channel = 17
        else:
            channel += 1
        for i in range(1, channel):
            tmpDKey = '{uid}_{channel}_{event_type}_flag'.format(uid=uid, event_type=51, channel=i)
            if tmpDKey is not False:
                llt = redisObject.get_ttl(tmpDKey)
                if llt > detect_interval:
                    redisObject.set_data(key=tmpDKey, val=1, expire=detect_interval)

            tmpDKey = '{uid}_{channel}_{event_type}_flag'.format(uid=uid, event_type=54, channel=i)
            if tmpDKey is not False:
                llt = redisObject.get_ttl(tmpDKey)
                if llt > detect_interval:
                    redisObject.set_data(key=tmpDKey, val=1, expire=detect_interval)


# http://test.dvema.com/detect/add?uidToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJQMldOR0pSRDJFSEE1RVU5MTExQSJ9.xOCI5lerk8JOs5OcAzunrKCfCrtuPIZ3AnkMmnd-bPY&n_time=1526845794&channel=1&event_type=51&is_st=0
# 移动侦测接口
class PushNotificationView(View):

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        # operation = kwargs.get('operation')
        return self.validation(request.GET)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        # operation = kwargs.get('operation')
        return self.validation(request.POST)

    def validation(self, request_dict):
        etk = request_dict.get('etk', None)
        channel = request_dict.get('channel', '1')
        n_time = request_dict.get('n_time', None)
        event_type = request_dict.get('event_type', None)
        is_st = request_dict.get('is_st', None)
        region = request_dict.get('region', '2')
        region = int(region)
        eto = ETkObject(etk)
        uid = eto.uid
        if len(uid) == 20:
            redisObj = RedisObject(db=6)
            # pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
            pkey = '{uid}_ptl'.format(uid=uid)
            ykey = '{uid}_redis_qs'.format(uid=uid)
            if redisObj.get_data(key=pkey):
                res_data = {'code': 0, 'msg': 'success,!33333333333'}
                return JsonResponse(status=200, data=res_data)
            else:
                redisObj.set_data(key=pkey, val=1, expire=60)
            ##############
            redis_data = redisObj.get_data(key=ykey)
            if redis_data:
                redis_list = eval(redis_data)
            else:
                # 设置推送时间为60秒一次
                redisObj.set_data(key=pkey, val=1, expire=60)
                print("从数据库查到数据")
                # 从数据库查询出来
                uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
                    values('token_val', 'app_type', 'appBundleId',
                           'push_type', 'userID_id', 'lang', 'm_code',
                           'tz', 'uid_set__nickname')
                # 新建一个list接收数据
                redis_list = []
                # 把数据库数据追加进redis_list
                for qs in uid_push_qs:
                    redis_list.append(qs)
                # 修改redis数据，并设置过期时间为10分钟
            if redis_list:
                redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
                # auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
                # bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
                aws_s3_guonei = 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'
                )
                aws_s3_guowai = boto3.client(
                    's3',
                    aws_access_key_id=AWS_ACCESS_KEY_ID[1],
                    aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
                    config=botocore.client.Config(signature_version='s3v4'),
                    region_name='us-east-1'
                )
                self.do_bulk_create_info(redis_list, n_time, channel, event_type, is_st, uid)
                if is_st == '0' or is_st == '2':
                    return JsonResponse(status=200, data={'code': 0, 'msg': 'success44444444444444444'})
                elif is_st == '1':
                    # Endpoint以杭州为例，其它Region请按实际情况填写。
                    # obj = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
                    # 设置此签名URL在60秒内有效。
                    # url = bucket.sign_url('PUT', obj, 7200)
                    thumbspng = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
                    if region == 2:  # 2:国内
                        response_url = aws_s3_guonei.generate_presigned_url(
                            ClientMethod='put_object',
                            Params={
                                'Bucket': 'push',
                                'Key': thumbspng
                            },
                            ExpiresIn=3600
                        )
                    else:  # 1：国外
                        response_url = aws_s3_guowai.generate_presigned_url(
                            ClientMethod='put_object',
                            Params={
                                'Bucket': 'foreignpush',
                                'Key': thumbspng
                            },
                            ExpiresIn=3600
                        )
                    # res_data = {'code': 0, 'img_push': url, 'msg': 'success'}
                    # response_url = response_url[:4] + response_url[5:]
                    res_data = {'code': 0, 'img_push': response_url, 'msg': 'success'}
                    return JsonResponse(status=200, data=res_data)
                elif is_st == '3':
                    # 人形检测带动图
                    img_url_list = []
                    for i in range(int(is_st)):
                        # obj = '{uid}/{channel}/{filename}_{st}.jpeg'. \
                        #     format(uid=uid, channel=channel, filename=n_time, st=i)
                        # 设置此签名URL在60秒内有效。
                        # url = bucket.sign_url('PUT', obj, 7200)

                        thumbspng = '{uid}/{channel}/{filename}_{st}.jpeg'. \
                            format(uid=uid, channel=channel, filename=n_time, st=i)
                        if region == 2:  # 2:国内
                            response_url = aws_s3_guonei.generate_presigned_url(
                                ClientMethod='put_object',
                                Params={
                                    'Bucket': 'push',
                                    'Key': thumbspng
                                },
                                ExpiresIn=3600
                            )
                        else:  # 1：国外
                            response_url = aws_s3_guowai.generate_presigned_url(
                                ClientMethod='put_object',
                                Params={
                                    'Bucket': 'foreignpush',
                                    'Key': thumbspng
                                },
                                ExpiresIn=3600
                            )
                        # response_url = response_url[:4] + response_url[5:]
                        img_url_list.append(response_url)

                        # img_url_list.append(url)
                    res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
                    return JsonResponse(status=200, data=res_data)
            else:
                return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
        else:
            return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong etk'})

    def do_bulk_create_info(self, uaqs, n_time, channel, event_type, is_st, uid):
        now_time = int(time.time())
        # 设备昵称
        userID_ids = []
        sys_msg_list = []
        is_sys_msg = self.is_sys_msg(int(event_type))
        is_st = int(is_st)
        eq_list = []
        nickname = uaqs[0]['uid_set__nickname']
        if not nickname:
            nickname = uid
        for ua in uaqs:
            lang = ua['lang']
            tz = ua['tz']
            userID_id = ua["userID_id"]
            if userID_id not in userID_ids:
                if is_sys_msg:
                    sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
                                                     event_type=event_type, is_sys=1)
                    sys_msg_list.append(SysMsgModel(
                        userID_id=userID_id,
                        msg=sys_msg_text,
                        addTime=now_time,
                        updTime=now_time,
                        uid=uid,
                        eventType=event_type))
                else:
                    eq_list.append(Equipment_Info(
                        userID_id=userID_id,
                        eventTime=n_time,
                        eventType=event_type,
                        devUid=uid,
                        devNickName=nickname,
                        Channel=channel,
                        alarm='Motion \tChannel:{channel}'.format(channel=channel),
                        is_st=is_st,
                        receiveTime=n_time,
                        addTime=now_time,
                        storage_location=2,
                        borderCoords='',
                    ))
        if eq_list:
            print('eq_list')
            Equipment_Info.objects.bulk_create(eq_list)
        if is_sys_msg:
            print('sys_msg')
            SysMsgModel.objects.bulk_create(sys_msg_list)
        return True

    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):
        n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz)
        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)
        else:
            if etype == 704:
                msg_type = 'Battery remaining:'
            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)
        return send_text


# 低电量推送接口
class PWnotificationView(View):

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        return self.validation(request.GET)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        return self.validation(request.POST)

    def validation(self, request_dict):
        logger = logging.getLogger('info')
        uid = request_dict.get('uid', None)

        # 限制每24小时推一次
        redisObj = RedisObject()
        is_limit = redisObj.CONN.setnx(uid+'limit_power_warning', 1)
        redisObj.CONN.expire(uid+'limit_power_warning', 24*60*60)
        if not is_limit:
            return JsonResponse(status=200, data={'code': 0, 'msg': 'push limited!'})

        channel = request_dict.get('channel', None)
        electricity = request_dict.get('electricity', None)
        logger.info('调用低电量推送接口的uid: {},electricity: {}'.format(uid, electricity))
        try:
            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():
                res_data = {'code': 173, 'msg': 'uid push data not exit!'}
                return JsonResponse(status=200, data=res_data)
            print(uid_push_qs)
            # 新建一个list接收数据
            redis_list = []
            # 把数据库数据追加进redis_list
            for qs in uid_push_qs:
                redis_list.append(qs)

            if not redis_list:
                res_data = {'code': 0, 'msg': 'no redis_list success!'}
                return JsonResponse(status=200, data=res_data)

            nickname = redis_list[0]['uid_set__nickname']
            if not nickname:
                nickname = uid

            now_time = int(time.time())
            channel = channel
            event_type = 704
            sys_msg_list = []
            userID_ids = []
            kwag_args = {
                'uid': uid,
                'channel': channel,
                'event_type': event_type,
                'n_time': now_time,
            }

            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=now_time, lang=lang, tz=tz,
                                             event_type=event_type, electricity=electricity)
                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
                    print('do_fcm')
                    do_fcm_code = self.do_fcm(**kwag_args)
                elif push_type == 2:  # android jpush
                    print('do_jpush')
                    do_jpush_code = self.do_jpush(**kwag_args)

                # 以下是存库
                userID_id = up["userID_id"]
                if userID_id not in userID_ids:
                    sys_msg_text = self.get_msg_text(channel=channel, n_time=now_time, lang=lang, tz=tz,
                                                     event_type=event_type, is_sys=1, electricity=electricity)
                    sys_msg_list.append(SysMsgModel(
                        userID_id=userID_id,
                        msg=sys_msg_text,
                        addTime=now_time,
                        updTime=now_time,
                        uid=uid,
                        eventType=event_type,
                    ))
                    userID_ids.append(userID_id)
            SysMsgModel.objects.bulk_create(sys_msg_list)
            return JsonResponse(status=200, data={'code': 0})
        except Exception as e:
            logger.info('低电量推送接口异常: {}'.format(e))
            return JsonResponse(status=500)

    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 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, electricity, is_sys=0):
        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 = '剩余电量:' + electricity
            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)
        else:
            if etype == 704:
                msg_type = 'Battery remaining:' + electricity
            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)
        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()
        # 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}
        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
        # 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:
            serverKey = FCM_CONFIG[appBundleId]
        except Exception as e:
            return 'serverKey abnormal'
        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

    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")

            # 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
        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 do_update_detect_interval(self, uid, channel, redisObject, detect_interval):
        if channel == 0:
            channel = 17
        else:
            channel += 1
        for i in range(1, channel):
            tmpDKey = '{uid}_{channel}_{event_type}_flag'.format(uid=uid, event_type=51, channel=i)
            if tmpDKey is not False:
                llt = redisObject.get_ttl(tmpDKey)
                if llt > detect_interval:
                    redisObject.set_data(key=tmpDKey, val=1, expire=detect_interval)

            tmpDKey = '{uid}_{channel}_{event_type}_flag'.format(uid=uid, event_type=54, channel=i)
            if tmpDKey is not False:
                llt = redisObject.get_ttl(tmpDKey)
                if llt > detect_interval:
                    redisObject.set_data(key=tmpDKey, val=1, expire=detect_interval)


def s3_client(region):
    if region == 2:  # 国内
        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'
        )
    else:  # 国外
        aws_s3_client = boto3.client(
            's3',
            aws_access_key_id=AWS_ACCESS_KEY_ID[1],
            aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
            config=botocore.client.Config(signature_version='s3v4'),
            region_name='us-east-1'
        )
    return aws_s3_client


def generate_s3_url(aws_s3_client, Params):
    response_url = aws_s3_client.generate_presigned_url(
        ClientMethod='put_object',
        Params=Params,
        ExpiresIn=3600
    )
    return response_url
