예제 #1
0
파일: DbMapper.cs 프로젝트: LexaGal/Plants
        public PlantMapping GetPlantMapping(Plant plant)
        {
            //if custom sensor
            StringBuilder builder = new StringBuilder();

            List<MeasurableParameter> customParameters = plant.MeasurableParameters.Where(m => m is CustomParameter).ToList();
            if (customParameters.Count != 0)
            {
                customParameters.ForEach(c => builder.Append(c.Id.ToString() + ','));
                builder.Remove(builder.Length - 1, 1);
            }
            return new PlantMapping(plant.Id, plant.Temperature.Id, plant.Humidity.Id,
                plant.SoilPh.Id, plant.Nutrient.Id, plant.Name.ToString(),
                builder.ToString());
        }
예제 #2
0
        public PlantsArea(Guid id, Guid userId, Plant plant, int number)
        {
            Id = id;
            UserId = userId;
            Plant = plant;
            Number = number;

            ServicesSchedulesStates = new ServicesSchedulesStates();

            PlantServicesStates = new PlantServicesStates();
            ParameterServicesInfo.ParametersServices
                .SelectMany(l => l.ServiceStates)
                .Distinct(new ServiceStateEqualityComparer())
                .Where(s => !s.IsCustom)
                .ToList()
                .ForEach(s => PlantServicesStates.AddServiceState(s.Clone() as ServiceState));
            
            Sensors = new List<Sensor>();
        }
예제 #3
0
파일: DbMapper.cs 프로젝트: LexaGal/Plants
        public Plant RestorePlant(PlantMapping plantMapping)
        {
            try
            {
                MeasurableParameterMapping temperatureMapping =
                    _sqlMeasurableParameterMappingRepository.Get(plantMapping.TemperatureId);
                MeasurableParameterMapping soilPhMapping =
                    _sqlMeasurableParameterMappingRepository.Get(plantMapping.SoilPhId);
                MeasurableParameterMapping humidityMapping =
                    _sqlMeasurableParameterMappingRepository.Get(plantMapping.HumidityId);
                MeasurableParameterMapping nutrientMapping =
                    _sqlMeasurableParameterMappingRepository.Get(plantMapping.NutrientId);

                Temperature temperature = RestoreMeasurableParameter(temperatureMapping) as Temperature;
                Humidity humidity = RestoreMeasurableParameter(humidityMapping) as Humidity;
                SoilPh soilPh = RestoreMeasurableParameter(soilPhMapping) as SoilPh;
                Nutrient nutrient = RestoreMeasurableParameter(nutrientMapping) as Nutrient;

                PlantNameEnum name = (PlantNameEnum)Enum.Parse(typeof(PlantNameEnum), plantMapping.Name);
                Plant plant = new Plant(plantMapping.Id, temperature, humidity, soilPh, nutrient, name);

                //if custom sensor
                if (!string.IsNullOrEmpty(plantMapping.CustomParametersIds))
                {
                    string[] ids = plantMapping.CustomParametersIds.Split(',');
                    List<MeasurableParameterMapping> measurableParameterMappings =
                        ids.Select(id => _sqlMeasurableParameterMappingRepository.Get(Guid.Parse(id))).ToList();

                    List<MeasurableParameter> measurableParameters =
                        measurableParameterMappings.Select(RestoreMeasurableParameter)
                            .ToList();

                    plant.AddMeasurableParameters(measurableParameters);
                }
                return plant;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace, $"Plant Id: {plantMapping.Id}");
                return null;
            }
        }
예제 #4
0
        private PlantsArea GetPlantsArea()
        {
            try
            {
                PlantNameEnum plantName = (PlantNameEnum) Enum.Parse(typeof (PlantNameEnum),
                    PlantNameBox.SelectionBoxItem.ToString());
                int number = Convert.ToInt32(Number.Text);

                int optimalT = Convert.ToInt32(OptimalTemperature.Text);
                int minT = Convert.ToInt32(MinTemperature.Text);
                int maxT = Convert.ToInt32(MaxTemperature.Text);
                Temperature temperature = new Temperature(Guid.NewGuid(), optimalT, minT, maxT);

                if (!temperature.HasValidParameters())
                {
                    throw new ApplicationException("Fields for Temperature are not valid - numeric values >= 0 and <= 100!");
                }

                int optimalH = Convert.ToInt32(OptimalHumidity.Text);
                int minH = Convert.ToInt32(MinHumidity.Text);
                int maxH = Convert.ToInt32(MaxHumidity.Text);
                Humidity humidity = new Humidity(Guid.NewGuid(), optimalH, minH, maxH);

                if (!humidity.HasValidParameters())
                {
                    throw new ApplicationException("Fields for Humidity are not valid - numeric values >= 0 and <= 100!");
                }

                int optimalS = Convert.ToInt32(OptimalSoilPh.Text);
                int minS = Convert.ToInt32(MinSoilPh.Text);
                int maxS = Convert.ToInt32(MaxSoilPh.Text);
                SoilPh soilPh = new SoilPh(Guid.NewGuid(), optimalS, minS, maxS);

                if (!soilPh.HasValidParameters())
                {
                    throw new ApplicationException("Fields for SoilPh are not valid - numeric values >= 0 and <= 100!");
                }

                int optimalN = Convert.ToInt32(OptimalNutrient.Text);
                int minN = Convert.ToInt32(MinNutrient.Text);
                int maxN = Convert.ToInt32(MaxNutrient.Text);
                Nutrient nutrient = new Nutrient(Guid.NewGuid(), optimalN, minN, maxN);

                if (!nutrient.HasValidParameters())
                {
                    throw new ApplicationException("Fields for Nutrient are not valid - numeric values >= 0 and <= 100!");
                }

                Plant plant = new Plant(Guid.NewGuid(), temperature, humidity, soilPh, nutrient, plantName);

                PlantsArea plantsArea = new PlantsArea(Guid.NewGuid(), default(Guid), plant, number);

                try
                {
                    TimeSpan temperatureTimeout = TimeSpan.Parse(TemperatureTimeout.Text);
                    TimeSpan humidityTimeout = TimeSpan.Parse(HumidityTimeout.Text);
                    TimeSpan soilPhTimeout = TimeSpan.Parse(SoilPhTimeout.Text);
                    TimeSpan nutrientTimeout = TimeSpan.Parse(NutrientTimeout.Text);

                    Sensor ts = new TemperatureSensor(Guid.NewGuid(), plantsArea, temperatureTimeout, temperature);
                    if (TemperatureCheckBox.IsChecked != null && !(bool) TemperatureCheckBox.IsChecked){ts.IsOffByUser = true;}

                    Sensor hs = new HumiditySensor(Guid.NewGuid(), plantsArea, humidityTimeout, humidity);
                    if (HumidityCheckBox.IsChecked != null && !(bool) HumidityCheckBox.IsChecked){hs.IsOffByUser = true;}

                    Sensor ss = new SoilPhSensor(Guid.NewGuid(), plantsArea, soilPhTimeout, soilPh);
                    if (SoilPhCheckBox.IsChecked != null && !(bool) SoilPhCheckBox.IsChecked){ss.IsOffByUser = true;}

                    Sensor ns = new NutrientSensor(Guid.NewGuid(), plantsArea, nutrientTimeout, nutrient);
                    if (NutrientCheckBox.IsChecked != null && !(bool) NutrientCheckBox.IsChecked){ns.IsOffByUser = true;}

                }
                catch (Exception)
                {
                    MessageBox.Show(@"Please, fill in all timeouts with TimeSpan values >= 0!");
                    return null;
                }

                plantsArea.ServicesSchedulesStates.AddServiceSchedule(new ServiceSchedule(Guid.NewGuid(), plantsArea.Id,
                    ServiceStateEnum.Nutrienting.ToString(), new TimeSpan(0, 0, 10), new TimeSpan(0, 1, 0),
                    new List<MeasurableParameter> {plantsArea.Plant.Nutrient, plantsArea.Plant.SoilPh}));

                plantsArea.ServicesSchedulesStates.AddServiceSchedule(new ServiceSchedule(Guid.NewGuid(), plantsArea.Id,
                    ServiceStateEnum.Watering.ToString(), new TimeSpan(0, 0, 10), new TimeSpan(0, 1, 0), 
                    new List<MeasurableParameter> { plantsArea.Plant.Humidity, plantsArea.Plant.Temperature }));
                
                return plantsArea;
            }
            catch (ApplicationException e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
            catch (FormatException e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        
        }