linhaohong 1 жил өмнө
parent
commit
705d58dae6

+ 187 - 179
AdminController/MessageMangementController.py

@@ -36,9 +36,9 @@ class MassageView(View):
         if operation == 'XXX':
         if operation == 'XXX':
             return 0
             return 0
         else:
         else:
-            # tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'), returntpye='pc')
-            # if tko.code != 0:
-            #     return response.json(tko.code)
+            tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'), returntpye='pc')
+            if tko.code != 0:
+                return response.json(tko.code)
             if operation == 'queryInfoList':
             if operation == 'queryInfoList':
                 return self.query_info_list(request_dict, response)
                 return self.query_info_list(request_dict, response)
             else:
             else:
@@ -58,210 +58,218 @@ class MassageView(View):
         @param response:
         @param response:
         @return:
         @return:
         """
         """
-        uids = request_dict.get('uids', None)
-        event_type = request_dict.get('eventType', None)
-        user_id = request_dict.get('userID', None)
-        # 时间
-        start_time = request_dict.get('startTime', None)
-        end_time = request_dict.get('endTime', None)
-        # 分页
-        page = int(request_dict.get('pageNo', None))
-        size = int(request_dict.get('pageSize', None))
-        # 区域
-        if SERVER_TYPE == 'Ansjer.cn_config.formal_settings' or SERVER_TYPE == 'Ansjer.cn_config.test_settings':
-            region = 2
-        else:
-            region = 1
-
-        query = Q()
-        if not start_time and not end_time:
-            # 默认查询近七天内数据
-            end_time = int(time.time())
-            start_time = LocalDateTimeUtil.get_before_days_timestamp(end_time, 7)
-            query &= Q(event_time__range=(start_time, end_time))
+        try:
+            uids = request_dict.get('uids', None)
+            event_type = request_dict.get('eventType', None)
+            user_id = request_dict.get('userID', None)
+            # 时间
+            start_time = request_dict.get('startTime', None)
+            end_time = request_dict.get('endTime', None)
+            # 分页
+            page = int(request_dict.get('pageNo', None))
+            size = int(request_dict.get('pageSize', None))
+            # 区域
+            if SERVER_TYPE == 'Ansjer.cn_config.formal_settings' or SERVER_TYPE == 'Ansjer.cn_config.test_settings':
+                region = 2
+            else:
+                region = 1
 
 
-        elif start_time and end_time:
-            query &= Q(event_time__range=(start_time, end_time))
+            query = Q()
+            if not start_time and not end_time:
+                # 默认查询近七天内数据
+                end_time = int(time.time())
+                start_time = LocalDateTimeUtil.get_before_days_timestamp(end_time, 7)
+                query &= Q(event_time__range=(start_time, end_time))
 
 
-        else:
-            response.json(10, "需要给出一个时间段")
+            elif start_time and end_time:
+                query &= Q(event_time__range=(start_time, end_time))
 
 
-        if user_id is None and uids is None:
-            return response.json(0, {"list": [], "total": 0})
+            else:
+                response.json(10, "需要给出一个时间段")
 
 
-        # 过滤条件
-        if user_id is not None and user_id != "":
-            query &= Q(device_user_id=user_id)
+            if user_id is None and uids is None:
+                return response.json(0, {"list": [], "total": 0})
 
 
-        if uids is not None and uids != "":
-            uid_list = uids.split(',')
-            query &= Q(device_uid__in=uid_list)
+            # 过滤条件
+            if user_id is not None and user_id != "":
+                query &= Q(device_user_id=user_id)
 
 
-        if event_type is not None:
-            event_type_list = EquipmentInfoService.get_comb_event_type(event_type)
-            event_type_list = list(set(event_type_list))
-            tags = EquipmentInfoService.get_event_tag(event_type)
-            if event_type_list:
-                query &= Q(event_type__in=event_type_list)
-                tags = ''
-                query &= Q(event_tag__regex=tags)
-            elif tags:
-                query &= Q(event_tag__regex=tags)
+            if uids is not None and uids != "":
+                uid_list = uids.split(',')
+                query &= Q(device_uid__in=uid_list)
 
 
-        # 联表查询
-        querysets = []
-        for i in range(1, 21):
-            table_name = f'EquipmentInfo{i}'
-            model_class = getattr(models, table_name)
-            annotated_queryset = model_class.objects.filter(query)
-            querysets.append(annotated_queryset)
+            if event_type is not None:
+                event_type_list = EquipmentInfoService.get_comb_event_type(event_type)
+                event_type_list = list(set(event_type_list))
+                tags = EquipmentInfoService.get_event_tag(event_type)
+                if event_type_list:
+                    query &= Q(event_type__in=event_type_list)
+                    tags = ''
+                    query &= Q(event_tag__regex=tags)
+                elif tags:
+                    query &= Q(event_tag__regex=tags)
 
 
-        equipment_info_combined_qs = querysets[0].union(*querysets[1:], all=True)
+            # 联表查询
+            querysets = []
+            for i in range(1, 21):
+                table_name = f'EquipmentInfo{i}'
+                model_class = getattr(models, table_name)
+                annotated_queryset = model_class.objects.filter(query)
+                querysets.append(annotated_queryset)
 
 
-        # 创建分页对象
-        equipment_info_combined_qs = equipment_info_combined_qs.order_by("-event_time")
-        paginator = Paginator(equipment_info_combined_qs, size)
-        # 获取请求页的数据
-        packages_page = paginator.page(page)
+            equipment_info_combined_qs = querysets[0].union(*querysets[1:], all=True)
 
 
-        # 连接存储桶
-        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(
-            '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'
-        )
-        aws_s3_cn = 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'
-        )
+            # 创建分页对象
+            equipment_info_combined_qs = equipment_info_combined_qs.order_by("-event_time")
+            paginator = Paginator(equipment_info_combined_qs, size)
+            # 获取请求页的数据
+            packages_page = paginator.page(page)
 
 
-        # ai消息标识所有组合标签
-        ai_all_event_type = EquipmentInfoService.get_all_comb_event_type()
+            # 连接存储桶
+            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(
+                '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'
+            )
+            aws_s3_cn = 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'
+            )
 
 
-        # 遍历调整返回数据
-        equipment_info_list = []
-        for equipment_info in packages_page:
-            uid = equipment_info.device_uid
-            channel = equipment_info.channel
-            event_time = equipment_info.event_time
-            storage_location = equipment_info.storage_location
-            border_coords = equipment_info.border_coords
-            event_tag = equipment_info.event_tag
+            # ai消息标识所有组合标签
+            ai_all_event_type = EquipmentInfoService.get_all_comb_event_type()
 
 
-            img_url = ""
-            if equipment_info.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)
-                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)
+            # 遍历调整返回数据
+            equipment_info_list = []
+            for equipment_info in packages_page:
+                uid = equipment_info.device_uid
+                channel = equipment_info.channel
+                event_time = equipment_info.event_time
+                storage_location = equipment_info.storage_location
+                border_coords = equipment_info.border_coords
+                event_tag = equipment_info.event_tag
 
 
-                img_url = img_url
-                img_list = [img_url]
+                img_url = ""
+                if equipment_info.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)
+                    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)
 
 
-            elif equipment_info.is_st == 2:
-                # 列表装载回放时间戳标记
-                split_vod_hls_obj = SplitVodHlsObject()
-                vodqs = split_vod_hls_obj.get_vod_hls_data(
-                    uid=uid, channel=channel, start_time=int(event_time)).values('bucket_id')
-                if not vodqs.exists():
-                    return response.json(173)
-                vod_bucket_qs = VodBucketModel.objects.filter(id=vodqs[0]['bucket_id']).values('bucket', 'endpoint')
-                if not vod_bucket_qs.exists():
-                    return response.json(173)
-                bucket_name = vod_bucket_qs[0]['bucket']
-                endpoint = vod_bucket_qs[0]['endpoint']
-                bucket = oss2.Bucket(auth, endpoint, bucket_name)
-                ts = '{}/vod{}/{}/ts0.ts'.format(uid, channel, event_time)
-                if storage_location == 1:  # 阿里云oss
-                    thumb0 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_0000,w_700'})
-                    thumb1 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_1000,w_700'})
-                    thumb2 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_2000,w_700'})
-                    img_url = ""
-                    img_list = [thumb0, thumb1, thumb2]
-                else:
-                    params = {'Key': ts}
-                    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_url = img_url
                     img_list = [img_url]
                     img_list = [img_url]
 
 
-            elif equipment_info.is_st == 3 or equipment_info.is_st == 4:
-                # 列表装载回放时间戳标记
-                img_list = []
-                for i in range(equipment_info.is_st):
-                    thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
+                elif equipment_info.is_st == 2:
+                    # 列表装载回放时间戳标记
+                    split_vod_hls_obj = SplitVodHlsObject()
+                    vodqs = split_vod_hls_obj.get_vod_hls_data(
+                        uid=uid, channel=channel, start_time=int(event_time)).values('bucket_id')
+                    if not vodqs.exists():
+                        return response.json(173)
+                    vod_bucket_qs = VodBucketModel.objects.filter(id=vodqs[0]['bucket_id']).values('bucket', 'endpoint')
+                    if not vod_bucket_qs.exists():
+                        return response.json(173)
+                    bucket_name = vod_bucket_qs[0]['bucket']
+                    endpoint = vod_bucket_qs[0]['endpoint']
+                    bucket = oss2.Bucket(auth, endpoint, bucket_name)
+                    ts = '{}/vod{}/{}/ts0.ts'.format(uid, channel, event_time)
                     if storage_location == 1:  # 阿里云oss
                     if storage_location == 1:  # 阿里云oss
-                        img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
+                        thumb0 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_0000,w_700'})
+                        thumb1 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_1000,w_700'})
+                        thumb2 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_2000,w_700'})
+                        img_url = ""
+                        img_list = [thumb0, thumb1, thumb2]
                     else:
                     else:
-                        params = {'Key': thumbspng}
-                        if region == 1:  # 国外AWS
+                        params = {'Key': ts}
+                        if region == 1:  # AWS国外
                             params['Bucket'] = 'foreignpush'
                             params['Bucket'] = 'foreignpush'
                             img_url = aws_s3.generate_presigned_url(
                             img_url = aws_s3.generate_presigned_url(
                                 'get_object', Params=params, ExpiresIn=300)
                                 'get_object', Params=params, ExpiresIn=300)
-                        else:  # 国内AWS
+                        else:  # AWS国内
                             params['Bucket'] = 'push'
                             params['Bucket'] = 'push'
                             img_url = aws_s3_cn.generate_presigned_url(
                             img_url = aws_s3_cn.generate_presigned_url(
                                 'get_object', Params=params, ExpiresIn=300)
                                 'get_object', Params=params, ExpiresIn=300)
-                    img_list.append(img_url)
+                        img_list = [img_url]
 
 
-            else:
-                img_list = []
+                elif equipment_info.is_st == 3 or equipment_info.is_st == 4:
+                    # 列表装载回放时间戳标记
+                    img_list = []
+                    for i in range(equipment_info.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)
+                        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)
+
+                else:
+                    img_list = []
+
+                uid_type = Device_Info.objects.filter(UID=uid).values('Type').first()
+                device_type = DeviceTypeModel.objects.filter(type=uid_type['Type']).values('name').first()
+                if device_type is None:
+                    device_type = {"name": uid_type['Type']}
+                border_coords = '' if border_coords == '' else eval(border_coords)
+                ai_event_type_list = []
 
 
-            uid_type = Device_Info.objects.filter(UID=uid).values('Type').first()
-            device_type = DeviceTypeModel.objects.filter(type=uid_type['Type']).values('name').first()
-            border_coords = '' if border_coords == '' else eval(border_coords)
-            ai_event_type_list = []
+                # 如果是ai消息类型,则分解eventType, 如:123 -> [1,2,3]
+                if border_coords and event_type in ai_all_event_type:
+                    ai_event_type_list = list(map(int, str(event_type)))
+                if EquipmentInfoService.is_combo_tag(event_type, event_tag):
+                    ai_event_type_list += EquipmentInfoService.get_combo_types(event_type, event_tag)
+                equipment_info_data = {
+                    "uid": uid,
+                    "status": equipment_info.status,
+                    "answerStatus": equipment_info.answer_status,
+                    "alarm": equipment_info.alarm,
+                    "isSt": equipment_info.is_st,
+                    "storageLocation": storage_location,
+                    "devNickName": equipment_info.device_nick_name,
+                    "channel": channel,
+                    "eventType": equipment_info.event_type,
+                    "eventTime": int(event_time),
+                    "addTime": equipment_info.add_time,
+                    "borderCoords": border_coords,
+                    "eventTag": event_tag,
+                    "img": img_url,
+                    "imgList": img_list,
+                    "uidType": device_type["name"],
+                    "aiEventTypeList": str(ai_event_type_list),
+                }
+                equipment_info_list.append(equipment_info_data)
 
 
-            # 如果是ai消息类型,则分解eventType, 如:123 -> [1,2,3]
-            if border_coords and event_type in ai_all_event_type:
-                ai_event_type_list = list(map(int, str(event_type)))
-            if EquipmentInfoService.is_combo_tag(event_type, event_tag):
-                ai_event_type_list += EquipmentInfoService.get_combo_types(event_type, event_tag)
-            equipment_info_data = {
-                "uid": uid,
-                "status": equipment_info.status,
-                "answerStatus": equipment_info.answer_status,
-                "alarm": equipment_info.alarm,
-                "isSt": equipment_info.is_st,
-                "storageLocation": storage_location,
-                "devNickName": equipment_info.device_nick_name,
-                "channel": channel,
-                "eventType": equipment_info.event_type,
-                "eventTime": int(event_time),
-                "addTime": equipment_info.add_time,
-                "borderCoords": border_coords,
-                "eventTag": event_tag,
-                "img": img_url,
-                "imgList": img_list,
-                "uidType": device_type["name"],
-                "aiEventTypeList": str(ai_event_type_list),
+            data = {
+                "list": equipment_info_list,
+                "total": paginator.count
             }
             }
-            equipment_info_list.append(equipment_info_data)
 
 
-        data = {
-            "list": equipment_info_list,
-            "total": paginator.count
-        }
+            return response.json(0, data)
+
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
 
-        return response.json(0, data)