ipBroadcastController.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/device/edge_service"
  6. "iot_manager_service/app/device/model"
  7. "iot_manager_service/app/device/service"
  8. "iot_manager_service/app/middleware"
  9. "iot_manager_service/util/common"
  10. "math"
  11. "net/http"
  12. "strconv"
  13. )
  14. // IP广播基本信息管理对象
  15. var IpBroadcast = new(ipBroadcastCtl)
  16. type ipBroadcastCtl struct{}
  17. func (c *ipBroadcastCtl) Detail(ctx *gin.Context) {
  18. id, e := strconv.Atoi(ctx.Query("id"))
  19. if e != nil {
  20. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  21. return
  22. }
  23. device, err := service.IpBroadcastService.Get(id)
  24. if err != nil {
  25. ctx.JSON(http.StatusOK, err)
  26. return
  27. }
  28. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, device))
  29. }
  30. func (c *ipBroadcastCtl) List(ctx *gin.Context) {
  31. searchValue := ctx.Query("searchValue")
  32. current, _ := strconv.Atoi(ctx.Query("current"))
  33. size, _ := strconv.Atoi(ctx.Query("size"))
  34. if current == 0 {
  35. current = 1
  36. }
  37. if size <= 0 || size > 100 {
  38. size = 10
  39. }
  40. devices, total, err := service.IpBroadcastService.List(searchValue, current, size)
  41. if err != nil {
  42. ctx.JSON(http.StatusOK, err)
  43. return
  44. }
  45. pages := math.Ceil(float64(total) / float64(size))
  46. rsp := model.RsqIpBroadcastList{
  47. Current: current,
  48. Size: size,
  49. Total: int(total),
  50. Pages: int(pages),
  51. }
  52. for _, device := range devices {
  53. rsp.Records = append(rsp.Records, device)
  54. }
  55. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
  56. }
  57. func (c *ipBroadcastCtl) CreateOrUpdate(ctx *gin.Context) {
  58. value, _ := ctx.Get(middleware.Authorization)
  59. claims := value.(*middleware.Claims)
  60. var req dao.IpBroadcast
  61. if err := ctx.ShouldBindJSON(&req); err != nil {
  62. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  63. return
  64. }
  65. err := service.IpBroadcastService.CreateOrUpdate(claims.UserId, claims.TenantId, req)
  66. ctx.JSON(http.StatusOK, err)
  67. }
  68. func (c *ipBroadcastCtl) Remove(ctx *gin.Context) {
  69. value, _ := ctx.Get(middleware.Authorization)
  70. claims := value.(*middleware.Claims)
  71. var req *model.ReqIpBroadcastRemove
  72. if err := ctx.ShouldBindJSON(&req); err != nil {
  73. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  74. return
  75. }
  76. err := service.IpBroadcastService.Remove(claims.UserId, claims.TenantId, req.IDs)
  77. if err != nil {
  78. ctx.JSON(http.StatusOK, err)
  79. return
  80. }
  81. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
  82. }
  83. func (c *ipBroadcastCtl) ImportExcel(ctx *gin.Context) {
  84. }
  85. func (c *ipBroadcastCtl) ExportExcel(ctx *gin.Context) {
  86. }
  87. func (c *ipBroadcastCtl) ExportTemplate(ctx *gin.Context) {
  88. }
  89. func (c *ipBroadcastCtl) SettingVolume(ctx *gin.Context) {
  90. value, _ := ctx.Get(middleware.Authorization)
  91. claims := value.(*middleware.Claims)
  92. var req dao.IpBroadcast
  93. if err := ctx.ShouldBindJSON(&req); err != nil {
  94. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  95. return
  96. }
  97. if req.SoundVolume <= 1 {
  98. req.SoundVolume = 1
  99. }
  100. err2 := edge_service.IpCastControlService{}.SetVol(req.CastSn, req.SoundVolume)
  101. if err2 != nil {
  102. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err2.Error(), nil))
  103. return
  104. }
  105. err := service.IpBroadcastService.CreateOrUpdate(claims.UserId, claims.TenantId, req)
  106. ctx.JSON(http.StatusOK, err)
  107. }