hikvision.go 20 KB

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