calcTask.go 3.8 KB

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