hikvision.go 20 KB

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