Compare commits
2 Commits
5e42cae290
...
564cecd4ff
Author | SHA1 | Date |
---|---|---|
schrom01 | 564cecd4ff | |
schrom01 | 2f5440696f |
|
@ -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);
|
||||
|
|
|
@ -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<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) {
|
||||
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) {
|
||||
} catch (IOException | MessagingException | HardinessZoneNotSetException e) {
|
||||
e.printStackTrace();
|
||||
// TODO logger
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
// TODO logger
|
||||
LOG.log(Level.SEVERE, "Could not execute Background Task: send Notification for due Tasks", e.getCause());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
} 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
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue