hikvision.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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, "index.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. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.controlling"), m, 15)
  339. if err != nil {
  340. h.logger.Error("控制云台失败")
  341. resp.HandleError(ctx, 1201, "控制云台失败", err)
  342. return
  343. }
  344. if hikvision.Code != "0" {
  345. atoi, _ := strconv.Atoi(hikvision.Code)
  346. resp.HandleError(ctx, atoi, hikvision.Msg, nil)
  347. return
  348. }
  349. resp.HandleSuccess(ctx, hikvision.Msg)
  350. }
  351. // VisitorInfoCount 获取今日访客信息包含:今日来访总人数(已签离人数,未签离人数),预约人数
  352. func (h *HikvisionHandler) VisitorInfoCount(c *gin.Context) {
  353. m := make(map[string]string)
  354. parkId := c.Query("parkId")
  355. m["parkId"] = parkId
  356. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.visitorInfo"), m, 15)
  357. if err != nil {
  358. h.logger.Error("获取访客信息失败")
  359. resp.HandleError(c, 1201, "获取访客信息失败", err)
  360. return
  361. }
  362. if hikvision.Code != "0" {
  363. atoi, _ := strconv.Atoi(hikvision.Code)
  364. resp.HandleError(c, atoi, hikvision.Msg, nil)
  365. return
  366. }
  367. resp.HandleSuccess(c, hikvision.Data)
  368. }
  369. // GetDoorSearch 查询门禁点列表v2
  370. func (h *HikvisionHandler) GetDoorSearch(ctx *gin.Context) {
  371. // 设置响应头
  372. ctx.Header("Content-Type", "text/event-stream")
  373. ctx.Header("Cache-Control", "no-cache")
  374. ctx.Header("Connection", "keep-alive")
  375. // 监听客户端断开连接
  376. conn := true
  377. notify := ctx.Writer.CloseNotify()
  378. var response model.Response
  379. //var doorlist []model.DoorList
  380. //var doorResp model.DoorResp
  381. m := make(map[string]string)
  382. m["pageNo"] = "1"
  383. m["pageSize"] = "1000"
  384. for conn {
  385. select {
  386. case <-notify:
  387. conn = false
  388. fmt.Println("断开连接")
  389. return
  390. default:
  391. doorSearch, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.doorSearch"), m, 15)
  392. if err != nil {
  393. h.logger.Error("获取门禁点列表失败")
  394. response.Code = 1203
  395. response.Msg = "获取门禁点列表失败"
  396. response.Data = nil
  397. res, _ := json.Marshal(&response)
  398. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  399. ctx.Writer.Flush()
  400. return
  401. }
  402. if doorSearch.Code != "0" {
  403. response.Code = 1203
  404. response.Msg = "获取门禁点列表失败"
  405. response.Data = nil
  406. res, _ := json.Marshal(&response)
  407. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  408. ctx.Writer.Flush()
  409. conn = false
  410. return
  411. }
  412. // 获取门禁状态
  413. //doorIndexCodes := []string{""}
  414. //for _, v := range doorResp.Data.List {
  415. // doorIndexCodes = append(doorIndexCodes, v.IndexCode)
  416. //}
  417. //data := map[string]string{
  418. // "doorIndexCodes": strings.Join(doorIndexCodes, ","),
  419. //}
  420. //doorStates, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.doorStates"), data, 15)
  421. //if err != nil {
  422. // h.logger.Error("获取门禁状态失败")
  423. // response.Code = 1203
  424. // response.Msg = "获取门禁状态失败"
  425. // response.Data = nil
  426. // res, _ := json.Marshal(&response)
  427. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  428. // ctx.Writer.Flush()
  429. // conn = false
  430. // return
  431. //}
  432. //if doorStates.Code != "0" {
  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. //
  443. //for i := 0; i < 3; i++ {
  444. // genUUID := uuid.GenUUID()
  445. // name := fmt.Sprintf("资源:%v", i+1)
  446. //
  447. // list := model.DoorList{
  448. // IndexCode: genUUID,
  449. // ResourceType: "door",
  450. // Name: name,
  451. // DoorNo: genUUID,
  452. // ChannelNo: genUUID,
  453. // ParentIndexCode: genUUID,
  454. // ControlOneId: genUUID,
  455. // ControlTwoId: genUUID,
  456. // ReaderInId: genUUID,
  457. // ReaderOutId: genUUID,
  458. // DoorSerial: i + 1,
  459. // TreatyType: genUUID,
  460. // RegionIndexCode: genUUID,
  461. // RegionPath: genUUID,
  462. // CreateTime: time.Now().Format("2006-01-02 15:04:05"),
  463. // UpdateTime: time.Now().Format("2006-01-02 15:04:05"),
  464. // Description: genUUID,
  465. // ChannelType: genUUID,
  466. // RegionName: genUUID,
  467. // RegionPathName: genUUID,
  468. // InstallLocation: genUUID,
  469. // }
  470. // doorlist = append(doorlist, list)
  471. //}
  472. //doorResp := model.DoorResp{
  473. // Code: "0",
  474. // Msg: "SUCCESS",
  475. // Data: struct {
  476. // Total int `json:"total"`
  477. // PageNo int `json:"pageNo"`
  478. // PageSize int `json:"pageSize"`
  479. // List []model.DoorList `json:"list"`
  480. // }{Total: 3, PageNo: 1, PageSize: 1, List: doorlist},
  481. //}
  482. response.Code = 200
  483. response.Msg = "获取门禁点列表成功"
  484. response.Data = doorSearch.Data
  485. res, _ := json.Marshal(&response)
  486. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  487. ctx.Writer.Flush()
  488. time.Sleep(10 * time.Second)
  489. }
  490. }
  491. }
  492. // DoControl 控制门禁
  493. func (h *HikvisionHandler) DoControl(ctx *gin.Context) {
  494. // 设置响应头
  495. ctx.Header("Content-Type", "text/event-stream")
  496. ctx.Header("Cache-Control", "no-cache")
  497. ctx.Header("Connection", "keep-alive")
  498. // 监听客户端断开连接
  499. conn := true
  500. notify := ctx.Writer.CloseNotify()
  501. doorIndexCodes := ctx.Query("doorIndexCodes")
  502. controlType := ctx.Query("controlType")
  503. if len(doorIndexCodes) <= 0 || len(controlType) <= 0 {
  504. resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
  505. return
  506. }
  507. fmt.Println("doorIndexCodes:", doorIndexCodes)
  508. m := make(map[string]string)
  509. m["doorIndexCodes"] = doorIndexCodes
  510. m["controlType"] = controlType
  511. var response model.Response
  512. for conn {
  513. select {
  514. case <-notify:
  515. conn = false
  516. fmt.Println("断开连接")
  517. return
  518. default:
  519. //hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.control"), m, 15)
  520. //if err != nil {
  521. // h.logger.Error("控制门禁失败")
  522. // response.Code = 1203
  523. // response.Msg = "控制门禁失败"
  524. // response.Data = nil
  525. // res, _ := json.Marshal(&response)
  526. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  527. // ctx.Writer.Flush()
  528. // conn = false
  529. // return
  530. //}
  531. //if hikvision.Code != "0" {
  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. genUUID := uuid.GenUUID()
  542. doControl := model.DoControl{
  543. Code: "0",
  544. Msg: "SUCCESS",
  545. Data: []model.DoControlData{{
  546. DoorIndexCode: genUUID,
  547. ControlResultCode: 0,
  548. ControlResultDesc: "success",
  549. }},
  550. }
  551. response.Code = 200
  552. response.Msg = "控制门禁成功"
  553. response.Data = doControl.Data
  554. res, _ := json.Marshal(&response)
  555. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  556. ctx.Writer.Flush()
  557. time.Sleep(10 * time.Second)
  558. }
  559. }
  560. }
  561. // RealTimeInspection 入侵报警事件日志查询
  562. func (h *HikvisionHandler) RealTimeInspection(ctx *gin.Context) {
  563. m := make(map[string]string)
  564. m["startTime"] = ctx.Query("startTime") //开始时间
  565. m["endTime"] = ctx.Query("endTime") //结束时间
  566. m["pageNo"] = ctx.Query("pageNo")
  567. m["pageSize"] = h.conf.GetString("hikvision.pageSize")
  568. var eventLogs model.EventLogs
  569. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.eventLogs"), m, 15)
  570. if err != nil {
  571. h.logger.Error("入侵报警事件日志查询失败")
  572. resp.HandleError(ctx, 1203, "入侵报警事件日志查询失败", err)
  573. return
  574. }
  575. if hikvision.Code != "0" {
  576. h.logger.Error("入侵报警事件日志查询失败")
  577. resp.HandleError(ctx, 1203, "入侵报警事件日志查询失败", hikvision.Code)
  578. return
  579. }
  580. marshal, err := json.Marshal(hikvision)
  581. if err != nil {
  582. h.logger.Error("json序列化失败")
  583. resp.HandleError(ctx, 1203, "json序列化失败", err)
  584. return
  585. }
  586. err = json.Unmarshal(marshal, &eventLogs)
  587. if err != nil {
  588. h.logger.Error("json反序列化失败")
  589. resp.HandleError(ctx, 1203, "json反序列化失败", err)
  590. return
  591. }
  592. for i, _ := range eventLogs.Data.List {
  593. eventLogs.Data.List[i].SrcType = model.ResourceType[eventLogs.Data.List[i].SrcType]
  594. }
  595. resp.HandleSuccess(ctx, hikvision.Data)
  596. }