hikvision.go 12 KB

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