infoBoardController.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "iot_manager_service/app/device/dao"
  6. "iot_manager_service/app/device/model"
  7. "iot_manager_service/app/device/service"
  8. "iot_manager_service/app/middleware"
  9. service2 "iot_manager_service/app/multimedia/service"
  10. "iot_manager_service/util/cache"
  11. "iot_manager_service/util/common"
  12. "math"
  13. "net/http"
  14. "strconv"
  15. )
  16. // 信息屏基本信息管理对象
  17. var InfoBoard = new(infoBoardCtl)
  18. type infoBoardCtl struct{}
  19. func (c *infoBoardCtl) Detail(ctx *gin.Context) {
  20. id, e := strconv.Atoi(ctx.Query("id"))
  21. if e != nil {
  22. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  23. return
  24. }
  25. device, err := service.InfoBoardService.Get(id)
  26. if err != nil {
  27. ctx.JSON(http.StatusOK, err)
  28. return
  29. }
  30. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, device))
  31. }
  32. func (c *infoBoardCtl) List(ctx *gin.Context) {
  33. searchValue := ctx.Query("searchValue")
  34. current, _ := strconv.Atoi(ctx.Query("current"))
  35. size, _ := strconv.Atoi(ctx.Query("size"))
  36. if current == 0 {
  37. current = 1
  38. }
  39. if size <= 0 || size > 100 {
  40. size = 10
  41. }
  42. devices, total, err := service.InfoBoardService.List(searchValue, current, size)
  43. if err != nil {
  44. ctx.JSON(http.StatusOK, err)
  45. return
  46. }
  47. pages := math.Ceil(float64(total) / float64(size))
  48. rsp := model.RsqInfoBoardList{
  49. Current: current,
  50. Size: size,
  51. Total: int(total),
  52. Pages: int(pages),
  53. Records: devices,
  54. }
  55. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
  56. }
  57. func (c *infoBoardCtl) CreateOrUpdate(ctx *gin.Context) {
  58. value, _ := ctx.Get(middleware.Authorization)
  59. claims := value.(*middleware.Claims)
  60. var req dao.InfoBoard
  61. if err := ctx.ShouldBindJSON(&req); err != nil {
  62. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  63. return
  64. }
  65. if req.ID > 0 {
  66. //更新 排程
  67. c.CronSyncLedPaying(req.ID)
  68. //fmt.Printf("req.ID = %v \n", req.ID)
  69. }
  70. err := service.InfoBoardService.CreateOrUpdate(claims.UserId, claims.TenantId, req)
  71. ctx.JSON(http.StatusOK, err)
  72. }
  73. func (c *infoBoardCtl) Remove(ctx *gin.Context) {
  74. value, _ := ctx.Get(middleware.Authorization)
  75. claims := value.(*middleware.Claims)
  76. var req *model.ReqInfoBoardRemove
  77. if err := ctx.ShouldBindJSON(&req); err != nil {
  78. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  79. return
  80. }
  81. err := service.InfoBoardService.Remove(claims.UserId, claims.TenantId, req.IDs)
  82. if err != nil {
  83. ctx.JSON(http.StatusOK, err)
  84. return
  85. }
  86. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
  87. }
  88. func (c *infoBoardCtl) ImportExcel(ctx *gin.Context) {
  89. }
  90. func (c *infoBoardCtl) ExportExcel(ctx *gin.Context) {
  91. }
  92. func (c *infoBoardCtl) ExportTemplate(ctx *gin.Context) {
  93. }
  94. func (c *infoBoardCtl) SystemOperation(ctx *gin.Context) {
  95. }
  96. func (c *infoBoardCtl) Enable(ctx *gin.Context) {
  97. //TODO:未做
  98. }
  99. func (c *infoBoardCtl) ShowSetting(ctx *gin.Context) {
  100. id, e := strconv.Atoi(ctx.Query("id"))
  101. if e != nil {
  102. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  103. return
  104. }
  105. device, err := service.InfoBoardService.Get(id)
  106. if err != nil {
  107. ctx.JSON(http.StatusOK, err)
  108. return
  109. }
  110. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, device))
  111. }
  112. type edgeCmdReq struct {
  113. InfoSn string `json:"infoSn"`
  114. Id int `json:"id"`
  115. Directive int `json:"directive"`
  116. Condition string `json:"condition"`
  117. }
  118. // EdgeCmd 向边缘端发送cmd命令
  119. func (c *infoBoardCtl) EdgeCmd(ctx *gin.Context) {
  120. var req *edgeCmdReq
  121. if err := ctx.ShouldBindJSON(&req); err != nil {
  122. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  123. return
  124. }
  125. value, _ := ctx.Get(middleware.Authorization)
  126. claims := value.(*middleware.Claims)
  127. if req.Directive == 101 {
  128. var req2 dao.InfoBoard
  129. req2.ID = req.Id
  130. req2.Condition = req.Condition
  131. service.InfoBoardService.CreateOrUpdate(claims.UserId, claims.TenantId, req2)
  132. }
  133. //更新排程
  134. if req.Directive == 4 {
  135. c.CronSyncLedPaying(req.Id)
  136. }
  137. service.InfoBoardService.EdgeCmd(claims.TenantId, req.InfoSn, req.Directive, req.Id)
  138. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
  139. }
  140. // CronSyncBrightnessAndMusicvolume 同步亮度与音量
  141. func (s *infoBoardCtl) CronSyncBrightnessAndMusicvolume() {
  142. board := dao.InfoBoard{}
  143. list, err := board.GetAllDevicesNotTenant()
  144. if err != nil {
  145. fmt.Printf("err = %v \n", err)
  146. return
  147. }
  148. //fmt.Printf("list = %v \n", list)
  149. for _, led := range list {
  150. _, retState := cache.GetDeviceState(led.Sn)
  151. //fmt.Printf("retState = %v \n", retState)
  152. if retState == "1" {
  153. service.InfoBoardService.EdgeCmd(led.TenantId, led.Sn, 101, led.ID)
  154. }
  155. }
  156. }
  157. // CronSyncLedPaying 同步播放当前节目
  158. func (s *infoBoardCtl) CronSyncLedPaying(id int) {
  159. board := dao.InfoBoard{}
  160. if id > 0 {
  161. board.ID = id
  162. }
  163. list, err := board.GetAllDevicesNotTenant()
  164. if err != nil {
  165. fmt.Printf("CronSyncLedPaying err = %v \n", err)
  166. return
  167. }
  168. for _, led := range list {
  169. _, retState := cache.GetDeviceState(led.Sn)
  170. currPaying := cache.GetDeviceLedData(led.Sn)
  171. if retState == "1" && len(currPaying) != 0 {
  172. playing := currPaying["playing"].(string) //得到当前正播放的节目
  173. fmt.Printf("led信息屏设备Sn = %v 当前正在播放[%v]", led.Sn, playing)
  174. playJson := service2.PublishLibrariesService.LedPaying(led.ID, playing)
  175. if playJson != "" {
  176. service.InfoBoardService.EdgePushLedProgram(led.TenantId, led.Sn, playJson)
  177. fmt.Printf("需要更新\n")
  178. } else {
  179. fmt.Printf("无需更新\n")
  180. }
  181. } else {
  182. //fmt.Printf("led设备不在线Sn = %v \n", led.Sn)
  183. }
  184. }
  185. }