camera.go 5.0 KB

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