hikvision.go 20 KB

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