Compare commits

...
14 changed files with 146 additions and 96 deletions
@@ -13,30 +13,46 @@ import javafx.stage.Stage;
import java.io.IOException; import java.io.IOException;
import java.util.Timer; import java.util.Timer;
/**
* Main class of the Application
*/
public class Main extends Application { public class Main extends Application {
Timer backGroundTaskTimer = new Timer(); Timer backGroundTaskTimer = new Timer();
BackgroundTasks backgroundTasks; BackgroundTasks backgroundTasks;
/**
* Method which is automatically called if Application is starting. It loads the scenes to stage and shows the stage.
* It creates a new Instance of BackgroundTasks and schedules them with a Timer instance to execute them every minute.
* @param stage Stage to show
* @throws IOException If loading Scenes can not access the fxml Resource File
*/
@Override @Override
public void start(Stage stage) throws IOException { public void start(Stage stage) throws IOException {
AppLoader appLoader = new AppLoader(); AppLoader appLoader = new AppLoader();
backgroundTasks = new BackgroundTasks((TaskList) appLoader.getAppDependency(TaskList.class),(CropList) appLoader.getAppDependency(CropList.class), (PlantList) appLoader.getAppDependency(PlantList.class));
appLoader.loadSceneToStage("MainFXML.fxml", stage); appLoader.loadSceneToStage("MainFXML.fxml", stage);
stage.setTitle("Gartenverwaltung"); stage.setTitle("Gartenverwaltung");
stage.show(); stage.show();
backgroundTasks = new BackgroundTasks((TaskList) appLoader.getAppDependency(TaskList.class),(CropList) appLoader.getAppDependency(CropList.class), (PlantList) appLoader.getAppDependency(PlantList.class));
backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 60000); backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 60000);
} }
/**
* Method which is automatically called when application is stopped.
* It cancels the timer to not execute the background tasks anymore.
*/
@Override @Override
public void stop(){ public void stop(){
backGroundTaskTimer.cancel(); backGroundTaskTimer.cancel();
} }
/**
* The Main method launches the application
* @param args There are no arguments needed.
*/
public static void main(String[] args) { public static void main(String[] args) {
launch(); launch();
} }
@@ -6,21 +6,58 @@ import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleBooleanProperty;
import java.util.List; import java.util.List;
import java.util.Objects;
/**
* Singleton Class to store default Settings and User Settings
*/
public class Settings { public class Settings {
private static final Settings instance; /*
private HardinessZone currentHardinessZone = HardinessZone.ZONE_8A; * The Class instance to use everywhere
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 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
private static final Settings instance;
/**
* The current Hardiness zone initialized as default value Zone_8A
*/
private HardinessZone currentHardinessZone = HardinessZone.ZONE_8A;
/**
* Setting to show or hide the Tutorial in Main Menu
*/
private final BooleanProperty showTutorial = new SimpleBooleanProperty(false);
/**
* The SMTP Credentials which are used to send E-Mail Notifications
* The following Google Account is created for Testing:
* E-Mail Address: pm3.hs22.it21b.win.team1@gmail.com
* Account Password: Gartenverwaltung.PM3.2022
* SMTP Password: bisefhhjtrrhtoqr
*/
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);
/**
* List of Receivers for E-Mailnotifications. Multiple E-Mailadresses can be separated by ";"
* Following Public E-mail address was used for Testing: pm3.hs22.it21b.win.team1@mailinator.com
* The E-Mail inbox of this address can be found here: https://www.mailinator.com/v4/public/inboxes.jsp?to=pm3.hs22.it21b.win.team1
*/
private String mailNotificationReceivers = "pm3.hs22.it21b.win.team1@mailinator.com";
/**
* String Template to create E-Mail Subject for Notifications
* Variables: Task Name
*/
private String mailNotificationSubjectTemplate = "Task %s is due!";
/**
* String Template to create E-Mail Text for Notifications
* Variables: Task Name, plant Name, nextExecution, 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";
/**
* Location of the garden can be set by user (Used for Weather Service)
*/
private String location = ""; private String location = "";
/*
Create Instance of Settings
*/
static { static {
instance = new Settings(); instance = new Settings();
} }
@@ -29,14 +66,21 @@ public class Settings {
return Settings.instance; return Settings.instance;
} }
/**
* Private constructor to prevent Classes from creating more instances
*/
private Settings() {} private Settings() {}
public HardinessZone getCurrentHardinessZone() { public HardinessZone getCurrentHardinessZone() {
return currentHardinessZone; return currentHardinessZone;
} }
/**
* Method to set the current Hardiness Zone. If no Hardiness Zone is given the default zone (ZONE_8A) will be used.
* @param currentHardinessZone the new Hardiness Zone
*/
public void setCurrentHardinessZone(HardinessZone currentHardinessZone) { public void setCurrentHardinessZone(HardinessZone currentHardinessZone) {
this.currentHardinessZone = currentHardinessZone; this.currentHardinessZone = Objects.requireNonNullElse(currentHardinessZone, HardinessZone.ZONE_8A);
} }
public void setShowTutorial (boolean showTutorial) { public void setShowTutorial (boolean showTutorial) {
@@ -7,7 +7,7 @@ import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.File; import java.io.InputStream;
/** /**
* Controller class for the Tutorial.fxml file * Controller class for the Tutorial.fxml file
@@ -46,10 +46,10 @@ public class TutorialController {
* @param fileName the file name of the source * @param fileName the file name of the source
*/ */
private void setImageView(ImageView imageView, String fileName) { private void setImageView(ImageView imageView, String fileName) {
File file = new File(String.valueOf(PlantsController.class.getResource(fileName))); InputStream is = PlantsController.class.getResourceAsStream("screenshots/" + fileName);
Image image; Image image;
if (file.exists()) { if (is != null) {
image = new Image(String.valueOf(PlantsController.class.getResource(fileName))); image = new Image(String.valueOf(PlantsController.class.getResource("screenshots/" + fileName)));
} else { } else {
image = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png"))); image = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));
} }
@@ -55,19 +55,19 @@ public class BackgroundTasks extends TimerTask {
movePastTasks(); movePastTasks();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
LOG.log(Level.SEVERE, "Could not execute Background Task: move past Tasks ", e.getCause()); LOG.log(Level.WARNING, "Could not execute Background Task: move past Tasks ", e.getCause());
} }
try { try {
weatherGardenTaskPlaner.refreshTasks(); weatherGardenTaskPlaner.refreshTasks();
} catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) { } catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
LOG.log(Level.SEVERE, "Could not execute Background Task: Refresh Tasks by WeatherGardenTaskPlaner ", e.getCause()); LOG.log(Level.WARNING, "Could not execute Background Task: Refresh Tasks by WeatherGardenTaskPlaner ", e.getCause());
} }
try { try {
notifier.sendNotifications(); notifier.sendNotifications();
} catch (IOException | MessagingException | HardinessZoneNotSetException e) { } catch (IOException | MessagingException | HardinessZoneNotSetException e) {
e.printStackTrace(); e.printStackTrace();
LOG.log(Level.SEVERE, "Could not execute Background Task: send Notification for due Tasks", e.getCause()); LOG.log(Level.WARNING, "Could not execute Background Task: send Notification for due Tasks", e.getCause());
} }
} }
} }
@@ -10,11 +10,8 @@ import ch.zhaw.gartenverwaltung.types.*;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import static java.util.stream.Collectors.toList;
/** /**
* The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days * The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days
*/ */
@@ -45,12 +42,19 @@ public class WeatherGradenTaskPlanner {
private void getSevereWeatherEvents() throws IOException { private void getSevereWeatherEvents() throws IOException {
SevereWeather actualWeather = weatherService.causeSevereWeather(1); SevereWeather actualWeather = weatherService.causeSevereWeather(1);
List<Crop> actualCrops = cropList.getCrops();
for (Crop crop : actualCrops) {
List<Task> actualCropTasks = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
if (SevereWeather.HAIL.equals(actualWeather)) { if (SevereWeather.HAIL.equals(actualWeather)) {
createPreHailTask(); createPreHailTask(crop, actualCropTasks);
} else if (SevereWeather.FROST.equals(actualWeather)) { } else if (SevereWeather.FROST.equals(actualWeather)) {
createPreFrostTask(); createPreFrostTask(crop, actualCropTasks);
} else if (SevereWeather.SNOW.equals(actualWeather)) { } else if (SevereWeather.SNOW.equals(actualWeather)) {
createPreSnowTask(); createPreSnowTask(crop, actualCropTasks);
}
} }
} }
@@ -60,87 +64,69 @@ public class WeatherGradenTaskPlanner {
} }
/** /**
* Method to create a PreHailTask * Method to create a PreHailTask and saves it in the tasklist
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void createPreHailTask() throws IOException { private void createPreHailTask(Crop crop, List<Task> actualTasksForCrop) throws IOException {
List<Crop> actualCrops = cropList.getCrops();
for (Crop crop : actualCrops) {
Task preHailTask = new Task("Hail", Task preHailTask = new Task("Hail",
"During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin", "During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin",
dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L)); dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L)); List<Task> hailTasks = actualTasksForCrop.stream().filter(task -> task.getName().equals("Hail")).toList();
List<Task> hailTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Hail")).toList();
List<Task> hailTaskListAtDate = new ArrayList<>(); if(isNoSevereWeatherTaskAtDate(preHailTask, hailTasks) && hailTasks.isEmpty()){
for (Task task : hailTasklist) {
if (task.getStartDate() == (preHailTask.getStartDate())) {
hailTaskListAtDate.add(task);
}
}
if(hailTaskListAtDate.isEmpty() && hailTasklist.isEmpty()){
taskList.saveTask(preHailTask); taskList.saveTask(preHailTask);
} }
} }
}
/** /**
* Method to create a PreFrosttask * Method to create a PreFrosttask and saves it in the tasklist
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void createPreFrostTask() throws IOException { private void createPreFrostTask(Crop crop, List<Task> actualTasksForCrop) throws IOException {
List<Crop> actualCrops = cropList.getCrops();
for (Crop crop : actualCrops) {
Task preFrostTask = new Task("Frost", Task preFrostTask = new Task("Frost",
"The temperatur falls below zero degrees, cover especially the root with wool", "The temperatur falls below zero degrees, cover especially the root with wool",
dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L)); dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L)); List<Task> frostTasks = actualTasksForCrop.stream().filter(task -> task.getName().equals("Frost")).toList();
List<Task> frostTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Frost")).toList();
List<Task> frostTaskListAtDate = new ArrayList<>(); if(isNoSevereWeatherTaskAtDate(preFrostTask, frostTasks) && frostTasks.isEmpty()){
for (Task task : frostTasklist) {
if (task.getStartDate() == preFrostTask.getStartDate()) {
frostTaskListAtDate.add(task);
}
}
if(frostTaskListAtDate.isEmpty() && frostTasklist.isEmpty()){
taskList.saveTask(preFrostTask); taskList.saveTask(preFrostTask);
} }
} }
}
/** /**
* Method to create a PreSnowTask * Method to create a PreSnowTask and saves it in the tasklist
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void createPreSnowTask() throws IOException { private void createPreSnowTask(Crop crop, List<Task> actualTasksForCrop) throws IOException {
List<Crop> actualCrops = cropList.getCrops();
for (Crop crop : actualCrops) {
Task preSnowTask = new Task("Snow", Task preSnowTask = new Task("Snow",
"The weather brings little snowfall. Cover your crops", "The weather brings little snowfall. Cover your crops",
dateSevereWeather, dateSevereWeather.plusDays(1L), crop.getCropId().orElse(-1L)); dateSevereWeather, dateSevereWeather.plusDays(1L), crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L)); List<Task> snowTasklist = actualTasksForCrop.stream().filter(task -> task.getName().equals("Snow")).toList();
List<Task> snowTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Snow")).toList();
List<Task> snowTaskListAtDate = new ArrayList<>(); if(isNoSevereWeatherTaskAtDate(preSnowTask, snowTasklist) && snowTasklist.isEmpty()){
for (Task task : snowTasklist) {
if (task.getStartDate() == preSnowTask.getStartDate()) {
snowTaskListAtDate.add(task);
}
}
if(snowTaskListAtDate .isEmpty() && snowTasklist.isEmpty()){
taskList.saveTask(preSnowTask); taskList.saveTask(preSnowTask);
} }
} }
/**
* Method to create a PreSnowTask and saves it in the tasklist
* @param preSevereWeatherTask the Task which would be added if there is not already
* the same Type of severe weather task
* @param severeWeatherTasks List of severe weather tasks from e specific severe weather
* @return true If there is not already a severe weather task of the same type of preSevereWeatherTask
* task at the date of the preSevereWeatherTask
*/
private boolean isNoSevereWeatherTaskAtDate(Task preSevereWeatherTask, List<Task> severeWeatherTasks) {
List<Task> severeWeatherTasksAtDate = new ArrayList<>();
for (Task task : severeWeatherTasks) {
if (task.getStartDate() == preSevereWeatherTask.getStartDate()) {
severeWeatherTasksAtDate.add(task);
}
}
return severeWeatherTasksAtDate.isEmpty();
} }
/** /**
@@ -1,5 +1,6 @@
package ch.zhaw.gartenverwaltung.bootstrap; package ch.zhaw.gartenverwaltung.bootstrap;
import ch.zhaw.gartenverwaltung.CropDetailController;
import ch.zhaw.gartenverwaltung.Main; import ch.zhaw.gartenverwaltung.Main;
import ch.zhaw.gartenverwaltung.io.*; import ch.zhaw.gartenverwaltung.io.*;
import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.Garden;
@@ -17,12 +18,15 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* Class responsible for bootstrapping the application wide dependencies * Class responsible for bootstrapping the application wide dependencies
* and injecting them into JavaFX Controllers. * and injecting them into JavaFX Controllers.
*/ */
public class AppLoader { public class AppLoader {
private static final Logger LOG = Logger.getLogger(CropDetailController.class.getName());
/** /**
* Caching the panes * Caching the panes
*/ */
@@ -146,7 +150,7 @@ public class AppLoader {
try { try {
afterInjectMethod.invoke(controller); afterInjectMethod.invoke(controller);
} catch (IllegalAccessException | InvocationTargetException e) { } catch (IllegalAccessException | InvocationTargetException e) {
// TODO: Log LOG.log(Level.SEVERE, "Could not invoke afterInjectMethod", e.getCause());
e.printStackTrace(); e.printStackTrace();
} }
}); });
@@ -1,5 +1,6 @@
package ch.zhaw.gartenverwaltung.models; package ch.zhaw.gartenverwaltung.models;
import ch.zhaw.gartenverwaltung.Settings;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.io.PlantList;
import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; import ch.zhaw.gartenverwaltung.types.GrowthPhaseType;
@@ -16,7 +17,6 @@ import java.util.stream.Collectors;
public class PlantListModel { public class PlantListModel {
private final PlantList plantList; private final PlantList plantList;
private HardinessZone currentZone;
/** /**
* Comparators to create sorted Plant List * Comparators to create sorted Plant List
@@ -33,15 +33,15 @@ public class PlantListModel {
} }
private void setDefaultZone() { private void setDefaultZone() {
currentZone = HardinessZone.ZONE_8A; // TODO: get Default Zone from Settings Settings.getInstance().setCurrentHardinessZone(null);
} }
public void setCurrentZone(HardinessZone currentZone) { public void setCurrentZone(HardinessZone currentZone) {
this.currentZone = currentZone; Settings.getInstance().setCurrentHardinessZone(currentZone);
} }
public HardinessZone getCurrentZone() { public HardinessZone getCurrentZone() {
return currentZone; return Settings.getInstance().getCurrentHardinessZone();
} }
/** /**
@@ -68,7 +68,7 @@ public record Plant(
return growthPhase.group(); return growthPhase.group();
} }
} }
return 0; // TODO implement return 0;
} }
/** /**
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

@@ -101,7 +101,7 @@ class PlantListModelTest {
@Test @Test
void setCurrentZone() { void setCurrentZone() {
checkCurrentZone(HardinessZone.ZONE_8A); // TODO change to get default zone from config checkCurrentZone(HardinessZone.ZONE_8A);
model.setCurrentZone(HardinessZone.ZONE_1A); model.setCurrentZone(HardinessZone.ZONE_1A);
checkCurrentZone(HardinessZone.ZONE_1A); checkCurrentZone(HardinessZone.ZONE_1A);
model.setCurrentZone(HardinessZone.ZONE_8A); model.setCurrentZone(HardinessZone.ZONE_8A);