123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package service
- import (
- "encoding/json"
- "fmt"
- "iot_manager_service/app/data/dao"
- "iot_manager_service/app/data/model"
- device "iot_manager_service/app/device/service"
- "iot_manager_service/util/cache"
- "iot_manager_service/util/logger"
- "time"
- )
- var EnvironmentDataService = new(environmentDataService)
- type environmentDataService struct{}
- func (s *environmentDataService) DataSync() {
- devices := device.OptoSensorService.GetAll()
- if len(devices) == 0 {
- return
- }
- var environmentDatas []dao.EnvironmentData
- for _, d := range devices {
- creatTime, values := cache.GetDeviceData(d.Sn)
- now := time.Now()
- environmentData := dao.EnvironmentData{
- DeviceId: d.ID,
- PostTime: creatTime,
- CreateTime: time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.Local),
- }
- for key, value := range values {
- switch key {
- case model.ModelInfoAirQuality:
- environmentData.Air = value
- case model.ModelInfoHumidity:
- environmentData.Humidity = value
- case model.ModelInfoPM25:
- environmentData.Pm25 = value
- case model.ModelInfoPM10:
- environmentData.Pm10 = value
- case model.ModelInfoTemprature:
- environmentData.Temperature = value
- case model.ModelInfowindSpeed:
- environmentData.WindSpeed = value
- case model.ModelInfoPressure:
- environmentData.Hpa = value
- case model.ModelInfoNoise:
- environmentData.Noise = value
- case model.ModelInfoLightIntensity:
- environmentData.Illuminance = value
- case model.ModelInfoWindDirection:
- environmentData.WindDirection = model.CalculateDirection(value)
- case model.ModelInfoRainfall:
- environmentData.Rainfall = value
- case model.ModelInfoUltravioletRays:
- environmentData.Ultraviolet = value
- }
- }
- // 7点的数据用于天数据
- if environmentData.CreateTime.Hour() == 7 {
- environmentData.IsDay = 1
- }
- b, _ := json.Marshal(&environmentData)
- cache.Redis.Set(fmt.Sprintf(model.EnvironmentDataKey, d.ID), string(b), 0)
- }
- environment := &dao.EnvironmentData{}
- err := environment.BatchSave(environmentDatas)
- if err != nil {
- logger.Logger.Errorf("DataSync Hour.BatchSave err = %s", err.Error())
- }
- }
- func (s *environmentDataService) DayDataSync() {
- summary := &dao.EnvironmentDataSummary{}
- yesterday := time.Now().Add(-24 * time.Hour)
- now := time.Now()
- start := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, time.Local)
- end := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
- err := summary.DaySummary(start, end)
- if err != nil {
- logger.Logger.Errorf("DayDataSync err = %s", err.Error())
- }
- }
- func (s *environmentDataService) Get(deviceId int) *dao.EnvironmentData {
- var EnvironmentData dao.EnvironmentData
- result, err := cache.Redis.Get(fmt.Sprintf(model.EnvironmentDataKey, deviceId)).Result()
- if err == nil {
- err = json.Unmarshal([]byte(result), &EnvironmentData)
- if err != nil {
- return nil
- }
- return &EnvironmentData
- }
- EnvironmentData = dao.EnvironmentData{
- DeviceId: deviceId,
- }
- _ = EnvironmentData.Get()
- return &EnvironmentData
- }
- func (s *environmentDataService) GetData(deviceId, isDay int, start, end time.Time) ([]dao.EnvironmentData, error) {
- var EnvironmentData dao.EnvironmentData
- EnvironmentData = dao.EnvironmentData{
- DeviceId: deviceId,
- IsDay: isDay,
- }
- return EnvironmentData.GetByTime(start, end)
- }
|