package controllers import ( "basic.com/pubsub/protomsg.git" "encoding/json" "fmt" "strconv" "basic.com/valib/logger.git" "strings" "webserver/cache" "github.com/gin-gonic/gin" "basic.com/dbapi.git" "webserver/extend/code" "webserver/extend/util" "webserver/service" ) type CameraController struct{} type CameraVo struct { Id string `json:"id"` Name string `json:"name"` Alias string `json:"alias"` //摄像机的别名 Type int `json:"type" ` Addr string `json:"addr"` Areaid string `json:"areaid"` Longitude float32 `json:"longitude"` Latitude float32 `json:"latitude"` Rtsp string `json:"rtsp"` Ip string `json:"ip"` Port int `json:"port"` Username string `json:"username"` Password string `json:"password"` Brand string `json:"brand"` Reserved string `json:"reserved"` IsRunning bool `json:"is_running"` //是否正在解码 RunEnable bool `json:"run_enable"` //控制实时处理或轮询处理的开关 RunType int `json:"run_type"` //处理类型:0:轮询,1:实时 RunServerId string `json:"run_server_id"` //当前正在处理的分析服务器id ResolutionWidth int `json:"resolution_width"`//分辨率宽 ResolutionHeight int `json:"resolution_height"`//分辨率高 Sensors []SensorVo `json:"sensors"` //传感器列表 } type Resolution struct { Width int `json:"width"` Height int `json:"height"` } type SensorVo struct { Id string `json:"id"` Type string `json:"type"` Ip string `json:"ip"` Port int `json:"port"` Username string `json:"username"` Password string `json:"password"` Threshold int `json:"threshold"` Enable bool `json:"enable"` } // @Summary 添加摄像机 // @Description "传入区域的id(areaid)和摄像机名字(name) 必须, 其他参数可以在摄像机配置点击保存进行添加" // @Security ApiKeyAuth // @Accept json // @Produce json // @Tags camera // @Param cameraStruct body controllers.CameraVo false "struct of camera;the areaid of area's id and name of camera;must need" // @Success 200 {string} json "{"code":200, success:true, msg:"请求处理成功", data:"添加后的摄像机信息"}" // @Failure 500 {string} json "{"code":500, success:false msg:"null ",data:"错误信息内容"}" // @Router /data/api-v/camera/add [post] func (cc CameraController) CameraAdd(c *gin.Context) { var cam CameraVo var api dbapi.CameraApi err := c.BindJSON(&cam) if err != nil { util.ResponseFormat(c, code.RequestParamError, "参数错误") return } paramBody := util.Struct2Map(cam) b, data := api.CameraAdd(paramBody) if b { util.ResponseFormat(c, code.Success, data) return } util.ResponseFormat(c, code.ComError, err) } // @Summary 更新摄像机 // @Description "用于更新摄像机操作" // @Security ApiKeyAuth // @Accept json // @Produce json // @Tags camera // @Param cam body controllers.CameraVo true "id:摄像机id default:cid0 (string), areaid:摄像机关联区域id default:1 (int), name:摄像机名字 default:Testname(string) 必填; 其他参数可配 " // @Success 200 {string} json "{"code":200, success:true, msg:"请求处理成功", data:"修改后的摄像机信息"}" // @Failure 500 {string} json "{"code":500, success:false msg:"null",data:"错误信息内容"}" // @Router /data/api-v/camera/update [put] func (cc CameraController) CameraUpdate(c *gin.Context) { var cam CameraVo var api dbapi.CameraApi err := c.BindJSON(&cam) if err != nil { logger.Debug("err:",err) util.ResponseFormat(c, code.RequestParamError, "参数错误") return } logger.Debug("camera sensors:", cam.Sensors) paramBody := util.Struct2Map(cam) if api.CameraUpdate(paramBody) { util.ResponseFormat(c, code.Success, cam) return } util.ResponseFormat(c, code.ComError, err) } // @Summary "删除摄像机" // @Description "根据摄像机id删除摄像机" // @Security ApiKeyAuth // @Produce json // @Tags camera // @Param cid path string true "摄像机id example: cid0" // @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/del/{cid} [delete] func (cc CameraController) CameraDel(c *gin.Context) { var api dbapi.CameraApi cid := c.Param("cid") logger.Debug("CameraDel.cid:",cid) if api.CameraDelete(cid) { util.ResponseFormat(c, code.Success, "删除成功") return } util.ResponseFormat(c, code.ComError, "删除失败") } // @Summary 显示摄像机 // @Description "显示摄像机" // @Security ApiKeyAuth // @Produce json // @Tags camera // @Param cid path string true "摄像机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/show/{cid} [get] func (cc CameraController) CameraSel(c *gin.Context) { var api dbapi.CameraApi cid := c.Param("cid") camera, err := api.GetCameraById(cid) if err != nil { util.ResponseFormat(c, code.ComError, "查询失败") return } //2019-11-02新增需求,显示每个摄像机的处理服务器信息,归属的节点服务器名称 m := util.Struct2Map(camera) m["runServerName"] = "" m["resolution_width"] = camera.ResolutionWidth m["resolution_height"] = camera.ResolutionHeight //摄像机分辨率 dRe0 := Resolution{ Width: 0, Height: 0, } dRe1 := Resolution{ Width: 1920, Height: 1080, } dRe2 := Resolution{ Width: 2688, Height: 1520, } m["resolutions"] = []Resolution{ dRe0, dRe1, dRe2 } if camera.RunServerId !="" { localConf, e := cache.GetServerInfo() if e ==nil { if camera.RunServerId == localConf.ServerId {//归属本台服务器 m["runServerName"] = localConf.ServerName } else {//属于其他服务器 var clusterApi dbapi.ClusterApi b, data := clusterApi.FindCluster() if b && data !=nil { cb, _ := json.Marshal(data) var clu ClusterDb if err := json.Unmarshal(cb, &clu);err ==nil { if len(clu.Nodes) >0 { for _,n :=range clu.Nodes { if camera.RunServerId == n.NodeId { m["runServerName"] = n.NodeName break } } } } } } } } util.ResponseFormat(c, code.Success, m) } type camerSdkStruct struct { Sdkid string `json:"sdkid"` Sdkname string `json:"sdkname"` Sdkargs string `json:"sdkargs"` } type CamerTask struct { Taskid string `json:"taskid"` Taskname string `json:"taskname"` } type Sdks []camerSdkStruct type ctpstruct struct { CamerTask Sdks `json:",omitempty"` } type Ctp map[CamerTask][]camerSdkStruct func (cam Ctp) MarshalJSON() ([]byte, error) { var test []ctpstruct var temp ctpstruct for key, value := range cam { temp.CamerTask = key temp.Sdks = value test = append(test, temp) } return json.Marshal(test) } // @Security ApiKeyAuth // @Summary 将摄像机挂到指定的目录树下 // @Description 将摄像机挂到指定的目录树下 // @Accept x-www-form-urlencoded // @Produce json // @Tags camera // @Param cameraId formData string true "摄像机id" // @Param areaId formData string true "目录区域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/updateCameraArea [post] func (cc CameraController) UpdateCameraArea(c *gin.Context) { cameraId := c.PostForm("cameraId") areaId := c.PostForm("areaId") if cameraId == "" || areaId == "" { util.ResponseFormat(c, code.RequestParamError, "参数有误") return } var api dbapi.CameraApi if api.UpdateCameraArea(cameraId, areaId) { util.ResponseFormat(c,code.Success,"更新成功") } else { util.ResponseFormat(c,code.ComError,"") } } // @Security ApiKeyAuth // @Summary 删除某一个目录树下的指定摄像机 // @Description 删除某一个目录树下的指定摄像机 // @Produce json // @Tags camera // @Param cameraId path string true "摄像机id" // @Param areaId path string true "指定目录区域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/cameraAreaDel [get] func (cc CameraController) CameraAreaDel(c *gin.Context) { cameraId := c.Param("cameraId") areaId := c.Param("areaId") fmt.Println(cameraId) fmt.Println(areaId) } // @Security ApiKeyAuth // @Summary 根据server获取所有摄像机列表及信息 // @Description 根据server获取所有摄像机列表及信息 // @Produce json // @Tags camera // @Param cameraName query string false "摄像机名称" // @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 (cc CameraController) GetAllCamerasByServer(c *gin.Context) { localConf, err2 := cache.GetServerInfo() if err2 !=nil || localConf.ServerId == "" { logger.Debug("localConfig is wrong!!!") util.ResponseFormat(c,code.ComError,"服务器本机未配置serverId") return } var api dbapi.CameraApi cameraName := c.Query("cameraName") b,d := api.GetAllCamerasByServer(localConf.ServerId, cameraName) if b { util.ResponseFormat(c,code.Success, d) } else { util.ResponseFormat(c,code.ComError, "") } } // @Security ApiKeyAuth // @Summary 获取运行类型获取摄像机列表 // @Description 获取运行类型获取摄像机列表 // @Produce json // @Tags camera // @Param runType query int true "0:查轮询,1:查实时" // @Param cameraName query string false "查询条件" // @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/getCamerasByRunType [get] func (cc CameraController) GetCamerasByRunType(c *gin.Context) { runTypeStr := c.Query("runType") cameraName := c.Query("cameraName") fmt.Println("runType:", runTypeStr) fmt.Println("cameraName:", cameraName) runType, err := strconv.Atoi(runTypeStr) if err != nil || (runType != 0 && runType != 1) { util.ResponseFormat(c, code.RequestParamError, "参数有误") return } var api dbapi.CameraApi b, list := api.GetCamerasByRunType(runType, cameraName) if b { dataBytes, _ := json.Marshal(list) var cams []CameraVo json.Unmarshal(dataBytes, &cams) util.ResponseFormat(c, code.Success, cams) } else { util.ResponseFormat(c, code.ComError, err) } } // @Security ApiKeyAuth // @Summary 切换摄像机运行实时或轮询的开关 // @Description 切换摄像机运行实时或轮询的开关 // @Accept x-www-form-urlencoded // @Produce json // @Tags camera // @Param cameraId formData string true "摄像机id" // @Param runEnable formData bool true "开启:true,关闭:false" // @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/updateRunEnable [post] func (cc CameraController) UpdateRunEnable(c *gin.Context) { cameraId := c.PostForm("cameraId") enableStr := c.PostForm("runEnable") runEnable, err := strconv.ParseBool(enableStr) if cameraId == "" || err != nil { util.ResponseFormat(c, code.RequestParamError, "参数有误") return } var api dbapi.CameraApi b, data := api.UpdateRunEnable(cameraId, runEnable) fmt.Println("data:", data) if b { util.ResponseFormat(c, code.Success, data) } else { util.ResponseFormat(c, code.ComError, "更新失败") } } // @Security ApiKeyAuth // @Summary 更新底图 // @Description 刷新底图 // @Produce json // @Tags camera // @Param cid path string true "摄像机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/updateSnapshotUrl/{cid} [get] func(cc CameraController) UpdateSnapshotUrl(c *gin.Context){ cid := c.Param("cid") //filename, err := service.UpdateSnapshotUrl(cid) if cid == "" { util.ResponseFormat(c, code.RequestParamError, "参数有误") return } if strings.HasPrefix(cid, File_Video_Id_Pre) || strings.HasPrefix(cid, File_Img_Id_Pre) || strings.HasPrefix(cid, File_Audio_Id_Pre) { var fileApi dbapi.FileAnalysisApi b, d := fileApi.Show(cid) if b { var file protomsg.FileAnalysis bytes, _ := json.Marshal(d) err := json.Unmarshal(bytes, &file) if err == nil { util.ResponseFormat(c, code.Success, map[string]string{ "cameraId": cid, "snapshotUrl": file.SnapshotUrl, }) return } else { util.ResponseFormat(c, code.ComError, "更新失败") return } } else { util.ResponseFormat(c, code.ComError, "更新失败") return } } filename, err := service.UpdateCapture(cid) if err != nil { logger.Debug("UpdateSnapshotUrl.err:",err) util.ResponseFormat(c, code.ComError, "更新失败") return } util.ResponseFormat(c, code.Success, map[string]string{ "cameraId": cid, "snapshotUrl": filename, }) } type CameraChangeRunVo struct { CameraIds []string `json:"camera_ids"` RunType int `json:"run_type"` } // @Security ApiKeyAuth // @Summary 摄像机轮询和实时状态切换 // @Description 摄像机轮询和实时状态切换 // @Produce json // @Tags camera // @Param changeRunBody body controllers.CameraChangeRunVo true "参数结构体,0:实时切轮询,1:轮询切实时" // @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/changeRunType [post] func (cc CameraController) ChangeRunType(c *gin.Context) { var ccrVo CameraChangeRunVo if err := c.BindJSON(&ccrVo); err != nil { util.ResponseFormat(c, code.RequestParamError, "参数有误") return } paramBody := util.Struct2Map(ccrVo) var api dbapi.CameraApi b, data := api.ChangeRunType(paramBody) if b { util.ResponseFormat(c, code.Success, data) } else { util.ResponseFormat(c, code.ComError, "更新失败") } } // @Security ApiKeyAuth // @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 (cc CameraController) StatisticRunInfo(c *gin.Context) { var api dbapi.CameraApi b, d := api.StatisticRunInfo() if b { util.ResponseFormat(c,code.Success,d) } else { util.ResponseFormat(c,code.ComError,"") } } /* // @Security ApiKeyAuth // @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/resolution/{cid} [get] func (cc CameraController) Resolution(c *gin.Context) { } */