#23 Implementation of Gardenschedule for wateringtask adjustment

This commit is contained in:
Gian-Andrea Hutter 2022-11-27 10:13:11 +01:00
parent 65b15b6a4c
commit 4072308ae6
4 changed files with 54 additions and 8 deletions

View File

@ -1,5 +1,5 @@
package ch.zhaw.gartenverwaltung.io;
public enum SevereWeather {
FROST,SNOW,HAIL,NO_SEVERE_WEATHER
FROST,SNOW,HAIL,NO_SEVERE_WEATHER,RAIN
}

View File

@ -1,7 +1,7 @@
package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Task;
import ch.zhaw.gartenverwaltung.models.PlantNotFoundException;
import ch.zhaw.gartenverwaltung.types.*;
import java.io.IOException;
import java.time.LocalDate;
@ -9,15 +9,24 @@ import java.util.List;
public class WeatherGradenTaskPlanner {
private final TaskList taskList;
private final PlantList plantList;
private final CropList cropList;
WeatherService weatherService;
private final LocalDate dateSevereWeather = LocalDate.of(22,12,15);
public WeatherGradenTaskPlanner(TaskList taskList) {
public WeatherGradenTaskPlanner(TaskList taskList, PlantList plantList, CropList cropList) {
this.taskList = taskList;
this.plantList = plantList;
this.cropList = cropList;
weatherService = new WeatherService();
}
public void getWeatherEvents() throws IOException {
public void refreshTasks() throws IOException, HardinessZoneNotSetException, PlantNotFoundException {
getSevereWeatherEvents();
getRainAmount();
}
private void getSevereWeatherEvents() throws IOException {
SevereWeather actualWeather = weatherService.causeSevereWeather(0);
if (SevereWeather.HAIL.equals(actualWeather)) {
createPreHail();
@ -28,6 +37,11 @@ public class WeatherGradenTaskPlanner {
}
}
private void getRainAmount() throws IOException, HardinessZoneNotSetException, PlantNotFoundException {
int rainAmount = weatherService.causeRainAmount(3);
adjustWateringTask(rainAmount);
}
public void createPreHail() throws IOException {
Task preHailTask = new Task("Hail",
@ -54,11 +68,26 @@ public class WeatherGradenTaskPlanner {
taskList.saveTask(preSnowTask);
}
public void calculateRainAmount(){
public void adjustWateringTask(int rainAmount) throws HardinessZoneNotSetException, IOException, PlantNotFoundException {
for (Crop crop : cropList.getCrops()) {
Plant plant = plantList.getPlantById(HardinessZone.ZONE_8A,crop.getPlantId()).orElseThrow(PlantNotFoundException::new);
for (GrowthPhase growthphase : plant.lifecycle()) {
if(growthphase.wateringCycle().litersPerSqM() < rainAmount){
adjustNextExecutionOfWateringTask(taskList.getTaskList(LocalDate.now(), LocalDate.now().plusDays(7)));
}
}
}
}
public void adjustWateringTask(List<Crop> crops){
private void adjustNextExecutionOfWateringTask(List<Task> cropTaskList){
for(Task task : cropTaskList){
if(task.getName().equals("watering task")){
task.setNextExecution(task.getNextExecution().plusDays(task.getInterval().orElse(1)));
}
}
}
}

View File

@ -1,6 +1,10 @@
package ch.zhaw.gartenverwaltung.io;
public class WeatherService {
private static final int NO_RAIN = 0;
private static final int LITTLE_RAIN = 15;
private static final int RAIN = 25;
private static final int HEAVY_RAIN = 50;
public SevereWeather causeSevereWeather(int randomWeather) {
return switch (randomWeather) {
@ -11,4 +15,15 @@ public class WeatherService {
};
}
public int causeRainAmount(int randomRainAmount) {
return switch (randomRainAmount) {
case 1 -> NO_RAIN;
case 2 -> LITTLE_RAIN;
case 3 -> RAIN;
case 4 -> HEAVY_RAIN;
default -> -1;
};
}
}

View File

@ -52,7 +52,9 @@ public class GardenSchedule {
for (GrowthPhase growthPhase : plant.lifecycleForGroup(growPhaseGroup)) {
for (TaskTemplate taskTemplate : growthPhase.taskTemplates()) {
addTask(taskTemplate.generateTask(crop.getStartDate(), crop.getCropId().orElse(0L)));
//TODO add wateringtask
}
//growthPhase.wateringCycle()
}
}