Pārlūkot izejas kodu

优化消息详情跳转接口

zhangdongming 1 gadu atpakaļ
vecāks
revīzija
577189fcfa
1 mainītis faili ar 113 papildinājumiem un 13 dzēšanām
  1. 113 13
      Controller/DetectControllerV2.py

+ 113 - 13
Controller/DetectControllerV2.py

@@ -23,6 +23,8 @@ from Object.utils import LocalDateTimeUtil
 from Service.CommonService import CommonService
 from Service.EquipmentInfoService import EquipmentInfoService
 from Service.VodHlsService import SplitVodHlsObject
+import redis
+from Ansjer.config import PUSH_REDIS_ADDRESS
 
 
 class DetectControllerViewV2(View):
@@ -524,30 +526,105 @@ class DetectControllerViewV2(View):
             LOGGER.error('oci查询消息列表异常error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
             return
 
+    # redis_obj,is_st,storage_location,uid,channel,event_type,event_time,event_tag)
+    def get_redis_url(self, **params):
+        oss_img_bucket = params['oss_img_bucket']
+
+        # 国内生产环境默认不实例OCI对象
+        oci = self.get_oci_client()
+        uid = params['uid']
+        is_st = params['is_st']
+        storage_location = params['storage_location']
+        region = params['region']
+        aws_s3 = params['aws_s3']
+        aws_s3_cn = params['aws_s3_cn']
+        redis_obj = params['redis_obj']
+        channel = params['channel']
+        event_time = params['event_time']
+        img_list = []
+        img_url = ''
+        if is_st == 1:
+            thumbspng = '{}/{}/{}.jpeg'.format(uid, channel, event_time)
+            if storage_location == 1:  # 阿里云oss
+                img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
+            elif storage_location in [3, 4]:
+                prefix_name = f'{uid}/'
+                img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
+                if img_url:
+                    img_url = img_url + thumbspng
+            else:
+                params = {'Key': thumbspng}
+                if region == 1:  # AWS国外
+                    params['Bucket'] = 'foreignpush'
+                    img_url = aws_s3.generate_presigned_url(
+                        'get_object', Params=params, ExpiresIn=300)
+                else:  # AWS国内
+                    params['Bucket'] = 'push'
+                    img_url = aws_s3_cn.generate_presigned_url(
+                        'get_object', Params=params, ExpiresIn=300)
+            img_list = [img_url]
+        elif is_st == 3 or is_st == 4:
+            # 列表装载回放时间戳标记
+            img_list = []
+            for i in range(is_st):
+                thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
+                if storage_location == 1:  # 阿里云oss
+                    img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
+                elif storage_location in [3, 4]:
+                    prefix_name = f'{uid}/'
+                    img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
+                    if img_url:
+                        img_url = img_url + thumbspng
+                else:
+                    params = {'Key': thumbspng}
+                    if region == 1:  # 国外AWS
+                        params['Bucket'] = 'foreignpush'
+                        img_url = aws_s3.generate_presigned_url(
+                            'get_object', Params=params, ExpiresIn=300)
+                    else:  # 国内AWS
+                        params['Bucket'] = 'push'
+                        img_url = aws_s3_cn.generate_presigned_url(
+                            'get_object', Params=params, ExpiresIn=300)
+                img_list.append(img_url)
+        ai_event_type_list = EquipmentInfoService.get_combo_types(params['event_type'], params['event_tag'])
+        msg_data = {
+            "id": "",
+            "status": False,
+            "answer_status": False,
+            "alarm": "",
+            "is_st": params['is_st'],
+            "storage_location": params['storage_location'],
+            "devUid": uid,
+            "devNickName": "",
+            "Channel": params['channel'],
+            "eventType": params['event_type'],
+            "eventTime": params['event_time'],
+            "receiveTime": "",
+            "addTime": 0,
+            "borderCoords": "",
+            "eventTag": params['event_tag'],
+            "img": img_url,
+            "img_list": img_list,
+            "uid_type": 0,
+            "ai_event_type_list": ai_event_type_list
+        }
+        return msg_data
+
     def do_transfer(self, request_dict, response, userID):
         event_time = request_dict.get('eventTime', None)
         event_type = request_dict.get('eventType', None)
         region = int(request_dict.get('region', None))
+        channel = int(request_dict.get('channel', 1))
         uid = request_dict.get('uid', None)
 
         if not all([event_time, region, uid]):
             return response.json(444)
 
         try:
-            kwargs = {'device_user_id': userID, 'device_uid': uid, 'event_time': event_time, 'event_type': event_type}
-            equipment_info_qs, count = EquipmentInfoService.get_equipment_info(**kwargs)
-
-            # 查询设备类型,昵称
-            dvqs = Device_Info.objects.filter(UID=uid, 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 count == 0:
-                return response.json(0, {'datas': [], 'count': 0})
+            msg_key = 'PUSH:MSG:IMAGE:{}:{}:{}'.format(uid, channel, event_time)
+            redis_client = redis.Redis(connection_pool=redis.ConnectionPool(host=PUSH_REDIS_ADDRESS, port=6379, db=3))
+            msg_data = redis_client.get(msg_key)
 
-            res = []
             auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
             oss_img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
             aws_s3 = boto3.client(
@@ -569,6 +646,29 @@ class DetectControllerViewV2(View):
             oci = self.get_oci_client()
             redis_obj = RedisObject(3)
 
+            if msg_data:
+                msg_dict = json.loads(msg_data)
+                params = {'redis_obj': redis_obj, 'is_st': msg_dict['is_st'], 'region': region, 'aws_s3': aws_s3,
+                          'storage_location': msg_dict['storage_location'], 'aws_s3_cn': aws_s3_cn,
+                          'uid': uid, 'channel': channel, 'event_type': event_type, 'oss_img_bucket': oss_img_bucket,
+                          'event_time': event_time, 'event_tag': msg_dict['event_tag']}
+                res = self.get_redis_url(**params)
+                return response.json(0, {'datas': res, 'count': 1})
+            kwargs = {'device_user_id': userID, 'device_uid': uid, 'event_time': event_time, 'event_type': event_type}
+            equipment_info_qs, count = EquipmentInfoService.get_equipment_info(**kwargs)
+
+            # 查询设备类型,昵称
+            dvqs = Device_Info.objects.filter(UID=uid, 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 count == 0:
+                return response.json(0, {'datas': [], 'count': 0})
+
+            res = []
+
             # ai消息标识所有组合标签
             ai_all_event_type = EquipmentInfoService.get_all_comb_event_type()
             for equipment_info in equipment_info_qs: