hikvision.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. doorIndexCodes := ctx.Query("doorIndexCodes")
  431. err, result := h.hikvisionService.GetDoorSearch(pageNo, pageSize, name, []string{doorIndexCodes})
  432. if err != nil {
  433. h.logger.Error("获取门禁设备列表失败")
  434. resp.HandleError(ctx, 1201, "获取门禁设备列表失败", err)
  435. return
  436. }
  437. marshal, err := json.Marshal(result)
  438. if err != nil {
  439. h.logger.Error("json序列化失败")
  440. resp.HandleError(ctx, 1202, "json序列化失败", nil)
  441. return
  442. }
  443. err = json.Unmarshal(marshal, &Acs)
  444. if err != nil {
  445. h.logger.Error("json反序列化失败")
  446. resp.HandleError(ctx, 1203, "json反序列化失败", nil)
  447. return
  448. }
  449. for i, _ := range Acs.Data.List {
  450. err, h2 := h.hikvisionService.GetAcsDoorStates([]string{Acs.Data.List[i].IndexCode})
  451. if err != nil {
  452. h.logger.Error("获取门禁设备在线状态失败")
  453. resp.HandleError(ctx, 1201, "获取门禁设备在线状态失败", err)
  454. return
  455. }
  456. bytes, err := json.Marshal(h2)
  457. if err != nil {
  458. h.logger.Error("json序列化失败")
  459. resp.HandleError(ctx, 1202, "json序列化失败", nil)
  460. return
  461. }
  462. err = json.Unmarshal(bytes, &AcsDevice)
  463. if err != nil {
  464. h.logger.Error("json反序列化失败")
  465. resp.HandleError(ctx, 1203, "json反序列化失败", nil)
  466. return
  467. }
  468. Acs.Data.List[i].State = AcsDevice.Data.AuthDoorList[0].DoorState
  469. }
  470. resp.HandleSuccess(ctx, Acs.Data)
  471. }
  472. // GetDoorStates 获取门禁设备在线状态
  473. func (h *HikvisionHandler) GetDoorStates(ctx *gin.Context) {
  474. // 设置响应头
  475. ctx.Header("Content-Type", "text/event-stream")
  476. ctx.Header("Cache-Control", "no-cache")
  477. ctx.Header("Connection", "keep-alive")
  478. // 监听客户端断开连接
  479. conn := true
  480. notify := ctx.Writer.CloseNotify()
  481. var response model.Response
  482. var AcsDevice model.AcsDevice
  483. indexCodes := ctx.Query("indexCodes")
  484. pageNo := ctx.Query("pageNo")
  485. pageSize := ctx.Query("pageSize")
  486. for conn {
  487. select {
  488. case <-notify:
  489. conn = false
  490. fmt.Println("断开连接")
  491. return
  492. default:
  493. err, h2 := h.hikvisionService.GetDoorStates(pageNo, pageSize, []string{indexCodes})
  494. if err != nil {
  495. h.logger.Error("获取门禁设备在线状态失败")
  496. response.Code = 1203
  497. response.Msg = "获取门禁设备在线状态失败"
  498. response.Data = nil
  499. res, _ := json.Marshal(&response)
  500. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  501. ctx.Writer.Flush()
  502. conn = false
  503. return
  504. }
  505. bytes, err := json.Marshal(h2)
  506. if err != nil {
  507. h.logger.Error("json序列化失败")
  508. response.Code = 1203
  509. response.Msg = "json序列化失败"
  510. response.Data = nil
  511. res, _ := json.Marshal(&response)
  512. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  513. }
  514. err = json.Unmarshal(bytes, &AcsDevice)
  515. if err != nil {
  516. h.logger.Error("json反序列化失败")
  517. response.Code = 1203
  518. response.Msg = "json反序列化失败"
  519. response.Data = nil
  520. res, _ := json.Marshal(&response)
  521. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  522. conn = false
  523. }
  524. response.Code = 200
  525. response.Msg = "获取门禁点列表成功"
  526. response.Data = AcsDevice.Data
  527. res, _ := json.Marshal(&response)
  528. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  529. ctx.Writer.Flush()
  530. time.Sleep(10 * time.Second)
  531. }
  532. }
  533. }
  534. // DoControl 控制门禁
  535. func (h *HikvisionHandler) DoControl(ctx *gin.Context) {
  536. doorIndexCodes := ctx.Query("doorIndexCodes")
  537. controlType := ctx.Query("controlType")
  538. if len(doorIndexCodes) <= 0 || len(controlType) <= 0 {
  539. resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
  540. return
  541. }
  542. fmt.Println("doorIndexCodes:", doorIndexCodes)
  543. m := make(map[string]string)
  544. m["doorIndexCodes"] = doorIndexCodes
  545. m["controlType"] = controlType
  546. var response model.Response
  547. genUUID := uuid.GenUUID()
  548. doControl := model.DoControl{
  549. Code: "0",
  550. Msg: "SUCCESS",
  551. Data: []model.DoControlData{{
  552. DoorIndexCode: genUUID,
  553. ControlResultCode: 0,
  554. ControlResultDesc: "success",
  555. }},
  556. }
  557. response.Code = 200
  558. response.Msg = "控制门禁成功"
  559. response.Data = doControl.Data
  560. res, _ := json.Marshal(&response)
  561. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  562. ctx.Writer.Flush()
  563. time.Sleep(10 * time.Second)
  564. }
  565. // RealTimeInspection 入侵报警事件日志查询
  566. func (h *HikvisionHandler) RealTimeInspection(ctx *gin.Context) {
  567. pageNo := ctx.Query("pageNo")
  568. pageSize := ctx.Query("pageSize")
  569. srcType := ctx.Query("srcType")
  570. startTime := ctx.Query("startTime")
  571. endTime := ctx.Query("endTime")
  572. srcName := ctx.Query("srcName")
  573. eventType := ctx.Query("eventType")
  574. eventTypeInt, _ := strconv.Atoi(eventType)
  575. var eventLogs model.EventLogs
  576. err, result := h.hikvisionService.GetEventLogs(pageNo, pageSize, srcType, startTime, endTime, srcName, eventTypeInt)
  577. if err != nil {
  578. resp.HandleError(ctx, 1203, "获取入侵报警事件日志查询失败", err)
  579. return
  580. }
  581. marshal, err := json.Marshal(result)
  582. if err != nil {
  583. h.logger.Error("json序列化失败")
  584. resp.HandleError(ctx, 1203, "json序列化失败", err)
  585. return
  586. }
  587. err = json.Unmarshal(marshal, &eventLogs)
  588. if err != nil {
  589. h.logger.Error("json反序列化失败")
  590. resp.HandleError(ctx, 1203, "json反序列化失败", err)
  591. return
  592. }
  593. for i, _ := range eventLogs.Data.List {
  594. eventLogs.Data.List[i].SrcType = model.ResourceType[eventLogs.Data.List[i].SrcType]
  595. //eventLogs.Data.List[i].EventType = model.IntrusionAlarmEventType[eventLogs.Data.List[i].EventType]
  596. }
  597. resp.HandleSuccess(ctx, eventLogs.Data)
  598. }
  599. // GetCameraSearch 获取摄像头查询
  600. func (h *HikvisionHandler) GetCameraSearch(ctx *gin.Context) {
  601. pageNo := ctx.Query("pageNo")
  602. pageSize := ctx.Query("pageSize")
  603. name := ctx.Query("name")
  604. err, search := h.hikvisionService.GetcameraSearch(pageNo, pageSize, name)
  605. if err != nil {
  606. resp.HandleError(ctx, 1203, "获取摄像头查询失败", err)
  607. return
  608. }
  609. var cameraSearch model.CameraSearch
  610. marshal, err := json.Marshal(search)
  611. if err != nil {
  612. h.logger.Error("json序列化失败")
  613. resp.HandleError(ctx, 1203, "json序列化失败", err)
  614. return
  615. }
  616. err = json.Unmarshal(marshal, &cameraSearch)
  617. if err != nil {
  618. h.logger.Error("json反序列化失败")
  619. resp.HandleError(ctx, 1203, "json反序列化失败", err)
  620. return
  621. }
  622. for i, _ := range cameraSearch.Data.List {
  623. err, result := h.hikvisionService.GetManualCapture(cameraSearch.Data.List[i].IndexCode)
  624. if err != nil {
  625. cameraSearch.Data.List[i].Url = ""
  626. } else {
  627. var manualCapture model.ManualCapture
  628. marshal, err := json.Marshal(result)
  629. if err != nil {
  630. h.logger.Error("json序列化失败")
  631. resp.HandleError(ctx, 1203, "json序列化失败", err)
  632. return
  633. }
  634. err = json.Unmarshal(marshal, &manualCapture)
  635. if err != nil {
  636. h.logger.Error("json反序列化失败")
  637. resp.HandleError(ctx, 1203, "json反序列化失败", err)
  638. return
  639. }
  640. cameraSearch.Data.List[i].Url = manualCapture.Data.PicUrl
  641. }
  642. }
  643. resp.HandleSuccess(ctx, cameraSearch.Data)
  644. }
  645. // GetCameraList 获取监控点资源
  646. func (h *HikvisionHandler) GetCameraList(ctx *gin.Context) {
  647. pageNo := ctx.Query("pageNo")
  648. pageSize := ctx.Query("pageSize")
  649. err, search := h.hikvisionService.GetCamerasList(pageNo, pageSize)
  650. if err != nil {
  651. resp.HandleError(ctx, 1203, "获取监控点资源失败", err)
  652. return
  653. }
  654. resp.HandleSuccess(ctx, search.Data)
  655. }
  656. // GetAcsDeviceSearch 获取门禁设备查询
  657. func (h *HikvisionHandler) GetAcsDeviceSearch(ctx *gin.Context) {
  658. pageNo := ctx.Query("pageNo")
  659. pageSize := ctx.Query("pageSize")
  660. name := ctx.Query("name")
  661. err, search := h.hikvisionService.GetAcsDeviceSearch(pageNo, pageSize, name)
  662. if err != nil {
  663. resp.HandleError(ctx, 1203, "获取门禁设备列表失败", err)
  664. return
  665. }
  666. resp.HandleSuccess(ctx, search.Data)
  667. }
  668. // GetDoorDoControl 控制门禁点
  669. func (h *HikvisionHandler) GetDoorDoControl(ctx *gin.Context) {
  670. doorIndexCodes := ctx.QueryArray("doorIndexCodes")
  671. controlType := ctx.Query("controlType")
  672. atoi, err2 := strconv.Atoi(controlType)
  673. if err2 != nil {
  674. resp.HandleError(ctx, 1203, "参数错误", err2)
  675. return
  676. }
  677. if len(doorIndexCodes) <= 0 || len(doorIndexCodes) > 10 {
  678. resp.HandleError(ctx, 1203, "参数错误", nil)
  679. return
  680. }
  681. err, search := h.hikvisionService.GetDoorDoControl(doorIndexCodes, atoi)
  682. if err != nil {
  683. resp.HandleError(ctx, 1203, "控制门禁失败", err.Error())
  684. return
  685. }
  686. resp.HandleSuccess(ctx, search.Data)
  687. }
  688. // GetAcsDoorStates GetDoorDoControl 门禁点状态
  689. func (h *HikvisionHandler) GetAcsDoorStates(ctx *gin.Context) {
  690. // 设置响应头
  691. ctx.Header("Content-Type", "text/event-stream")
  692. ctx.Header("Cache-Control", "no-cache")
  693. ctx.Header("Connection", "keep-alive")
  694. // 监听客户端断开连接
  695. conn := true
  696. notify := ctx.Writer.CloseNotify()
  697. var response model.Response
  698. var Acs model.DoorSearch
  699. var AcsDevice model.AcsDoorStates
  700. var acsDoor model.AcsDoor
  701. doorIndexCodes := ctx.Query("doorIndexCodes")
  702. if doorIndexCodes == "" {
  703. resp.HandleError(ctx, 1203, "参数错误", nil)
  704. return
  705. }
  706. for conn {
  707. select {
  708. case <-notify:
  709. conn = false
  710. fmt.Println("断开连接")
  711. return
  712. default:
  713. err, search := h.hikvisionService.GetAcsDoorStates([]string{doorIndexCodes})
  714. if err != nil {
  715. h.logger.Error("获取门禁设备在线状态失败")
  716. resp.HandleError(ctx, 1201, "获取门禁设备在线状态失败", err)
  717. return
  718. }
  719. bytes, err := json.Marshal(search)
  720. if err != nil {
  721. h.logger.Error("json序列化失败")
  722. resp.HandleError(ctx, 1202, "json序列化失败", nil)
  723. return
  724. }
  725. err = json.Unmarshal(bytes, &AcsDevice)
  726. if err != nil {
  727. h.logger.Error("json反序列化失败")
  728. resp.HandleError(ctx, 1203, "json反序列化失败", nil)
  729. return
  730. }
  731. err, result := h.hikvisionService.GetDoorSearch("1", "1", "", []string{AcsDevice.Data.AuthDoorList[0].DoorIndexCode})
  732. if err != nil {
  733. h.logger.Error("获取门禁点信息失败")
  734. resp.HandleError(ctx, 1201, "获取门禁点信息失败", err)
  735. return
  736. }
  737. bytes, err = json.Marshal(result)
  738. if err != nil {
  739. h.logger.Error("json序列化失败")
  740. resp.HandleError(ctx, 1202, "json序列化失败", nil)
  741. return
  742. }
  743. err = json.Unmarshal(bytes, &Acs)
  744. if err != nil {
  745. h.logger.Error("json反序列化失败")
  746. resp.HandleError(ctx, 1203, "json反序列化失败", nil)
  747. return
  748. }
  749. for i, _ := range Acs.Data.List {
  750. Acs.Data.List[i].State = AcsDevice.Data.AuthDoorList[0].DoorState
  751. acsDoor.DoorNo = Acs.Data.List[i].DoorNo
  752. acsDoor.DoorSerial = Acs.Data.List[i].DoorSerial
  753. acsDoor.IndexCode = Acs.Data.List[i].IndexCode
  754. acsDoor.State = AcsDevice.Data.AuthDoorList[0].DoorState
  755. if AcsDevice.Data.AuthDoorList[0].DoorState > 3 {
  756. acsDoor.StateName = "故障"
  757. acsDoor.Color = 16711680
  758. } else {
  759. acsDoor.StateName = model.StateName[AcsDevice.Data.AuthDoorList[0].DoorState]
  760. acsDoor.Color = model.Color[AcsDevice.Data.AuthDoorList[0].DoorState]
  761. }
  762. acsDoor.CreateTime = Acs.Data.List[i].CreateTime
  763. acsDoor.UpdateTime = Acs.Data.List[i].UpdateTime
  764. acsDoor.Name = Acs.Data.List[i].Name
  765. acsDoor.RegionIndexCode = Acs.Data.List[i].RegionIndexCode
  766. acsDoor.RegionName = Acs.Data.List[i].RegionName
  767. acsDoor.RegionPath = Acs.Data.List[i].RegionPath
  768. acsDoor.RegionPathName = Acs.Data.List[i].RegionPathName
  769. acsDoor.ResourceType = Acs.Data.List[i].ResourceType
  770. acsDoor.TreatyType = Acs.Data.List[i].TreatyType
  771. acsDoor.Description = Acs.Data.List[i].Description
  772. acsDoor.InstallLocation = Acs.Data.List[i].InstallLocation
  773. acsDoor.ParentIndexCode = Acs.Data.List[i].ParentIndexCode
  774. acsDoor.ReaderInId = Acs.Data.List[i].ReaderInId
  775. acsDoor.ReaderOutId = Acs.Data.List[i].ReaderOutId
  776. acsDoor.ChannelNo = Acs.Data.List[i].ChannelNo
  777. acsDoor.ControlOneId = Acs.Data.List[i].ControlOneId
  778. acsDoor.ControlTwoId = Acs.Data.List[i].ControlTwoId
  779. acsDoor.ChannelType = Acs.Data.List[i].ChannelType
  780. }
  781. response.Code = 200
  782. response.Msg = "获取门禁点列表成功"
  783. response.Data = acsDoor
  784. res, _ := json.Marshal(&response)
  785. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  786. ctx.Writer.Flush()
  787. time.Sleep(10 * time.Second)
  788. }
  789. }
  790. }