123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826 |
- package handler
- import (
- "city_chips/internal/model"
- "city_chips/internal/service"
- "city_chips/pkg/helper/resp"
- "city_chips/pkg/helper/uuid"
- "encoding/json"
- "fmt"
- "github.com/tidwall/gjson"
- "math/rand"
- "net/http"
- "strconv"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/spf13/viper"
- )
- 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)
- }
- // GetPatrolPoint 获取巡更点
- func (h *HikvisionHandler) GetPatrolPoint(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()
- type Response struct {
- RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
- Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
- Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
- Data any `json:"data"`
- }
- stationNo := ctx.Query("id")
- if len(stationNo) == 0 {
- resp.HandleError(ctx, 1201, "缺少必要参数", nil)
- return
- }
- var response Response
- for conn {
- select {
- case <-notify:
- conn = false
- fmt.Println("断开连接")
- return
- default:
- // 模拟数据
- data := make(map[string]any)
- response.Code = 200
- response.RequestId = stationNo
- response.Msg = "success"
- data["state"] = rand.Intn(2) // // 1开 0关 2故障
- data["巡更名称"] = model.GetRandomItem(model.Locations)
- data["巡更人"] = model.GetRandomItem(model.Names)
- data["巡更时间"] = time.Now().Format("2006-01-02 15:04:05")
- data["巡更位置"] = model.GetRandomItem(model.InspectionLocations)
- response.Data = data
- res, _ := json.Marshal(&response)
- fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
- ctx.Writer.Flush()
- time.Sleep(10 * time.Second)
- }
- }
- }
- // 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]any)
- cameraIndexCode := ctx.Query("cameraIndexCode")
- control := ctx.Query("control")
- m["cameraIndexCode"] = cameraIndexCode
- m["protocol"] = "ws"
- if len(cameraIndexCode) <= 0 || cameraIndexCode == "" {
- resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
- return
- }
- hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.previewURLs"), 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
- }
- marshalString, err := json.Marshal(hikvision)
- if err != nil {
- resp.HandleError(ctx, 1202, "json序列化失败", nil)
- return
- }
- var backquote bool
- if control == "" {
- backquote = true
- } else {
- backquote = false
- }
- url := gjson.Get(string(marshalString), "data.url")
- ctx.HTML(http.StatusOK, "h5player.html", gin.H{
- "title": "测试",
- "wsurl": url,
- "cameraIndexCode": cameraIndexCode,
- "control": backquote,
- })
- }
- // 视频监控云台控制{
- // "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]any)
- 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]any)
- 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) {
- var Acs model.DoorSearch
- var AcsDevice model.AcsDoorStates
- name := ctx.Query("name")
- pageNo := ctx.Query("pageNo")
- pageSize := ctx.Query("pageSize")
- doorIndexCodes := ctx.Query("doorIndexCodes")
- err, result := h.hikvisionService.GetDoorSearch(pageNo, pageSize, name, []string{doorIndexCodes})
- if err != nil {
- h.logger.Error("获取门禁设备列表失败")
- resp.HandleError(ctx, 1201, "获取门禁设备列表失败", err)
- return
- }
- marshal, err := json.Marshal(result)
- if err != nil {
- h.logger.Error("json序列化失败")
- resp.HandleError(ctx, 1202, "json序列化失败", nil)
- return
- }
- err = json.Unmarshal(marshal, &Acs)
- if err != nil {
- h.logger.Error("json反序列化失败")
- resp.HandleError(ctx, 1203, "json反序列化失败", nil)
- return
- }
- for i, _ := range Acs.Data.List {
- err, h2 := h.hikvisionService.GetAcsDoorStates([]string{Acs.Data.List[i].IndexCode})
- if err != nil {
- h.logger.Error("获取门禁设备在线状态失败")
- resp.HandleError(ctx, 1201, "获取门禁设备在线状态失败", err)
- return
- }
- bytes, err := json.Marshal(h2)
- if err != nil {
- h.logger.Error("json序列化失败")
- resp.HandleError(ctx, 1202, "json序列化失败", nil)
- return
- }
- err = json.Unmarshal(bytes, &AcsDevice)
- if err != nil {
- h.logger.Error("json反序列化失败")
- resp.HandleError(ctx, 1203, "json反序列化失败", nil)
- return
- }
- Acs.Data.List[i].State = AcsDevice.Data.AuthDoorList[0].DoorState
- }
- resp.HandleSuccess(ctx, Acs.Data)
- }
- // GetDoorStates 获取门禁设备在线状态
- func (h *HikvisionHandler) GetDoorStates(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 AcsDevice model.AcsDevice
- indexCodes := ctx.Query("indexCodes")
- pageNo := ctx.Query("pageNo")
- pageSize := ctx.Query("pageSize")
- for conn {
- select {
- case <-notify:
- conn = false
- fmt.Println("断开连接")
- return
- default:
- err, h2 := h.hikvisionService.GetDoorStates(pageNo, pageSize, []string{indexCodes})
- 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
- }
- bytes, err := json.Marshal(h2)
- if err != nil {
- h.logger.Error("json序列化失败")
- response.Code = 1203
- response.Msg = "json序列化失败"
- response.Data = nil
- res, _ := json.Marshal(&response)
- fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
- }
- err = json.Unmarshal(bytes, &AcsDevice)
- if err != nil {
- h.logger.Error("json反序列化失败")
- response.Code = 1203
- response.Msg = "json反序列化失败"
- response.Data = nil
- res, _ := json.Marshal(&response)
- fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
- conn = false
- }
- response.Code = 200
- response.Msg = "获取门禁点列表成功"
- response.Data = AcsDevice.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) {
- 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
- 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) {
- pageNo := ctx.Query("pageNo")
- pageSize := ctx.Query("pageSize")
- srcType := ctx.Query("srcType")
- startTime := ctx.Query("startTime")
- endTime := ctx.Query("endTime")
- srcName := ctx.Query("srcName")
- eventType := ctx.Query("eventType")
- eventTypeInt, _ := strconv.Atoi(eventType)
- var eventLogs model.EventLogs
- err, result := h.hikvisionService.GetEventLogs(pageNo, pageSize, srcType, startTime, endTime, srcName, eventTypeInt)
- if err != nil {
- resp.HandleError(ctx, 1203, "获取入侵报警事件日志查询失败", err)
- return
- }
- marshal, err := json.Marshal(result)
- 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]
- //eventLogs.Data.List[i].EventType = model.IntrusionAlarmEventType[eventLogs.Data.List[i].EventType]
- }
- resp.HandleSuccess(ctx, eventLogs.Data)
- }
- // GetCameraSearch 获取摄像头查询
- func (h *HikvisionHandler) GetCameraSearch(ctx *gin.Context) {
- pageNo := ctx.Query("pageNo")
- pageSize := ctx.Query("pageSize")
- name := ctx.Query("name")
- err, search := h.hikvisionService.GetcameraSearch(pageNo, pageSize, name)
- if err != nil {
- resp.HandleError(ctx, 1203, "获取摄像头查询失败", err)
- return
- }
- var cameraSearch model.CameraSearch
- marshal, err := json.Marshal(search)
- if err != nil {
- h.logger.Error("json序列化失败")
- resp.HandleError(ctx, 1203, "json序列化失败", err)
- return
- }
- err = json.Unmarshal(marshal, &cameraSearch)
- if err != nil {
- h.logger.Error("json反序列化失败")
- resp.HandleError(ctx, 1203, "json反序列化失败", err)
- return
- }
- for i, _ := range cameraSearch.Data.List {
- err, result := h.hikvisionService.GetManualCapture(cameraSearch.Data.List[i].IndexCode)
- if err != nil {
- cameraSearch.Data.List[i].Url = ""
- } else {
- var manualCapture model.ManualCapture
- marshal, err := json.Marshal(result)
- if err != nil {
- h.logger.Error("json序列化失败")
- resp.HandleError(ctx, 1203, "json序列化失败", err)
- return
- }
- err = json.Unmarshal(marshal, &manualCapture)
- if err != nil {
- h.logger.Error("json反序列化失败")
- resp.HandleError(ctx, 1203, "json反序列化失败", err)
- return
- }
- cameraSearch.Data.List[i].Url = manualCapture.Data.PicUrl
- }
- }
- resp.HandleSuccess(ctx, cameraSearch.Data)
- }
- // GetCameraList 获取监控点资源
- func (h *HikvisionHandler) GetCameraList(ctx *gin.Context) {
- pageNo := ctx.Query("pageNo")
- pageSize := ctx.Query("pageSize")
- err, search := h.hikvisionService.GetCamerasList(pageNo, pageSize)
- if err != nil {
- resp.HandleError(ctx, 1203, "获取监控点资源失败", err)
- return
- }
- resp.HandleSuccess(ctx, search.Data)
- }
- // GetAcsDeviceSearch 获取门禁设备查询
- func (h *HikvisionHandler) GetAcsDeviceSearch(ctx *gin.Context) {
- pageNo := ctx.Query("pageNo")
- pageSize := ctx.Query("pageSize")
- name := ctx.Query("name")
- err, search := h.hikvisionService.GetAcsDeviceSearch(pageNo, pageSize, name)
- if err != nil {
- resp.HandleError(ctx, 1203, "获取门禁设备列表失败", err)
- return
- }
- resp.HandleSuccess(ctx, search.Data)
- }
- // GetDoorDoControl 控制门禁点
- func (h *HikvisionHandler) GetDoorDoControl(ctx *gin.Context) {
- doorIndexCodes := ctx.QueryArray("doorIndexCodes")
- controlType := ctx.Query("controlType")
- atoi, err2 := strconv.Atoi(controlType)
- if err2 != nil {
- resp.HandleError(ctx, 1203, "参数错误", err2)
- return
- }
- if len(doorIndexCodes) <= 0 || len(doorIndexCodes) > 10 {
- resp.HandleError(ctx, 1203, "参数错误", nil)
- return
- }
- err, search := h.hikvisionService.GetDoorDoControl(doorIndexCodes, atoi)
- if err != nil {
- resp.HandleError(ctx, 1203, "控制门禁失败", err.Error())
- return
- }
- resp.HandleSuccess(ctx, search.Data)
- }
- // GetAcsDoorStates GetDoorDoControl 门禁点状态
- func (h *HikvisionHandler) GetAcsDoorStates(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 Acs model.DoorSearch
- var AcsDevice model.AcsDoorStates
- var acsDoor model.AcsDoor
- doorIndexCodes := ctx.Query("doorIndexCodes")
- if doorIndexCodes == "" {
- resp.HandleError(ctx, 1203, "参数错误", nil)
- return
- }
- for conn {
- select {
- case <-notify:
- conn = false
- fmt.Println("断开连接")
- return
- default:
- err, search := h.hikvisionService.GetAcsDoorStates([]string{doorIndexCodes})
- if err != nil {
- h.logger.Error("获取门禁设备在线状态失败")
- resp.HandleError(ctx, 1201, "获取门禁设备在线状态失败", err)
- return
- }
- bytes, err := json.Marshal(search)
- if err != nil {
- h.logger.Error("json序列化失败")
- resp.HandleError(ctx, 1202, "json序列化失败", nil)
- return
- }
- err = json.Unmarshal(bytes, &AcsDevice)
- if err != nil {
- h.logger.Error("json反序列化失败")
- resp.HandleError(ctx, 1203, "json反序列化失败", nil)
- return
- }
- err, result := h.hikvisionService.GetDoorSearch("1", "1", "", []string{AcsDevice.Data.AuthDoorList[0].DoorIndexCode})
- if err != nil {
- h.logger.Error("获取门禁点信息失败")
- resp.HandleError(ctx, 1201, "获取门禁点信息失败", err)
- return
- }
- bytes, err = json.Marshal(result)
- if err != nil {
- h.logger.Error("json序列化失败")
- resp.HandleError(ctx, 1202, "json序列化失败", nil)
- return
- }
- err = json.Unmarshal(bytes, &Acs)
- if err != nil {
- h.logger.Error("json反序列化失败")
- resp.HandleError(ctx, 1203, "json反序列化失败", nil)
- return
- }
- for i, _ := range Acs.Data.List {
- Acs.Data.List[i].State = AcsDevice.Data.AuthDoorList[0].DoorState
- acsDoor.DoorNo = Acs.Data.List[i].DoorNo
- acsDoor.DoorSerial = Acs.Data.List[i].DoorSerial
- acsDoor.IndexCode = Acs.Data.List[i].IndexCode
- acsDoor.State = AcsDevice.Data.AuthDoorList[0].DoorState
- if AcsDevice.Data.AuthDoorList[0].DoorState > 3 {
- acsDoor.StateName = "故障"
- acsDoor.Color = 16711680
- } else {
- acsDoor.StateName = model.StateName[AcsDevice.Data.AuthDoorList[0].DoorState]
- acsDoor.Color = model.Color[AcsDevice.Data.AuthDoorList[0].DoorState]
- }
- acsDoor.CreateTime = Acs.Data.List[i].CreateTime
- acsDoor.UpdateTime = Acs.Data.List[i].UpdateTime
- acsDoor.Name = Acs.Data.List[i].Name
- acsDoor.RegionIndexCode = Acs.Data.List[i].RegionIndexCode
- acsDoor.RegionName = Acs.Data.List[i].RegionName
- acsDoor.RegionPath = Acs.Data.List[i].RegionPath
- acsDoor.RegionPathName = Acs.Data.List[i].RegionPathName
- acsDoor.ResourceType = Acs.Data.List[i].ResourceType
- acsDoor.TreatyType = Acs.Data.List[i].TreatyType
- acsDoor.Description = Acs.Data.List[i].Description
- acsDoor.InstallLocation = Acs.Data.List[i].InstallLocation
- acsDoor.ParentIndexCode = Acs.Data.List[i].ParentIndexCode
- acsDoor.ReaderInId = Acs.Data.List[i].ReaderInId
- acsDoor.ReaderOutId = Acs.Data.List[i].ReaderOutId
- acsDoor.ChannelNo = Acs.Data.List[i].ChannelNo
- acsDoor.ControlOneId = Acs.Data.List[i].ControlOneId
- acsDoor.ControlTwoId = Acs.Data.List[i].ControlTwoId
- acsDoor.ChannelType = Acs.Data.List[i].ChannelType
- }
- response.Code = 200
- response.Msg = "获取门禁点列表成功"
- response.Data = acsDoor
- res, _ := json.Marshal(&response)
- fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
- ctx.Writer.Flush()
- time.Sleep(10 * time.Second)
- }
- }
- }
|