hikvision.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. "wsurl": "ws://127.0.0.1/" + cameraIndexCode,
  284. "cameraIndexCode": cameraIndexCode,
  285. })
  286. //fmt.Println("cameraIndexCode", h.conf.GetString("hikvision.api.previewURLs"))
  287. //hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.previewURLs"), m, 15)
  288. ////返回结果{
  289. //// "code": "0",
  290. //// "msg": "success",
  291. //// "data": {
  292. //// "url": "rtsp://10.2.145.66:655/EUrl/CLJ52BW"
  293. //// }
  294. ////}
  295. //if err != nil {
  296. // h.logger.Error("获取获取监控点资源失败")
  297. // resp.HandleError(ctx, 1201, "获取获取监控点资源失败", err)
  298. // return
  299. //}
  300. //if hikvision.Code != "0" {
  301. // atoi, _ := strconv.Atoi(hikvision.Code)
  302. // resp.HandleError(ctx, atoi, hikvision.Msg, nil)
  303. // return
  304. //}
  305. //marshalString, err := json.Marshal(hikvision)
  306. //if err != nil {
  307. // resp.HandleError(ctx, 1202, "json序列化失败", nil)
  308. // return
  309. //}
  310. //url := gjson.Get(string(marshalString), "data.url")
  311. //
  312. //resp.HandleSuccess(ctx, url)
  313. }
  314. // 视频监控云台控制{
  315. // "cameraIndexCode": "748d84750e3a4a5bbad3cd4af9ed5101",
  316. // "action": 1, 0-开始
  317. //1-停止
  318. //注:GOTO_PRESET命令下填任意值均可转到预置点,建议填0即可
  319. // "command": "GOTO_PRESET",
  320. // "speed": 4,
  321. // "presetIndex": 20
  322. //}
  323. func (h *HikvisionHandler) Gimbalcontrol(ctx *gin.Context) {
  324. m := make(map[string]string)
  325. cameraIndexCode := ctx.Query("cameraIndexCode")
  326. command := ctx.Query("command")
  327. action := ctx.Query("action")
  328. speed := ctx.Query("speed")
  329. presetIndex := ctx.Query("presetIndex")
  330. m["cameraIndexCode"] = cameraIndexCode
  331. m["action"] = action
  332. m["command"] = command
  333. m["speed"] = speed
  334. m["presetIndex"] = presetIndex
  335. if len(cameraIndexCode) <= 0 || len(command) <= 0 || len(action) <= 0 {
  336. resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
  337. return
  338. }
  339. resp.HandleSuccess(ctx, "操作成功")
  340. return
  341. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.controlling"), m, 15)
  342. if err != nil {
  343. h.logger.Error("控制云台失败")
  344. resp.HandleError(ctx, 1201, "控制云台失败", err)
  345. return
  346. }
  347. if hikvision.Code != "0" {
  348. atoi, _ := strconv.Atoi(hikvision.Code)
  349. resp.HandleError(ctx, atoi, hikvision.Msg, nil)
  350. return
  351. }
  352. resp.HandleSuccess(ctx, hikvision.Msg)
  353. }
  354. // VisitorInfoCount 获取今日访客信息包含:今日来访总人数(已签离人数,未签离人数),预约人数
  355. func (h *HikvisionHandler) VisitorInfoCount(c *gin.Context) {
  356. m := make(map[string]string)
  357. parkId := c.Query("parkId")
  358. m["parkId"] = parkId
  359. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.visitorInfo"), m, 15)
  360. if err != nil {
  361. h.logger.Error("获取访客信息失败")
  362. resp.HandleError(c, 1201, "获取访客信息失败", err)
  363. return
  364. }
  365. if hikvision.Code != "0" {
  366. atoi, _ := strconv.Atoi(hikvision.Code)
  367. resp.HandleError(c, atoi, hikvision.Msg, nil)
  368. return
  369. }
  370. resp.HandleSuccess(c, hikvision.Data)
  371. }
  372. // GetDoorSearch 查询门禁点列表v2
  373. func (h *HikvisionHandler) GetDoorSearch(ctx *gin.Context) {
  374. // 设置响应头
  375. ctx.Header("Content-Type", "text/event-stream")
  376. ctx.Header("Cache-Control", "no-cache")
  377. ctx.Header("Connection", "keep-alive")
  378. // 监听客户端断开连接
  379. conn := true
  380. notify := ctx.Writer.CloseNotify()
  381. var response model.Response
  382. //var doorlist []model.DoorList
  383. //var doorResp model.DoorResp
  384. m := make(map[string]string)
  385. m["pageNo"] = "1"
  386. m["pageSize"] = "1000"
  387. for conn {
  388. select {
  389. case <-notify:
  390. conn = false
  391. fmt.Println("断开连接")
  392. return
  393. default:
  394. doorSearch, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.doorSearch"), m, 15)
  395. if err != nil {
  396. h.logger.Error("获取门禁点列表失败")
  397. response.Code = 1203
  398. response.Msg = "获取门禁点列表失败"
  399. response.Data = nil
  400. res, _ := json.Marshal(&response)
  401. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  402. ctx.Writer.Flush()
  403. return
  404. }
  405. if doorSearch.Code != "0" {
  406. response.Code = 1203
  407. response.Msg = "获取门禁点列表失败"
  408. response.Data = nil
  409. res, _ := json.Marshal(&response)
  410. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  411. ctx.Writer.Flush()
  412. conn = false
  413. return
  414. }
  415. // 获取门禁状态
  416. //doorIndexCodes := []string{""}
  417. //for _, v := range doorResp.Data.List {
  418. // doorIndexCodes = append(doorIndexCodes, v.IndexCode)
  419. //}
  420. //data := map[string]string{
  421. // "doorIndexCodes": strings.Join(doorIndexCodes, ","),
  422. //}
  423. //doorStates, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.doorStates"), data, 15)
  424. //if err != nil {
  425. // h.logger.Error("获取门禁状态失败")
  426. // response.Code = 1203
  427. // response.Msg = "获取门禁状态失败"
  428. // response.Data = nil
  429. // res, _ := json.Marshal(&response)
  430. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  431. // ctx.Writer.Flush()
  432. // conn = false
  433. // return
  434. //}
  435. //if doorStates.Code != "0" {
  436. // response.Code = 1203
  437. // response.Msg = "获取门禁状态失败"
  438. // response.Data = nil
  439. // res, _ := json.Marshal(&response)
  440. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  441. // ctx.Writer.Flush()
  442. // conn = false
  443. // return
  444. //}
  445. //
  446. //for i := 0; i < 3; i++ {
  447. // genUUID := uuid.GenUUID()
  448. // name := fmt.Sprintf("资源:%v", i+1)
  449. //
  450. // list := model.DoorList{
  451. // IndexCode: genUUID,
  452. // ResourceType: "door",
  453. // Name: name,
  454. // DoorNo: genUUID,
  455. // ChannelNo: genUUID,
  456. // ParentIndexCode: genUUID,
  457. // ControlOneId: genUUID,
  458. // ControlTwoId: genUUID,
  459. // ReaderInId: genUUID,
  460. // ReaderOutId: genUUID,
  461. // DoorSerial: i + 1,
  462. // TreatyType: genUUID,
  463. // RegionIndexCode: genUUID,
  464. // RegionPath: genUUID,
  465. // CreateTime: time.Now().Format("2006-01-02 15:04:05"),
  466. // UpdateTime: time.Now().Format("2006-01-02 15:04:05"),
  467. // Description: genUUID,
  468. // ChannelType: genUUID,
  469. // RegionName: genUUID,
  470. // RegionPathName: genUUID,
  471. // InstallLocation: genUUID,
  472. // }
  473. // doorlist = append(doorlist, list)
  474. //}
  475. //doorResp := model.DoorResp{
  476. // Code: "0",
  477. // Msg: "SUCCESS",
  478. // Data: struct {
  479. // Total int `json:"total"`
  480. // PageNo int `json:"pageNo"`
  481. // PageSize int `json:"pageSize"`
  482. // List []model.DoorList `json:"list"`
  483. // }{Total: 3, PageNo: 1, PageSize: 1, List: doorlist},
  484. //}
  485. response.Code = 200
  486. response.Msg = "获取门禁点列表成功"
  487. response.Data = doorSearch.Data
  488. res, _ := json.Marshal(&response)
  489. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  490. ctx.Writer.Flush()
  491. time.Sleep(10 * time.Second)
  492. }
  493. }
  494. }
  495. // DoControl 控制门禁
  496. func (h *HikvisionHandler) DoControl(ctx *gin.Context) {
  497. // 设置响应头
  498. ctx.Header("Content-Type", "text/event-stream")
  499. ctx.Header("Cache-Control", "no-cache")
  500. ctx.Header("Connection", "keep-alive")
  501. // 监听客户端断开连接
  502. conn := true
  503. notify := ctx.Writer.CloseNotify()
  504. doorIndexCodes := ctx.Query("doorIndexCodes")
  505. controlType := ctx.Query("controlType")
  506. if len(doorIndexCodes) <= 0 || len(controlType) <= 0 {
  507. resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
  508. return
  509. }
  510. fmt.Println("doorIndexCodes:", doorIndexCodes)
  511. m := make(map[string]string)
  512. m["doorIndexCodes"] = doorIndexCodes
  513. m["controlType"] = controlType
  514. var response model.Response
  515. for conn {
  516. select {
  517. case <-notify:
  518. conn = false
  519. fmt.Println("断开连接")
  520. return
  521. default:
  522. //hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.control"), m, 15)
  523. //if err != nil {
  524. // h.logger.Error("控制门禁失败")
  525. // response.Code = 1203
  526. // response.Msg = "控制门禁失败"
  527. // response.Data = nil
  528. // res, _ := json.Marshal(&response)
  529. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  530. // ctx.Writer.Flush()
  531. // conn = false
  532. // return
  533. //}
  534. //if hikvision.Code != "0" {
  535. // response.Code = 1203
  536. // response.Msg = "控制门禁失败"
  537. // response.Data = nil
  538. // res, _ := json.Marshal(&response)
  539. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  540. // ctx.Writer.Flush()
  541. // conn = false
  542. // return
  543. //}
  544. genUUID := uuid.GenUUID()
  545. doControl := model.DoControl{
  546. Code: "0",
  547. Msg: "SUCCESS",
  548. Data: []model.DoControlData{{
  549. DoorIndexCode: genUUID,
  550. ControlResultCode: 0,
  551. ControlResultDesc: "success",
  552. }},
  553. }
  554. response.Code = 200
  555. response.Msg = "控制门禁成功"
  556. response.Data = doControl.Data
  557. res, _ := json.Marshal(&response)
  558. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  559. ctx.Writer.Flush()
  560. time.Sleep(10 * time.Second)
  561. }
  562. }
  563. }
  564. // RealTimeInspection 入侵报警事件日志查询
  565. func (h *HikvisionHandler) RealTimeInspection(ctx *gin.Context) {
  566. m := make(map[string]string)
  567. m["startTime"] = ctx.Query("startTime") //开始时间
  568. m["endTime"] = ctx.Query("endTime") //结束时间
  569. m["pageNo"] = ctx.Query("pageNo")
  570. m["pageSize"] = h.conf.GetString("hikvision.pageSize")
  571. var eventLogs model.EventLogs
  572. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.eventLogs"), m, 15)
  573. if err != nil {
  574. h.logger.Error("入侵报警事件日志查询失败")
  575. resp.HandleError(ctx, 1203, "入侵报警事件日志查询失败", err)
  576. return
  577. }
  578. if hikvision.Code != "0" {
  579. h.logger.Error("入侵报警事件日志查询失败")
  580. resp.HandleError(ctx, 1203, "入侵报警事件日志查询失败", hikvision.Code)
  581. return
  582. }
  583. marshal, err := json.Marshal(hikvision)
  584. if err != nil {
  585. h.logger.Error("json序列化失败")
  586. resp.HandleError(ctx, 1203, "json序列化失败", err)
  587. return
  588. }
  589. err = json.Unmarshal(marshal, &eventLogs)
  590. if err != nil {
  591. h.logger.Error("json反序列化失败")
  592. resp.HandleError(ctx, 1203, "json反序列化失败", err)
  593. return
  594. }
  595. for i, _ := range eventLogs.Data.List {
  596. eventLogs.Data.List[i].SrcType = model.ResourceType[eventLogs.Data.List[i].SrcType]
  597. }
  598. resp.HandleSuccess(ctx, hikvision.Data)
  599. }