calcTask.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package controller
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/robfig/cron"
  6. "io/ioutil"
  7. "iot_manager_service/app/data/model"
  8. "iot_manager_service/app/data/service"
  9. controller "iot_manager_service/app/device/controller"
  10. device "iot_manager_service/app/device/service"
  11. system "iot_manager_service/app/system/dao"
  12. warnService "iot_manager_service/app/warn/service"
  13. "iot_manager_service/util/cache"
  14. "iot_manager_service/util/logger"
  15. "net/http"
  16. "time"
  17. )
  18. func CalcTask() {
  19. hourSpec := "0 0 */1 * * ?"
  20. //minuteSpec := "0 */1 * * * ?"
  21. daySpec := "0 1 0 * * ?"
  22. c := cron.New()
  23. _ = c.AddFunc(hourSpec, func() {
  24. devices := device.GarbageService.GetAll()
  25. var deviceIds []string
  26. for _, d := range devices {
  27. deviceIds = append(deviceIds, d.DeviceIMei)
  28. }
  29. GarbageDataSync(deviceIds)
  30. })
  31. _ = c.AddFunc(hourSpec, func() {
  32. service.EnvironmentDataService.DataSync()
  33. })
  34. _ = c.AddFunc(daySpec, func() {
  35. service.EnvironmentDataService.DayDataSync()
  36. })
  37. //处理告警消息
  38. _ = c.AddFunc(hourSpec, func() {
  39. //抓取数据
  40. tenant := system.Tenant{}
  41. tenants, _, _ := tenant.GetTenants(0, 1000)
  42. for _, t := range tenants {
  43. warnService.PlatformAlarmService.Refresh(t.TenantId)
  44. }
  45. //发送告警
  46. warnService.TaskWarnService.HandlingAlarms()
  47. })
  48. //【led信息屏】每天早上07:10同步亮度与音量
  49. c.AddFunc("0 10 7 * * ?", func() {
  50. //c.AddFunc("*/10 * * * * ?", func() {
  51. controller.InfoBoard.CronSyncBrightnessAndMusicvolume()
  52. })
  53. //【led信息屏】每天晚上18:10同步亮度与音量
  54. c.AddFunc("0 10 18 * * ?", func() {
  55. controller.InfoBoard.CronSyncBrightnessAndMusicvolume()
  56. })
  57. //【led信息屏】播放节目同步
  58. c.AddFunc("*/10 * * * * ?", func() {
  59. controller.InfoBoard.CronSyncLedPaying(0)
  60. })
  61. c.Start()
  62. }
  63. func GarbageDataSync(deviceIds []string) {
  64. if len(deviceIds) == 0 {
  65. return
  66. }
  67. url := "http://124.239.149.63:5000/admin/garbage/can/getGarbageCanByDeviceIdsList"
  68. req := model.GarbageDataReq{
  69. DeviceIds: deviceIds,
  70. }
  71. data, err := json.Marshal(&req)
  72. if err != nil {
  73. logger.Logger.Warningf("GarbageDataSync Marshal err = %s", err.Error())
  74. return
  75. }
  76. body := bytes.NewBuffer(data)
  77. request, err := http.NewRequest("POST", url, body)
  78. if err != nil {
  79. logger.Logger.Warningf("GarbageDataSync NewRequest err = %s", err.Error())
  80. return
  81. }
  82. request.Header.Set("Authorization", "Bearer "+GarbageLogin())
  83. request.Header.Set("Content-Type", "application/json")
  84. response, err := http.DefaultClient.Do(request)
  85. if err != nil {
  86. logger.Logger.Warningf("GarbageDataSync Do err = %s", err.Error())
  87. return
  88. }
  89. rspData, err := ioutil.ReadAll(response.Body)
  90. if err != nil {
  91. logger.Logger.Warningf("GarbageDataSync ReadAll err = %s", err.Error())
  92. return
  93. }
  94. var rsp model.GarbageDataRsp
  95. err = json.Unmarshal(rspData, &rsp)
  96. if err != nil {
  97. logger.Logger.Warningf("GarbageDataSync Unmarshal err = %s", err.Error())
  98. return
  99. }
  100. if rsp.Code != 0 {
  101. logger.Logger.Warningf("GarbageDataSync rsp.Code != 0 msg = %s", rsp.Msg)
  102. return
  103. }
  104. service.GarbageDataService.BatchSave(rsp.Data)
  105. }
  106. func GarbageLogin() string {
  107. value, err := cache.Redis.Get(model.GarbageLoginKey).Result()
  108. if err == nil {
  109. return value
  110. }
  111. url := "http://124.239.149.63:5000/admin/user/login"
  112. req := model.GarbageLoginReq{
  113. Username: "湖南溢满",
  114. Password: "T2rMLUCMaaBXfjBjZ7myOA==",
  115. }
  116. data, err := json.Marshal(&req)
  117. if err != nil {
  118. logger.Logger.Warningf("GarbageLogin Marshal err = %s", err.Error())
  119. return ""
  120. }
  121. body := bytes.NewBuffer(data)
  122. request, err := http.NewRequest("POST", url, body)
  123. if err != nil {
  124. logger.Logger.Warningf("GarbageLogin NewRequest err = %s", err.Error())
  125. return ""
  126. }
  127. request.Header.Set("Content-Type", "application/json")
  128. response, err := http.DefaultClient.Do(request)
  129. if err != nil {
  130. logger.Logger.Warningf("GarbageLogin Do err = %s", err.Error())
  131. return ""
  132. }
  133. rspData, err := ioutil.ReadAll(response.Body)
  134. if err != nil {
  135. logger.Logger.Warningf("GarbageLogin ReadAll err = %s", err.Error())
  136. return ""
  137. }
  138. var rsp model.GarbageLoginRsp
  139. err = json.Unmarshal(rspData, &rsp)
  140. if err != nil {
  141. logger.Logger.Warningf("GarbageLogin Unmarshal err = %s", err.Error())
  142. return ""
  143. }
  144. if rsp.Code != 0 {
  145. logger.Logger.Warningf("GarbageLogin rsp.Code != 0 msg = %s", rsp.Msg)
  146. return ""
  147. }
  148. cache.Redis.Set(model.GarbageLoginKey, rsp.Data.AccessToken, 2*time.Hour)
  149. return rsp.Data.AccessToken
  150. }