cameraController.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/model"
  5. "iot_manager_service/app/service"
  6. "iot_manager_service/app/utils"
  7. "math"
  8. "net/http"
  9. "strconv"
  10. )
  11. // 摄像头管理对象
  12. var Camera = new(cameraCtl)
  13. type cameraCtl struct{}
  14. func (c *cameraCtl) Detail(ctx *gin.Context) {
  15. id, e := strconv.Atoi(ctx.Query("id"))
  16. if e != nil {
  17. ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(e.Error(), nil))
  18. return
  19. }
  20. device, err := service.CameraService.Get(id)
  21. if err != nil {
  22. ctx.JSON(http.StatusOK, err)
  23. return
  24. }
  25. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device))
  26. }
  27. func (c *cameraCtl) List(ctx *gin.Context) {
  28. searchValue := ctx.Query("searchValue")
  29. cameraType := ctx.Query("cameraType")
  30. current, _ := strconv.Atoi(ctx.Query("current"))
  31. size, _ := strconv.Atoi(ctx.Query("size"))
  32. if current == 0 {
  33. current = 1
  34. }
  35. if size <= 0 || size > 100 {
  36. size = 10
  37. }
  38. devices, err := service.CameraService.List(searchValue, cameraType, current, size)
  39. if err != nil {
  40. ctx.JSON(http.StatusOK, err)
  41. return
  42. }
  43. pages := math.Ceil(float64(len(devices)) / float64(size))
  44. rsp := model.RspCameraList{
  45. Current: current,
  46. Size: size,
  47. Total: len(devices),
  48. Pages: int(pages),
  49. Records: devices,
  50. }
  51. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, rsp))
  52. }
  53. func (c *cameraCtl) CreateOrUpdate(ctx *gin.Context) {
  54. // 参数验证
  55. var req *model.CameraDetail
  56. if err := ctx.ShouldBind(&req); err != nil {
  57. ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
  58. return
  59. }
  60. err := service.CameraService.CreateOrUpdate(req)
  61. if err != nil {
  62. ctx.JSON(http.StatusOK, err)
  63. return
  64. }
  65. ctx.JSON(http.StatusOK, nil)
  66. }
  67. func (c *cameraCtl) Remove(ctx *gin.Context) {
  68. var req *model.ReqCameraRemove
  69. if err := ctx.ShouldBindJSON(&req); err != nil {
  70. ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
  71. return
  72. }
  73. err := service.CameraService.Remove(req.IDs)
  74. if err != nil {
  75. ctx.JSON(http.StatusOK, err)
  76. return
  77. }
  78. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
  79. }
  80. func (c *cameraCtl) Count(ctx *gin.Context) {
  81. }
  82. func (c *cameraCtl) ImportExcel(ctx *gin.Context) {
  83. }
  84. func (c *cameraCtl) ExportExcel(ctx *gin.Context) {
  85. }
  86. func (c *cameraCtl) ExportTemplate(ctx *gin.Context) {
  87. }
  88. func (c *cameraCtl) GetList(ctx *gin.Context) {
  89. devices, err := service.CameraService.GetList()
  90. if err != nil {
  91. ctx.JSON(http.StatusOK, err)
  92. return
  93. }
  94. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
  95. }
  96. func (c *cameraCtl) Enable(ctx *gin.Context) {
  97. var req *model.ReqCameraEnable
  98. if err := ctx.ShouldBindJSON(&req); err != nil {
  99. ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
  100. return
  101. }
  102. if req.Status != model.Enable_Enable && req.Status != model.Enable_Disable {
  103. ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(model.EnableInvalid, nil))
  104. return
  105. }
  106. err := service.CameraService.Enable(req.ID, req.Status)
  107. if err != nil {
  108. ctx.JSON(http.StatusOK, err)
  109. return
  110. }
  111. ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
  112. }