environmentDataService.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "iot_manager_service/app/data/dao"
  6. "iot_manager_service/app/data/model"
  7. device "iot_manager_service/app/device/service"
  8. "iot_manager_service/util/cache"
  9. "iot_manager_service/util/logger"
  10. "time"
  11. )
  12. var EnvironmentDataService = new(environmentDataService)
  13. type environmentDataService struct{}
  14. func (s *environmentDataService) DataSync() {
  15. devices := device.OptoSensorService.GetAll()
  16. if len(devices) == 0 {
  17. return
  18. }
  19. var environmentDatas []dao.EnvironmentData
  20. for _, d := range devices {
  21. creatTime, values := cache.GetDeviceData(d.Sn)
  22. now := time.Now()
  23. environmentData := dao.EnvironmentData{
  24. DeviceId: d.ID,
  25. PostTime: creatTime,
  26. CreateTime: time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.Local),
  27. }
  28. for key, value := range values {
  29. switch key {
  30. case model.ModelInfoAirQuality:
  31. environmentData.Air = value
  32. case model.ModelInfoHumidity:
  33. environmentData.Humidity = value
  34. case model.ModelInfoPM25:
  35. environmentData.Pm25 = value
  36. case model.ModelInfoPM10:
  37. environmentData.Pm10 = value
  38. case model.ModelInfoTemprature:
  39. environmentData.Temperature = value
  40. case model.ModelInfowindSpeed:
  41. environmentData.WindSpeed = value
  42. case model.ModelInfoPressure:
  43. environmentData.Hpa = value
  44. case model.ModelInfoNoise:
  45. environmentData.Noise = value
  46. case model.ModelInfoLightIntensity:
  47. environmentData.Illuminance = value
  48. case model.ModelInfoWindDirection:
  49. environmentData.WindDirection = model.CalculateDirection(value)
  50. case model.ModelInfoRainfall:
  51. environmentData.Rainfall = value
  52. case model.ModelInfoUltravioletRays:
  53. environmentData.Ultraviolet = value
  54. }
  55. }
  56. // 7点的数据用于天数据
  57. if environmentData.CreateTime.Hour() == 7 {
  58. environmentData.IsDay = 1
  59. }
  60. b, _ := json.Marshal(&environmentData)
  61. cache.Redis.Set(fmt.Sprintf(model.EnvironmentDataKey, d.ID), string(b), 0)
  62. }
  63. environment := &dao.EnvironmentData{}
  64. err := environment.BatchSave(environmentDatas)
  65. if err != nil {
  66. logger.Logger.Errorf("DataSync Hour.BatchSave err = %s", err.Error())
  67. }
  68. }
  69. func (s *environmentDataService) DayDataSync() {
  70. summary := &dao.EnvironmentDataSummary{}
  71. yesterday := time.Now().Add(-24 * time.Hour)
  72. now := time.Now()
  73. start := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, time.Local)
  74. end := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  75. err := summary.DaySummary(start, end)
  76. if err != nil {
  77. logger.Logger.Errorf("DayDataSync err = %s", err.Error())
  78. }
  79. }
  80. func (s *environmentDataService) Get(deviceId int) *dao.EnvironmentData {
  81. var EnvironmentData dao.EnvironmentData
  82. result, err := cache.Redis.Get(fmt.Sprintf(model.EnvironmentDataKey, deviceId)).Result()
  83. if err == nil {
  84. err = json.Unmarshal([]byte(result), &EnvironmentData)
  85. if err != nil {
  86. return nil
  87. }
  88. return &EnvironmentData
  89. }
  90. EnvironmentData = dao.EnvironmentData{
  91. DeviceId: deviceId,
  92. }
  93. _ = EnvironmentData.Get()
  94. return &EnvironmentData
  95. }
  96. func (s *environmentDataService) GetData(deviceId, isDay int, start, end time.Time) ([]dao.EnvironmentData, error) {
  97. var EnvironmentData dao.EnvironmentData
  98. EnvironmentData = dao.EnvironmentData{
  99. DeviceId: deviceId,
  100. IsDay: isDay,
  101. }
  102. return EnvironmentData.GetByTime(start, end)
  103. }