hikvision.go 23 KB

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