Exemplo n.º 1
0
def update_channel():
    """更新通道"""
    if not request.content_type == 'application/json':
        return render_response(-1,msg="Content-type must be application/json.")

    form = ChannelForm.from_json(request.json)
    channel_id = request.json.get('channel_id')

    if not form.validate() or not is_uuid(channel_id):
        msg = "parameter error:{}".format(form.errors)
        logger.warning(msg)
        return render_response(-1, msg=msg)

    channel = Channel.query.get(channel_id)
    if not channel:
        msg = "channel_id: {} is not exist".format(channel_id)
        logger.error(msg)
        return render_response(-1, msg=msg)

    channel.camera_id = form.camera_id.data
    channel.channel_name = form.channel_name.data
    channel.channel_number = form.channel_number.data
    channel.gb_number = form.gb_number.data
    channel.yun_tai = form.yun_tai.data
    channel.status = form.status.data
    try:
        channel.update()
    except Exception as e:
        logger.error()
        return render_response(-1, msg='datebase error')
    return render_response(0)
Exemplo n.º 2
0
def get_channel_by_id():
    """通过id获取通道"""
    if not request.content_type == 'application/json':
        return render_response(-1,msg="Content-type must be application/json.")

    channel_id = request.json.get('channel_id')

    rep = db.session.query(Channel, Camera). \
        filter(Channel.is_delete == False, Channel.id == channel_id).first()
    if not rep:
        logger.warning("channel not exist. id:{}".format(channel_id))
        return render_response(-1, msg="channel not exist")
    else:
        channel, camera = rep
        data = dict()
        data["channel_id"] = channel.id
        data["channel_name"] = channel.channel_name
        data["channel_number"] = channel.channel_number
        data["camera_id"] = channel.camera.id
        data["camera_name"] = channel.camera.camera_name
        data["organization_id"] = channel.camera.organization_id
        data["platform_id"] = channel.camera.cameraplatform_id
        data["gb_number"] = channel.gb_number
        data["yun_tai"] = channel.yun_tai
        data["status"] = channel.status
        data["creator"] = "admin"
        data["create_time"] = channel.create_time.strftime("%Y-%m-%d %H:%M:%S")
        data["update_time"] = channel.update_time.strftime("%Y-%m-%d %H:%M:%S")
        data["organization_name"] = camera.organization.organization_name \
            if camera.organization_id else ""
        data["platform_name"] = camera.cameraplatform.camera_platform_name \
            if camera.cameraplatform_id else ""

    return render_response(0, data=data)
Exemplo n.º 3
0
    def start_call_api(self):
        # 从redis中获取data 调用api
        logger.info("启动redis 任务监听进程。")
        while True:
            ret = redis_store.brpop(["task_high", "task_middle", "task_low"])
            data = ret[1]
            logger.info("从redis中获取视频诊断任务: {}.".format(data))
            try:
                res = self.call_api(data)  # 调用api
            except Exception as e:
                # print("****", e)
                logger.critical("视频诊断接口调用错误: {}".format(e))
                continue

            if res['code'] == 1:  # api调用成功
                # print("***** success")
                logger.info("视频诊断接口调用成功:{}".format(data))
                pass

            elif res["code"] == -10000:  # 失败:线程饱和
                logger.warning("视频诊断服务,线程饱和。")
                while True:
                    time.sleep(1)
                    res = self.call_api(data)
                    if res['code'] == 1:
                        break
            else:
                # print("video diagnosis api call error, code: {}".format(res["code"]))
                logger.error("视频诊断接口调用失败,错误码:{}, data:{}".format(
                    res["code"], data))
Exemplo n.º 4
0
def add_channel():
    """添加通道"""
    if not request.content_type == 'application/json':
        return render_response(-1,msg="Content-type must be application/json.")

    form = ChannelForm.from_json(request.json)
    if not form.validate():
        msg = "Content-type error:{}".format(form.errors)
        logger.warning(msg)
        return render_response(-1, msg=msg)
    try:
        kwargs = form.data
        channel = Channel(**kwargs)
        channel.add()
    except Exception as e:
        logger.error(e)
        return render_response(-1, msg='database error')
    return render_response(0)
Exemplo n.º 5
0
    def assign_task(self, task_id):
        """
        由任务调度器调用。对接视频质量诊断平台,和zabbix的接口
        :param task_id:
        :return:
        """
        logger.info("任务开始执行 task_id:{}".format(task_id))

        with app.app_context():
            task = Task.query.get(task_id)

            if not task or task.is_delete == True:
                logger.warning("任务执行失败,已删除或不存在。task_id: {}".format(task_id))
                return False

            self.update_task_status(task)  # 更新任务状态

            # camera_channel = db.session.query(Camera, Channel). \
            #     filter(Channel.camera_id == Camera.id,
            #            TaskChannelRelationship.channel_id == Channel.id,
            #            TaskChannelRelationship.task_id == task_id,
            #            Channel.is_delete == False
            #            ).all()

            # 获取相机信息用于视频质量诊断
            channels = Channel.query.filter_by(task_id=task_id,
                                               is_delete=False).all()
            cameraInfo = []
            for channel in channels:
                #TODO
                if channel.is_platform and channel.channel_id and channel.channel_number \
                        and channel.platform_host and channel.platform_port:
                    # 平台下的相机
                    each_info = {
                        "plateType": 1,
                        "serverName": "Channel",
                        "serverId": channel.channel_id,
                        "serverPath": channel.channel_number,
                        "serverIP": channel.platform_host,
                        "serverPort": int(channel.platform_port),
                        "userName": channel.platform_user or "",
                        "passWord": channel.platform_password or ""
                    }
                    cameraInfo.append(each_info)

                # else:
                # IPC 或 NVR
                # each_info = {
                #     "plateType": 2,
                #     "serverName": "Channel",
                #     "serverId": channel.channel_id,
                #     "serverPath": str(channel.channel_number) or "",
                #     "serverIP": channel.platform_host,
                #     "serverPort": int(channel.platform_port),
                #     "userName": channel.platform_user,
                #     "passWord": channel.platform_password
                # }
                #cameraInfo.append(each_info)

            # 任务检测项目关系表
            items_rel = task.items
            thresholds = {}  # 获取检测项目阀值信息
            for each in items_rel:
                thresholds[each.item.item_name] = each.threshold
            # 视频质量诊断算法参数。
            algorithmParam = {
                "fuzziness": thresholds.get("fuzziness") or 99,
                "occlusion": thresholds.get("occlusion") or 0,
                "signal": thresholds.get("signal") or 0,
                "frozen": thresholds.get("frozen") or 0,
                "content": thresholds.get("content") or 99,
                "brightness": thresholds.get("brightness") or 99,
                "crossGrain": thresholds.get("crossGrain") or 0,
                "rollScreen": thresholds.get("rollScreen") or 99,
                "chromaticColor": thresholds.get("chromaticColor") or 99,
                "noise": thresholds.get("noise") or 99,
                "dark": thresholds.get("dark") or 99,
                "move": thresholds.get("move") or 0,
                "shake": thresholds.get("move") or 0
            }

            video_diagnosis_data = {
                "userData":
                "{{\"taskId\":\"{}\",\"CheckName\":\"查验位 1\"}}".format(
                    task_id),
                # "subscribeUrl": "redis://172.16.1.190:6379/test/sub/",
                "subscribeUrl":
                self.redis_subscribe_url,
                "cameraInfo":
                cameraInfo,
                "algorithmParam":
                algorithmParam
            }
            # 更新任务状态

            # 启动视频质量诊断服务
            self.video_quality_diagnosis_add_redis(video_diagnosis_data,
                                                   priority=task.task_priority)

            # 获取相机信息ip用于网络检测。 {id:ip,id:ip}
            camera_ip_list = dict()

            # for camera, channel in camera_channel:
            #     camera_ip_list[camera.id] = camera.camera_ip
            # delay = thresholds.get("delay")
            # loss = thresholds.get("packetLossRate")
            # if delay or loss:
            #     self.network_diagnosis(task_id, camera_list=camera_ip_list, delay=delay, loss=loss)

            return True