def video_quality_diagnosis_add_redis(self, data, priority=1): # 启动视频质量诊断,推送信息到redis队列中。 if priority == 1: key = 'task_low' elif priority == 2: key = 'task_middle' else: key = 'task_high' data = json.dumps(data) redis_store.lpush(key, data) logger.info("视频诊断任务写入redis, data:{}".format(data))
def add_camera_list(): logger.info("收到分组下添加摄像机命令") try: task_info = get_json(request) logger.info("收到的json是:{}".format(str(task_info))) group_id = task_info["group_id"] camera_list = task_info["camera_list"] userdata = task_info["userdata"] logger.info((group_id, camera_list, userdata)) except BaseException as e: # json格式错误 logger.error(e) return get_res(-1, userdata) try: for camera_id in camera_list: camerainfo = Camera.query.filter( and_(Camera.id == camera_id, Camera.is_delete == False)).first() camerainfo.cameragroup_id = group_id db.session.add(camerainfo) db.session.commit() except BaseException as e: # 接口调用失败 logger.error(e) db.session.rollback() return get_res(-1, userdata) logger.info("分组下添加摄像机成功") return get_res(0, userdata)
def delete_task(): """根据id删除任务""" if not request.content_type == 'application/json': return render_response(-1, msg="Content-type must be application/json.") task_info = request.json task_ids = task_info.get("task_id") if not task_ids: return render_response(-1, msg="task_id is required.") userdata = task_info.get("userdata") logger.info((task_ids, userdata)) if isinstance(task_ids, str): task_ids = [task_ids] # 对task_id进行查询 try: for task_id in task_ids: taskinfo = Task.query.filter((Task.id == task_id) & (Task.is_delete == False)).first() if taskinfo is None: logger.error("task not exist.") return render_response(-1, userdata=userdata, msg='task not exist.') taskinfo.is_delete = True db.session.add(taskinfo) # 删除调度器中的任务 try: scheduler.remove_job(task_id) except: pass db.session.commit() except Exception as e: # 接口调用失败 logger.error("task delete error:{}".format(e)) db.session.rollback() msg = "database delete error." return render_response(-1, msg=msg, userdata=userdata) # logger.info("删除任务成功") return render_response(0, userdata=userdata, msg="delete success.")
def search_camera_groups(): logger.info("收到查询摄像机分组树命令") try: task_info = get_json(request) logger.info("收到的json是:{}".format(str(task_info))) userdata = task_info["userdata"] logger.info(userdata) except BaseException as e: # json格式错误 logger.error(e) return get_res(-1, userdata) data_list = list() ret = dict() # 对camera_group表进行查询 try: groupinfo_list = CameraGroup.query.filter( CameraGroup.is_delete == False).all() for groupinfo in groupinfo_list: group_dict = dict() group_dict["group_id"] = groupinfo.id group_dict["group_name"] = groupinfo.group_name group_dict["parent_id"] = groupinfo.parent_id data_list.append(group_dict) except BaseException as e: # 接口调用失败 logger.error(e) return get_res(-1, userdata) ret["code"] = 0 ret["data"] = data_list ret["userdata"] = userdata logger.debug("返回的json是:{}".format(ret)) return jsonify(ret)
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))
def search_camera_list(): logger.info("收到查询分组下摄像机列表命令") data_list = list() ret = dict() try: task_info = get_json(request) logger.info("收到的json是:{}".format(str(task_info))) group_id = task_info["group_id"] userdata = task_info["userdata"] logger.info((group_id, userdata)) except BaseException as e: # json格式错误 logger.error(e) return get_res(-1, userdata) # 对camera表进行查询 try: camerainfo_list = Camera.query.filter( and_(Camera.cameragroup_id == group_id, Camera.is_delete == False)).all() for camerainfo in camerainfo_list: camera_dict = dict() camera_dict["camera_id"] = camerainfo.id camera_dict["camera_name"] = camerainfo.camera_name data_list.append(camera_dict) except BaseException as e: # 接口调用失败 logger.error(e) return get_res(-1, userdata) ret["code"] = 0 ret["data"] = data_list ret["userdata"] = userdata logger.debug("返回的json是:{}".format(ret)) return jsonify(ret)
def finish_network_diagnosis(task_id): """ 完成网络诊断,获取故障信息 :param task_id: """ problem_hosts = zabbix.problem(task_id) # 合并故障信息 cameras = problem_hosts.delay + problem_hosts.loss # 类型:{ "b60755f55144455f88df8a633d12cd1a": 1535440059 } cameras_dict = dict() # 去重并使用最新的时间 for each in cameras: timestamp = cameras_dict.get(each.clock) if timestamp: if int(each.clock) > timestamp: cameras_dict[each.id] = int(each.clock) else: cameras_dict[each.id] = int(each.clock) logger.info("网络诊断结果task_id:{}, res: {}".format(task_id, cameras_dict)) # 存数据库 with app.app_context(): try: for id, timestamp in cameras_dict.items(): fault = Fault() fault.task_id = task_id fault.camera_id = id fault.type2 = True fault.create_time = datetime.datetime.fromtimestamp( timestamp) db.session.add(fault) db.session.commit() # 停用主机 zabbix.delete(task_id) except Exception as e: logger.error("网络诊断故障存储失败task_id:{}, err:{}".format(task_id, e))
def network_diagnosis(self, task_id, camera_list, loss=None, delay=None, minutes=1): """ 启动网络诊断 """ try: if zabbix.exist(task_id): # 重新添加zabbix, 防止数据库已经修改。 zabbix.delete(task_id) # TODO # zabbix.start(task_id) else: pass zabbix.add(task_id, delay=delay, loss=loss, camera_list=camera_list) except ZabbixAPIException as e: logger.error("zabbix网络诊断添加失败 task_id:{}, err: {}".format( task_id, e)) else: # 网络诊断持续时间,默认5分钟 finish_time = datetime.datetime.now() + datetime.timedelta( minutes=minutes) id = uuid.uuid4().hex scheduler.add_job(id=id, func=self.finish_network_diagnosis, args=(task_id, ), replace_existing=True, trigger='date', run_date=finish_time) logger.info("启动zabbix网络诊断成功, task_id:{}。".format(task_id))
def add_camera_groups(): logger.info("收到添加摄像机分组命令") try: task_info = get_json(request) logger.info("收到的json是:{}".format(str(task_info))) group_name = task_info["group_name"] group_desc = task_info["group_desc"] parent_id = task_info["parent_id"] userdata = task_info["userdata"] logger.info((group_name, group_desc, parent_id, userdata)) except BaseException as e: # json格式错误 logger.error(e) return get_res(-1, userdata) try: check_name = CameraGroup.query.filter( and_(CameraGroup.group_name == group_name, CameraGroup.is_delete == False)).first() if check_name: logger.error("摄像机分组名称:{}已存在".format(group_name)) return get_res(-1, userdata) if parent_id: check_parent_id = CameraGroup.query.filter( and_(CameraGroup.id == parent_id, CameraGroup.is_delete == False)).first() if check_parent_id is None: logger.error("摄像机分组id不存在:{}".format(parent_id)) return get_res(-1, userdata) # 添加摄像机分组 camera_group = CameraGroup() # camera_group.id = str(uuid.uuid4()) camera_group.group_name = group_name camera_group.group_desc = group_desc camera_group.parent_id = parent_id logger.debug(camera_group) camera_group.add() except BaseException as e: # 接口调用失败 logger.error(e) camera_group.rollback() return get_res(-1, userdata) logger.info("添加摄像机分组成功") return get_res(0, userdata)
def update_camera_groups(): logger.info("收到编辑摄像机分组命令") try: task_info = get_json(request) logger.info("收到的json是:{}".format(str(task_info))) group_id = task_info["group_id"] group_name = task_info["group_name"] group_desc = task_info["group_desc"] parent_id = task_info["parent_id"] userdata = task_info["userdata"] logger.info((group_id, group_name, group_desc, parent_id, userdata)) except BaseException as e: # json格式错误 logger.error(e) return get_res(-1, userdata) # 对group_id进行查询 try: groupinfo = CameraGroup.query.filter( and_(CameraGroup.id == group_id, CameraGroup.is_delete == False)).first() if groupinfo is None: logger.error("摄像头分组id不存在") return get_res(-1, userdata) if parent_id: check_parent_id = CameraGroup.query.filter( and_(CameraGroup.id == parent_id, CameraGroup.is_delete == False)).first() if check_parent_id is None: logger.error("摄像机分组id不存在:{}".format(parent_id)) return get_res(-1, userdata) groupinfo.group_name = group_name groupinfo.group_desc = group_desc groupinfo.parent_id = parent_id groupinfo.update() except BaseException as e: # 接口调用失败 logger.error(e) groupinfo.rollback() return get_res(-1, userdata) logger.info("编辑摄像机分组成功") return get_res(0, userdata)
def delete_camera_groups(): logger.info("收到删除摄像机分组命令") try: task_info = get_json(request) logger.info("收到的json是:{}".format(str(task_info))) group_id = task_info["group_id"] userdata = task_info["userdata"] logger.info((group_id, userdata)) except BaseException as e: # json格式错误 logger.error(e) return get_res(-1, userdata) # 对group_id进行查询 try: groupinfo = CameraGroup.query.filter( and_(CameraGroup.id == group_id, CameraGroup.is_delete == False)).first() if groupinfo is None: logger.error("摄像头分组id不存在") return get_res(-1, userdata) group_list = CameraGroup.query.filter( and_(CameraGroup.parent_id == group_id, CameraGroup.is_delete == False)).all() if len(group_list) is not 0: logger.error("该摄像机分组下摄像机不为空") for group in group_list: group.parent_id = None db.session.add(group) groupinfo.is_delete = True db.session.add(groupinfo) db.session.commit() except BaseException as e: # 接口调用失败 logger.error(e) db.session.rollback() return get_res(-1, userdata) logger.info("删除摄像机分组成功") return get_res(0, userdata)
def dimsearch_camera_list(): logger.info("摄像头名称模糊查询命令") data_list = list() ret = dict() try: task_info = get_json(request) logger.info("收到的json是:{}".format(str(task_info))) pagesize = task_info["pagesize"] pagenum = task_info["pagenum"] keyword = task_info["keyword"] userdata = task_info["userdata"] logger.info((pagesize, pagenum, keyword, userdata)) except BaseException as e: # json格式错误 logger.error(e) return get_res(-1, userdata) try: if keyword: # 对camera表进行查询 pagination = Camera.query.filter( and_(Camera.camera_name.like("%" + keyword + "%"), Camera.cameragroup_id == None,Camera.is_delete == False)).paginate( pagenum, per_page=pagesize, error_out=False) else: pagination = Camera.query.filter( and_(Camera.cameragroup_id == None, Camera.is_delete == False)).paginate( pagenum, per_page=pagesize, error_out=False) camerainfo_list = pagination.items for camerainfo in camerainfo_list: camera_dict = dict() camera_dict["camera_id"] = camerainfo.id camera_dict["camera_name"] = camerainfo.camera_name data_list.append(camera_dict) except BaseException as e: # 接口调用失败 logger.error(e) return get_res(-1, userdata) ret["code"] = 0 ret["data"] = data_list ret["all_page"] = pagination.pages ret["page"] = pagination.page ret["all_data"] = pagination.total ret["userdata"] = userdata logger.debug("返回的json是:{}".format(ret)) return jsonify(ret)
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