示例#1
0
 public static bool HasProfile(this InMemoryUser value, ProfileType profile)
 {
     if (value.Profiles == null)
     {
         return(false);
     }
     return(value.Profiles.Contains(new UserProfile()
     {
         Name = profile.GetName()
     }));
 }
 private static WhereClauseRegisterCondition ForProfile(ProfileType profile)
 {
     return(new WhereClauseRegisterCondition {
         UserProfile = profile.GetName(),
     });
 }
示例#3
0
        public async Task <List <ProfileDto> > GetDtos(string deviceId, int locationId = -1)
        {
            if (locationId == -1)
            {
                locationId = await Db.Locations.Where(x => x.DeviceId == deviceId && x.Active).Select(x => x.Id).SingleOrDefaultAsync();
            }

            var profiles = await Db.Profiles.Where(x => x.User.DeviceId == deviceId).ToListAsync();

            var profileDtos = new List <ProfileDto>();

            foreach (var profile in profiles)
            {
                var profileDto = new ProfileDto();

                bool?state = null;
                MinTemperatureEntity minTemp = null;
                if (profile.TypeId == ProfileType.MODE_AUTO_PROFILE_ID)
                {
                    minTemp = await minTemperatureRepository.Get(locationId, profile.Id);

                    if (minTemp == null)
                    {
                        minTemp = await minTemperatureRepository.Post(locationId, DEFAULT_TEMP, profile.Id);

                        if (minTemp == null)
                        {
                            throw new RepositoryException($"{profile.Id} 'idli profil ve {locationId} id'li location için MinTemp bilgisi yok ve yenisi oluşturulamıyor.");
                        }
                    }
                }
                else if (profile.TypeId == ProfileType.MODE_AUTO_SERVER_PROFILE_ID)
                {
                    minTemp = await minTemperatureRepository.Get(profile.Id);

                    if (minTemp == null)
                    {
                        throw new RepositoryException($"{profile.Id} 'idli profil ve {locationId} id'li location için MinTemp bilgisi yok ve yenisi oluşturulamıyor.");
                    }

                    var location = await Db.Locations.Where(x => x.Id == minTemp.LocationId && x.Active).SingleOrDefaultAsync();

                    profileDto.SelectedThermometer = new LocationDto()
                    {
                        Id             = location.Id,
                        Name           = location.Name,
                        DeviceId       = location.DeviceId,
                        DeviceTypeName = DeviceType.GetName(location.Device.TypeId),
                        MinTempValue   = minTemp.Value
                    };
                }
                if (profile.TypeId == ProfileType.MODE_MANUAL_ID)
                {
                    state = await stateRepository.Get(deviceId);
                }


                profileDto.Id           = profile.Id;
                profileDto.MinTempValue = minTemp?.Value;
                profileDto.ProfileName  = profile.Name;
                profileDto.TypeName     = ProfileType.GetName(profile.TypeId);
                profileDto.State        = state;
                profileDto.Active       = profile.Active;

                profileDtos.Add(profileDto);
            }

            return(profileDtos);
        }
示例#4
0
 private UserProfile FindUserProfile(ProfileType profileType)
 {
     return(_dao.FindSingleByQuery <UserProfile>(UserProfile.UserProfileByName, profileType.GetName()));
 }
示例#5
0
        public async Task <SettingsDto> GetActiveDto(string deviceId)
        {
            var settings = new SettingsDto();

            var activeProfile = await Db.Profiles.Where(x => x.User.DeviceId == deviceId && x.Active).SingleOrDefaultAsync();

            if (activeProfile == null)
            {
                var device = await Db.Devices.Where(x => x.Id == deviceId).SingleOrDefaultAsync();

                if (device.CenterDeviceId != null)
                {
                    throw RepositoryException.NotCenterDevice(deviceId);
                }

                var profiles = await Db.Profiles.Where(x => x.User.DeviceId == deviceId).ToListAsync();

                if (profiles.Any())
                {
                    // profiles exist but none of them is active | make active one of them ( ???? )
                    activeProfile        = profiles.FirstOrDefault();
                    activeProfile.Active = true;
                    await Db.SaveChangesAsync();
                }
                else
                {
                    // profiles not exist create default profiles for first time use
                    await CreateDefaultProfiles(deviceId);
                }
            }

            settings.Guid = await GetGuid(deviceId);

            settings.ProfileName = activeProfile.Name;
            settings.Mode        = ProfileType.GetName(activeProfile.TypeId);

            if (activeProfile.TypeId == ProfileType.MODE_AUTO_PROFILE_ID)
            {
                var locationId = await Db.Locations.Where(x => x.DeviceId == deviceId && x.Active).Select(x => x.Id).SingleOrDefaultAsync();

                var minTempValue = await Db.MinTemperatures.Where(x => x.LocationId == locationId && x.ProfileId == activeProfile.Id).SingleOrDefaultAsync();

                if (minTempValue == null)
                {
                    throw new RepositoryException($"{locationId} id'li Location'ın default MinTemp bilgisi bulunamadı.");
                }

                settings.MinTemperature = minTempValue.Value;
            }
            else if (activeProfile.TypeId == ProfileType.MODE_MANUAL_ID)
            {
                settings.State = await stateRepository.Get(deviceId);
            }
            else if (activeProfile.TypeId == ProfileType.MODE_AUTO_SERVER_PROFILE_ID)
            {
                var minTemp = await Db.MinTemperatures.Where(x => x.ProfileId == activeProfile.Id).SingleOrDefaultAsync(); // location bilinmediği için locationId verilmeden sorgulanıyor

                if (minTemp == null)
                {
                    throw new RepositoryException($"{activeProfile.Id} id'li Profile'ın MinTemp bilgisi bulunamadı.");
                }

                var latestWeather = await weatherRepository.GetLastMinutes(minTemp.LocationId, 30);

                if (latestWeather == null || latestWeather.Count == 0)
                {
                    settings.State = true;
                }
                else
                {
                    var     weathers       = latestWeather.Take(2).ToList();
                    var     averageWeather = weathers.Sum(x => x.Temperature) / (double)weathers.Count();
                    decimal difference     = (decimal)averageWeather - (decimal)minTemp.Value;
                    if (difference <= -0.1m)
                    {
                        settings.State = true;
                    }
                    else if (difference >= 0.12m)
                    {
                        settings.State = false;
                    }
                    else
                    {
                        settings.State = true;
                    }
                }
            }
            else if (activeProfile.TypeId == ProfileType.MODE_AUTO_PROFILE_SCHEDULED_1_ID)
            {
                // todo: fix it
                settings.ProfileDtos = await profileRepository.GetDtos(deviceId);

                settings.ProfilePreferenceDtos = await profileRepository.GetProfilePreferenceDtos(activeProfile.Id);
            }
            else if (activeProfile.TypeId == ProfileType.MODE_AUTO_SCHEDULED_1_ID)
            {
                // todo: fix it
                var locationId = await Db.Locations.Where(x => x.DeviceId == deviceId && x.Active).Select(x => x.Id).SingleOrDefaultAsync();

                settings.MinTemperatureDtos = await Db.MinTemperatures.Where(x => x.LocationId == locationId && x.ProfileId == activeProfile.Id).Select(x => new MinTemperatureDto()
                {
                    Value = x.Value
                }).ToListAsync();
            }

            return(settings);
        }