camera.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package app
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/flipped-aurora/gin-vue-admin/server/global"
  6. "github.com/gin-gonic/gin"
  7. "github.com/redis/go-redis/v9"
  8. "github.com/sirupsen/logrus"
  9. "go.uber.org/zap"
  10. "lc-fangdaosha/model/app"
  11. "lc-fangdaosha/model/common/request"
  12. "lc-fangdaosha/model/common/response"
  13. "lc-fangdaosha/service"
  14. "net/http"
  15. "strings"
  16. )
  17. type CameraApi struct {
  18. }
  19. var cameraService = service.ServiceGroupApp.AppServiceGroup.CameraService
  20. // CreateCamera 创建摄像机
  21. // @Tags Camera
  22. // @Summary 创建摄像机
  23. // @Security ApiKeyAuth
  24. // @accept application/json
  25. // @Produce application/json
  26. // @Param data body app.Camera true "创建摄像机"
  27. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  28. // @Router /camera/createCamera [post]
  29. func (cameraApi *CameraApi) CreateCamera(c *gin.Context) {
  30. var camera app.Camera
  31. err := c.ShouldBindJSON(&camera)
  32. if err != nil {
  33. response.FailWithMessage(err.Error(), c)
  34. return
  35. }
  36. if err := cameraService.CreateCamera(&camera); err != nil {
  37. global.GVA_LOG.Error("创建失败!", zap.Error(err))
  38. response.FailWithMessage("创建失败", c)
  39. } else {
  40. response.OkWithMessage("创建成功", c)
  41. }
  42. }
  43. // DeleteCamera 删除摄像机
  44. // @Tags Camera
  45. // @Summary 删除摄像机
  46. // @Security ApiKeyAuth
  47. // @accept application/json
  48. // @Produce application/json
  49. // @Param data body app.Camera true "删除摄像机"
  50. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  51. // @Router /camera/deleteCamera [delete]
  52. func (cameraApi *CameraApi) DeleteCamera(c *gin.Context) {
  53. var camera app.Camera
  54. err := c.ShouldBindJSON(&camera)
  55. if err != nil {
  56. response.FailWithMessage(err.Error(), c)
  57. return
  58. }
  59. if err := cameraService.DeleteCamera(camera); err != nil {
  60. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  61. response.FailWithMessage("删除失败", c)
  62. } else {
  63. response.OkWithMessage("删除成功", c)
  64. }
  65. }
  66. // DeleteCameraByIds 批量删除摄像机
  67. // @Tags Camera
  68. // @Summary 批量删除摄像机
  69. // @Security ApiKeyAuth
  70. // @accept application/json
  71. // @Produce application/json
  72. // @Param data body request.IdsReq true "批量删除摄像机"
  73. // @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
  74. // @Router /camera/deleteCameraByIds [delete]
  75. func (cameraApi *CameraApi) DeleteCameraByIds(c *gin.Context) {
  76. var IDS request.IdsReq
  77. err := c.ShouldBindJSON(&IDS)
  78. if err != nil {
  79. response.FailWithMessage(err.Error(), c)
  80. return
  81. }
  82. if err := cameraService.DeleteCameraByIds(IDS); err != nil {
  83. global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
  84. response.FailWithMessage("批量删除失败", c)
  85. } else {
  86. response.OkWithMessage("批量删除成功", c)
  87. }
  88. }
  89. // UpdateCamera 更新摄像机
  90. // @Tags Camera
  91. // @Summary 更新摄像机
  92. // @Security ApiKeyAuth
  93. // @accept application/json
  94. // @Produce application/json
  95. // @Param data body app.Camera true "更新摄像机"
  96. // @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
  97. // @Router /camera/updateCamera [put]
  98. func (cameraApi *CameraApi) UpdateCamera(c *gin.Context) {
  99. var camera app.Camera
  100. err := c.ShouldBindJSON(&camera)
  101. if err != nil {
  102. response.FailWithMessage(err.Error(), c)
  103. return
  104. }
  105. if err := cameraService.UpdateCamera(camera); err != nil {
  106. global.GVA_LOG.Error("更新失败!", zap.Error(err))
  107. response.FailWithMessage("更新失败", c)
  108. } else {
  109. response.OkWithMessage("更新成功", c)
  110. }
  111. }
  112. // FindCamera 用id查询摄像机
  113. // @Tags Camera
  114. // @Summary 用id查询摄像机
  115. // @Security ApiKeyAuth
  116. // @accept application/json
  117. // @Produce application/json
  118. // @Param data query app.Camera true "用id查询摄像机"
  119. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
  120. // @Router /camera/findCamera [get]
  121. func (cameraApi *CameraApi) FindCamera(c *gin.Context) {
  122. var camera app.Camera
  123. err := c.ShouldBindQuery(&camera)
  124. if err != nil {
  125. response.FailWithMessage(err.Error(), c)
  126. return
  127. }
  128. if recamera, err := cameraService.GetCamera(camera.ID); err != nil {
  129. global.GVA_LOG.Error("查询失败!", zap.Error(err))
  130. response.FailWithMessage("查询失败", c)
  131. } else {
  132. response.OkWithData(gin.H{"recamera": recamera}, c)
  133. }
  134. }
  135. // Status 获取摄像机的状态
  136. func (cameraApi *CameraApi) Status(c *gin.Context) {
  137. topic := c.GetHeader("topic")
  138. topics := strings.Split(topic, "/")
  139. if len(topics) != 2 {
  140. logrus.Error("前端http header : topic数据不对", zap.String("topic", topic))
  141. response.FailWithMessage("topic头数据不对", c)
  142. }
  143. get, err := global.GVA_REDIS.Get(context.Background(), fmt.Sprintf("lcfns:status:%s:%s", topics[0], topics[1])).Result()
  144. if err == redis.Nil {
  145. //-1边缘端不在线
  146. response.OkWithData(-1, c)
  147. }
  148. if err != nil {
  149. response.FailWithMessage("redis 查询失败", c)
  150. }
  151. //摄像头:1在线,0不在线
  152. response.OkWithData(get, c)
  153. }
  154. // RewindText 获取回话数据
  155. func (cameraApi *CameraApi) RewindText(ctx *gin.Context) {
  156. streamId := ctx.Query("streamId")
  157. date := ctx.Query("date")
  158. operate, err := cameraService.GetRewindText(streamId, date)
  159. if err != nil {
  160. ctx.JSON(http.StatusOK, err)
  161. }
  162. if operate == "" {
  163. ctx.JSON(http.StatusOK, nil)
  164. return
  165. }
  166. response.OkWithData(operate, ctx)
  167. }