hikvision.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package handler
  2. import (
  3. "city_chips/internal/model"
  4. "city_chips/internal/service"
  5. "city_chips/pkg/helper/resp"
  6. "encoding/json"
  7. "fmt"
  8. "math/rand"
  9. "strconv"
  10. "time"
  11. "github.com/gin-gonic/gin"
  12. "github.com/spf13/viper"
  13. "github.com/tidwall/gjson"
  14. )
  15. type HikvisionHandler struct {
  16. *Handler
  17. hikvisionService service.HikvisionService
  18. conf *viper.Viper
  19. }
  20. func NewHikvisionHandler(handler *Handler, hikvisionService service.HikvisionService, conf *viper.Viper) *HikvisionHandler {
  21. return &HikvisionHandler{
  22. Handler: handler,
  23. hikvisionService: hikvisionService,
  24. conf: conf,
  25. }
  26. }
  27. // GetMonitor 获取视频监控
  28. func (h *HikvisionHandler) GetMonitor(ctx *gin.Context) {
  29. m := make(map[string]any)
  30. LicensePlateRecognition := make(map[string]any)
  31. Blacklist := make(map[string]any)
  32. var monitor []model.Monitor
  33. for i := 0; i < 7; i++ {
  34. name := fmt.Sprintf("周%v", i+1)
  35. LicensePlateRecognition[name] = rand.Intn(100)
  36. Blacklist[name] = rand.Intn(100)
  37. }
  38. for i := 0; i < 10; i++ {
  39. name := fmt.Sprintf("监控设备%v", i+1)
  40. m2 := model.Monitor{
  41. Id: i + 1,
  42. Name: name,
  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. name := fmt.Sprintf("设备%v", i+1)
  72. inspection := model.Device{
  73. Id: i + 1,
  74. Name: name,
  75. State: rand.Intn(2),
  76. Date: time.Now().Format("2006-01-02 15:04:05"),
  77. }
  78. realTime = append(realTime, inspection)
  79. }
  80. m["DeviceAlerts"] = rand.Intn(100) //设备预警
  81. m["LowRiskWarning"] = rand.Intn(100) //低危预警
  82. m["MediumRiskWarning"] = rand.Intn(100) //中危预警
  83. m["HighRiskWarning"] = rand.Intn(1000) //高危预警
  84. m["HighRiskWarning"] = rand.Intn(1000) //高危预警
  85. m["OverHazardWarning"] = rand.Intn(1000) //超危预警
  86. m["EventRisk"] = rand.Intn(100) //事件风险
  87. m["AlarmRisk"] = rand.Intn(100) //告警风险
  88. m["trend"] = rand.Intn(100) //圆环中间百分比
  89. m["realTime"] = realTime //实时巡检
  90. m["AlarmTrend24Hour"] = AlarmTrend24Hour //24小时告警趋势
  91. resp.HandleSuccess(ctx, m)
  92. }
  93. // GetElectronicInspections 电子巡查
  94. func (h *HikvisionHandler) GetElectronicInspections(ctx *gin.Context) {
  95. m := make(map[string]any)
  96. Inspect := make(map[string]any)
  97. var device []model.Device
  98. var realTime []model.RealTimeInspection
  99. for i := 0; i < 24; i++ {
  100. name := fmt.Sprintf("%v时", i+1)
  101. Inspect[name] = rand.Intn(100)
  102. }
  103. for i := 0; i < 10; i++ {
  104. name := fmt.Sprintf("设备%v", i+1)
  105. m2 := model.Device{
  106. Id: i + 1,
  107. Name: name,
  108. State: rand.Intn(2),
  109. Date: time.Now().Format("2006-01-02 15:04:05"),
  110. }
  111. device = append(device, m2)
  112. }
  113. for i := 0; i < 10; i++ {
  114. name := fmt.Sprintf("巡检事件%v", i+1)
  115. Location := fmt.Sprintf("位置%v", i+1)
  116. inspection := model.RealTimeInspection{
  117. Id: i + 1,
  118. Name: name,
  119. Location: Location,
  120. Event: name,
  121. }
  122. realTime = append(realTime, inspection)
  123. }
  124. m["TodayTotal"] = rand.Intn(100) //今日总数
  125. m["InspectionPoints"] = rand.Intn(100) //巡检点数
  126. m["InspectionPlan"] = rand.Intn(100) //巡检计划
  127. m["InspectTheLine"] = rand.Intn(1000) //巡检线路
  128. m["Inspect"] = Inspect //巡检统计
  129. m["realTime"] = realTime //实时巡检
  130. m["DeviceList"] = device //设备列表
  131. resp.HandleSuccess(ctx, m)
  132. }
  133. // 访客系统
  134. func (h *HikvisionHandler) GetVisitor(ctx *gin.Context) {
  135. m := make(map[string]any)
  136. PrevailingTrends := make(map[string]any)
  137. var visitor []model.Visitor
  138. for i := 0; i < 24; i++ {
  139. name := fmt.Sprintf("人员%v", i+1)
  140. address := fmt.Sprintf("%v时", i+1)
  141. m2 := model.Visitor{
  142. Id: i + 1,
  143. Name: name,
  144. State: rand.Intn(2),
  145. Phone: strconv.Itoa(rand.Intn(1000000000)),
  146. Location: address,
  147. Date: time.Now().Format("2006-01-02 15:04:05"),
  148. }
  149. PrevailingTrends[address] = rand.Intn(100)
  150. visitor = append(visitor, m2)
  151. }
  152. m["VisitorCount"] = rand.Intn(1000) //访客总量
  153. m["SignInCount"] = rand.Intn(1000) //签到数量
  154. m["TransitCount"] = rand.Intn(1000) //通行数量
  155. m["Exit"] = rand.Intn(1000) //离场数量
  156. m["Client"] = rand.Intn(100) //客户
  157. m["Vendor"] = rand.Intn(100) //供应商
  158. m["Interviewees"] = rand.Intn(100) //面试者
  159. m["GovernmentPersonnel"] = rand.Intn(100) //政府人员
  160. m["Other"] = rand.Intn(100) //其他
  161. m["PrevailingTrends"] = PrevailingTrends //通行趋势
  162. m["VisitorRegistration"] = visitor //访客登记
  163. m["VisitorsSwipeCards"] = visitor //访客刷卡记录
  164. resp.HandleSuccess(ctx, m)
  165. }
  166. // 客流统计
  167. func (h *HikvisionHandler) GetPassenger(ctx *gin.Context) {
  168. m := make(map[string]any)
  169. Type1 := make(map[string]any)
  170. Type2 := make(map[string]any)
  171. Type3 := make(map[string]any)
  172. Type4 := make(map[string]any)
  173. rankings := make(map[string]any)
  174. customers := make(map[string]any)
  175. var event []model.RealTimeInspection
  176. for i := 0; i < 7; i++ {
  177. sprintf := fmt.Sprintf("2025-5-%v", i+1)
  178. ranking := fmt.Sprintf("类型-%v", i+1)
  179. rankings[ranking] = rand.Intn(100)
  180. Type1[sprintf] = rand.Intn(100)
  181. Type2[sprintf] = rand.Intn(100)
  182. Type3[sprintf] = rand.Intn(100)
  183. Type4[sprintf] = rand.Intn(100)
  184. }
  185. for i := 0; i < 3; i++ {
  186. sprintf := fmt.Sprintf("客群-%v", i+1)
  187. customers[sprintf] = rand.Intn(100)
  188. }
  189. for i := 0; i < 10; i++ {
  190. sprintf := fmt.Sprintf("安全事件-%v", i+1)
  191. location := fmt.Sprintf("位置-%v", i+1)
  192. inspection := model.RealTimeInspection{
  193. Id: i + 1,
  194. Name: sprintf,
  195. Location: location,
  196. Event: time.Now().Format("2006-01-02 15:04:05"),
  197. }
  198. event = append(event, inspection)
  199. }
  200. m["SecurityLevel"] = rand.Intn(500) //安全等级
  201. m["NetworkEquipment"] = rand.Intn(100) //网络设备
  202. m["Normal"] = rand.Intn(100) //正常
  203. m["Fault"] = rand.Intn(100) //故障
  204. m["Offline"] = rand.Intn(100) //离线
  205. m["Type1"] = Type1 //客流监控type1
  206. m["Type2"] = Type2 //客流监控type2
  207. m["Type3"] = Type3 //客流监控type3
  208. m["Type4"] = Type4 //客流监控type4
  209. m["Rankings"] = rankings //指标区排行榜
  210. m["Customers"] = customers //客群分析统计
  211. m["Customers"] = customers //客群分析统计
  212. m["Event"] = event //安全事件列表
  213. resp.HandleSuccess(ctx, m)
  214. }
  215. // 门禁系统
  216. func (h *HikvisionHandler) GetAccess(ctx *gin.Context) {
  217. m := make(map[string]any)
  218. invasio1 := make(map[string]any)
  219. invasio2 := make(map[string]any)
  220. DailyTotal := make(map[string]any)
  221. Cumulative := make(map[string]any)
  222. var devices []model.RealTimeInspection
  223. var alarmList []model.AlarmList
  224. for i := 0; i < 7; i++ {
  225. sprintf := fmt.Sprintf("2025-5-%v", i+1)
  226. invasio1[sprintf] = rand.Intn(100)
  227. invasio2[sprintf] = rand.Intn(100)
  228. }
  229. for i := 0; i < 24; i++ {
  230. sprintf := fmt.Sprintf("%v时", i+1)
  231. DailyTotal[sprintf] = rand.Intn(100)
  232. Cumulative[sprintf] = rand.Intn(100)
  233. }
  234. for i := 0; i < 10; i++ {
  235. sprintf := fmt.Sprintf("设备-%v", i+1)
  236. location := fmt.Sprintf("位置-%v", i+1)
  237. inspection := model.RealTimeInspection{
  238. Id: i + 1,
  239. Name: sprintf,
  240. Location: location,
  241. Event: time.Now().Format("2006-01-02 15:04:05"),
  242. }
  243. alarm := model.AlarmList{
  244. Id: i + 1,
  245. AlarmContent: fmt.Sprintf("设备-%v", i+1),
  246. Location: location,
  247. State: rand.Intn(2),
  248. Date: time.Now().Format("2006-01-02 15:04:05"),
  249. }
  250. devices = append(devices, inspection)
  251. alarmList = append(alarmList, alarm)
  252. }
  253. m["DeviceCount"] = rand.Intn(500) //设备总数
  254. m["Online"] = rand.Intn(100) //在线
  255. m["Abnormal"] = rand.Intn(100) //异常
  256. m["Fault"] = rand.Intn(100) //故障
  257. m["Offline"] = rand.Intn(100) //离线
  258. m["Attendance"] = rand.Intn(100) //出勤率
  259. m["Invasio1"] = invasio1 //入侵事件1
  260. m["Invasio2"] = invasio2 //入侵事件2
  261. m["DailyTotal"] = DailyTotal //每日统计
  262. m["Cumulative"] = Cumulative //累计统计
  263. m["AlarmList"] = alarmList //实时告警与通知
  264. m["devices"] = devices //设备列表
  265. resp.HandleSuccess(ctx, m)
  266. }
  267. // GetHikvisionMonitoring 获取视频监控流{
  268. // "cameraIndexCode": "748d84750e3a4a5bbad3cd4af9ed5101",
  269. // "streamType": 0,
  270. // "protocol": "rtsp",
  271. // "transmode": 1,
  272. // "expand": "transcode=0",
  273. // "streamform": "ps"
  274. // }
  275. func (h *HikvisionHandler) GetHikvisionMonitoring(ctx *gin.Context) {
  276. m := make(map[string]string)
  277. cameraIndexCode := ctx.Query("cameraIndexCode")
  278. m["cameraIndexCode"] = cameraIndexCode
  279. m["streamType"] = "0"
  280. m["protocol"] = "rtsp"
  281. m["transmode"] = "1"
  282. m["expand"] = "transcode=0"
  283. m["streamform"] = "ps"
  284. if len(cameraIndexCode) <= 0 || cameraIndexCode == "" {
  285. resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
  286. return
  287. }
  288. fmt.Println(m)
  289. resp.HandleSuccess(ctx, "rtsp://10.2.145.66:655/EUrl/CLJ52BW")
  290. return
  291. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.cameras"), m, 15)
  292. //返回结果{
  293. // "code": "0",
  294. // "msg": "success",
  295. // "data": {
  296. // "url": "rtsp://10.2.145.66:655/EUrl/CLJ52BW"
  297. // }
  298. //}
  299. if err != nil {
  300. h.logger.Error("获取获取监控点资源失败")
  301. resp.HandleError(ctx, 1201, "获取获取监控点资源失败", err)
  302. return
  303. }
  304. if hikvision.Code != "0" {
  305. atoi, _ := strconv.Atoi(hikvision.Code)
  306. resp.HandleError(ctx, atoi, hikvision.Msg, nil)
  307. return
  308. }
  309. marshalString, err := json.Marshal(hikvision)
  310. if err != nil {
  311. resp.HandleError(ctx, 1202, "json序列化失败", nil)
  312. return
  313. }
  314. url := gjson.Get(string(marshalString), "data.url")
  315. resp.HandleSuccess(ctx, url)
  316. }
  317. // 视频监控云台控制{
  318. // "cameraIndexCode": "748d84750e3a4a5bbad3cd4af9ed5101",
  319. // "action": 1, 0-开始
  320. //1-停止
  321. //注:GOTO_PRESET命令下填任意值均可转到预置点,建议填0即可
  322. // "command": "GOTO_PRESET",
  323. // "speed": 4,
  324. // "presetIndex": 20
  325. //}
  326. func (h *HikvisionHandler) Gimbalcontrol(ctx *gin.Context) {
  327. m := make(map[string]string)
  328. cameraIndexCode := ctx.Query("cameraIndexCode")
  329. command := ctx.Query("command")
  330. action := ctx.Query("action")
  331. speed := ctx.Query("speed")
  332. presetIndex := ctx.Query("presetIndex")
  333. m["cameraIndexCode"] = cameraIndexCode
  334. m["action"] = action
  335. m["command"] = command
  336. m["speed"] = speed
  337. m["presetIndex"] = presetIndex
  338. if len(cameraIndexCode) <= 0 || len(command) <= 0 || len(action) <= 0 {
  339. resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
  340. return
  341. }
  342. resp.HandleSuccess(ctx, m)
  343. return
  344. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.controlling"), m, 15)
  345. if err != nil {
  346. h.logger.Error("控制云台失败")
  347. resp.HandleError(ctx, 1201, "控制云台失败", err)
  348. return
  349. }
  350. if hikvision.Code != "0" {
  351. atoi, _ := strconv.Atoi(hikvision.Code)
  352. resp.HandleError(ctx, atoi, hikvision.Msg, nil)
  353. return
  354. }
  355. resp.HandleSuccess(ctx, hikvision.Msg)
  356. }
  357. // VisitorInfoCount 获取今日访客信息包含:今日来访总人数(已签离人数,未签离人数),预约人数
  358. func (h *HikvisionHandler) VisitorInfoCount(c *gin.Context) {
  359. m := make(map[string]string)
  360. parkId := c.Query("parkId")
  361. m["parkId"] = parkId
  362. resp.HandleSuccess(c, "appointmentTotal: 1, notSignOutTotal: 1, signOutTotal: 1, signTotal: 1, orderCount: 1, visitCount: 1, visitCountForTemp: 1, visitCountForOrder: 1, signOutCount: 1, notSignOutCount: 1")
  363. return
  364. hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.visitorInfo"), m, 15)
  365. if err != nil {
  366. h.logger.Error("获取访客信息失败")
  367. resp.HandleError(c, 1201, "获取访客信息失败", err)
  368. return
  369. }
  370. if hikvision.Code != "0" {
  371. atoi, _ := strconv.Atoi(hikvision.Code)
  372. resp.HandleError(c, atoi, hikvision.Msg, nil)
  373. return
  374. }
  375. resp.HandleSuccess(c, hikvision.Data)
  376. }