package controllers import ( "strconv" "strings" commonModel "vamicro/camera-common/models" "vamicro/camera-common/vo" "vamicro/chanmanage-service/cache" "vamicro/chanmanage-service/models" "vamicro/chanmanage-service/service" "vamicro/config" "basic.com/pubsub/protomsg.git" "basic.com/valib/bhomeclient.git" "basic.com/valib/bhomedbapi.git" "basic.com/valib/logger.git" ) type PollSetController struct { } // @Summary 保存轮询周期 // @Description 保存轮询周期 // @Produce json // @Tags 轮询配置 // @Param period formData int true "轮询周期" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/pollConfig/savePollPeriod [post] func (psc *PollSetController) SavePollPeriod(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { periodStr := c.PostForm("period") period, err := strconv.Atoi(periodStr) if periodStr == "" || err != nil { return &bhomeclient.Reply{Msg: "参数有误"} } var pcE models.PollConfig pcE.ServerId = config.Server.AnalyServerId pcE.PollPeriod = int32(period) sv := service.NewPollSetService(h.Bk) p, err := sv.GetPollConfig() if err == nil { pcE.Enable = p.Enable pcE.Delay = p.Delay if sv.UpdatePollConfig(pcE) { return &bhomeclient.Reply{Success: true, Data: pcE} } else { return &bhomeclient.Reply{Msg: "保存失败"} } } else { if sv.SavePollConfig(pcE) { return &bhomeclient.Reply{Success: true, Data: pcE} } else { return &bhomeclient.Reply{Msg: "保存失败"} } } } // @Summary 统计实时和轮询的运行路数情况 // @Description 统计实时和轮询的运行路数情况 // @Produce json // @Tags camera // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/camera/statisticRunInfo [get] func (psc *PollSetController) StatisticRunInfo(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { sv := service.NewPollSetService(h.Bk) d := sv.StatisticRunInfo() return &bhomeclient.Reply{Success: true, Data: d} } // @Summary 保存轮询延时 // @Description 保存轮询延时 // @Produce json // @Tags 轮询配置 // @Param delay formData int true "轮询延时时间" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/pollConfig/savePollDelay [post] func (psc *PollSetController) SavePollDelay(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { delayStr := c.PostForm("delay") delay, err := strconv.Atoi(delayStr) if delayStr == "" || err != nil { return &bhomeclient.Reply{Msg: "参数有误"} } var pcE models.PollConfig pcE.ServerId = config.Server.AnalyServerId pcE.Delay = int32(delay) sv := service.NewPollSetService(h.Bk) p, err := sv.GetPollConfig() if err == nil { pcE.Enable = p.Enable pcE.PollPeriod = p.PollPeriod if sv.UpdatePollConfig(pcE) { return &bhomeclient.Reply{Success: true, Data: pcE} } else { return &bhomeclient.Reply{Msg: "保存失败"} } } else { if sv.SavePollConfig(pcE) { return &bhomeclient.Reply{Success: true, Data: pcE} } else { return &bhomeclient.Reply{Msg: "保存失败"} } } } // @Summary 获取本机轮询配置 // @Description 获取本机轮询配置 // @Produce json // @Tags 轮询配置 // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/pollConfig/getPollConfig [get] func (psc *PollSetController) GetPollConfig(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { sv := service.NewPollSetService(h.Bk) pcE, err := sv.GetPollConfig() if err != nil { return &bhomeclient.Reply{Msg: "查询失败"} } else { return &bhomeclient.Reply{Success: true, Data: pcE} } } type PollEnableVo struct { Enable bool `json:"enable"` } // @Summary 切换轮询开关 // @Description 切换轮询开关 // @Accept json // @Produce json // @Tags 轮询配置 // @Param argBody body controllers.PollEnableVo true "开关参数" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/pollConfig/updateEnable [post] func (psc *PollSetController) UpdateEnable(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var argBody PollEnableVo if err := c.BindJSON(&argBody); err != nil { return &bhomeclient.Reply{Msg: "参数有误"} } sv := service.NewPollSetService(h.Bk) if sv.UpdateEnable(argBody.Enable) { return &bhomeclient.Reply{Success: true, Msg: "修改成功"} } else { return &bhomeclient.Reply{Msg: "修改失败"} } } type ChannelCountSet struct { PollChannelCount int `json:"pollChannelCount"` VideoChannelCount int `json:"videoChannelCount"` } // @Summary 设置轮询算力和本地算力的数量 // @Description 设置轮询算力和本地算力的数量 // @Accept json // @Produce json // @Tags 轮询配置 // @Param argBody body controllers.ChannelCountSet true "轮询通道数量以及本地通道数量" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/pollConfig/updateChannelCount [post] func (psc *PollSetController) UpdateChannelCount(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var argBody ChannelCountSet c.BindJSON(&argBody) sv := service.NewPollSetService(h.Bk) if ok := sv.UpdateChannelCount(argBody.PollChannelCount, argBody.VideoChannelCount); ok { sv.ResetChannelCount() return &bhomeclient.Reply{Success: true, Msg: "修改成功"} } else { logger.Warn("Update Channel Count failure") return &bhomeclient.Reply{Msg: "修改失败"} } } // @Summary 根据server获取所有摄像机列表及信息 // @Description 根据server获取所有摄像机列表及信息 // @Produce json // @Tags camera // @Param cameraName query string false "摄像机名称" // @Param runType query string false "0:全部,1:分析,2:监控,3:联动" // @Param cameraId query string false "摄像机id" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/camera/getAllCamerasByServer [get] func (psc *PollSetController) GetAllCamerasByServer(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { //serverId := c.Query("serverId") cameraName := c.Query("cameraName") runType := c.Query("runType") cameraId := c.Query("cameraId") areaId := c.Query("areaId") list := make([]protomsg.Camera, 0) camAllMap := make(map[string]protomsg.Camera, 0) var camApi bhomedbapi.CameraApi localCams := camApi.FindAll(cameraName, runType, cameraId, areaId) //获取所有本地摄像机 if localCams != nil && len(localCams) > 0 { list = append(list, localCams...) for _, c := range localCams { camAllMap[c.Id] = c } } //logger.Debug("GetAllCamerasByServer 取完本地摄像机耗时:", time.Since(timeStart)) var gbApi bhomedbapi.Gb28181Api gbCams := gbApi.FindAll(cameraName, runType, cameraId, areaId) if gbCams != nil { list = append(list, gbCams...) for _, c := range gbCams { camAllMap[c.Id] = c } } //logger.Debug("GetAllCamerasByServer 取完国标摄像机耗时:", time.Since(timeStart)) serverName := "" localConf, e1 := cache.GetServerInfo() //logger.Debug("GetAllCamerasByServer 取完ServerInfo耗时:", time.Since(timeStart)) //logger.Debug("e1:", e1, "localConf:", localConf) if e1 == nil { serverName = localConf.ServerName } nodesMap := make(map[string]protomsg.Node) var clusterApi bhomedbapi.ClusterApi flag, cInfo := clusterApi.FindCluster() //logger.Debug("flag:", flag, "cInfo:", cInfo) //logger.Debug("GetAllCamerasByServer 取完Cluster耗时:", time.Since(timeStart)) if flag && cInfo.Nodes != nil { for _, node := range cInfo.Nodes { nodesMap[node.Id] = *node } } var crApi bhomedbapi.CameraRuleApi criList := make([]vo.CameraRunInfo, 0) if list != nil && len(list) > 0 { allRules, e2 := cache.GetCameraRules() //logger.Debug("GetAllCamerasByServer 取完所有摄像机场景耗时:", time.Since(timeStart)) ruleM := make(map[string]protomsg.CameraAndRules) if e2 == nil && allRules != nil { for _, r := range allRules { if r.CameraInfo != nil { ruleM[r.CameraInfo.Id] = r } } } _, allLinks := crApi.FindAllCameraLink() //获取督查任务 for _, cE := range list { var cri vo.CameraRunInfo cri.CopyFromProtoCamera(cE) if cri.RunServerId != "" { if cri.RunServerId == config.Server.AnalyServerId { cri.RunServerName = serverName } else { if ne, ok := nodesMap[cri.RunServerId]; ok { cri.RunServerName = ne.NodeName } } } // if cgs, ok := ruleM[cE.Id]; ok { // for _, g := range cgs.Rules { // var crTask vo.CameraRunTask // crTask.HasRule = g.Enable // crTask.TaskName = g.SceneName // cri.Tasks = append(cri.Tasks, crTask) // } // } else { // cri.Tasks = []vo.CameraRunTask{} // } var ids []string ids = append(ids, cE.Id) cri.AllTasks, _ = models.GetListTask(ids) //处理摄像机的状态 if cE.RunType == commonModel.TYPE_RUNTYPE_POLL || cE.RunType == commonModel.TYPE_RUNTYPE_REALTIME { if crInfo, ok := ruleM[cE.Id]; ok && crInfo.Rules != nil && len(crInfo.Rules) > 0 { if cE.IsRunning { cri.Status = commonModel.Camera_Status_Doing } else { cri.Status = commonModel.Camera_Status_Wait } } else { cri.Status = commonModel.Camera_Status_NoRule } } else { cri.Status = commonModel.Camera_Status_Err } //处理联动信息 if allLinks != nil && len(allLinks) > 0 { for _, l := range allLinks { if strings.Contains(l.CameraIds, cE.Id) { linkIds := strings.Split(l.CameraIds, ",") if linkIds != nil && len(linkIds) > 1 { linkG := make([]vo.LinkCam, 0) for _, lId := range linkIds { if lv, lOk := camAllMap[lId]; lOk { linkG = append(linkG, vo.LinkCam{ Id: lId, Name: lv.Name, }) } } cri.LinkCams = append(cri.LinkCams, linkG) } } } } criList = append(criList, cri) } } //logger.Debug("GetAllCamerasByServer 处理完状态及联动耗时:", time.Since(timeStart)) return &bhomeclient.Reply{Success: true, Data: criList} }