Merge remote-tracking branch 'origin/feature_weather' into feature_weather

This commit is contained in:
Gian-Andrea Hutter 2022-12-04 16:32:51 +01:00
commit d0c6525d7a
3 changed files with 36 additions and 27 deletions

View File

@ -22,6 +22,7 @@ public class Main extends Application {
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)); 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); backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 1000);
appLoader.loadSceneToStage("MainFXML.fxml", stage); appLoader.loadSceneToStage("MainFXML.fxml", stage);

View File

@ -1,44 +1,62 @@
package ch.zhaw.gartenverwaltung.backgroundtasks; package ch.zhaw.gartenverwaltung.backgroundtasks;
import ch.zhaw.gartenverwaltung.CropDetailController;
import ch.zhaw.gartenverwaltung.backgroundtasks.weather.WeatherGradenTaskPlanner; import ch.zhaw.gartenverwaltung.backgroundtasks.weather.WeatherGradenTaskPlanner;
import ch.zhaw.gartenverwaltung.io.CropList; import ch.zhaw.gartenverwaltung.io.CropList;
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.io.TaskList; import ch.zhaw.gartenverwaltung.io.TaskList;
import ch.zhaw.gartenverwaltung.models.Garden;
import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; import ch.zhaw.gartenverwaltung.models.PlantNotFoundException;
import ch.zhaw.gartenverwaltung.types.Task;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BackgroundTasks extends TimerTask { 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 Notifier notifier;
private final WeatherGradenTaskPlanner weatherGardenTaskPlaner; private final WeatherGradenTaskPlanner weatherGardenTaskPlaner;
private void movePastTasks() throws IOException {
List<Task> 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) { public BackgroundTasks(TaskList taskList, CropList cropList, PlantList plantList) {
this.taskList = taskList;
notifier = new Notifier(taskList, plantList, cropList); notifier = new Notifier(taskList, plantList, cropList);
weatherGardenTaskPlaner = new WeatherGradenTaskPlanner(taskList, plantList, cropList); weatherGardenTaskPlaner = new WeatherGradenTaskPlanner(taskList, plantList, cropList);
} }
@Override @Override
public void run() { 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 { try {
weatherGardenTaskPlaner.refreshTasks(); weatherGardenTaskPlaner.refreshTasks();
} catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) { } catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
// TODO logger LOG.log(Level.SEVERE, "Could not execute Background Task: Refresh Tasks by WeatherGardenTaskPlaner ", e.getCause());
} }
try {
try { try {
notifier.sendNotifications(); notifier.sendNotifications();
} catch (MessagingException e) { } catch (IOException | MessagingException | HardinessZoneNotSetException e) {
e.printStackTrace(); e.printStackTrace();
// TODO logger LOG.log(Level.SEVERE, "Could not execute Background Task: send Notification for due Tasks", e.getCause());
}
} catch (IOException e) {
e.printStackTrace();
// TODO logger
} }
} }
} }

View File

@ -25,30 +25,20 @@ public class Notifier {
this.plantList = plantList; this.plantList = plantList;
} }
private void sendNotification(Task task) { private void sendNotification(Task task) throws IOException, MessagingException, HardinessZoneNotSetException {
String plantName = "unkown plant"; String plantName = "unkown plant";
try {
if(cropList.getCropById((task.getCropId())).isPresent()){ if(cropList.getCropById((task.getCropId())).isPresent()){
Crop crop = cropList.getCropById(task.getCropId()).get(); Crop crop = cropList.getCropById(task.getCropId()).get();
if(plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).isPresent()) { if(plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).isPresent()) {
plantName = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get().name(); 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 messageSubject = String.format(Settings.getInstance().getMailNotificationSubjectTemplate(), task.getName());
String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), plantName, task.getNextExecution(), task.getDescription()); String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), plantName, task.getNextExecution(), task.getDescription());
try {
eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText); eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText);
} catch (MessagingException e) {
e.printStackTrace();
// TODO Logger
}
} }
public void sendNotifications() throws IOException, MessagingException { public void sendNotifications() throws IOException, MessagingException, HardinessZoneNotSetException {
for (Task task : taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)) { for (Task task : taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)) {
if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) { if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) {
sendNotification(task); sendNotification(task);