hikvision.go 28 KB

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