hikvision.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. package handler
  2. import (
  3. "city_chips/internal/model"
  4. "city_chips/internal/service"
  5. "city_chips/pkg/helper/resp"
  6. "city_chips/pkg/helper/uuid"
  7. "encoding/json"
  8. "fmt"
  9. "math/rand"
  10. "net/http"
  11. "strconv"
  12. "time"
  13. "github.com/gin-gonic/gin"
  14. "github.com/spf13/viper"
  15. "go.uber.org/zap"
  16. )
  17. type HikvisionHandler struct {
  18. *Handler
  19. hikvisionService service.HikvisionService
  20. conf *viper.Viper
  21. }
  22. func NewHikvisionHandler(handler *Handler, hikvisionService service.HikvisionService, conf *viper.Viper) *HikvisionHandler {
  23. return &HikvisionHandler{
  24. Handler: handler,
  25. hikvisionService: hikvisionService,
  26. conf: conf,
  27. }
  28. }
  29. // GetMonitor 获取视频监控
  30. func (h *HikvisionHandler) GetMonitor(ctx *gin.Context) {
  31. m := make(map[string]any)
  32. LicensePlateRecognition := make(map[string]any)
  33. Blacklist := make(map[string]any)
  34. var monitor []model.Monitor
  35. var monitornms []model.MonitorNms
  36. for i := 0; i < 7; i++ {
  37. name := fmt.Sprintf("周%v", i+1)
  38. LicensePlateRecognition[name] = rand.Intn(100)
  39. Blacklist[name] = rand.Intn(100)
  40. }
  41. for i := 0; i < 20; i++ {
  42. m2 := model.Monitor{
  43. Id: i + 1,
  44. State: rand.Intn(2),
  45. Location: model.GetRandomItem(model.MJlocations),
  46. Name: model.GetRandomItem(model.MonitorDeviceNames),
  47. }
  48. nms := model.MonitorNms{
  49. Id: i + 1,
  50. Name: model.GetRandomItem(model.MonitorDeviceNames),
  51. InspectResult: rand.Intn(6),
  52. }
  53. monitor = append(monitor, m2)
  54. monitornms = append(monitornms, nms)
  55. }
  56. //err, resource := h.hikvisionService.DeviceResource("camera")
  57. //if err != nil {
  58. // resp.HandleError(ctx, 1201, "查询监控点数量失败", nil)
  59. // return
  60. //}
  61. //m["MonitorCount"] = resource.Data.Total //监控点总数
  62. m["MonitorCount"] = rand.Intn(1000) //监控点总数
  63. m["DeviceOnline"] = rand.Intn(1000) //设备在线
  64. m["DeviceOffline"] = rand.Intn(1000) //设备离线
  65. m["StorageCapacity"] = rand.Intn(1000) //存储容量
  66. m["MonitorList"] = monitor //监控列表
  67. m["MonitorNms"] = monitornms //监控检测
  68. m["ImageIsNormal"] = rand.Intn(1000) //图像正常
  69. m["ImageAbnormalities"] = rand.Intn(1000) //图像异常
  70. m["DiagnosisFailed"] = rand.Intn(1000) //诊断失败
  71. m["NotDetected"] = rand.Intn(1000) //未检测
  72. resp.HandleSuccess(ctx, m)
  73. }
  74. // GetInvade 获取入侵检测
  75. func (h *HikvisionHandler) GetInvade(ctx *gin.Context) {
  76. m := make(map[string]any)
  77. AlarmTrend24Hour := make(map[string]any)
  78. var realTime []model.Device
  79. for i := 0; i < 24; i++ {
  80. name := fmt.Sprintf("%v时", i+1)
  81. AlarmTrend24Hour[name] = rand.Intn(100)
  82. }
  83. for i := 0; i < 10; i++ {
  84. inspection := model.Device{
  85. Id: i + 1,
  86. Name: model.GetRandomItem(model.IntrusionDeviceNames),
  87. State: rand.Intn(2),
  88. Date: time.Now().Format("2006-01-02 15:04:05"),
  89. }
  90. realTime = append(realTime, inspection)
  91. }
  92. m["DeviceAlerts"] = rand.Intn(100) //设备预警
  93. m["LowRiskWarning"] = rand.Intn(100) //低危预警
  94. m["MediumRiskWarning"] = rand.Intn(100) //中危预警
  95. m["HighRiskWarning"] = rand.Intn(1000) //高危预警
  96. m["HighRiskWarning"] = rand.Intn(1000) //高危预警
  97. m["OverHazardWarning"] = rand.Intn(1000) //超危预警
  98. m["EventRisk"] = rand.Intn(100) //事件风险
  99. m["AlarmRisk"] = rand.Intn(100) //告警风险
  100. m["trend"] = rand.Intn(100) //圆环中间百分比
  101. m["realTime"] = realTime //实时巡检
  102. m["AlarmTrend24Hour"] = AlarmTrend24Hour //24小时告警趋势
  103. resp.HandleSuccess(ctx, m)
  104. }
  105. // GetElectronicInspections 电子巡查
  106. func (h *HikvisionHandler) GetElectronicInspections(ctx *gin.Context) {
  107. m := make(map[string]any)
  108. Inspect := make(map[string]any)
  109. var device []model.Device
  110. var realTime []model.RealTimeInspection
  111. for i := 0; i < 24; i++ {
  112. name := fmt.Sprintf("%v时", i+1)
  113. Inspect[name] = rand.Intn(100)
  114. }
  115. for i := 0; i < 20; i++ {
  116. m2 := model.Device{
  117. Id: i + 1,
  118. Name: model.GetRandomItem(model.RealInspectionDeviceNames),
  119. State: rand.Intn(2),
  120. Date: time.Now().Format("2006-01-02 15:04:05"),
  121. }
  122. device = append(device, m2)
  123. }
  124. for i := 0; i < 10; i++ {
  125. name := fmt.Sprintf("巡检事件%v", i+1)
  126. inspection := model.RealTimeInspection{
  127. Id: i + 1,
  128. Name: model.GetRandomItem(model.InspectionEvents),
  129. Location: model.GetRandomItem(model.InspectionLocations),
  130. Event: name,
  131. Date: time.Now().Format("2006-01-02 15:04:05"),
  132. }
  133. realTime = append(realTime, inspection)
  134. }
  135. m["TodayTotal"] = rand.Intn(100) //今日总数
  136. m["InspectionPoints"] = rand.Intn(100) //巡检点数
  137. m["InspectionPlan"] = rand.Intn(100) //巡检计划
  138. m["InspectTheLine"] = rand.Intn(1000) //巡检线路
  139. m["Inspect"] = Inspect //巡检统计
  140. m["realTime"] = realTime //实时巡检
  141. m["DeviceList"] = device //设备列表
  142. resp.HandleSuccess(ctx, m)
  143. }
  144. // GetVisitor 访客系统
  145. func (h *HikvisionHandler) GetVisitor(ctx *gin.Context) {
  146. m := make(map[string]any)
  147. PrevailingTrends := make(map[string]any)
  148. var visitor []model.Visitor
  149. for i := 0; i < 24; i++ {
  150. address := fmt.Sprintf("%v时", i+1)
  151. m2 := model.Visitor{
  152. Id: i + 1,
  153. Name: model.GetRandomItem(model.VisitorNames),
  154. State: rand.Intn(2),
  155. Phone: model.GeneratePhoneNumber(),
  156. Location: address,
  157. Date: time.Now().Format("2006-01-02 15:04:05"),
  158. }
  159. PrevailingTrends[address] = rand.Intn(100)
  160. visitor = append(visitor, m2)
  161. }
  162. m["VisitorCount"] = rand.Intn(1000) //访客总量
  163. m["SignInCount"] = rand.Intn(1000) //签到数量
  164. m["TransitCount"] = rand.Intn(1000) //通行数量
  165. m["Exit"] = rand.Intn(1000) //离场数量
  166. m["Client"] = rand.Intn(100) //客户
  167. m["Vendor"] = rand.Intn(100) //供应商
  168. m["Interviewees"] = rand.Intn(100) //面试者
  169. m["GovernmentPersonnel"] = rand.Intn(100) //政府人员
  170. m["Other"] = rand.Intn(100) //其他
  171. m["PrevailingTrends"] = PrevailingTrends //通行趋势
  172. m["VisitorRegistration"] = visitor //访客登记
  173. m["VisitorsSwipeCards"] = visitor //访客刷卡记录
  174. resp.HandleSuccess(ctx, m)
  175. }
  176. // GetPassenger 客流统计
  177. func (h *HikvisionHandler) GetPassenger(ctx *gin.Context) {
  178. m := make(map[string]any)
  179. Type1 := make(map[string]any)
  180. Type2 := make(map[string]any)
  181. Type3 := make(map[string]any)
  182. Type4 := make(map[string]any)
  183. rankings := make(map[string]any)
  184. customers := make(map[string]any)
  185. var event []model.RealTimeInspection
  186. for i := 0; i < 7; i++ {
  187. sprintf := fmt.Sprintf("2025-5-%v", i+1)
  188. rankings[model.GetRandomItem(model.CustomerGroups)] = rand.Intn(100)
  189. Type1[sprintf] = rand.Intn(100)
  190. Type2[sprintf] = rand.Intn(100)
  191. Type3[sprintf] = rand.Intn(100)
  192. Type4[sprintf] = rand.Intn(100)
  193. }
  194. for i := 0; i < 3; i++ {
  195. customers[model.GetRandomItem(model.CustomerGroups)] = rand.Intn(100)
  196. }
  197. for i := 0; i < 10; i++ {
  198. inspection := model.RealTimeInspection{
  199. Id: i + 1,
  200. Name: model.GetRandomItem(model.SecurityEvents),
  201. Location: model.GetRandomItem(model.LocationsADD),
  202. Event: time.Now().Format("2006-01-02 15:04:05"),
  203. }
  204. event = append(event, inspection)
  205. }
  206. m["SecurityLevel"] = rand.Intn(500) //安全等级
  207. m["NetworkEquipment"] = rand.Intn(100) //网络设备
  208. m["Normal"] = rand.Intn(100) //正常
  209. m["Fault"] = rand.Intn(100) //故障
  210. m["Offline"] = rand.Intn(100) //离线
  211. m["Type1"] = Type1 //客流监控type1
  212. m["Type2"] = Type2 //客流监控type2
  213. m["Type3"] = Type3 //客流监控type3
  214. m["Type4"] = Type4 //客流监控type4
  215. m["Rankings"] = rankings //指标区排行榜
  216. m["Customers"] = customers //客群分析统计
  217. m["Customers"] = customers //客群分析统计
  218. m["Event"] = event //安全事件列表
  219. resp.HandleSuccess(ctx, m)
  220. }
  221. // 门禁系统
  222. func (h *HikvisionHandler) GetAccess(ctx *gin.Context) {
  223. m := make(map[string]any)
  224. invasio1 := make(map[string]any)
  225. invasio2 := make(map[string]any)
  226. DailyTotal := make(map[string]any)
  227. Cumulative := make(map[string]any)
  228. var devices []model.RealTimeInspection
  229. var alarmList []model.AlarmList
  230. for i := 0; i < 7; i++ {
  231. sprintf := fmt.Sprintf("2025-5-%v", i+1)
  232. invasio1[sprintf] = rand.Intn(100)
  233. invasio2[sprintf] = rand.Intn(100)
  234. }
  235. for i := 0; i < 24; i++ {
  236. sprintf := fmt.Sprintf("%v时", i+1)
  237. DailyTotal[sprintf] = rand.Intn(100)
  238. Cumulative[sprintf] = rand.Intn(100)
  239. }
  240. for i := 0; i < 10; i++ {
  241. inspection := model.RealTimeInspection{
  242. Id: i + 1,
  243. Name: model.GetRandomItem(model.MJDeviceNames),
  244. Location: model.GetRandomItem(model.MJlocations),
  245. Event: time.Now().Format("2006-01-02 15:04:05"),
  246. }
  247. alarm := model.AlarmList{
  248. Id: i + 1,
  249. AlarmContent: model.GetRandomItem(model.MJDeviceNames),
  250. Location: model.GetRandomItem(model.MJlocations),
  251. State: rand.Intn(2),
  252. Date: time.Now().Format("2006-01-02 15:04:05"),
  253. }
  254. devices = append(devices, inspection)
  255. alarmList = append(alarmList, alarm)
  256. }
  257. m["DeviceCount"] = rand.Intn(500) //设备总数
  258. m["Online"] = rand.Intn(100) //在线
  259. m["Abnormal"] = rand.Intn(100) //异常
  260. m["Fault"] = rand.Intn(100) //故障
  261. m["Offline"] = rand.Intn(100) //离线
  262. m["Attendance"] = rand.Intn(100) //出勤率
  263. m["Invasio1"] = invasio1 //入侵事件1
  264. m["Invasio2"] = invasio2 //入侵事件2
  265. m["DailyTotal"] = DailyTotal //每日统计
  266. m["Cumulative"] = Cumulative //累计统计
  267. m["AlarmList"] = alarmList //实时告警与通知
  268. m["devices"] = devices //设备列表
  269. resp.HandleSuccess(ctx, m)
  270. }
  271. // GetHikvisionMonitoring 获取视频监控流{
  272. // "cameraIndexCode": "748d84750e3a4a5bbad3cd4af9ed5101",
  273. // "streamType": 0,
  274. // "protocol": "rtsp",
  275. // "transmode": 1,
  276. // "expand": "transcode=0",
  277. // "streamform": "ps"
  278. // }
  279. func (h *HikvisionHandler) GetHikvisionMonitoring(ctx *gin.Context) {
  280. m := make(map[string]string)
  281. cameraIndexCode := ctx.Query("cameraIndexCode")
  282. m["cameraIndexCode"] = cameraIndexCode
  283. m["protocol"] = "ws"
  284. if len(cameraIndexCode) <= 0 || cameraIndexCode == "" {
  285. resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
  286. return
  287. }
  288. ctx.HTML(http.StatusOK, "h5player.html", gin.H{
  289. "title": "测试",
  290. "wsurl": "ws://127.0.0.1/" + cameraIndexCode,
  291. "cameraIndexCode": cameraIndexCode,
  292. })
  293. //fmt.Println("cameraIndexCode", h.conf.GetString("hikvision.api.previewURLs"))
  294. //hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.previewURLs"), m, 15)
  295. ////返回结果{
  296. //// "code": "0",
  297. //// "msg": "success",
  298. //// "data": {
  299. //// "url": "rtsp://10.2.145.66:655/EUrl/CLJ52BW"
  300. //// }
  301. ////}
  302. //if err != nil {
  303. // h.logger.Error("获取获取监控点资源失败")
  304. // resp.HandleError(ctx, 1201, "获取获取监控点资源失败", err)
  305. // return
  306. //}
  307. //if hikvision.Code != "0" {
  308. // atoi, _ := strconv.Atoi(hikvision.Code)
  309. // resp.HandleError(ctx, atoi, hikvision.Msg, nil)
  310. // return
  311. //}
  312. //marshalString, err := json.Marshal(hikvision)
  313. //if err != nil {
  314. // resp.HandleError(ctx, 1202, "json序列化失败", nil)
  315. // return
  316. //}
  317. //url := gjson.Get(string(marshalString), "data.url")
  318. //
  319. //resp.HandleSuccess(ctx, url)
  320. }
  321. // 视频监控云台控制{
  322. // "cameraIndexCode": "748d84750e3a4a5bbad3cd4af9ed5101",
  323. // "action": 1, 0-开始
  324. //1-停止
  325. //注:GOTO_PRESET命令下填任意值均可转到预置点,建议填0即可
  326. // "command": "GOTO_PRESET",
  327. // "speed": 4,
  328. // "presetIndex": 20
  329. //}
  330. func (h *HikvisionHandler) Gimbalcontrol(ctx *gin.Context) {
  331. m := make(map[string]string)
  332. cameraIndexCode := ctx.Query("cameraIndexCode")
  333. command := ctx.Query("command")
  334. action := ctx.Query("action")
  335. speed := ctx.Query("speed")
  336. presetIndex := ctx.Query("presetIndex")
  337. m["cameraIndexCode"] = cameraIndexCode
  338. m["action"] = action
  339. m["command"] = command
  340. m["speed"] = speed
  341. m["presetIndex"] = presetIndex
  342. if len(cameraIndexCode) <= 0 || len(command) <= 0 || len(action) <= 0 {
  343. resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
  344. return
  345. }
  346. // resp.HandleSuccess(ctx, "操作成功")
  347. // return
  348. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.controlling"), m, 15)
  349. if err != nil {
  350. h.logger.Error("控制云台失败")
  351. resp.HandleError(ctx, 1201, "控制云台失败", err)
  352. return
  353. }
  354. if hikvision.Code != "0" {
  355. atoi, _ := strconv.Atoi(hikvision.Code)
  356. resp.HandleError(ctx, atoi, hikvision.Msg, nil)
  357. return
  358. }
  359. resp.HandleSuccess(ctx, hikvision.Msg)
  360. }
  361. // VisitorInfoCount 获取今日访客信息包含:今日来访总人数(已签离人数,未签离人数),预约人数
  362. func (h *HikvisionHandler) VisitorInfoCount(c *gin.Context) {
  363. m := make(map[string]string)
  364. parkId := c.Query("parkId")
  365. m["parkId"] = parkId
  366. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.visitorInfo"), m, 15)
  367. if err != nil {
  368. h.logger.Error("获取访客信息失败")
  369. resp.HandleError(c, 1201, "获取访客信息失败", err)
  370. return
  371. }
  372. if hikvision.Code != "0" {
  373. atoi, _ := strconv.Atoi(hikvision.Code)
  374. resp.HandleError(c, atoi, hikvision.Msg, nil)
  375. return
  376. }
  377. resp.HandleSuccess(c, hikvision.Data)
  378. }
  379. // GetDoorSearch 查询门禁点列表v2
  380. func (h *HikvisionHandler) GetDoorSearch(ctx *gin.Context) {
  381. // 设置响应头
  382. ctx.Header("Content-Type", "text/event-stream")
  383. ctx.Header("Cache-Control", "no-cache")
  384. ctx.Header("Connection", "keep-alive")
  385. // 监听客户端断开连接
  386. conn := true
  387. notify := ctx.Writer.CloseNotify()
  388. var response model.Response
  389. //var doorlist []model.DoorList
  390. //var doorResp model.DoorResp
  391. m := make(map[string]string)
  392. m["pageNo"] = "1"
  393. m["pageSize"] = "1000"
  394. for conn {
  395. select {
  396. case <-notify:
  397. conn = false
  398. fmt.Println("断开连接")
  399. return
  400. default:
  401. doorSearch, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.doorSearch"), m, 15)
  402. if err != nil {
  403. h.logger.Error("获取门禁点列表失败")
  404. response.Code = 1203
  405. response.Msg = "获取门禁点列表失败"
  406. response.Data = nil
  407. res, _ := json.Marshal(&response)
  408. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  409. ctx.Writer.Flush()
  410. return
  411. }
  412. if doorSearch.Code != "0" {
  413. response.Code = 1203
  414. response.Msg = "获取门禁点列表失败"
  415. response.Data = nil
  416. res, _ := json.Marshal(&response)
  417. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  418. ctx.Writer.Flush()
  419. conn = false
  420. return
  421. }
  422. // 获取门禁状态
  423. //doorIndexCodes := []string{""}
  424. //for _, v := range doorResp.Data.List {
  425. // doorIndexCodes = append(doorIndexCodes, v.IndexCode)
  426. //}
  427. //data := map[string]string{
  428. // "doorIndexCodes": strings.Join(doorIndexCodes, ","),
  429. //}
  430. //doorStates, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.doorStates"), data, 15)
  431. //if err != nil {
  432. // h.logger.Error("获取门禁状态失败")
  433. // response.Code = 1203
  434. // response.Msg = "获取门禁状态失败"
  435. // response.Data = nil
  436. // res, _ := json.Marshal(&response)
  437. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  438. // ctx.Writer.Flush()
  439. // conn = false
  440. // return
  441. //}
  442. //if doorStates.Code != "0" {
  443. // response.Code = 1203
  444. // response.Msg = "获取门禁状态失败"
  445. // response.Data = nil
  446. // res, _ := json.Marshal(&response)
  447. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  448. // ctx.Writer.Flush()
  449. // conn = false
  450. // return
  451. //}
  452. //
  453. //for i := 0; i < 3; i++ {
  454. // genUUID := uuid.GenUUID()
  455. // name := fmt.Sprintf("资源:%v", i+1)
  456. //
  457. // list := model.DoorList{
  458. // IndexCode: genUUID,
  459. // ResourceType: "door",
  460. // Name: name,
  461. // DoorNo: genUUID,
  462. // ChannelNo: genUUID,
  463. // ParentIndexCode: genUUID,
  464. // ControlOneId: genUUID,
  465. // ControlTwoId: genUUID,
  466. // ReaderInId: genUUID,
  467. // ReaderOutId: genUUID,
  468. // DoorSerial: i + 1,
  469. // TreatyType: genUUID,
  470. // RegionIndexCode: genUUID,
  471. // RegionPath: genUUID,
  472. // CreateTime: time.Now().Format("2006-01-02 15:04:05"),
  473. // UpdateTime: time.Now().Format("2006-01-02 15:04:05"),
  474. // Description: genUUID,
  475. // ChannelType: genUUID,
  476. // RegionName: genUUID,
  477. // RegionPathName: genUUID,
  478. // InstallLocation: genUUID,
  479. // }
  480. // doorlist = append(doorlist, list)
  481. //}
  482. //doorResp := model.DoorResp{
  483. // Code: "0",
  484. // Msg: "SUCCESS",
  485. // Data: struct {
  486. // Total int `json:"total"`
  487. // PageNo int `json:"pageNo"`
  488. // PageSize int `json:"pageSize"`
  489. // List []model.DoorList `json:"list"`
  490. // }{Total: 3, PageNo: 1, PageSize: 1, List: doorlist},
  491. //}
  492. response.Code = 200
  493. response.Msg = "获取门禁点列表成功"
  494. response.Data = doorSearch.Data
  495. res, _ := json.Marshal(&response)
  496. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  497. ctx.Writer.Flush()
  498. time.Sleep(10 * time.Second)
  499. }
  500. }
  501. }
  502. // DoControl 控制门禁
  503. func (h *HikvisionHandler) DoControl(ctx *gin.Context) {
  504. // 设置响应头
  505. ctx.Header("Content-Type", "text/event-stream")
  506. ctx.Header("Cache-Control", "no-cache")
  507. ctx.Header("Connection", "keep-alive")
  508. // 监听客户端断开连接
  509. conn := true
  510. notify := ctx.Writer.CloseNotify()
  511. doorIndexCodes := ctx.Query("doorIndexCodes")
  512. controlType := ctx.Query("controlType")
  513. if len(doorIndexCodes) <= 0 || len(controlType) <= 0 {
  514. resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
  515. return
  516. }
  517. fmt.Println("doorIndexCodes:", doorIndexCodes)
  518. m := make(map[string]string)
  519. m["doorIndexCodes"] = doorIndexCodes
  520. m["controlType"] = controlType
  521. var response model.Response
  522. for conn {
  523. select {
  524. case <-notify:
  525. conn = false
  526. fmt.Println("断开连接")
  527. return
  528. default:
  529. //hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.control"), m, 15)
  530. //if err != nil {
  531. // h.logger.Error("控制门禁失败")
  532. // response.Code = 1203
  533. // response.Msg = "控制门禁失败"
  534. // response.Data = nil
  535. // res, _ := json.Marshal(&response)
  536. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  537. // ctx.Writer.Flush()
  538. // conn = false
  539. // return
  540. //}
  541. //if hikvision.Code != "0" {
  542. // response.Code = 1203
  543. // response.Msg = "控制门禁失败"
  544. // response.Data = nil
  545. // res, _ := json.Marshal(&response)
  546. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  547. // ctx.Writer.Flush()
  548. // conn = false
  549. // return
  550. //}
  551. genUUID := uuid.GenUUID()
  552. doControl := model.DoControl{
  553. Code: "0",
  554. Msg: "SUCCESS",
  555. Data: []model.DoControlData{{
  556. DoorIndexCode: genUUID,
  557. ControlResultCode: 0,
  558. ControlResultDesc: "success",
  559. }},
  560. }
  561. response.Code = 200
  562. response.Msg = "控制门禁成功"
  563. response.Data = doControl.Data
  564. res, _ := json.Marshal(&response)
  565. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  566. ctx.Writer.Flush()
  567. time.Sleep(10 * time.Second)
  568. }
  569. }
  570. }
  571. // RealTimeInspection 入侵报警事件日志查询
  572. func (h *HikvisionHandler) RealTimeInspection(ctx *gin.Context) {
  573. m := make(map[string]string)
  574. m["startTime"] = ctx.Query("startTime") //开始时间
  575. m["endTime"] = ctx.Query("endTime") //结束时间
  576. m["pageNo"] = ctx.Query("pageNo")
  577. m["pageSize"] = h.conf.GetString("hikvision.pageSize")
  578. var eventLogs model.EventLogs
  579. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.eventLogs"), m, 15)
  580. if err != nil {
  581. h.logger.Error("入侵报警事件日志查询失败", zap.Error(err))
  582. resp.HandleError(ctx, 1203, "入侵报警事件日志查询失败", err)
  583. return
  584. }
  585. if hikvision.Code != "0" {
  586. h.logger.Error("入侵报警事件日志查询失败")
  587. resp.HandleError(ctx, 1203, "入侵报警事件日志查询失败", hikvision.Code)
  588. return
  589. }
  590. marshal, err := json.Marshal(hikvision)
  591. if err != nil {
  592. h.logger.Error("json序列化失败")
  593. resp.HandleError(ctx, 1203, "json序列化失败", err)
  594. return
  595. }
  596. err = json.Unmarshal(marshal, &eventLogs)
  597. if err != nil {
  598. h.logger.Error("json反序列化失败")
  599. resp.HandleError(ctx, 1203, "json反序列化失败", err)
  600. return
  601. }
  602. for i, _ := range eventLogs.Data.List {
  603. eventLogs.Data.List[i].SrcType = model.ResourceType[eventLogs.Data.List[i].SrcType]
  604. }
  605. resp.HandleSuccess(ctx, hikvision.Data)
  606. }