Merge pull request #85 from schrom01/feature_weather

#23 bugfix testclass WeatherGradenTaskPlannerTest and bugfix Task and…
This commit is contained in:
giavaphi 2022-12-10 13:58:26 +01:00 committed by GitHub Enterprise
commit 083d934472
4 changed files with 71 additions and 17 deletions

View File

@ -25,11 +25,12 @@ public class BackgroundTasks extends TimerTask {
private void movePastTasks() throws IOException { private void movePastTasks() throws IOException {
List<Task> taskList = this.taskList.getTaskList(LocalDate.MIN, LocalDate.now().minusDays(1)); List<Task> taskList = this.taskList.getTaskList(LocalDate.MIN, LocalDate.now().minusDays(1));
taskList.forEach(task -> { for (Task task : taskList) {
if (!task.isDone()) { if (!task.isDone()) {
task.setNextExecution(LocalDate.now()); task.setNextExecution(LocalDate.now());
this.taskList.saveTask(task);
}
} }
});
} }
public BackgroundTasks(TaskList taskList, CropList cropList, PlantList plantList) { public BackgroundTasks(TaskList taskList, CropList cropList, PlantList plantList) {

View File

@ -9,8 +9,12 @@ import ch.zhaw.gartenverwaltung.types.*;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import static java.util.stream.Collectors.toList;
/** /**
* The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days * The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days
* *
@ -43,8 +47,7 @@ public class WeatherGradenTaskPlanner {
private void getSevereWeatherEvents() throws IOException { private void getSevereWeatherEvents() throws IOException {
SevereWeather actualWeather = weatherService.causeSevereWeather(1); SevereWeather actualWeather = weatherService.causeSevereWeather(1);
if (SevereWeather.HAIL.equals(actualWeather)) { if (SevereWeather.HAIL.equals(actualWeather)) {
//ToDo creates hail task all 3 seconds, pls fix createPreHailTask();
//createPreHailTask();
} else if (SevereWeather.FROST.equals(actualWeather)) { } else if (SevereWeather.FROST.equals(actualWeather)) {
createPreFrostTask(); createPreFrostTask();
} else if (SevereWeather.SNOW.equals(actualWeather)) { } else if (SevereWeather.SNOW.equals(actualWeather)) {
@ -63,11 +66,27 @@ public class WeatherGradenTaskPlanner {
*/ */
private void createPreHailTask() throws IOException { private void createPreHailTask() throws IOException {
List<Crop> actualCrops = cropList.getCrops();
for (Crop crop : actualCrops) {
Task preHailTask = new Task("Hail", Task preHailTask = new Task("Hail",
"During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin", "During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin",
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L); dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L));
taskList.saveTask(preHailTask);
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> hailTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Hail")).toList();
List<Task> hailTaskListAtDate = new ArrayList<>();
for (Task task : hailTasklist) {
if (task.getStartDate() == (preHailTask.getStartDate())) {
hailTaskListAtDate.add(task);
}
}
if(hailTaskListAtDate.isEmpty() && hailTasklist.isEmpty()){
taskList.saveTask(preHailTask);
}
}
} }
/** /**
@ -75,23 +94,56 @@ public class WeatherGradenTaskPlanner {
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void createPreFrostTask() throws IOException { private void createPreFrostTask() throws IOException {
List<Crop> actualCrops = cropList.getCrops();
for (Crop crop : actualCrops) {
Task preFrostTask = new Task("Frost", Task preFrostTask = new Task("Frost",
"The temperatur falls below zero degrees, cover especially the root with wool", "The temperatur falls below zero degrees, cover especially the root with wool",
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L); dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> frostTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Frost")).toList();
List<Task> frostTaskListAtDate = new ArrayList<>();
for (Task task : frostTasklist) {
if (task.getStartDate() == preFrostTask.getStartDate()) {
frostTaskListAtDate.add(task);
}
}
if(frostTaskListAtDate.isEmpty() && frostTasklist.isEmpty()){
taskList.saveTask(preFrostTask); taskList.saveTask(preFrostTask);
} }
}
}
/** /**
* Method to create a PreSnowTask * Method to create a PreSnowTask
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void createPreSnowTask() throws IOException { private void createPreSnowTask() throws IOException {
List<Crop> actualCrops = cropList.getCrops();
for (Crop crop : actualCrops) {
Task preSnowTask = new Task("Snow", Task preSnowTask = new Task("Snow",
"The weather brings little snowfall. Cover your crops", "The weather brings little snowfall. Cover your crops",
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L); dateSevereWeather, dateSevereWeather.plusDays(1L), crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> snowTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Snow")).toList();
List<Task> snowTaskListAtDate = new ArrayList<>();
for (Task task : snowTasklist) {
if (task.getStartDate() == preSnowTask.getStartDate()) {
snowTaskListAtDate.add(task);
}
}
if(snowTaskListAtDate .isEmpty() && snowTasklist.isEmpty()){
taskList.saveTask(preSnowTask); taskList.saveTask(preSnowTask);
} }
}
}
/** /**
* Method to adjust the water plant tasks * Method to adjust the water plant tasks
* @param rainAmount Amount of rain from the last 7 days * @param rainAmount Amount of rain from the last 7 days

View File

@ -63,6 +63,7 @@ public class Task {
this.startDate = startDate; this.startDate = startDate;
nextExecution = startDate; nextExecution = startDate;
this.endDate = endDate; this.endDate = endDate;
this.cropId = cropId;
} }
/** /**

View File

@ -52,7 +52,7 @@ public class WeatherGardenTaskPlannerTest {
exampleTask.setNextExecution(LocalDate.now().plusDays(1L)); exampleTask.setNextExecution(LocalDate.now().plusDays(1L));
exampleWeatherTask = new Task("Hail", exampleWeatherTask = new Task("Hail",
"During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin", "During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin",
LocalDate.now().minusDays(1L),LocalDate.now().plusDays(1L),3L); LocalDate.now(),LocalDate.now().plusDays(1L),3L);
taskList = new JsonTaskList(testFile); taskList = new JsonTaskList(testFile);