environmentDataService.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/common"
  10. "iot_manager_service/util/logger"
  11. "strconv"
  12. "time"
  13. )
  14. var EnvironmentDataService = new(environmentDataService)
  15. type environmentDataService struct{}
  16. func (s *environmentDataService) DataSync() {
  17. devices := device.OptoSensorService.GetAll()
  18. //fmt.Printf("devices = %v \n", devices)
  19. if len(devices) == 0 {
  20. return
  21. }
  22. var environmentDatas []dao.EnvironmentData
  23. for _, d := range devices {
  24. //fmt.Printf("d.Sn = %v \n", d.Sn)
  25. creatTime, values := cache.GetDeviceData(d.Sn)
  26. //fmt.Printf("values = %v \n", values)
  27. now := time.Now()
  28. environmentData := dao.EnvironmentData{
  29. DeviceId: d.ID,
  30. PostTime: creatTime,
  31. TenantId: d.TenantId,
  32. CreateTime: time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.Local),
  33. }
  34. for key, value := range values {
  35. switch key {
  36. case model.ModelInfoNoise:
  37. environmentData.Noise = value
  38. case model.ModelInfoPM25:
  39. environmentData.Pm25 = value
  40. case model.ModelInfoPM10:
  41. environmentData.Pm10 = value
  42. case model.ModelInfoTemprature:
  43. environmentData.Temperature = value
  44. case model.ModelInfoHumidity:
  45. environmentData.Humidity = value
  46. case model.ModelInfoPressure:
  47. environmentData.Hpa = value
  48. case model.ModelInfowindSpeed:
  49. environmentData.WindSpeed = value
  50. case model.ModelInfoWindDirection:
  51. //environmentData.WindDirection = model.CalculateDirection(value)
  52. environmentData.WindDirection = model.CalculateDirection(value)
  53. case model.ModelInfoRainfall:
  54. environmentData.Rainfall = value
  55. case model.ModelInfoUltravioletRays:
  56. environmentData.Ultraviolet = value
  57. case model.ModelInfoLightIntensity:
  58. environmentData.Illuminance = value
  59. }
  60. }
  61. aqi := common.CountAqi(float64(environmentData.Pm25), float64(environmentData.Pm10))
  62. value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", aqi), 32)
  63. environmentData.Air = float32(value)
  64. environmentData.AirName = common.CalculateAirQuality(float64(environmentData.Air))
  65. // 7点的数据用于天数据
  66. environmentData.CreateDate = environmentData.CreateTime.Format("2006-01-02 15:04:06")
  67. if environmentData.CreateTime.Hour() == 7 {
  68. environmentData.IsDay = 1
  69. environmentData.CreateDate = environmentData.CreateTime.Format("2006-01-02")
  70. }
  71. environmentDatas = append(environmentDatas, environmentData)
  72. b, _ := json.Marshal(&environmentData)
  73. cache.Redis.Set(fmt.Sprintf(model.EnvironmentDataKey, d.ID), string(b), 0)
  74. }
  75. //fmt.Printf("environmentDatas = %v \n", environmentDatas)
  76. environment := &dao.EnvironmentData{}
  77. err := environment.BatchSave(environmentDatas)
  78. if err != nil {
  79. logger.Logger.Errorf("DataSync Hour.BatchSave err = %s", err.Error())
  80. }
  81. }
  82. func (s *environmentDataService) DayDataSync() {
  83. summary := &dao.EnvironmentDataSummary{}
  84. yesterday := time.Now().Add(-24 * time.Hour)
  85. now := time.Now()
  86. start := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, time.Local)
  87. end := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  88. err := summary.DaySummary(start, end)
  89. if err != nil {
  90. logger.Logger.Errorf("DayDataSync err = %s", err.Error())
  91. }
  92. }
  93. func (s *environmentDataService) Get(deviceId int) *dao.EnvironmentData {
  94. var EnvironmentData dao.EnvironmentData
  95. result, err := cache.Redis.Get(fmt.Sprintf(model.EnvironmentDataKey, deviceId)).Result()
  96. if err == nil {
  97. err = json.Unmarshal([]byte(result), &EnvironmentData)
  98. if err != nil {
  99. fmt.Printf("err = %v \n", err)
  100. return nil
  101. }
  102. return &EnvironmentData
  103. }
  104. EnvironmentData = dao.EnvironmentData{
  105. DeviceId: deviceId,
  106. }
  107. _ = EnvironmentData.Get()
  108. return &EnvironmentData
  109. }
  110. func (s *environmentDataService) GetData(deviceId, isDay int, start, end time.Time) ([]dao.EnvironmentData, error) {
  111. var EnvironmentData dao.EnvironmentData
  112. EnvironmentData = dao.EnvironmentData{
  113. DeviceId: deviceId,
  114. IsDay: isDay,
  115. }
  116. return EnvironmentData.GetByTime(start, end)
  117. }
  118. func (s *environmentDataService) GetScopeData(deviceId int, isDay int, queryType int) (interface{}, error) {
  119. var chartHistoryList []dao.EnvironmentScopeData
  120. var chartNowList []dao.EnvironmentScopeData
  121. var unit string
  122. switch queryType {
  123. case 1:
  124. unit = "aqi"
  125. case 2:
  126. unit = "ug/m³"
  127. case 3:
  128. unit = "ug/m³"
  129. case 4:
  130. unit = "℃"
  131. case 5:
  132. unit = "%"
  133. case 6:
  134. unit = "m/s"
  135. case 7:
  136. unit = "hPa"
  137. case 8:
  138. unit = "dB"
  139. }
  140. var EnvironmentData dao.EnvironmentData
  141. EnvironmentData = dao.EnvironmentData{
  142. DeviceId: deviceId,
  143. IsDay: isDay,
  144. }
  145. chartHistoryList = EnvironmentData.GetEnvironmentScope(queryType, "history")
  146. chartNowList = EnvironmentData.GetEnvironmentScope(queryType, "now")
  147. data := make(map[string]interface{})
  148. data["unit"] = unit
  149. data["chartHistoryList"] = chartHistoryList
  150. data["chartNowList"] = chartNowList
  151. return data, nil
  152. }