From 670938ef85844b77b6a38d5e0d5880aa74dcbec6 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Tue, 22 Nov 2022 14:44:56 +0100 Subject: [PATCH 01/23] #23 added constructor in Task class, implemented SevereWeather, WeatherService, WeatherGradenTaskPlanner --- .../gartenverwaltung/io/SevereWeather.java | 5 ++ .../io/WeatherGradenTaskPlanner.java | 64 +++++++++++++++++++ .../gartenverwaltung/io/WeatherService.java | 14 ++++ .../ch/zhaw/gartenverwaltung/types/Task.java | 8 +++ 4 files changed, 91 insertions(+) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java b/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java new file mode 100644 index 0000000..d76036f --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java @@ -0,0 +1,5 @@ +package ch.zhaw.gartenverwaltung.io; + +public enum SevereWeather { + FROST,SNOW,HAIL,NO_SEVERE_WEATHER +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java new file mode 100644 index 0000000..ec8dd48 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java @@ -0,0 +1,64 @@ +package ch.zhaw.gartenverwaltung.io; + +import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.Task; + +import java.io.IOException; +import java.time.LocalDate; +import java.util.List; + +public class WeatherGradenTaskPlanner { + private final TaskList taskList; + WeatherService weatherService; + private final LocalDate dateSevereWeather = LocalDate.of(22,12,15); + + public WeatherGradenTaskPlanner(TaskList taskList) { + this.taskList = taskList; + weatherService = new WeatherService(); + } + + public void getWeatherEvents() throws IOException { + Enum actualWeather = weatherService.causeSevereWeather(0); + if (SevereWeather.HAIL.equals(actualWeather)) { + createPreHail(); + } else if (SevereWeather.FROST.equals(actualWeather)) { + createPreFrost(); + } else if (SevereWeather.SNOW.equals(actualWeather)) { + createPreSnow(); + } + } + + public void createPreHail() throws IOException { + + 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", + dateSevereWeather); + preHailTask.withId(Long.MAX_VALUE); + taskList.saveTask(preHailTask); + + } + + public void createPreFrost() throws IOException { + Task preFrostTask = new Task("Frost", + "The temperatur falls below zero degrees, cover especially the root with wool", + dateSevereWeather); + preFrostTask.withId(Long.MAX_VALUE); + taskList.saveTask(preFrostTask); + } + + public void createPreSnow() throws IOException { + Task preSnowTask = new Task("Snow", + "The weather brings little snowfall. Cover your crops", + dateSevereWeather); + preSnowTask.withId(Long.MAX_VALUE); + taskList.saveTask(preSnowTask); + } + + public void calculateRainAmount(){ + + } + + public void adjustWateringTask(List crops){ + + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java new file mode 100644 index 0000000..fffe1bc --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java @@ -0,0 +1,14 @@ +package ch.zhaw.gartenverwaltung.io; + +public class WeatherService { + + public Enum causeSevereWeather(int randomWeather){ + return switch (randomWeather) { + case 1 -> SevereWeather.HAIL; + case 2 -> SevereWeather.SNOW; + case 3 -> SevereWeather.FROST; + default -> null; + }; + + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index df15fc4..a2bc6a0 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -33,6 +33,14 @@ public class Task { this.startDate = startDate; this.cropId = cropId; } + /** + * Constructor for weather events + */ + public Task(String name, String description, LocalDate startDate) { + this.name = name; + this.description = description; + this.startDate = startDate; + } public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval, long cropId) { this.name = name; From d2b8fe4ba26597ec5074ab84f575e5e43dcdfdb0 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Tue, 22 Nov 2022 14:48:09 +0100 Subject: [PATCH 02/23] #23 small adjustments --- .../zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java index ec8dd48..ead783f 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java @@ -32,7 +32,7 @@ public class WeatherGradenTaskPlanner { 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", - dateSevereWeather); + dateSevereWeather.minusDays(1L)); preHailTask.withId(Long.MAX_VALUE); taskList.saveTask(preHailTask); @@ -41,7 +41,7 @@ public class WeatherGradenTaskPlanner { public void createPreFrost() throws IOException { Task preFrostTask = new Task("Frost", "The temperatur falls below zero degrees, cover especially the root with wool", - dateSevereWeather); + dateSevereWeather.minusDays(1L)); preFrostTask.withId(Long.MAX_VALUE); taskList.saveTask(preFrostTask); } @@ -49,7 +49,7 @@ public class WeatherGradenTaskPlanner { public void createPreSnow() throws IOException { Task preSnowTask = new Task("Snow", "The weather brings little snowfall. Cover your crops", - dateSevereWeather); + dateSevereWeather.minusDays(1L)); preSnowTask.withId(Long.MAX_VALUE); taskList.saveTask(preSnowTask); } From a437236788ccadc5fd3bd56b3fdb5d6e5acfb553 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 24 Nov 2022 23:45:33 +0100 Subject: [PATCH 03/23] created Class Notifier --- .../gartenverwaltung/notifier/Notifier.java | 38 +++++++++++++++++++ .../ch/zhaw/gartenverwaltung/types/Task.java | 2 + src/main/java/module-info.java | 2 + .../gartenverwaltung/io/template-taskdb.json | 12 ++++++ .../gartenverwaltung/io/test-plantdb.json | 27 +++++-------- 5 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java new file mode 100644 index 0000000..857fa1d --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java @@ -0,0 +1,38 @@ +package ch.zhaw.gartenverwaltung.notifier; + +import ch.zhaw.gartenverwaltung.models.GardenSchedule; +import ch.zhaw.gartenverwaltung.types.Task; + +import java.io.IOException; +import java.time.LocalDate; +import java.util.function.Consumer; + +public class Notifier implements Runnable{ + private final GardenSchedule gardenSchedule; + + public Notifier(GardenSchedule gardenSchedule) { + this.gardenSchedule = gardenSchedule; + } + + private void sendNotification(Task task){ + // TODO implement + } + + @Override + public void run() { + try { + gardenSchedule.getTaskList().forEach(new Consumer() { + @Override + public void accept(Task task) { + if(task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))){ + sendNotification(task); + task.setNextNotification(task.getNextExecution()); + } + } + }); + } catch (IOException e) { + // TODO Logger + e.printStackTrace(); + } + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index dab4d39..509eb7a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -69,8 +69,10 @@ public class Task { public void done(){ if(interval != null && !nextExecution.plusDays(interval).isAfter(endDate)){ nextExecution = nextExecution.plusDays(interval); + nextNotification = nextExecution; } else { nextExecution = null; + nextNotification = null; } } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 44103dd..38607ea 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -13,4 +13,6 @@ module ch.zhaw.gartenverwaltung { exports ch.zhaw.gartenverwaltung.types; exports ch.zhaw.gartenverwaltung.models; exports ch.zhaw.gartenverwaltung.json; + exports ch.zhaw.gartenverwaltung.notifier; + opens ch.zhaw.gartenverwaltung.notifier to javafx.fxml; } \ No newline at end of file diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/template-taskdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/template-taskdb.json index 7728bb0..b10124d 100644 --- a/src/test/resources/ch/zhaw/gartenverwaltung/io/template-taskdb.json +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/template-taskdb.json @@ -4,6 +4,8 @@ "name" : "sow plant", "description": "Plant the seeds, crops in de bed.", "startDate" : "2022-05-01", + "nextExecution": "2022-05-01", + "nextNotification": "2022-05-01", "endDate" : "2022-05-01", "interval" : 0, "cropId" : 0 @@ -13,6 +15,8 @@ "name" : "water plant", "description": "water the plant, so that the soil is wet around the plant.", "startDate" : "2022-05-01", + "nextExecution": "2022-05-01", + "nextNotification": "2022-05-01", "endDate" : "2022-09-01", "interval" : 2, "cropId" : 0 @@ -22,6 +26,8 @@ "name" : "fertilize plant", "description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture", "startDate" : "2022-06-01", + "nextExecution": "2022-05-01", + "nextNotification": "2022-05-01", "endDate" : "2022-08-01", "interval" : 28, "cropId" : 0 @@ -31,6 +37,8 @@ "name" : "covering plant", "description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage", "startDate" : "2022-07-01", + "nextExecution": "2022-05-01", + "nextNotification": "2022-05-01", "endDate" : "2022-07-01", "interval" : 0, "cropId" : 0 @@ -40,6 +48,8 @@ "name" : "look after plant", "description": "Look for pest or illness at the leaves of the plant. Check the soil around the plant, if the roots are enough covered with soil", "startDate" : "2022-05-01", + "nextExecution": "2022-05-01", + "nextNotification": "2022-05-01", "endDate" : "2022-09-01", "interval" : 5, "cropId" : 0 @@ -49,6 +59,8 @@ "name" : "harvest plant", "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", "startDate" : "2022-09-01", + "nextExecution": "2022-05-01", + "nextNotification": "2022-05-01", "endDate" : "2022-09-01", "interval" : 0, "cropId" : 0 diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/test-plantdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-plantdb.json index 5440e42..4268df1 100644 --- a/src/test/resources/ch/zhaw/gartenverwaltung/io/test-plantdb.json +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-plantdb.json @@ -32,8 +32,7 @@ "relativeStartDate": -14, "relativeEndDate": null, "description": "Take an egg carton and fill it with soil. Put the seedling deep enough so its half covered with soil. Keep it in 10-15 * Celsius with lots of light.", - "interval": null, - "isOptional": false + "interval": null } ] }, @@ -54,8 +53,7 @@ "relativeStartDate": 0, "relativeEndDate": null, "description": "When the plants are 20 cm tall, begin hilling the potatoes by gently mounding the soil from the center of your rows around the stems of the plant. Mound up the soil around the plant until just the top few leaves show above the soil. Two weeks later, hill up the soil again when the plants grow another 20 cm.", - "interval": 21, - "isOptional": false + "interval": 21 } ] }, @@ -76,8 +74,7 @@ "relativeStartDate": 0, "relativeEndDate": null, "description": "Once the foliage has wilted and dried completely, harvest on a dry day. Store in a dark and cool location.", - "interval": null, - "isOptional": false + "interval": null } ] } @@ -106,8 +103,7 @@ "relativeStartDate": 0, "relativeEndDate": 0, "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", - "interval": null, - "isOptional": false + "interval": null } ] }, @@ -130,8 +126,7 @@ "relativeStartDate": 0, "relativeEndDate": null, "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", - "interval": 15, - "isOptional": true + "interval": 15 } ] }, @@ -152,8 +147,7 @@ "relativeStartDate": 0, "relativeEndDate": 14, "description": "When the leaves turn to a yellowish brown. Do not harvest earlier. The plant will show when it's ready.", - "interval": null, - "isOptional": false + "interval": null } ] } @@ -193,8 +187,7 @@ "relativeStartDate": 0, "relativeEndDate": 0, "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", - "interval": null, - "isOptional": false + "interval": null } ] }, @@ -217,8 +210,7 @@ "relativeStartDate": 0, "relativeEndDate": null, "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", - "interval": 15, - "isOptional": true + "interval": 15 } ] }, @@ -241,8 +233,7 @@ "relativeStartDate": 0, "relativeEndDate": 14, "description": "When ready for harvest, the leaves on your onion plants will start to flop over. This happens at the \"neck\" of the onion and it signals that the plant has stopped growing and is ready for storage. Onions should be harvested soon thereafter", - "interval": null, - "isOptional": false + "interval": null } ] } From 07569b83e1e90eb3f7bdfe847f0bb4a1e3be64ca Mon Sep 17 00:00:00 2001 From: David Guler Date: Fri, 25 Nov 2022 12:39:10 +0100 Subject: [PATCH 04/23] fix: syntax errors --- .../ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java | 2 +- src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java index ead783f..1d19cd9 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java @@ -18,7 +18,7 @@ public class WeatherGradenTaskPlanner { } public void getWeatherEvents() throws IOException { - Enum actualWeather = weatherService.causeSevereWeather(0); + SevereWeather actualWeather = weatherService.causeSevereWeather(0); if (SevereWeather.HAIL.equals(actualWeather)) { createPreHail(); } else if (SevereWeather.FROST.equals(actualWeather)) { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java index fffe1bc..a82177c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java @@ -2,7 +2,7 @@ package ch.zhaw.gartenverwaltung.io; public class WeatherService { - public Enum causeSevereWeather(int randomWeather){ + public SevereWeather causeSevereWeather(int randomWeather) { return switch (randomWeather) { case 1 -> SevereWeather.HAIL; case 2 -> SevereWeather.SNOW; From 2d88c9ea9108449d3d60f60285cb5bfec54f2945 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 25 Nov 2022 12:58:03 +0100 Subject: [PATCH 05/23] implemented multithreading --- .../gartenverwaltung/HelloApplication.java | 7 +++++ .../gartenverwaltung/bootstrap/AppLoader.java | 6 ++++ .../models/GardenSchedule.java | 1 - .../gartenverwaltung/notifier/Notifier.java | 28 +++++++++++++------ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java b/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java index 9018a31..0b8a0a1 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java @@ -1,16 +1,23 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; +import ch.zhaw.gartenverwaltung.notifier.Notifier; import javafx.application.Application; import javafx.stage.Stage; import java.io.IOException; public class HelloApplication extends Application { + @Override public void start(Stage stage) throws IOException { AppLoader appLoader = new AppLoader(); + Notifier notifier = new Notifier(appLoader.getGardenSchedule()); + Thread notificationThread = new Thread(notifier); + notificationThread.start(); + notificationThread.interrupt(); + appLoader.loadSceneToStage("MainFXML.fxml", stage); stage.setTitle("Gartenverwaltung"); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java index 59a4f7d..6c660ab 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java @@ -10,6 +10,7 @@ import ch.zhaw.gartenverwaltung.io.TaskList; import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.models.PlantListModel; +import ch.zhaw.gartenverwaltung.notifier.Notifier; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.DialogPane; @@ -41,6 +42,7 @@ public class AppLoader { public AppLoader() throws IOException { + } @@ -154,4 +156,8 @@ public class AppLoader { default -> null; }; } + + public GardenSchedule getGardenSchedule() { + return gardenSchedule; + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index f4b2bb3..936eaf6 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -9,7 +9,6 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.function.Consumer; import java.util.stream.Collectors; public class GardenSchedule { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java index 857fa1d..26e22fc 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java @@ -9,6 +9,7 @@ import java.util.function.Consumer; public class Notifier implements Runnable{ private final GardenSchedule gardenSchedule; + private boolean interrupted = false; public Notifier(GardenSchedule gardenSchedule) { this.gardenSchedule = gardenSchedule; @@ -18,16 +19,13 @@ public class Notifier implements Runnable{ // TODO implement } - @Override - public void run() { + private void sendNotifications(){ + System.out.println("sending Notifications"); try { - gardenSchedule.getTaskList().forEach(new Consumer() { - @Override - public void accept(Task task) { - if(task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))){ - sendNotification(task); - task.setNextNotification(task.getNextExecution()); - } + gardenSchedule.getTaskList().forEach(task -> { + if(task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))){ + sendNotification(task); + task.setNextNotification(task.getNextExecution()); } }); } catch (IOException e) { @@ -35,4 +33,16 @@ public class Notifier implements Runnable{ e.printStackTrace(); } } + + @Override + public void run() { + while(!interrupted){ + try { + sendNotifications(); + Thread.sleep(3000); + } catch (InterruptedException e) { + interrupted = true; + } + } + } } From 4072308ae64259e67b0f2d0966f50467c2079e9d Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Sun, 27 Nov 2022 10:13:11 +0100 Subject: [PATCH 06/23] #23 Implementation of Gardenschedule for wateringtask adjustment --- .../gartenverwaltung/io/SevereWeather.java | 2 +- .../io/WeatherGradenTaskPlanner.java | 43 ++++++++++++++++--- .../gartenverwaltung/io/WeatherService.java | 15 +++++++ .../models/GardenSchedule.java | 2 + 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java b/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java index d76036f..239e34a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java @@ -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 } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java index 1d19cd9..1e13b74 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java @@ -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 crops){ - + private void adjustNextExecutionOfWateringTask(List cropTaskList){ + for(Task task : cropTaskList){ + if(task.getName().equals("watering task")){ + task.setNextExecution(task.getNextExecution().plusDays(task.getInterval().orElse(1))); + } + } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java index a82177c..510b292 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java @@ -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; + }; + + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index c3292f6..dde9dec 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -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() } } From 233ce6b088599114f647d43e644f93f36bdf7937 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Sun, 27 Nov 2022 23:42:29 +0100 Subject: [PATCH 07/23] #23 add Task generation watertask --- .../ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java | 8 ++------ .../ch/zhaw/gartenverwaltung/models/GardenSchedule.java | 9 +++++++-- src/main/java/ch/zhaw/gartenverwaltung/types/Task.java | 1 + 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java index 9c14010..218ffda 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java @@ -1,12 +1,7 @@ package ch.zhaw.gartenverwaltung.bootstrap; import ch.zhaw.gartenverwaltung.HelloApplication; -import ch.zhaw.gartenverwaltung.io.CropList; -import ch.zhaw.gartenverwaltung.io.JsonCropList; -import ch.zhaw.gartenverwaltung.io.JsonPlantList; -import ch.zhaw.gartenverwaltung.io.JsonTaskList; -import ch.zhaw.gartenverwaltung.io.PlantList; -import ch.zhaw.gartenverwaltung.io.TaskList; +import ch.zhaw.gartenverwaltung.io.*; import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.models.PlantListModel; @@ -37,6 +32,7 @@ public class AppLoader { private final TaskList taskList = new JsonTaskList(); private final GardenSchedule gardenSchedule = new GardenSchedule(taskList, plantList); + private final WeatherGradenTaskPlanner weatherGradenTaskPlanner = new WeatherGradenTaskPlanner(taskList,plantList,cropList); private final Garden garden = new Garden(gardenSchedule, cropList); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index dde9dec..9502674 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -52,9 +52,14 @@ 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 + + } + //TODO add wateringtask + if(growthPhase.wateringCycle() != null){ + addTask(new Task("watering Task", "pour water over the plant circa : "+ growthPhase.wateringCycle().litersPerSqM() +" per square meter", + growthPhase.startDate().atYear(LocalDate.now().getYear()), growthPhase.endDate().atYear(LocalDate.now().getYear()), + growthPhase.wateringCycle().interval(), crop.getCropId().orElse(0L))); } - //growthPhase.wateringCycle() } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index 16fd155..c7d506a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -39,6 +39,7 @@ public class Task { nextExecution = startDate; this.cropId = cropId; } + /** * Constructor for weather events */ From fbf1700c34d343b2debde0bb324986a1858590ab Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 28 Nov 2022 08:00:31 +0100 Subject: [PATCH 08/23] implemented EMailSender --- build.gradle | 2 + .../gartenverwaltung/HelloApplication.java | 16 +++++-- .../ch/zhaw/gartenverwaltung/Settings.java | 28 ++++++++++- .../backgroundtasks/BackgroundTasks.java | 42 ++++++++++++++++ .../backgroundtasks/Notifier.java | 42 ++++++++++++++++ .../backgroundtasks/email/EMailSender.java | 44 +++++++++++++++++ .../email/SmtpCredentials.java | 41 ++++++++++++++++ .../gartenverwaltung/bootstrap/AppLoader.java | 13 +++-- .../gartenverwaltung/notifier/Notifier.java | 48 ------------------- src/main/java/module-info.java | 8 +++- 10 files changed, 225 insertions(+), 59 deletions(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/SmtpCredentials.java delete mode 100644 src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java diff --git a/build.gradle b/build.gradle index 35ebaa2..8c3b14a 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,8 @@ dependencies { implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.4' testImplementation 'org.mockito:mockito-core:4.3.+' + implementation 'com.sun.mail:javax.mail:1.6.2' + } test { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java b/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java index 0b8a0a1..1d8ed5a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java @@ -1,22 +1,23 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; -import ch.zhaw.gartenverwaltung.notifier.Notifier; +import ch.zhaw.gartenverwaltung.backgroundtasks.BackgroundTasks; import javafx.application.Application; import javafx.stage.Stage; import java.io.IOException; +import java.util.Timer; public class HelloApplication extends Application { + Timer backGroundTaskTimer = new Timer(); + BackgroundTasks backgroundTasks; @Override public void start(Stage stage) throws IOException { AppLoader appLoader = new AppLoader(); - Notifier notifier = new Notifier(appLoader.getGardenSchedule()); - Thread notificationThread = new Thread(notifier); - notificationThread.start(); - notificationThread.interrupt(); + backgroundTasks = new BackgroundTasks(appLoader.getTaskList(), appLoader.getGarden(), appLoader.getPlantList()); + backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 1000); appLoader.loadSceneToStage("MainFXML.fxml", stage); @@ -24,6 +25,11 @@ public class HelloApplication extends Application { stage.show(); } + @Override + public void stop(){ + backGroundTaskTimer.cancel(); + } + public static void main(String[] args) { launch(); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Settings.java b/src/main/java/ch/zhaw/gartenverwaltung/Settings.java index c15454c..63cf6e7 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/Settings.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/Settings.java @@ -1,13 +1,23 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.backgroundtasks.email.SmtpCredentials; import ch.zhaw.gartenverwaltung.types.HardinessZone; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; +import java.util.List; + public class Settings { + private static final Settings instance; private HardinessZone currentHardinessZone = HardinessZone.ZONE_8A; - private static Settings instance; private final BooleanProperty showTutorial = new SimpleBooleanProperty(false); + private SmtpCredentials smtpCredentials = new SmtpCredentials("imap.gmail.com", "587", "pm3.hs22.it21b.win.team1@gmail.com", "pm3.hs22.it21b.win.team1@gmail.com", "bisefhhjtrrhtoqr", true); + // Gmail Address: pm3.hs22.it21b.win.team1@gmail.com + // Gmail Passwort: Gartenverwaltung.PM3.2022 + // E-Mail Inbox: https://www.mailinator.com/v4/public/inboxes.jsp?to=pm3.hs22.it21b.win.team1 + private String mailNotificationReceivers = "pm3.hs22.it21b.win.team1@mailinator.com"; + private String mailNotificationSubjectTemplate = "Task %s is due!"; // {0} = Task Name + private String mailNotificationTextTemplate = "Dear user\nYour gardentask %s is due since %tF. Don't forget to confirm in your application if the task is done.\nTask description:\n%s"; // {0} = Task Name, {1} = nextExecution, {2} = Task description static { instance = new Settings(); @@ -38,4 +48,20 @@ public class Settings { public boolean getShowTutorial() { return this.showTutorial.get(); } + + public SmtpCredentials getSmtpCredentials() { + return smtpCredentials; + } + + public String getMailNotificationReceivers() { + return mailNotificationReceivers; + } + + public String getMailNotificationSubjectTemplate() { + return mailNotificationSubjectTemplate; + } + + public String getMailNotificationTextTemplate() { + return mailNotificationTextTemplate; + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java new file mode 100644 index 0000000..52e902e --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java @@ -0,0 +1,42 @@ +package ch.zhaw.gartenverwaltung.backgroundtasks; + +import ch.zhaw.gartenverwaltung.io.PlantList; +import ch.zhaw.gartenverwaltung.io.TaskList; +import ch.zhaw.gartenverwaltung.models.Garden; + +import javax.mail.MessagingException; +import java.io.IOException; +import java.util.TimerTask; + +public class BackgroundTasks extends TimerTask { + private final TaskList taskList; + private final Garden garden; + private final PlantList plantList; + private final Notifier notifier; + //TODO uncomment: privat final WeatherGardenTaskPlaner weatherGardenTaskPlaner; + + public BackgroundTasks(TaskList taskList, Garden garden, PlantList plantList) { + this.taskList = taskList; + this.garden = garden; + this.plantList = plantList; + notifier = new Notifier(taskList, garden, plantList); + //TODO uncomment: weatherGardenTaskPlaner = new WeatherGardenTaskPlaner(taskList); + } + + @Override + public void run() { + // TODO uncomment: weatherGardenTaskPlaner.refreshTasks(); + try { + try { + notifier.sendNotifications(); + } catch (MessagingException e) { + e.printStackTrace(); + // TODO logger + } + } catch (IOException e) { + e.printStackTrace(); + // TODO logger + } + } +} + diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java new file mode 100644 index 0000000..f36913f --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java @@ -0,0 +1,42 @@ +package ch.zhaw.gartenverwaltung.backgroundtasks; + +import ch.zhaw.gartenverwaltung.Settings; +import ch.zhaw.gartenverwaltung.backgroundtasks.email.EMailSender; +import ch.zhaw.gartenverwaltung.io.PlantList; +import ch.zhaw.gartenverwaltung.io.TaskList; +import ch.zhaw.gartenverwaltung.models.Garden; +import ch.zhaw.gartenverwaltung.types.Task; + +import javax.mail.MessagingException; +import java.io.IOException; +import java.time.LocalDate; + +public class Notifier { + private final TaskList taskList; + private final Garden garden; + private final PlantList plantList; + private final EMailSender eMailSender = new EMailSender(); + + public Notifier(TaskList taskList, Garden garden, PlantList plantList) { + this.taskList = taskList; + this.garden = garden; + this.plantList = plantList; + } + + private void sendNotification(Task task) throws MessagingException { + String messageSubject = String.format(Settings.getInstance().getMailNotificationSubjectTemplate(), task.getName()); + String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), task.getNextExecution(), task.getDescription()); + //TODO create Exception for email sent fail + eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText); + } + + public void sendNotifications() throws IOException, MessagingException { + System.out.println("sending Notifications"); + for (Task task : taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)) { + if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) { + sendNotification(task); + task.setNextNotification(LocalDate.now().plusDays(1)); + } + } + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java new file mode 100644 index 0000000..734967c --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java @@ -0,0 +1,44 @@ +package ch.zhaw.gartenverwaltung.backgroundtasks.email; + +import ch.zhaw.gartenverwaltung.Settings; + +import javax.mail.Address; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +public class EMailSender { + + + public EMailSender(){ + + } + + public void sendMails(String recipients, String subject, String text) throws MessagingException { + // TODO replace printMail with implementation + printMail(recipients, subject, text); // TODO Remove Printing E-Mail to console to test it + MimeMessage message = new MimeMessage(Settings.getInstance().getSmtpCredentials().getSession()); + message.addHeader("Content-type", "text/HTML; charset=UTF-8"); + message.addHeader("format", "flowed"); + message.addHeader("Content-Transfer-Encoding", "8bit"); + message.setFrom(new InternetAddress(Settings.getInstance().getSmtpCredentials().fromAddress())); + message.setReplyTo(InternetAddress.parse(Settings.getInstance().getSmtpCredentials().fromAddress(), false)); + message.setSubject(subject); + message.setText(text); + message.setSentDate(new Date()); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false)); + System.out.println("Message is ready: "); + Transport.send(message); + } + + private void printMail(String receiver, String subject, String text){ + System.out.printf("\nSending E-Mail:\nTo: %s\nSubject: %s\nMessage:\n%s\n", receiver, subject, text); + } + +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/SmtpCredentials.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/SmtpCredentials.java new file mode 100644 index 0000000..5e066ec --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/SmtpCredentials.java @@ -0,0 +1,41 @@ +package ch.zhaw.gartenverwaltung.backgroundtasks.email; + +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import java.net.Authenticator; +import java.util.Properties; + +public record SmtpCredentials( + String host, + String port, + String fromAddress, + String username, + String password, + boolean startTLS + ) { + + private Properties getProperties() { + Properties properties = new Properties(); + properties.put("mail.smtp.host", host); + properties.put("mail.smtp.port", port); + properties.put("mail.smtp.auth", "true"); + properties.put("mail.smtp.starttls.enable", startTLS ? "true" : "false"); + return properties; + } + + private javax.mail.Authenticator getAuthenticator() { + return new javax.mail.Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }; + } + + public Session getSession() { + return Session.getInstance(getProperties(), getAuthenticator()); + } + + + +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java index 6c660ab..2065278 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java @@ -10,7 +10,6 @@ import ch.zhaw.gartenverwaltung.io.TaskList; import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.models.PlantListModel; -import ch.zhaw.gartenverwaltung.notifier.Notifier; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.DialogPane; @@ -157,7 +156,15 @@ public class AppLoader { }; } - public GardenSchedule getGardenSchedule() { - return gardenSchedule; + public PlantList getPlantList() { + return plantList; + } + + public Garden getGarden() { + return garden; + } + + public TaskList getTaskList() { + return taskList; } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java deleted file mode 100644 index 26e22fc..0000000 --- a/src/main/java/ch/zhaw/gartenverwaltung/notifier/Notifier.java +++ /dev/null @@ -1,48 +0,0 @@ -package ch.zhaw.gartenverwaltung.notifier; - -import ch.zhaw.gartenverwaltung.models.GardenSchedule; -import ch.zhaw.gartenverwaltung.types.Task; - -import java.io.IOException; -import java.time.LocalDate; -import java.util.function.Consumer; - -public class Notifier implements Runnable{ - private final GardenSchedule gardenSchedule; - private boolean interrupted = false; - - public Notifier(GardenSchedule gardenSchedule) { - this.gardenSchedule = gardenSchedule; - } - - private void sendNotification(Task task){ - // TODO implement - } - - private void sendNotifications(){ - System.out.println("sending Notifications"); - try { - gardenSchedule.getTaskList().forEach(task -> { - if(task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))){ - sendNotification(task); - task.setNextNotification(task.getNextExecution()); - } - }); - } catch (IOException e) { - // TODO Logger - e.printStackTrace(); - } - } - - @Override - public void run() { - while(!interrupted){ - try { - sendNotifications(); - Thread.sleep(3000); - } catch (InterruptedException e) { - interrupted = true; - } - } - } -} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 38607ea..159fd0b 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -5,6 +5,8 @@ module ch.zhaw.gartenverwaltung { requires com.fasterxml.jackson.datatype.jsr310; requires com.fasterxml.jackson.datatype.jdk8; requires java.logging; + requires java.mail; + opens ch.zhaw.gartenverwaltung to javafx.fxml; opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind; @@ -13,6 +15,8 @@ module ch.zhaw.gartenverwaltung { exports ch.zhaw.gartenverwaltung.types; exports ch.zhaw.gartenverwaltung.models; exports ch.zhaw.gartenverwaltung.json; - exports ch.zhaw.gartenverwaltung.notifier; - opens ch.zhaw.gartenverwaltung.notifier to javafx.fxml; + exports ch.zhaw.gartenverwaltung.backgroundtasks; + opens ch.zhaw.gartenverwaltung.backgroundtasks to javafx.fxml; + exports ch.zhaw.gartenverwaltung.backgroundtasks.email; + opens ch.zhaw.gartenverwaltung.backgroundtasks.email to javafx.fxml; } \ No newline at end of file From fd184e1248386bdedf41fa5642a3385dbcb0f56f Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 28 Nov 2022 09:11:37 +0100 Subject: [PATCH 09/23] implemented Exception handling in EMailSender --- .../backgroundtasks/BackgroundTasks.java | 6 ------ .../backgroundtasks/email/EMailSender.java | 13 +++++++------ 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java index 52e902e..2cb5e2e 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java @@ -9,16 +9,10 @@ import java.io.IOException; import java.util.TimerTask; public class BackgroundTasks extends TimerTask { - private final TaskList taskList; - private final Garden garden; - private final PlantList plantList; private final Notifier notifier; //TODO uncomment: privat final WeatherGardenTaskPlaner weatherGardenTaskPlaner; public BackgroundTasks(TaskList taskList, Garden garden, PlantList plantList) { - this.taskList = taskList; - this.garden = garden; - this.plantList = plantList; notifier = new Notifier(taskList, garden, plantList); //TODO uncomment: weatherGardenTaskPlaner = new WeatherGardenTaskPlaner(taskList); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java index 734967c..82f895a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java @@ -2,10 +2,7 @@ package ch.zhaw.gartenverwaltung.backgroundtasks.email; import ch.zhaw.gartenverwaltung.Settings; -import javax.mail.Address; -import javax.mail.Message; -import javax.mail.MessagingException; -import javax.mail.Transport; +import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.ArrayList; @@ -33,8 +30,12 @@ public class EMailSender { message.setText(text); message.setSentDate(new Date()); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false)); - System.out.println("Message is ready: "); - Transport.send(message); + try{ + Transport.send(message); + } catch (MessagingException e) { + e.printStackTrace(); + // TODO logger + } } private void printMail(String receiver, String subject, String text){ From cae6f950ad6cd80c0ea6402d0115f47683ecb762 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Mon, 28 Nov 2022 10:20:00 +0100 Subject: [PATCH 10/23] #23 comments added --- .../ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java index 1e13b74..9e14a03 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java @@ -73,6 +73,7 @@ public class WeatherGradenTaskPlanner { for (Crop crop : cropList.getCrops()) { Plant plant = plantList.getPlantById(HardinessZone.ZONE_8A,crop.getPlantId()).orElseThrow(PlantNotFoundException::new); + // nur für aktuelle growthphase for (GrowthPhase growthphase : plant.lifecycle()) { if(growthphase.wateringCycle().litersPerSqM() < rainAmount){ @@ -82,7 +83,7 @@ public class WeatherGradenTaskPlanner { } } - +// vom startdatum abhängig, private void adjustNextExecutionOfWateringTask(List cropTaskList){ for(Task task : cropTaskList){ if(task.getName().equals("watering task")){ From eccb519bfbde8824bbabc06988cb36b2b7bf54e0 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Mon, 28 Nov 2022 13:53:47 +0100 Subject: [PATCH 11/23] #23 watering task refactor --- .../gartenverwaltung/bootstrap/AppLoader.java | 1 - .../io/WeatherGradenTaskPlanner.java | 7 +- .../models/GardenSchedule.java | 11 +- .../ch/zhaw/gartenverwaltung/types/Crop.java | 3 + .../gartenverwaltung/types/GrowthPhase.java | 1 - .../ch/zhaw/gartenverwaltung/types/Plant.java | 1 + .../ch/zhaw/gartenverwaltung/io/plantdb.json | 68 ++---- .../models/GardenPlanModelTest.java | 14 +- .../models/GardenScheduleTest.java | 26 ++- .../models/PlantListModelTest.java | 19 +- .../ch/zhaw/gartenverwaltung/io/plantdb.json | 215 ++++++++++++++++++ .../gartenverwaltung/io/test-plantdb.json | 74 ++---- .../models/template-taskdb.json | 2 + .../gartenverwaltung/models/test-taskdb.json | 2 + 14 files changed, 307 insertions(+), 137 deletions(-) create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/io/plantdb.json create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/models/template-taskdb.json create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/models/test-taskdb.json diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java index 218ffda..44cea0d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java @@ -32,7 +32,6 @@ public class AppLoader { private final TaskList taskList = new JsonTaskList(); private final GardenSchedule gardenSchedule = new GardenSchedule(taskList, plantList); - private final WeatherGradenTaskPlanner weatherGradenTaskPlanner = new WeatherGradenTaskPlanner(taskList,plantList,cropList); private final Garden garden = new Garden(gardenSchedule, cropList); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java index 9e14a03..adf33fb 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java @@ -74,11 +74,8 @@ public class WeatherGradenTaskPlanner { Plant plant = plantList.getPlantById(HardinessZone.ZONE_8A,crop.getPlantId()).orElseThrow(PlantNotFoundException::new); // nur für aktuelle growthphase - for (GrowthPhase growthphase : plant.lifecycle()) { - - if(growthphase.wateringCycle().litersPerSqM() < rainAmount){ - adjustNextExecutionOfWateringTask(taskList.getTaskList(LocalDate.now(), LocalDate.now().plusDays(7))); - } + if(plant.wateringCycle().litersPerSqM() < rainAmount){ + adjustNextExecutionOfWateringTask(taskList.getTaskList(LocalDate.now(), LocalDate.now().plusDays(7))); } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index 9502674..babcc01 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -49,16 +49,13 @@ public class GardenSchedule { public void planTasksForCrop(Crop crop) throws PlantNotFoundException, HardinessZoneNotSetException, IOException { Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).orElseThrow(PlantNotFoundException::new); int growPhaseGroup = plant.getGrowphaseGroupForDate(crop.getStartDate()); + addTask(new Task("watering Task", "pour water over the plant circa : "+ plant.wateringCycle().litersPerSqM() +" per square meter", + crop.getStartDate(), crop.getStartDate().plusDays(plant.timeToHarvest(0)), + plant.wateringCycle().interval(), crop.getCropId().orElse(0L))); + for (GrowthPhase growthPhase : plant.lifecycleForGroup(growPhaseGroup)) { for (TaskTemplate taskTemplate : growthPhase.taskTemplates()) { addTask(taskTemplate.generateTask(crop.getStartDate(), crop.getCropId().orElse(0L))); - - } - //TODO add wateringtask - if(growthPhase.wateringCycle() != null){ - addTask(new Task("watering Task", "pour water over the plant circa : "+ growthPhase.wateringCycle().litersPerSqM() +" per square meter", - growthPhase.startDate().atYear(LocalDate.now().getYear()), growthPhase.endDate().atYear(LocalDate.now().getYear()), - growthPhase.wateringCycle().interval(), crop.getCropId().orElse(0L))); } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java index e8f539f..58cd453 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java @@ -1,5 +1,8 @@ package ch.zhaw.gartenverwaltung.types; +import ch.zhaw.gartenverwaltung.io.JsonPlantList; +import ch.zhaw.gartenverwaltung.io.PlantList; + import java.time.LocalDate; import java.util.Objects; import java.util.Optional; diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/GrowthPhase.java b/src/main/java/ch/zhaw/gartenverwaltung/types/GrowthPhase.java index 9348f2b..6efcc44 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/GrowthPhase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/GrowthPhase.java @@ -12,7 +12,6 @@ public record GrowthPhase( MonthDay startDate, MonthDay endDate, int group, - WateringCycle wateringCycle, @JsonDeserialize(using = GrowthPhaseTypeDeserializer.class) GrowthPhaseType type, @JsonDeserialize(using = HardinessZoneDeserializer.class) HardinessZone zone, List taskTemplates) { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java index b77540c..4cf269f 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java @@ -18,6 +18,7 @@ public record Plant( int light, String soil, List pests, + WateringCycle wateringCycle, List lifecycle) { /** diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index fac9a5b..abd61ef 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -14,6 +14,11 @@ "measures": "Less water." } ], + "wateringCycle": { + "litersPerSqM": 25, + "interval": null, + "notes": [] + }, "lifecycle": [ { "startDate": "03-10", @@ -21,11 +26,6 @@ "type": "SOW", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [] - }, "taskTemplates": [ { "name": "Germinate", @@ -42,11 +42,6 @@ "type": "PLANT", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 25, - "interval": 7, - "notes": [] - }, "taskTemplates": [ { "name": "hilling", @@ -63,11 +58,6 @@ "type": "HARVEST", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [] - }, "taskTemplates": [ { "name": "Harvest", @@ -85,6 +75,11 @@ "name": "Early Carrot", "description": "Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.", "image": "carrot.jpg", + "wateringCycle": { + "litersPerSqM": 15, + "interval": 3, + "notes": [] + }, "lifecycle": [ { "startDate": "02-20", @@ -92,11 +87,7 @@ "zone": "ZONE_8A", "type": "SOW", "group": 0, - "wateringCycle": { - "litersPerSqM": 15, - "interval": 3, - "notes": [] - }, + "taskTemplates": [ { "name": "hilling", @@ -113,13 +104,6 @@ "zone": "ZONE_8A", "type": "PLANT", "group": 0, - "wateringCycle": { - "litersPerSqM": 25, - "interval": 3, - "notes": [ - "Be careful not to pour water over the leaves, as this will lead to sunburn." - ] - }, "taskTemplates": [ { "name": "hilling", @@ -136,11 +120,6 @@ "zone": "ZONE_8A", "type": "HARVEST", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [] - }, "taskTemplates": [ { "name": "Harvesting", @@ -167,6 +146,12 @@ "name": "Summertime Onion", "description": "Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.", "image": "onion.jpg", + "wateringCycle": { + "litersPerSqM": 15, + "interval": 4, + "notes": [ + ] + }, "lifecycle": [ { "startDate": "03-15", @@ -174,12 +159,6 @@ "type": "SOW", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 15, - "interval": 4, - "notes": [ - ] - }, "taskTemplates": [ { "name": "Plant Sets", @@ -196,13 +175,6 @@ "type": "PLANT", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 25, - "interval": 3, - "notes": [ - "" - ] - }, "taskTemplates": [ { "name": "hilling", @@ -219,12 +191,6 @@ "type": "HARVEST", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [ - ] - }, "taskTemplates": [ { "name": "Harvesting", diff --git a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java index e39a525..c87f916 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java @@ -49,11 +49,12 @@ public class GardenPlanModelTest { 0, "sandy to loamy, loose soil, free of stones", new ArrayList<>(), - List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()), - new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), - new GrowthPhase(MonthDay.of(8, 5), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), - new GrowthPhase(MonthDay.of(2, 8), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), - new GrowthPhase(MonthDay.of(10, 2), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))); + new WateringCycle(15, 0, null), + List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(8, 5), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(2, 8), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(10, 2), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))); exampleCropOnion = new Crop(examplePlantOnion.id(), LocalDate.of(2023, 3, 1)) .withId(3); @@ -66,7 +67,8 @@ public class GardenPlanModelTest { 0, "sandy to loamy, loose soil, free of stones", new ArrayList<>(), - List.of(new GrowthPhase(MonthDay.of(4, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))); + new WateringCycle(25, 0, null), + List.of(new GrowthPhase(MonthDay.of(4, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))); exampleCropCarrot = new Crop(examplePlantCarrot.id(), LocalDate.now()) .withId(5); diff --git a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java index 5e32687..8711aee 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java @@ -7,8 +7,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.time.LocalDate; import java.time.MonthDay; +import java.time.format.DateTimeFormatter; import java.util.*; import static org.junit.jupiter.api.Assertions.*; @@ -88,12 +94,13 @@ class GardenScheduleTest { 0, "sandy to loamy, loose soil, free of stones", new ArrayList<>(), + new WateringCycle(15, 0, null), List.of( - new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, List.of( + new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, List.of( exampleTaskTemplateList.get(0), exampleTaskTemplateList.get(1) )), - new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, List.of( + new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, List.of( exampleTaskTemplateList.get(2), exampleTaskTemplateList.get(3) )) @@ -187,7 +194,7 @@ class GardenScheduleTest { } @Test - void planTasksForCrop() throws HardinessZoneNotSetException, PlantNotFoundException, IOException { + void planTasksForCrop() throws HardinessZoneNotSetException, PlantNotFoundException, IOException, URISyntaxException { model.planTasksForCrop(new Crop(20, exampleStartDate).withId(30)); verify(exampleTaskTemplateList.get(0), times(1)).generateTask(exampleStartDate, 30); verify(exampleTaskTemplateList.get(1), times(1)).generateTask(exampleStartDate, 30); @@ -197,5 +204,18 @@ class GardenScheduleTest { verify(taskList, times(1)).saveTask(exampleTaskList.get(1)); verify(taskList, times(1)).saveTask(exampleTaskList.get(2)); verify(taskList, times(1)).saveTask(exampleTaskList.get(3)); + + final URL dbDataSource = this.getClass().getResource("test-taskdb.json"); + final URL testFile = this.getClass().getResource("template-taskdb.json"); + + Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING); + JsonTaskList testTaskList = new JsonTaskList(dbDataSource); + + model = spy(new GardenSchedule(testTaskList,plantList)); + model.planTasksForCrop(new Crop(20, exampleStartDate).withId(30)); + assertEquals(5,testTaskList.getTaskList(LocalDate.MIN,LocalDate.MAX).size()); + testTaskList.getTaskList(LocalDate.MIN,LocalDate.MAX).get(0); } + + } \ No newline at end of file diff --git a/src/test/java/ch/zhaw/gartenverwaltung/models/PlantListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/PlantListModelTest.java index 0033299..5f71e1f 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/models/PlantListModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/PlantListModelTest.java @@ -49,11 +49,12 @@ class PlantListModelTest { 0, "sandy to loamy, loose soil, free of stones", new ArrayList<>(), - List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()), - new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), - new GrowthPhase(MonthDay.of(8, 5), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), - new GrowthPhase(MonthDay.of(2, 8), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), - new GrowthPhase(MonthDay.of(10, 2), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))) + new WateringCycle(25, 0, null), + List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(8, 5), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(2, 8), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(10, 2), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))) ); examplePlantList.add(new Plant( 0, @@ -64,8 +65,9 @@ class PlantListModelTest { 6, "sandy", new ArrayList<>(), - List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()), - new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))) + new WateringCycle(0, 0, null), + List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))) ); examplePlantList.add(new Plant( 1, @@ -76,7 +78,8 @@ class PlantListModelTest { 0, "sandy to loamy, loose soil, free of stones", new ArrayList<>(), - List.of(new GrowthPhase(MonthDay.of(4, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))) + new WateringCycle(25, 0, null), + List.of(new GrowthPhase(MonthDay.of(4, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))) ); } diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/plantdb.json new file mode 100644 index 0000000..abd61ef --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -0,0 +1,215 @@ +[ + { + "id": 0, + "name": "Potato", + "description": "The potato is a tuber, round or oval, with small white roots called 'eyes', that are growth buds. The size varies depending on the variety; the colour of the skin can be white, yellow or even purple.", + "light": 6, + "spacing": "35", + "soil": "sandy", + "image": "potato.jpg", + "pests": [ + { + "name": "Rot", + "description": "Rot, any of several plant diseases, caused by any of hundreds of species of soil-borne bacteria, fungi, and funguslike organisms (Oomycota). Rot diseases are characterized by plant decomposition and putrefaction. The decay may be hard, dry, spongy, watery, mushy, or slimy and may affect any plant part.", + "measures": "Less water." + } + ], + "wateringCycle": { + "litersPerSqM": 25, + "interval": null, + "notes": [] + }, + "lifecycle": [ + { + "startDate": "03-10", + "endDate": "04-10", + "type": "SOW", + "zone": "ZONE_8A", + "group": 0, + "taskTemplates": [ + { + "name": "Germinate", + "relativeStartDate": -14, + "relativeEndDate": null, + "description": "Take an egg carton and fill it with soil. Put the seedling deep enough so its half covered with soil. Keep it in 10-15 * Celsius with lots of light.", + "interval": null + } + ] + }, + { + "startDate": "04-10", + "endDate": "07-10", + "type": "PLANT", + "zone": "ZONE_8A", + "group": 0, + "taskTemplates": [ + { + "name": "hilling", + "relativeStartDate": 0, + "relativeEndDate": null, + "description": "When the plants are 20 cm tall, begin hilling the potatoes by gently mounding the soil from the center of your rows around the stems of the plant. Mound up the soil around the plant until just the top few leaves show above the soil. Two weeks later, hill up the soil again when the plants grow another 20 cm.", + "interval": 21 + } + ] + }, + { + "startDate": "06-10", + "endDate": "08-10", + "type": "HARVEST", + "zone": "ZONE_8A", + "group": 0, + "taskTemplates": [ + { + "name": "Harvest", + "relativeStartDate": 0, + "relativeEndDate": null, + "description": "Once the foliage has wilted and dried completely, harvest on a dry day. Store in a dark and cool location.", + "interval": null + } + ] + } + ] + }, + { + "id": 1, + "name": "Early Carrot", + "description": "Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.", + "image": "carrot.jpg", + "wateringCycle": { + "litersPerSqM": 15, + "interval": 3, + "notes": [] + }, + "lifecycle": [ + { + "startDate": "02-20", + "endDate": "03-10", + "zone": "ZONE_8A", + "type": "SOW", + "group": 0, + + "taskTemplates": [ + { + "name": "hilling", + "relativeStartDate": 0, + "relativeEndDate": 0, + "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", + "interval": null + } + ] + }, + { + "startDate": "03-10", + "endDate": "05-10", + "zone": "ZONE_8A", + "type": "PLANT", + "group": 0, + "taskTemplates": [ + { + "name": "hilling", + "relativeStartDate": 0, + "relativeEndDate": null, + "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", + "interval": 15 + } + ] + }, + { + "startDate": "05-10", + "endDate": "05-20", + "zone": "ZONE_8A", + "type": "HARVEST", + "group": 0, + "taskTemplates": [ + { + "name": "Harvesting", + "relativeStartDate": 0, + "relativeEndDate": 14, + "description": "When the leaves turn to a yellowish brown. Do not harvest earlier. The plant will show when it's ready.", + "interval": null + } + ] + } + ], + "soil": "sandy to loamy, loose soil, free of stones", + "spacing": "5,35,2.5", + "pests": [ + { + "name": "Rot", + "description": "rot, any of several plant diseases, caused by any of hundreds of species of soil-borne bacteria, fungi, and funguslike organisms (Oomycota). Rot diseases are characterized by plant decomposition and putrefaction. The decay may be hard, dry, spongy, watery, mushy, or slimy and may affect any plant part.", + "measures": "less water" + } + ] + }, + { + "id": 2, + "name": "Summertime Onion", + "description": "Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.", + "image": "onion.jpg", + "wateringCycle": { + "litersPerSqM": 15, + "interval": 4, + "notes": [ + ] + }, + "lifecycle": [ + { + "startDate": "03-15", + "endDate": "04-10", + "type": "SOW", + "zone": "ZONE_8A", + "group": 0, + "taskTemplates": [ + { + "name": "Plant Sets", + "relativeStartDate": 0, + "relativeEndDate": 0, + "description": "Plant the sets about 5cm deep into the soil.", + "interval": null + } + ] + }, + { + "startDate": "04-10", + "endDate": "07-10", + "type": "PLANT", + "zone": "ZONE_8A", + "group": 0, + "taskTemplates": [ + { + "name": "hilling", + "relativeStartDate": 0, + "relativeEndDate": null, + "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", + "interval": 15 + } + ] + }, + { + "startDate": "07-10", + "endDate": "09-20", + "type": "HARVEST", + "zone": "ZONE_8A", + "group": 0, + "taskTemplates": [ + { + "name": "Harvesting", + "relativeStartDate": 0, + "relativeEndDate": 14, + "description": "When ready for harvest, the leaves on your onion plants will start to flop over. This happens at the \"neck\" of the onion and it signals that the plant has stopped growing and is ready for storage. Onions should be harvested soon thereafter", + "interval": null + } + ] + } + ], + "soil": "sandy to loamy, loose soil, free of stones", + "spacing": "15,30,2", + "pests": [ + { + "name": "Rot", + "description": "rot, any of several plant diseases, caused by any of hundreds of species of soil-borne bacteria, fungi, and funguslike organisms (Oomycota). Rot diseases are characterized by plant decomposition and putrefaction. The decay may be hard, dry, spongy, watery, mushy, or slimy and may affect any plant part.", + "measures": "less water" + } + ] + } +] diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/test-plantdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-plantdb.json index 4268df1..abd61ef 100644 --- a/src/test/resources/ch/zhaw/gartenverwaltung/io/test-plantdb.json +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-plantdb.json @@ -14,6 +14,11 @@ "measures": "Less water." } ], + "wateringCycle": { + "litersPerSqM": 25, + "interval": null, + "notes": [] + }, "lifecycle": [ { "startDate": "03-10", @@ -21,11 +26,6 @@ "type": "SOW", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [] - }, "taskTemplates": [ { "name": "Germinate", @@ -42,11 +42,6 @@ "type": "PLANT", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 25, - "interval": 7, - "notes": [] - }, "taskTemplates": [ { "name": "hilling", @@ -63,11 +58,6 @@ "type": "HARVEST", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [] - }, "taskTemplates": [ { "name": "Harvest", @@ -85,6 +75,11 @@ "name": "Early Carrot", "description": "Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.", "image": "carrot.jpg", + "wateringCycle": { + "litersPerSqM": 15, + "interval": 3, + "notes": [] + }, "lifecycle": [ { "startDate": "02-20", @@ -92,11 +87,7 @@ "zone": "ZONE_8A", "type": "SOW", "group": 0, - "wateringCycle": { - "litersPerSqM": 15, - "interval": 3, - "notes": [] - }, + "taskTemplates": [ { "name": "hilling", @@ -113,13 +104,6 @@ "zone": "ZONE_8A", "type": "PLANT", "group": 0, - "wateringCycle": { - "litersPerSqM": 25, - "interval": 3, - "notes": [ - "Be careful not to pour water over the leaves, as this will lead to sunburn." - ] - }, "taskTemplates": [ { "name": "hilling", @@ -136,11 +120,6 @@ "zone": "ZONE_8A", "type": "HARVEST", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [] - }, "taskTemplates": [ { "name": "Harvesting", @@ -167,6 +146,12 @@ "name": "Summertime Onion", "description": "Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.", "image": "onion.jpg", + "wateringCycle": { + "litersPerSqM": 15, + "interval": 4, + "notes": [ + ] + }, "lifecycle": [ { "startDate": "03-15", @@ -174,19 +159,12 @@ "type": "SOW", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 15, - "interval": 4, - "notes": [ - - ] - }, "taskTemplates": [ { - "name": "hilling", + "name": "Plant Sets", "relativeStartDate": 0, "relativeEndDate": 0, - "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", + "description": "Plant the sets about 5cm deep into the soil.", "interval": null } ] @@ -197,13 +175,6 @@ "type": "PLANT", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 25, - "interval": 3, - "notes": [ - "" - ] - }, "taskTemplates": [ { "name": "hilling", @@ -220,13 +191,6 @@ "type": "HARVEST", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [ - - ] - }, "taskTemplates": [ { "name": "Harvesting", diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/models/template-taskdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/models/template-taskdb.json new file mode 100644 index 0000000..32960f8 --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/models/template-taskdb.json @@ -0,0 +1,2 @@ +[ +] \ No newline at end of file diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/models/test-taskdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/models/test-taskdb.json new file mode 100644 index 0000000..32960f8 --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/models/test-taskdb.json @@ -0,0 +1,2 @@ +[ +] \ No newline at end of file From 220d13818500aec6ffc59069a667bfc542e4ee18 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 28 Nov 2022 19:31:08 +0100 Subject: [PATCH 12/23] fixed saving Task after editing --- .../ch/zhaw/gartenverwaltung/Settings.java | 2 +- .../backgroundtasks/Notifier.java | 27 ++++++++++++++++--- .../backgroundtasks/email/EMailSender.java | 7 +---- .../META-INF/javamail.default.address.map | 0 4 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 src/main/resources/META-INF/javamail.default.address.map diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Settings.java b/src/main/java/ch/zhaw/gartenverwaltung/Settings.java index 63cf6e7..462237c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/Settings.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/Settings.java @@ -17,7 +17,7 @@ public class Settings { // E-Mail Inbox: https://www.mailinator.com/v4/public/inboxes.jsp?to=pm3.hs22.it21b.win.team1 private String mailNotificationReceivers = "pm3.hs22.it21b.win.team1@mailinator.com"; private String mailNotificationSubjectTemplate = "Task %s is due!"; // {0} = Task Name - private String mailNotificationTextTemplate = "Dear user\nYour gardentask %s is due since %tF. Don't forget to confirm in your application if the task is done.\nTask description:\n%s"; // {0} = Task Name, {1} = nextExecution, {2} = Task description + private String mailNotificationTextTemplate = "Dear user\nYour gardentask %s for plant %s is due at %tF. Don't forget to confirm in your application if the task is done.\nTask description:\n%s"; // {0} = Task Name, {1} = plantname, {2} = nextExecution, {3} = Task description static { instance = new Settings(); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java index f36913f..c172e83 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java @@ -2,9 +2,11 @@ package ch.zhaw.gartenverwaltung.backgroundtasks; import ch.zhaw.gartenverwaltung.Settings; import ch.zhaw.gartenverwaltung.backgroundtasks.email.EMailSender; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.io.TaskList; import ch.zhaw.gartenverwaltung.models.Garden; +import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; import javax.mail.MessagingException; @@ -23,11 +25,27 @@ public class Notifier { this.plantList = plantList; } - private void sendNotification(Task task) throws MessagingException { + private void sendNotification(Task task) { + String plantName = "unkown plant"; + try { + if(garden.getCrop(task.getCropId()).isPresent()){ + Crop crop = garden.getCrop(task.getCropId()).get(); + if(plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).isPresent()) { + plantName = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get().name(); + } + } + } catch (IOException | HardinessZoneNotSetException e) { + e.printStackTrace(); + // TODO logger + } String messageSubject = String.format(Settings.getInstance().getMailNotificationSubjectTemplate(), task.getName()); - String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), task.getNextExecution(), task.getDescription()); - //TODO create Exception for email sent fail - eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText); + String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), plantName, task.getNextExecution(), task.getDescription()); + try { + eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText); + } catch (MessagingException e) { + e.printStackTrace(); + // TODO Logger + } } public void sendNotifications() throws IOException, MessagingException { @@ -36,6 +54,7 @@ public class Notifier { if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) { sendNotification(task); task.setNextNotification(LocalDate.now().plusDays(1)); + taskList.saveTask(task); } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java index 82f895a..1fa470c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java @@ -30,12 +30,7 @@ public class EMailSender { message.setText(text); message.setSentDate(new Date()); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false)); - try{ - Transport.send(message); - } catch (MessagingException e) { - e.printStackTrace(); - // TODO logger - } + Transport.send(message); } private void printMail(String receiver, String subject, String text){ diff --git a/src/main/resources/META-INF/javamail.default.address.map b/src/main/resources/META-INF/javamail.default.address.map new file mode 100644 index 0000000..e69de29 From f43bfeebd23ddde2139a66bcad5752b4db3f4abb Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Tue, 29 Nov 2022 11:32:20 +0100 Subject: [PATCH 13/23] #23 bugfix plantdb.json --- .../ch/zhaw/gartenverwaltung/io/plantdb.json | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index 04aca8f..3dda3a7 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -227,6 +227,11 @@ "measures": "Less water." } ], + "wateringCycle": { + "litersPerSqM": 0, + "interval": null, + "notes": [] + }, "lifecycle": [ { "startDate": "12-01", @@ -234,11 +239,7 @@ "type": "SOW", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [] - }, + "taskTemplates": [ { "name": "Germinate", @@ -255,11 +256,6 @@ "type": "PLANT", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 25, - "interval": 7, - "notes": [] - }, "taskTemplates": [ { "name": "hilling", @@ -276,11 +272,6 @@ "type": "HARVEST", "zone": "ZONE_8A", "group": 0, - "wateringCycle": { - "litersPerSqM": 0, - "interval": null, - "notes": [] - }, "taskTemplates": [ { "name": "Harvest", From fd28ca7cc2862116f27720e2c07148a1587f9b16 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 29 Nov 2022 11:55:49 +0100 Subject: [PATCH 14/23] Merged Notification Branch with Weather Branch --- build.gradle | 2 +- .../{HelloApplication.java => Main.java} | 7 +++++-- .../{io => backgroundtasks/weather}/SevereWeather.java | 2 +- .../weather}/WeatherGradenTaskPlanner.java | 6 +++++- .../weather}/WeatherService.java | 2 +- .../ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java | 10 +++++----- 6 files changed, 18 insertions(+), 11 deletions(-) rename src/main/java/ch/zhaw/gartenverwaltung/{HelloApplication.java => Main.java} (68%) rename src/main/java/ch/zhaw/gartenverwaltung/{io => backgroundtasks/weather}/SevereWeather.java (56%) rename src/main/java/ch/zhaw/gartenverwaltung/{io => backgroundtasks/weather}/WeatherGradenTaskPlanner.java (93%) rename src/main/java/ch/zhaw/gartenverwaltung/{io => backgroundtasks/weather}/WeatherService.java (92%) diff --git a/build.gradle b/build.gradle index 8c3b14a..e789fc4 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,7 @@ tasks.withType(JavaCompile) { application { mainModule = 'ch.zhaw.gartenverwaltung' - mainClass = 'ch.zhaw.gartenverwaltung.HelloApplication' + mainClass = 'ch.zhaw.gartenverwaltung.Main' } javafx { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java b/src/main/java/ch/zhaw/gartenverwaltung/Main.java similarity index 68% rename from src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java rename to src/main/java/ch/zhaw/gartenverwaltung/Main.java index 1d8ed5a..1b3dfc0 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/Main.java @@ -2,13 +2,16 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; import ch.zhaw.gartenverwaltung.backgroundtasks.BackgroundTasks; +import ch.zhaw.gartenverwaltung.io.PlantList; +import ch.zhaw.gartenverwaltung.io.TaskList; +import ch.zhaw.gartenverwaltung.models.Garden; import javafx.application.Application; import javafx.stage.Stage; import java.io.IOException; import java.util.Timer; -public class HelloApplication extends Application { +public class Main extends Application { Timer backGroundTaskTimer = new Timer(); BackgroundTasks backgroundTasks; @@ -16,7 +19,7 @@ public class HelloApplication extends Application { public void start(Stage stage) throws IOException { AppLoader appLoader = new AppLoader(); - backgroundTasks = new BackgroundTasks(appLoader.getTaskList(), appLoader.getGarden(), appLoader.getPlantList()); + backgroundTasks = new BackgroundTasks((TaskList) appLoader.getAppDependency(TaskList.class),(Garden) appLoader.getAppDependency(Garden.class), (PlantList) appLoader.getAppDependency(PlantList.class)); backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 1000); appLoader.loadSceneToStage("MainFXML.fxml", stage); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/SevereWeather.java similarity index 56% rename from src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java rename to src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/SevereWeather.java index 239e34a..fa075e7 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/SevereWeather.java @@ -1,4 +1,4 @@ -package ch.zhaw.gartenverwaltung.io; +package ch.zhaw.gartenverwaltung.backgroundtasks.weather; public enum SevereWeather { FROST,SNOW,HAIL,NO_SEVERE_WEATHER,RAIN diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java similarity index 93% rename from src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java rename to src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java index adf33fb..edf9d1e 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -1,5 +1,9 @@ -package ch.zhaw.gartenverwaltung.io; +package ch.zhaw.gartenverwaltung.backgroundtasks.weather; +import ch.zhaw.gartenverwaltung.io.CropList; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.io.PlantList; +import ch.zhaw.gartenverwaltung.io.TaskList; import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; import ch.zhaw.gartenverwaltung.types.*; diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherService.java similarity index 92% rename from src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java rename to src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherService.java index 510b292..89fdf76 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherService.java @@ -1,4 +1,4 @@ -package ch.zhaw.gartenverwaltung.io; +package ch.zhaw.gartenverwaltung.backgroundtasks.weather; public class WeatherService { private static final int NO_RAIN = 0; diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java index f461714..2499d8b 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java @@ -1,6 +1,6 @@ package ch.zhaw.gartenverwaltung.bootstrap; -import ch.zhaw.gartenverwaltung.HelloApplication; +import ch.zhaw.gartenverwaltung.Main; import ch.zhaw.gartenverwaltung.io.*; import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.GardenSchedule; @@ -77,7 +77,7 @@ public class AppLoader { * @throws IOException if the file could not be loaded */ public Object loadSceneToStage(String fxmlFile, Stage appendee) throws IOException { - FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(Main.class.getResource(fxmlFile))); Pane root = loader.load(); Scene scene = new Scene(root); String css = Objects.requireNonNull(this.getClass().getResource("styles.css")).toExternalForm(); @@ -99,7 +99,7 @@ public class AppLoader { * @throws IOException if the file could not be loaded */ public Object loadPaneToDialog(String fxmlFile, DialogPane appendee) throws IOException { - FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(Main.class.getResource(fxmlFile))); appendee.setContent(loader.load()); Object controller = loader.getController(); annotationInject(controller); @@ -114,7 +114,7 @@ public class AppLoader { * @throws IOException if the file could not be loaded */ public void loadAndCacheFxml(String fxmlFile) throws IOException { - FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(Main.class.getResource(fxmlFile))); Pane pane = loader.load(); panes.put(fxmlFile, pane); annotationInject(loader.getController()); @@ -152,7 +152,7 @@ public class AppLoader { }); } - private Object getAppDependency(Class type) { + public Object getAppDependency(Class type) { return dependencies.get(type.getSimpleName()); } } From 55f92f92bded75d98e4c3bc6c448974acb85cc8e Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 29 Nov 2022 12:12:16 +0100 Subject: [PATCH 15/23] Merged Notification Branch with Weather Branch --- src/main/java/ch/zhaw/gartenverwaltung/Main.java | 4 +++- .../backgroundtasks/BackgroundTasks.java | 12 +++++++----- .../gartenverwaltung/backgroundtasks/Notifier.java | 12 ++++++------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Main.java b/src/main/java/ch/zhaw/gartenverwaltung/Main.java index 1b3dfc0..9005ec0 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/Main.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/Main.java @@ -2,9 +2,11 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; import ch.zhaw.gartenverwaltung.backgroundtasks.BackgroundTasks; +import ch.zhaw.gartenverwaltung.io.CropList; import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.io.TaskList; import ch.zhaw.gartenverwaltung.models.Garden; +import ch.zhaw.gartenverwaltung.types.Crop; import javafx.application.Application; import javafx.stage.Stage; @@ -19,7 +21,7 @@ public class Main extends Application { public void start(Stage stage) throws IOException { AppLoader appLoader = new AppLoader(); - backgroundTasks = new BackgroundTasks((TaskList) appLoader.getAppDependency(TaskList.class),(Garden) appLoader.getAppDependency(Garden.class), (PlantList) appLoader.getAppDependency(PlantList.class)); + backgroundTasks = new BackgroundTasks((TaskList) appLoader.getAppDependency(TaskList.class),(CropList) appLoader.getAppDependency(CropList.class), (PlantList) appLoader.getAppDependency(PlantList.class)); backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 1000); appLoader.loadSceneToStage("MainFXML.fxml", stage); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java index 2cb5e2e..bfa5571 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java @@ -1,5 +1,7 @@ package ch.zhaw.gartenverwaltung.backgroundtasks; +import ch.zhaw.gartenverwaltung.backgroundtasks.weather.WeatherGradenTaskPlanner; +import ch.zhaw.gartenverwaltung.io.CropList; import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.io.TaskList; import ch.zhaw.gartenverwaltung.models.Garden; @@ -9,12 +11,12 @@ import java.io.IOException; import java.util.TimerTask; public class BackgroundTasks extends TimerTask { - private final Notifier notifier; - //TODO uncomment: privat final WeatherGardenTaskPlaner weatherGardenTaskPlaner; + private final Notifier notifier; + private final WeatherGradenTaskPlanner weatherGardenTaskPlaner; - public BackgroundTasks(TaskList taskList, Garden garden, PlantList plantList) { - notifier = new Notifier(taskList, garden, plantList); - //TODO uncomment: weatherGardenTaskPlaner = new WeatherGardenTaskPlaner(taskList); + public BackgroundTasks(TaskList taskList, CropList cropList, PlantList plantList) { + notifier = new Notifier(taskList, plantList, cropList); + weatherGardenTaskPlaner = new WeatherGradenTaskPlanner(taskList, plantList, cropList); } @Override diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java index c172e83..da44149 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java @@ -2,10 +2,10 @@ package ch.zhaw.gartenverwaltung.backgroundtasks; import ch.zhaw.gartenverwaltung.Settings; import ch.zhaw.gartenverwaltung.backgroundtasks.email.EMailSender; +import ch.zhaw.gartenverwaltung.io.CropList; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.io.TaskList; -import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; @@ -15,21 +15,21 @@ import java.time.LocalDate; public class Notifier { private final TaskList taskList; - private final Garden garden; + private final CropList cropList; private final PlantList plantList; private final EMailSender eMailSender = new EMailSender(); - public Notifier(TaskList taskList, Garden garden, PlantList plantList) { + public Notifier(TaskList taskList, PlantList plantList, CropList cropList) { this.taskList = taskList; - this.garden = garden; + this.cropList = cropList; this.plantList = plantList; } private void sendNotification(Task task) { String plantName = "unkown plant"; try { - if(garden.getCrop(task.getCropId()).isPresent()){ - Crop crop = garden.getCrop(task.getCropId()).get(); + if(cropList.getCropById((task.getCropId())).isPresent()){ + Crop crop = cropList.getCropById(task.getCropId()).get(); if(plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).isPresent()) { plantName = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get().name(); } From 6ec0ccaeaa339d6ac01528f11b0a5acea206c1d3 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Tue, 29 Nov 2022 12:12:18 +0100 Subject: [PATCH 16/23] #23 bugfix GardenScheduleTest and PlantTest --- .../models/GardenScheduleTest.java | 4 ++++ .../gartenverwaltung/types/PlantTest.java | 21 ++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java index 8711aee..8bce1be 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java @@ -96,6 +96,7 @@ class GardenScheduleTest { new ArrayList<>(), new WateringCycle(15, 0, null), List.of( + new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, List.of( exampleTaskTemplateList.get(0), exampleTaskTemplateList.get(1) @@ -103,6 +104,9 @@ class GardenScheduleTest { new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, List.of( exampleTaskTemplateList.get(2), exampleTaskTemplateList.get(3) + )), + new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.SOW, HardinessZone.ZONE_8A, List.of( + )) ))); } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/types/PlantTest.java b/src/test/java/ch/zhaw/gartenverwaltung/types/PlantTest.java index 88cdf54..903d036 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/types/PlantTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/types/PlantTest.java @@ -20,16 +20,16 @@ class PlantTest { @BeforeEach void setUp() { List growthPhases = new ArrayList<>(); - growthPhases.add(new GrowthPhase(MonthDay.of(2, 1), MonthDay.of(4, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.SOW, HardinessZone.ZONE_8A, new ArrayList<>())); - growthPhases.add(new GrowthPhase(MonthDay.of(4, 2), MonthDay.of(6, 5), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())); - growthPhases.add(new GrowthPhase(MonthDay.of(6, 3), MonthDay.of(8, 6), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>())); - growthPhases.add(new GrowthPhase(MonthDay.of(3, 1), MonthDay.of(5, 4), 1, new WateringCycle(0, 0, null), GrowthPhaseType.SOW, HardinessZone.ZONE_8A, new ArrayList<>())); - growthPhases.add(new GrowthPhase(MonthDay.of(5, 2), MonthDay.of(7, 5), 1, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())); - growthPhases.add(new GrowthPhase(MonthDay.of(7, 3), MonthDay.of(9, 6), 1, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>())); - growthPhases.add(new GrowthPhase(MonthDay.of(4, 1), MonthDay.of(6, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.SOW, HardinessZone.ZONE_1A, new ArrayList<>())); - growthPhases.add(new GrowthPhase(MonthDay.of(6, 2), MonthDay.of(8, 5), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_1A, new ArrayList<>())); - growthPhases.add(new GrowthPhase(MonthDay.of(7, 2), MonthDay.of(9, 5), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_1A, new ArrayList<>())); - growthPhases.add(new GrowthPhase(MonthDay.of(8, 3), MonthDay.of(10, 6), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_1A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(2, 1), MonthDay.of(4, 4), 0, GrowthPhaseType.SOW, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(4, 2), MonthDay.of(6, 5), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(6, 3), MonthDay.of(8, 6), 0, GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(3, 1), MonthDay.of(5, 4), 1, GrowthPhaseType.SOW, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(5, 2), MonthDay.of(7, 5), 1, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(7, 3), MonthDay.of(9, 6), 1, GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(4, 1), MonthDay.of(6, 4), 0, GrowthPhaseType.SOW, HardinessZone.ZONE_1A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(6, 2), MonthDay.of(8, 5), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_1A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(7, 2), MonthDay.of(9, 5), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_1A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(8, 3), MonthDay.of(10, 6), 0, GrowthPhaseType.HARVEST, HardinessZone.ZONE_1A, new ArrayList<>())); testPlant = new Plant( 20, @@ -40,6 +40,7 @@ class PlantTest { 0, "sandy to loamy, loose soil, free of stones", new ArrayList<>(), + new WateringCycle(25, 0, null), growthPhases); } From 8ecc14db91e5b799fd311321d9dee15643c1307d Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 29 Nov 2022 12:19:45 +0100 Subject: [PATCH 17/23] Merged Notification Branch with Weather Branch --- .../backgroundtasks/BackgroundTasks.java | 9 ++++++++- .../weather/WeatherGradenTaskPlanner.java | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java index bfa5571..33c58f6 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java @@ -2,9 +2,11 @@ package ch.zhaw.gartenverwaltung.backgroundtasks; import ch.zhaw.gartenverwaltung.backgroundtasks.weather.WeatherGradenTaskPlanner; import ch.zhaw.gartenverwaltung.io.CropList; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.io.TaskList; import ch.zhaw.gartenverwaltung.models.Garden; +import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; import javax.mail.MessagingException; import java.io.IOException; @@ -21,7 +23,12 @@ public class BackgroundTasks extends TimerTask { @Override public void run() { - // TODO uncomment: weatherGardenTaskPlaner.refreshTasks(); + try { + weatherGardenTaskPlaner.refreshTasks(); + } catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) { + e.printStackTrace(); + // TODO logger + } try { try { notifier.sendNotifications(); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java index edf9d1e..16c327d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -28,6 +28,7 @@ public class WeatherGradenTaskPlanner { public void refreshTasks() throws IOException, HardinessZoneNotSetException, PlantNotFoundException { getSevereWeatherEvents(); getRainAmount(); + System.out.println("refreshing Tasks"); } private void getSevereWeatherEvents() throws IOException { @@ -77,7 +78,6 @@ public class WeatherGradenTaskPlanner { for (Crop crop : cropList.getCrops()) { Plant plant = plantList.getPlantById(HardinessZone.ZONE_8A,crop.getPlantId()).orElseThrow(PlantNotFoundException::new); - // nur für aktuelle growthphase if(plant.wateringCycle().litersPerSqM() < rainAmount){ adjustNextExecutionOfWateringTask(taskList.getTaskList(LocalDate.now(), LocalDate.now().plusDays(7))); } From 028bc0240d8d8596a4296b194cab1da3a5d7607c Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 29 Nov 2022 12:25:54 +0100 Subject: [PATCH 18/23] Merged Notification Branch with Weather Branch --- .../java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java | 1 - .../backgroundtasks/weather/WeatherGradenTaskPlanner.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java index da44149..a77ae06 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java @@ -49,7 +49,6 @@ public class Notifier { } public void sendNotifications() throws IOException, MessagingException { - System.out.println("sending Notifications"); for (Task task : taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)) { if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) { sendNotification(task); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java index 16c327d..84ec59e 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -28,7 +28,6 @@ public class WeatherGradenTaskPlanner { public void refreshTasks() throws IOException, HardinessZoneNotSetException, PlantNotFoundException { getSevereWeatherEvents(); getRainAmount(); - System.out.println("refreshing Tasks"); } private void getSevereWeatherEvents() throws IOException { From 5e42cae2902f30fa80c82e7268900fcccd8c06db Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Tue, 29 Nov 2022 21:55:42 +0100 Subject: [PATCH 19/23] #23 bugfix adjust task --- .../backgroundtasks/weather/WeatherGradenTaskPlanner.java | 2 +- src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java index 84ec59e..55a9bf5 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -86,7 +86,7 @@ public class WeatherGradenTaskPlanner { // vom startdatum abhängig, private void adjustNextExecutionOfWateringTask(List cropTaskList){ for(Task task : cropTaskList){ - if(task.getName().equals("watering task")){ + if(task.getName().equals("water plant")){ task.setNextExecution(task.getNextExecution().plusDays(task.getInterval().orElse(1))); } } diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json index 7b86231..889451a 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json @@ -14,10 +14,10 @@ "id": 2, "name": "water plant", "description": "water the plant, so that the soil is wet around the plant.", - "startDate": "2022-05-01", - "nextExecution": "2022-05-01", + "startDate": "2022-10-29", + "nextExecution": "2022-11-30", "nextNotification": "2022-05-01", - "endDate": "2022-09-01", + "endDate": "2022-12-31", "interval": 2, "cropId": 0 }, From 2f5440696fb93f5ad1443a3bc3ff426a34137f0a Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 1 Dec 2022 23:45:53 +0100 Subject: [PATCH 20/23] Cleanup of Class BackgroundTasks, added Logger --- .../java/ch/zhaw/gartenverwaltung/Main.java | 1 + .../backgroundtasks/BackgroundTasks.java | 38 ++++++++++++++----- .../backgroundtasks/Notifier.java | 24 ++++-------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Main.java b/src/main/java/ch/zhaw/gartenverwaltung/Main.java index 9005ec0..d31860d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/Main.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/Main.java @@ -22,6 +22,7 @@ public class Main extends Application { AppLoader appLoader = new AppLoader(); backgroundTasks = new BackgroundTasks((TaskList) appLoader.getAppDependency(TaskList.class),(CropList) appLoader.getAppDependency(CropList.class), (PlantList) appLoader.getAppDependency(PlantList.class)); + // TODO reduce period backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 1000); appLoader.loadSceneToStage("MainFXML.fxml", stage); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java index 33c58f6..96eed67 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java @@ -1,44 +1,62 @@ package ch.zhaw.gartenverwaltung.backgroundtasks; +import ch.zhaw.gartenverwaltung.CropDetailController; import ch.zhaw.gartenverwaltung.backgroundtasks.weather.WeatherGradenTaskPlanner; import ch.zhaw.gartenverwaltung.io.CropList; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.io.TaskList; -import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.types.Task; import javax.mail.MessagingException; import java.io.IOException; +import java.time.LocalDate; +import java.util.List; import java.util.TimerTask; +import java.util.logging.Level; +import java.util.logging.Logger; public class BackgroundTasks extends TimerTask { + private static final Logger LOG = Logger.getLogger(CropDetailController.class.getName()); + private final TaskList taskList; private final Notifier notifier; private final WeatherGradenTaskPlanner weatherGardenTaskPlaner; + private void movePastTasks() throws IOException { + List taskList = this.taskList.getTaskList(LocalDate.MIN, LocalDate.now().minusDays(1)); + taskList.forEach(task -> { + if (!task.isDone()) { + task.setNextExecution(LocalDate.now()); + } + }); + } + public BackgroundTasks(TaskList taskList, CropList cropList, PlantList plantList) { + this.taskList = taskList; notifier = new Notifier(taskList, plantList, cropList); weatherGardenTaskPlaner = new WeatherGradenTaskPlanner(taskList, plantList, cropList); } @Override public void run() { + try { + movePastTasks(); + } catch (IOException e) { + e.printStackTrace(); + LOG.log(Level.SEVERE, "Could not execute Background Task: move past Tasks ", e.getCause()); + } try { weatherGardenTaskPlaner.refreshTasks(); } catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) { e.printStackTrace(); - // TODO logger + LOG.log(Level.SEVERE, "Could not execute Background Task: Refresh Tasks by WeatherGardenTaskPlaner ", e.getCause()); } try { - try { - notifier.sendNotifications(); - } catch (MessagingException e) { - e.printStackTrace(); - // TODO logger - } - } catch (IOException e) { + notifier.sendNotifications(); + } catch (IOException | MessagingException | HardinessZoneNotSetException e) { e.printStackTrace(); - // TODO logger + LOG.log(Level.SEVERE, "Could not execute Background Task: send Notification for due Tasks", e.getCause()); } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java index a77ae06..072cd25 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java @@ -25,30 +25,20 @@ public class Notifier { this.plantList = plantList; } - private void sendNotification(Task task) { + private void sendNotification(Task task) throws IOException, MessagingException, HardinessZoneNotSetException { String plantName = "unkown plant"; - try { - if(cropList.getCropById((task.getCropId())).isPresent()){ - Crop crop = cropList.getCropById(task.getCropId()).get(); - if(plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).isPresent()) { - plantName = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get().name(); - } + if(cropList.getCropById((task.getCropId())).isPresent()){ + Crop crop = cropList.getCropById(task.getCropId()).get(); + if(plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).isPresent()) { + plantName = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get().name(); } - } catch (IOException | HardinessZoneNotSetException e) { - e.printStackTrace(); - // TODO logger } String messageSubject = String.format(Settings.getInstance().getMailNotificationSubjectTemplate(), task.getName()); String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), plantName, task.getNextExecution(), task.getDescription()); - try { - eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText); - } catch (MessagingException e) { - e.printStackTrace(); - // TODO Logger - } + eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText); } - public void sendNotifications() throws IOException, MessagingException { + public void sendNotifications() throws IOException, MessagingException, HardinessZoneNotSetException { for (Task task : taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)) { if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) { sendNotification(task); From a29066ed05f2112189122c09a307b2b8178a86c8 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Sun, 4 Dec 2022 16:32:30 +0100 Subject: [PATCH 21/23] #23 bugfix adjust task --- .../weather/WeatherGradenTaskPlanner.java | 13 +++++-------- .../java/ch/zhaw/gartenverwaltung/types/Task.java | 3 ++- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java index 55a9bf5..7c6a3ee 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -16,7 +16,7 @@ public class WeatherGradenTaskPlanner { private final PlantList plantList; private final CropList cropList; WeatherService weatherService; - private final LocalDate dateSevereWeather = LocalDate.of(22,12,15); + private final LocalDate dateSevereWeather = LocalDate.of(2022,12,15); public WeatherGradenTaskPlanner(TaskList taskList, PlantList plantList, CropList cropList) { this.taskList = taskList; @@ -31,7 +31,7 @@ public class WeatherGradenTaskPlanner { } private void getSevereWeatherEvents() throws IOException { - SevereWeather actualWeather = weatherService.causeSevereWeather(0); + SevereWeather actualWeather = weatherService.causeSevereWeather(1); if (SevereWeather.HAIL.equals(actualWeather)) { createPreHail(); } else if (SevereWeather.FROST.equals(actualWeather)) { @@ -50,8 +50,7 @@ public class WeatherGradenTaskPlanner { 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", - dateSevereWeather.minusDays(1L)); - preHailTask.withId(Long.MAX_VALUE); + dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L)); taskList.saveTask(preHailTask); } @@ -59,16 +58,14 @@ public class WeatherGradenTaskPlanner { public void createPreFrost() throws IOException { Task preFrostTask = new Task("Frost", "The temperatur falls below zero degrees, cover especially the root with wool", - dateSevereWeather.minusDays(1L)); - preFrostTask.withId(Long.MAX_VALUE); + dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L)); taskList.saveTask(preFrostTask); } public void createPreSnow() throws IOException { Task preSnowTask = new Task("Snow", "The weather brings little snowfall. Cover your crops", - dateSevereWeather.minusDays(1L)); - preSnowTask.withId(Long.MAX_VALUE); + dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L)); taskList.saveTask(preSnowTask); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index c8b976b..ef97003 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -44,10 +44,11 @@ public class Task { /** * Constructor for weather events */ - public Task(String name, String description, LocalDate startDate) { + public Task(String name, String description, LocalDate startDate, LocalDate endDate) { this.name = name; this.description = description; this.startDate = startDate; + this.endDate = endDate; } public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval, long cropId) { From 0fa2b814275220b860ba562ef6dd16f4877d0d5b Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Mon, 5 Dec 2022 11:02:15 +0100 Subject: [PATCH 22/23] #23 bugfix adjust task --- .../weather/WeatherGradenTaskPlanner.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java index 7c6a3ee..d36962a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -33,11 +33,11 @@ public class WeatherGradenTaskPlanner { private void getSevereWeatherEvents() throws IOException { SevereWeather actualWeather = weatherService.causeSevereWeather(1); if (SevereWeather.HAIL.equals(actualWeather)) { - createPreHail(); + createPreHailTask(); } else if (SevereWeather.FROST.equals(actualWeather)) { - createPreFrost(); + createPreFrostTask(); } else if (SevereWeather.SNOW.equals(actualWeather)) { - createPreSnow(); + createPreSnowTask(); } } @@ -46,7 +46,7 @@ public class WeatherGradenTaskPlanner { adjustWateringTask(rainAmount); } - public void createPreHail() throws IOException { + public void createPreHailTask() throws IOException { 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", @@ -55,14 +55,14 @@ public class WeatherGradenTaskPlanner { } - public void createPreFrost() throws IOException { + public void createPreFrostTask() throws IOException { Task preFrostTask = new Task("Frost", "The temperatur falls below zero degrees, cover especially the root with wool", dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L)); taskList.saveTask(preFrostTask); } - public void createPreSnow() throws IOException { + public void createPreSnowTask() throws IOException { Task preSnowTask = new Task("Snow", "The weather brings little snowfall. Cover your crops", dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L)); @@ -75,13 +75,13 @@ public class WeatherGradenTaskPlanner { Plant plant = plantList.getPlantById(HardinessZone.ZONE_8A,crop.getPlantId()).orElseThrow(PlantNotFoundException::new); if(plant.wateringCycle().litersPerSqM() < rainAmount){ - adjustNextExecutionOfWateringTask(taskList.getTaskList(LocalDate.now(), LocalDate.now().plusDays(7))); + adjustNextExecutionOfWateringTasks(taskList.getTaskList(LocalDate.now(), LocalDate.now().plusDays(7))); } } } // vom startdatum abhängig, - private void adjustNextExecutionOfWateringTask(List cropTaskList){ + private void adjustNextExecutionOfWateringTasks(List cropTaskList){ for(Task task : cropTaskList){ if(task.getName().equals("water plant")){ task.setNextExecution(task.getNextExecution().plusDays(task.getInterval().orElse(1))); From 28f76e7a041d0d8c8922a229b771da1fdae32af5 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Mon, 5 Dec 2022 11:40:09 +0100 Subject: [PATCH 23/23] #23 documentation --- .../weather/WeatherGradenTaskPlanner.java | 46 +++++++++++++++++-- .../weather/WeatherService.java | 5 +- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java index d36962a..ea9acfc 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -11,6 +11,10 @@ import java.io.IOException; import java.time.LocalDate; import java.util.List; +/** + * The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days + * + */ public class WeatherGradenTaskPlanner { private final TaskList taskList; private final PlantList plantList; @@ -25,6 +29,12 @@ public class WeatherGradenTaskPlanner { weatherService = new WeatherService(); } + /** + * Method to refresh watering tasks and severe weather tasks + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If the hardiness zone is not available + * @throws PlantNotFoundException if the plant is not available for the watering task + */ public void refreshTasks() throws IOException, HardinessZoneNotSetException, PlantNotFoundException { getSevereWeatherEvents(); getRainAmount(); @@ -46,7 +56,11 @@ public class WeatherGradenTaskPlanner { adjustWateringTask(rainAmount); } - public void createPreHailTask() throws IOException { + /** + * Method to create a PreHailTask + * @throws IOException If the database cannot be accessed + */ + private void createPreHailTask() throws IOException { 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", @@ -55,21 +69,36 @@ public class WeatherGradenTaskPlanner { } - public void createPreFrostTask() throws IOException { + /** + * Method to create a PreFrosttask + * @throws IOException If the database cannot be accessed + */ + private void createPreFrostTask() throws IOException { Task preFrostTask = new Task("Frost", "The temperatur falls below zero degrees, cover especially the root with wool", dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L)); taskList.saveTask(preFrostTask); } - public void createPreSnowTask() throws IOException { + /** + * Method to create a PreSnowTask + * @throws IOException If the database cannot be accessed + */ + private void createPreSnowTask() throws IOException { Task preSnowTask = new Task("Snow", "The weather brings little snowfall. Cover your crops", dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L)); taskList.saveTask(preSnowTask); } - public void adjustWateringTask(int rainAmount) throws HardinessZoneNotSetException, IOException, PlantNotFoundException { + /** + * Method to adjust the water plant tasks + * @param rainAmount Amount of rain from the last 7 days + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If the hardiness zone is not available + * @throws PlantNotFoundException if the plant is not available for the watering task + */ + private 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); @@ -80,7 +109,14 @@ public class WeatherGradenTaskPlanner { } } -// vom startdatum abhängig, + + /** + * Method to set next execution date of the water plant tasks + * @param cropTaskList List with tasks from crops + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If the hardiness zone is not available + * @throws PlantNotFoundException if the plant is not available for the watering task + */ private void adjustNextExecutionOfWateringTasks(List cropTaskList){ for(Task task : cropTaskList){ if(task.getName().equals("water plant")){ diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherService.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherService.java index 89fdf76..d189f11 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherService.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherService.java @@ -1,5 +1,8 @@ package ch.zhaw.gartenverwaltung.backgroundtasks.weather; - +/** + * The WeatherService is a class to cause weather events for the WeatherGardenTaskPlanner + * + */ public class WeatherService { private static final int NO_RAIN = 0; private static final int LITTLE_RAIN = 15;