package handler import ( "city_chips/internal/model" "city_chips/internal/service" "city_chips/pkg/helper/resp" "city_chips/pkg/helper/uuid" "encoding/json" "fmt" "math/rand" "net/http" "strconv" "time" "github.com/gin-gonic/gin" "github.com/spf13/viper" "go.uber.org/zap" ) type HikvisionHandler struct { *Handler hikvisionService service.HikvisionService conf *viper.Viper } func NewHikvisionHandler(handler *Handler, hikvisionService service.HikvisionService, conf *viper.Viper) *HikvisionHandler { return &HikvisionHandler{ Handler: handler, hikvisionService: hikvisionService, conf: conf, } } // GetMonitor 获取视频监控 func (h *HikvisionHandler) GetMonitor(ctx *gin.Context) { m := make(map[string]any) LicensePlateRecognition := make(map[string]any) Blacklist := make(map[string]any) var monitor []model.Monitor var monitornms []model.MonitorNms for i := 0; i < 7; i++ { name := fmt.Sprintf("周%v", i+1) LicensePlateRecognition[name] = rand.Intn(100) Blacklist[name] = rand.Intn(100) } for i := 0; i < 20; i++ { m2 := model.Monitor{ Id: i + 1, State: rand.Intn(2), Location: model.GetRandomItem(model.MJlocations), Name: model.GetRandomItem(model.MonitorDeviceNames), } nms := model.MonitorNms{ Id: i + 1, Name: model.GetRandomItem(model.MonitorDeviceNames), InspectResult: rand.Intn(6), } monitor = append(monitor, m2) monitornms = append(monitornms, nms) } //err, resource := h.hikvisionService.DeviceResource("camera") //if err != nil { // resp.HandleError(ctx, 1201, "查询监控点数量失败", nil) // return //} //m["MonitorCount"] = resource.Data.Total //监控点总数 m["MonitorCount"] = rand.Intn(1000) //监控点总数 m["DeviceOnline"] = rand.Intn(1000) //设备在线 m["DeviceOffline"] = rand.Intn(1000) //设备离线 m["StorageCapacity"] = rand.Intn(1000) //存储容量 m["MonitorList"] = monitor //监控列表 m["MonitorNms"] = monitornms //监控检测 m["ImageIsNormal"] = rand.Intn(1000) //图像正常 m["ImageAbnormalities"] = rand.Intn(1000) //图像异常 m["DiagnosisFailed"] = rand.Intn(1000) //诊断失败 m["NotDetected"] = rand.Intn(1000) //未检测 resp.HandleSuccess(ctx, m) } // GetInvade 获取入侵检测 func (h *HikvisionHandler) GetInvade(ctx *gin.Context) { m := make(map[string]any) AlarmTrend24Hour := make(map[string]any) var realTime []model.Device for i := 0; i < 24; i++ { name := fmt.Sprintf("%v时", i+1) AlarmTrend24Hour[name] = rand.Intn(100) } for i := 0; i < 10; i++ { inspection := model.Device{ Id: i + 1, Name: model.GetRandomItem(model.IntrusionDeviceNames), State: rand.Intn(2), Date: time.Now().Format("2006-01-02 15:04:05"), } realTime = append(realTime, inspection) } m["DeviceAlerts"] = rand.Intn(100) //设备预警 m["LowRiskWarning"] = rand.Intn(100) //低危预警 m["MediumRiskWarning"] = rand.Intn(100) //中危预警 m["HighRiskWarning"] = rand.Intn(1000) //高危预警 m["HighRiskWarning"] = rand.Intn(1000) //高危预警 m["OverHazardWarning"] = rand.Intn(1000) //超危预警 m["EventRisk"] = rand.Intn(100) //事件风险 m["AlarmRisk"] = rand.Intn(100) //告警风险 m["trend"] = rand.Intn(100) //圆环中间百分比 m["realTime"] = realTime //实时巡检 m["AlarmTrend24Hour"] = AlarmTrend24Hour //24小时告警趋势 resp.HandleSuccess(ctx, m) } // GetElectronicInspections 电子巡查 func (h *HikvisionHandler) GetElectronicInspections(ctx *gin.Context) { m := make(map[string]any) Inspect := make(map[string]any) var device []model.Device var realTime []model.RealTimeInspection for i := 0; i < 24; i++ { name := fmt.Sprintf("%v时", i+1) Inspect[name] = rand.Intn(100) } for i := 0; i < 20; i++ { m2 := model.Device{ Id: i + 1, Name: model.GetRandomItem(model.RealInspectionDeviceNames), State: rand.Intn(2), Date: time.Now().Format("2006-01-02 15:04:05"), } device = append(device, m2) } for i := 0; i < 10; i++ { name := fmt.Sprintf("巡检事件%v", i+1) inspection := model.RealTimeInspection{ Id: i + 1, Name: model.GetRandomItem(model.InspectionEvents), Location: model.GetRandomItem(model.InspectionLocations), Event: name, Date: time.Now().Format("2006-01-02 15:04:05"), } realTime = append(realTime, inspection) } m["TodayTotal"] = rand.Intn(100) //今日总数 m["InspectionPoints"] = rand.Intn(100) //巡检点数 m["InspectionPlan"] = rand.Intn(100) //巡检计划 m["InspectTheLine"] = rand.Intn(1000) //巡检线路 m["Inspect"] = Inspect //巡检统计 m["realTime"] = realTime //实时巡检 m["DeviceList"] = device //设备列表 resp.HandleSuccess(ctx, m) } // GetVisitor 访客系统 func (h *HikvisionHandler) GetVisitor(ctx *gin.Context) { m := make(map[string]any) PrevailingTrends := make(map[string]any) var visitor []model.Visitor for i := 0; i < 24; i++ { address := fmt.Sprintf("%v时", i+1) m2 := model.Visitor{ Id: i + 1, Name: model.GetRandomItem(model.VisitorNames), State: rand.Intn(2), Phone: model.GeneratePhoneNumber(), Location: address, Date: time.Now().Format("2006-01-02 15:04:05"), } PrevailingTrends[address] = rand.Intn(100) visitor = append(visitor, m2) } m["VisitorCount"] = rand.Intn(1000) //访客总量 m["SignInCount"] = rand.Intn(1000) //签到数量 m["TransitCount"] = rand.Intn(1000) //通行数量 m["Exit"] = rand.Intn(1000) //离场数量 m["Client"] = rand.Intn(100) //客户 m["Vendor"] = rand.Intn(100) //供应商 m["Interviewees"] = rand.Intn(100) //面试者 m["GovernmentPersonnel"] = rand.Intn(100) //政府人员 m["Other"] = rand.Intn(100) //其他 m["PrevailingTrends"] = PrevailingTrends //通行趋势 m["VisitorRegistration"] = visitor //访客登记 m["VisitorsSwipeCards"] = visitor //访客刷卡记录 resp.HandleSuccess(ctx, m) } // GetPassenger 客流统计 func (h *HikvisionHandler) GetPassenger(ctx *gin.Context) { m := make(map[string]any) Type1 := make(map[string]any) Type2 := make(map[string]any) Type3 := make(map[string]any) Type4 := make(map[string]any) rankings := make(map[string]any) customers := make(map[string]any) var event []model.RealTimeInspection for i := 0; i < 7; i++ { sprintf := fmt.Sprintf("2025-5-%v", i+1) rankings[model.GetRandomItem(model.CustomerGroups)] = rand.Intn(100) Type1[sprintf] = rand.Intn(100) Type2[sprintf] = rand.Intn(100) Type3[sprintf] = rand.Intn(100) Type4[sprintf] = rand.Intn(100) } for i := 0; i < 3; i++ { customers[model.GetRandomItem(model.CustomerGroups)] = rand.Intn(100) } for i := 0; i < 10; i++ { inspection := model.RealTimeInspection{ Id: i + 1, Name: model.GetRandomItem(model.SecurityEvents), Location: model.GetRandomItem(model.LocationsADD), Event: time.Now().Format("2006-01-02 15:04:05"), } event = append(event, inspection) } m["SecurityLevel"] = rand.Intn(500) //安全等级 m["NetworkEquipment"] = rand.Intn(100) //网络设备 m["Normal"] = rand.Intn(100) //正常 m["Fault"] = rand.Intn(100) //故障 m["Offline"] = rand.Intn(100) //离线 m["Type1"] = Type1 //客流监控type1 m["Type2"] = Type2 //客流监控type2 m["Type3"] = Type3 //客流监控type3 m["Type4"] = Type4 //客流监控type4 m["Rankings"] = rankings //指标区排行榜 m["Customers"] = customers //客群分析统计 m["Customers"] = customers //客群分析统计 m["Event"] = event //安全事件列表 resp.HandleSuccess(ctx, m) } // 门禁系统 func (h *HikvisionHandler) GetAccess(ctx *gin.Context) { m := make(map[string]any) invasio1 := make(map[string]any) invasio2 := make(map[string]any) DailyTotal := make(map[string]any) Cumulative := make(map[string]any) var devices []model.RealTimeInspection var alarmList []model.AlarmList for i := 0; i < 7; i++ { sprintf := fmt.Sprintf("2025-5-%v", i+1) invasio1[sprintf] = rand.Intn(100) invasio2[sprintf] = rand.Intn(100) } for i := 0; i < 24; i++ { sprintf := fmt.Sprintf("%v时", i+1) DailyTotal[sprintf] = rand.Intn(100) Cumulative[sprintf] = rand.Intn(100) } for i := 0; i < 10; i++ { inspection := model.RealTimeInspection{ Id: i + 1, Name: model.GetRandomItem(model.MJDeviceNames), Location: model.GetRandomItem(model.MJlocations), Event: time.Now().Format("2006-01-02 15:04:05"), } alarm := model.AlarmList{ Id: i + 1, AlarmContent: model.GetRandomItem(model.MJDeviceNames), Location: model.GetRandomItem(model.MJlocations), State: rand.Intn(2), Date: time.Now().Format("2006-01-02 15:04:05"), } devices = append(devices, inspection) alarmList = append(alarmList, alarm) } m["DeviceCount"] = rand.Intn(500) //设备总数 m["Online"] = rand.Intn(100) //在线 m["Abnormal"] = rand.Intn(100) //异常 m["Fault"] = rand.Intn(100) //故障 m["Offline"] = rand.Intn(100) //离线 m["Attendance"] = rand.Intn(100) //出勤率 m["Invasio1"] = invasio1 //入侵事件1 m["Invasio2"] = invasio2 //入侵事件2 m["DailyTotal"] = DailyTotal //每日统计 m["Cumulative"] = Cumulative //累计统计 m["AlarmList"] = alarmList //实时告警与通知 m["devices"] = devices //设备列表 resp.HandleSuccess(ctx, m) } // GetHikvisionMonitoring 获取视频监控流{ // "cameraIndexCode": "748d84750e3a4a5bbad3cd4af9ed5101", // "streamType": 0, // "protocol": "rtsp", // "transmode": 1, // "expand": "transcode=0", // "streamform": "ps" // } func (h *HikvisionHandler) GetHikvisionMonitoring(ctx *gin.Context) { m := make(map[string]string) cameraIndexCode := ctx.Query("cameraIndexCode") m["cameraIndexCode"] = cameraIndexCode m["protocol"] = "ws" if len(cameraIndexCode) <= 0 || cameraIndexCode == "" { resp.HandleError(ctx, 1203, "设备编码不能为空", nil) return } ctx.HTML(http.StatusOK, "h5player.html", gin.H{ "title": "测试", "wsurl": "ws://127.0.0.1/" + cameraIndexCode, "cameraIndexCode": cameraIndexCode, }) //fmt.Println("cameraIndexCode", h.conf.GetString("hikvision.api.previewURLs")) //hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.previewURLs"), m, 15) ////返回结果{ //// "code": "0", //// "msg": "success", //// "data": { //// "url": "rtsp://10.2.145.66:655/EUrl/CLJ52BW" //// } ////} //if err != nil { // h.logger.Error("获取获取监控点资源失败") // resp.HandleError(ctx, 1201, "获取获取监控点资源失败", err) // return //} //if hikvision.Code != "0" { // atoi, _ := strconv.Atoi(hikvision.Code) // resp.HandleError(ctx, atoi, hikvision.Msg, nil) // return //} //marshalString, err := json.Marshal(hikvision) //if err != nil { // resp.HandleError(ctx, 1202, "json序列化失败", nil) // return //} //url := gjson.Get(string(marshalString), "data.url") // //resp.HandleSuccess(ctx, url) } // 视频监控云台控制{ // "cameraIndexCode": "748d84750e3a4a5bbad3cd4af9ed5101", // "action": 1, 0-开始 //1-停止 //注:GOTO_PRESET命令下填任意值均可转到预置点,建议填0即可 // "command": "GOTO_PRESET", // "speed": 4, // "presetIndex": 20 //} func (h *HikvisionHandler) Gimbalcontrol(ctx *gin.Context) { m := make(map[string]string) cameraIndexCode := ctx.Query("cameraIndexCode") command := ctx.Query("command") action := ctx.Query("action") speed := ctx.Query("speed") presetIndex := ctx.Query("presetIndex") m["cameraIndexCode"] = cameraIndexCode m["action"] = action m["command"] = command m["speed"] = speed m["presetIndex"] = presetIndex if len(cameraIndexCode) <= 0 || len(command) <= 0 || len(action) <= 0 { resp.HandleError(ctx, 1203, "设备编码不能为空", nil) return } // resp.HandleSuccess(ctx, "操作成功") // return hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.controlling"), m, 15) if err != nil { h.logger.Error("控制云台失败") resp.HandleError(ctx, 1201, "控制云台失败", err) return } if hikvision.Code != "0" { atoi, _ := strconv.Atoi(hikvision.Code) resp.HandleError(ctx, atoi, hikvision.Msg, nil) return } resp.HandleSuccess(ctx, hikvision.Msg) } // VisitorInfoCount 获取今日访客信息包含:今日来访总人数(已签离人数,未签离人数),预约人数 func (h *HikvisionHandler) VisitorInfoCount(c *gin.Context) { m := make(map[string]string) parkId := c.Query("parkId") m["parkId"] = parkId hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.visitorInfo"), m, 15) if err != nil { h.logger.Error("获取访客信息失败") resp.HandleError(c, 1201, "获取访客信息失败", err) return } if hikvision.Code != "0" { atoi, _ := strconv.Atoi(hikvision.Code) resp.HandleError(c, atoi, hikvision.Msg, nil) return } resp.HandleSuccess(c, hikvision.Data) } // GetDoorSearch 查询门禁点列表v2 func (h *HikvisionHandler) GetDoorSearch(ctx *gin.Context) { // 设置响应头 ctx.Header("Content-Type", "text/event-stream") ctx.Header("Cache-Control", "no-cache") ctx.Header("Connection", "keep-alive") // 监听客户端断开连接 conn := true notify := ctx.Writer.CloseNotify() var response model.Response //var doorlist []model.DoorList //var doorResp model.DoorResp m := make(map[string]string) m["pageNo"] = "1" m["pageSize"] = "1000" for conn { select { case <-notify: conn = false fmt.Println("断开连接") return default: doorSearch, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.doorSearch"), m, 15) if err != nil { h.logger.Error("获取门禁点列表失败") response.Code = 1203 response.Msg = "获取门禁点列表失败" response.Data = nil res, _ := json.Marshal(&response) fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) ctx.Writer.Flush() return } if doorSearch.Code != "0" { response.Code = 1203 response.Msg = "获取门禁点列表失败" response.Data = nil res, _ := json.Marshal(&response) fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) ctx.Writer.Flush() conn = false return } // 获取门禁状态 //doorIndexCodes := []string{""} //for _, v := range doorResp.Data.List { // doorIndexCodes = append(doorIndexCodes, v.IndexCode) //} //data := map[string]string{ // "doorIndexCodes": strings.Join(doorIndexCodes, ","), //} //doorStates, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.doorStates"), data, 15) //if err != nil { // h.logger.Error("获取门禁状态失败") // response.Code = 1203 // response.Msg = "获取门禁状态失败" // response.Data = nil // res, _ := json.Marshal(&response) // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) // ctx.Writer.Flush() // conn = false // return //} //if doorStates.Code != "0" { // response.Code = 1203 // response.Msg = "获取门禁状态失败" // response.Data = nil // res, _ := json.Marshal(&response) // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) // ctx.Writer.Flush() // conn = false // return //} // //for i := 0; i < 3; i++ { // genUUID := uuid.GenUUID() // name := fmt.Sprintf("资源:%v", i+1) // // list := model.DoorList{ // IndexCode: genUUID, // ResourceType: "door", // Name: name, // DoorNo: genUUID, // ChannelNo: genUUID, // ParentIndexCode: genUUID, // ControlOneId: genUUID, // ControlTwoId: genUUID, // ReaderInId: genUUID, // ReaderOutId: genUUID, // DoorSerial: i + 1, // TreatyType: genUUID, // RegionIndexCode: genUUID, // RegionPath: genUUID, // CreateTime: time.Now().Format("2006-01-02 15:04:05"), // UpdateTime: time.Now().Format("2006-01-02 15:04:05"), // Description: genUUID, // ChannelType: genUUID, // RegionName: genUUID, // RegionPathName: genUUID, // InstallLocation: genUUID, // } // doorlist = append(doorlist, list) //} //doorResp := model.DoorResp{ // Code: "0", // Msg: "SUCCESS", // Data: struct { // Total int `json:"total"` // PageNo int `json:"pageNo"` // PageSize int `json:"pageSize"` // List []model.DoorList `json:"list"` // }{Total: 3, PageNo: 1, PageSize: 1, List: doorlist}, //} response.Code = 200 response.Msg = "获取门禁点列表成功" response.Data = doorSearch.Data res, _ := json.Marshal(&response) fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) ctx.Writer.Flush() time.Sleep(10 * time.Second) } } } // DoControl 控制门禁 func (h *HikvisionHandler) DoControl(ctx *gin.Context) { // 设置响应头 ctx.Header("Content-Type", "text/event-stream") ctx.Header("Cache-Control", "no-cache") ctx.Header("Connection", "keep-alive") // 监听客户端断开连接 conn := true notify := ctx.Writer.CloseNotify() doorIndexCodes := ctx.Query("doorIndexCodes") controlType := ctx.Query("controlType") if len(doorIndexCodes) <= 0 || len(controlType) <= 0 { resp.HandleError(ctx, 1203, "设备编码不能为空", nil) return } fmt.Println("doorIndexCodes:", doorIndexCodes) m := make(map[string]string) m["doorIndexCodes"] = doorIndexCodes m["controlType"] = controlType var response model.Response for conn { select { case <-notify: conn = false fmt.Println("断开连接") return default: //hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.control"), m, 15) //if err != nil { // h.logger.Error("控制门禁失败") // response.Code = 1203 // response.Msg = "控制门禁失败" // response.Data = nil // res, _ := json.Marshal(&response) // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) // ctx.Writer.Flush() // conn = false // return //} //if hikvision.Code != "0" { // response.Code = 1203 // response.Msg = "控制门禁失败" // response.Data = nil // res, _ := json.Marshal(&response) // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) // ctx.Writer.Flush() // conn = false // return //} genUUID := uuid.GenUUID() doControl := model.DoControl{ Code: "0", Msg: "SUCCESS", Data: []model.DoControlData{{ DoorIndexCode: genUUID, ControlResultCode: 0, ControlResultDesc: "success", }}, } response.Code = 200 response.Msg = "控制门禁成功" response.Data = doControl.Data res, _ := json.Marshal(&response) fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res)) ctx.Writer.Flush() time.Sleep(10 * time.Second) } } } // RealTimeInspection 入侵报警事件日志查询 func (h *HikvisionHandler) RealTimeInspection(ctx *gin.Context) { m := make(map[string]string) m["startTime"] = ctx.Query("startTime") //开始时间 m["endTime"] = ctx.Query("endTime") //结束时间 m["pageNo"] = ctx.Query("pageNo") m["pageSize"] = h.conf.GetString("hikvision.pageSize") var eventLogs model.EventLogs hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.eventLogs"), m, 15) if err != nil { h.logger.Error("入侵报警事件日志查询失败", zap.Error(err)) resp.HandleError(ctx, 1203, "入侵报警事件日志查询失败", err) return } if hikvision.Code != "0" { h.logger.Error("入侵报警事件日志查询失败") resp.HandleError(ctx, 1203, "入侵报警事件日志查询失败", hikvision.Code) return } marshal, err := json.Marshal(hikvision) if err != nil { h.logger.Error("json序列化失败") resp.HandleError(ctx, 1203, "json序列化失败", err) return } err = json.Unmarshal(marshal, &eventLogs) if err != nil { h.logger.Error("json反序列化失败") resp.HandleError(ctx, 1203, "json反序列化失败", err) return } for i, _ := range eventLogs.Data.List { eventLogs.Data.List[i].SrcType = model.ResourceType[eventLogs.Data.List[i].SrcType] } resp.HandleSuccess(ctx, hikvision.Data) }