calcTask.go 3.4 KB

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