Compare commits

...

2 Commits

5 changed files with 51 additions and 45 deletions

View File

@ -11,6 +11,8 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -122,10 +124,12 @@ public class LinuxScheduler {
* minute (059) * minute (059)
*/ */
public static String cronExpression(StationSettings settings) throws CronExpressionException { public static String cronExpression(StationSettings settings) throws CronExpressionException {
String minute = Integer.toString(settings.getScheduleStartTime().getMinute()); LocalDateTime scheduleDateTime = settings.getScheduleStart().withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
String hour = Integer.toString(settings.getScheduleStartTime().getHour());
String dayOfMonth = Integer.toString(settings.getScheduleStartDate().getDayOfMonth()); String minute = Integer.toString(scheduleDateTime.getMinute());
String dayOfWeek = Integer.toString(settings.getScheduleStartDate().getDayOfMonth()); String hour = Integer.toString(scheduleDateTime.getHour());
String dayOfMonth = Integer.toString(scheduleDateTime.getDayOfMonth());
String dayOfWeek = Integer.toString(scheduleDateTime.getDayOfMonth());
String scheduleString = ""; String scheduleString = "";
switch(settings.getMessagePeriod()) { switch(settings.getMessagePeriod()) {

View File

@ -112,6 +112,9 @@ public class StationSelectionController {
@FXML @FXML
private void handleRemoveButtonPress(ActionEvent event) { private void handleRemoveButtonPress(ActionEvent event) {
int selectedIndex = stationListView.getSelectionModel().getSelectedIndex(); int selectedIndex = stationListView.getSelectionModel().getSelectedIndex();
if (selectedIndex < 0) {
return;
}
StationSettings station = stationList.get(selectedIndex); StationSettings station = stationList.get(selectedIndex);
String stationName = station.toString(); String stationName = station.toString();

View File

@ -12,6 +12,8 @@ import java.nio.file.SimpleFileVisitor;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.Period; import java.time.Period;
import java.util.ArrayList; import java.util.ArrayList;
@ -32,8 +34,7 @@ public class StationSettings {
private String name; private String name;
private String password; private String password;
private ArrayList<String> prefixes; private ArrayList<String> prefixes;
private LocalDate scheduleStartDate; private ZonedDateTime scheduleStart;
private LocalTime scheduleStartTime;
private String username; private String username;
public enum MessageMethod { public enum MessageMethod {
@ -54,8 +55,7 @@ public class StationSettings {
messageLength = 100; messageLength = 100;
messageMethod = MessageMethod.SFTP; messageMethod = MessageMethod.SFTP;
messagePeriod = MessagePeriod.DAILY; messagePeriod = MessagePeriod.DAILY;
scheduleStartDate = LocalDate.now(); scheduleStart = ZonedDateTime.now();
scheduleStartTime = LocalTime.now();
} }
public StationSettings(String newName) { public StationSettings(String newName) {
@ -65,8 +65,7 @@ public class StationSettings {
messageLength = 100; messageLength = 100;
messageMethod = MessageMethod.SFTP; messageMethod = MessageMethod.SFTP;
messagePeriod = MessagePeriod.DAILY; messagePeriod = MessagePeriod.DAILY;
scheduleStartDate = LocalDate.now(); scheduleStart = ZonedDateTime.now();
scheduleStartTime = LocalTime.now();
} }
public static void deleteStationData(String stationName) throws IOException { public static void deleteStationData(String stationName) throws IOException {
@ -184,42 +183,43 @@ public class StationSettings {
} }
public LocalDateTime nextSendTime() throws StationSettingsException { public LocalDateTime nextSendTime() throws StationSettingsException {
Period sinceScheduleStart = Period.between(scheduleStartDate , LocalDate.now()); LocalDate startDate = scheduleStart.withZoneSameInstant(ZoneId.systemDefault()).toLocalDate();
LocalTime startTime = scheduleStart.withZoneSameInstant(ZoneId.systemDefault()).toLocalTime();
Period sinceScheduleStart = Period.between(startDate, LocalDate.now());
// If this period's time has not yet passed, then show that time. // If this period's time has not yet passed, then show that time.
// Otherwise, show the next period's time. // Otherwise, show the next period's time.
switch (messagePeriod) { switch (messagePeriod) {
case DAILY: case DAILY:
if (LocalTime.now().isBefore(scheduleStartTime)) { if (LocalTime.now().isBefore(startTime)) {
return LocalDate.now().atTime(scheduleStartTime); return LocalDate.now().atTime(startTime);
} else { } else {
return LocalDate.now().plusDays(1).atTime(scheduleStartTime); return LocalDate.now().plusDays(1).atTime(startTime);
} }
case WEEKLY: case WEEKLY:
int weekdayDifference = scheduleStartDate.getDayOfWeek().getValue() - LocalDate.now().getDayOfWeek().getValue(); int weekdayDifference = startDate.getDayOfWeek().getValue() - LocalDate.now().getDayOfWeek().getValue();
System.out.println("weekdayDifference: " + weekdayDifference);
if (weekdayDifference > 0) { if (weekdayDifference > 0) {
return LocalDate.now().plusDays(weekdayDifference).atTime(scheduleStartTime); return LocalDate.now().plusDays(weekdayDifference).atTime(startTime);
} else if (weekdayDifference == 0){ } else if (weekdayDifference == 0){
if (LocalTime.now().isBefore(scheduleStartTime)) { if (LocalTime.now().isBefore(startTime)) {
return LocalDate.now().atTime(scheduleStartTime); return LocalDate.now().atTime(startTime);
} else { } else {
return LocalDate.now().plusWeeks(1).atTime(scheduleStartTime); return LocalDate.now().plusWeeks(1).atTime(startTime);
} }
} else { } else {
return LocalDate.now().plusDays(7 + weekdayDifference).atTime(scheduleStartTime); return LocalDate.now().plusDays(7 + weekdayDifference).atTime(startTime);
} }
case MONTHLY: case MONTHLY:
int monthdayDifference = scheduleStartDate.getDayOfMonth() - LocalDate.now().getDayOfMonth(); int monthdayDifference = startDate.getDayOfMonth() - LocalDate.now().getDayOfMonth();
if (monthdayDifference > 0) { if (monthdayDifference > 0) {
return LocalDate.now().plusDays(monthdayDifference).atTime(scheduleStartTime); return LocalDate.now().plusDays(monthdayDifference).atTime(startTime);
} else if (monthdayDifference == 0) { } else if (monthdayDifference == 0) {
if (LocalTime.now().isBefore(scheduleStartTime)) { if (LocalTime.now().isBefore(startTime)) {
return LocalDate.now().atTime(scheduleStartTime); return LocalDate.now().atTime(startTime);
} else { } else {
return LocalDate.now().plusMonths(1).atTime(scheduleStartTime); return LocalDate.now().plusMonths(1).atTime(startTime);
} }
} else { } else {
return LocalDate.now().plusMonths(1).plusDays(monthdayDifference).atTime(scheduleStartTime); return LocalDate.now().plusMonths(1).plusDays(monthdayDifference).atTime(startTime);
} }
default: default:
throw new StationSettingsException("Invalid period value: " + messagePeriod); throw new StationSettingsException("Invalid period value: " + messagePeriod);
@ -305,20 +305,12 @@ public class StationSettings {
return prefixes; return prefixes;
} }
public LocalDate getScheduleStartDate() { public ZonedDateTime getScheduleStart() {
return scheduleStartDate; return scheduleStart;
} }
public void setScheduleStartDate(LocalDate newScheduleStartDate) { public void setScheduleStart(ZonedDateTime newScheduleStart) {
scheduleStartDate = newScheduleStartDate; scheduleStart = newScheduleStart;
}
public LocalTime getScheduleStartTime() {
return scheduleStartTime;
}
public void setScheduleStartTime(LocalTime newScheduleStartTime) {
scheduleStartTime = newScheduleStartTime;
} }
public String getUsername() { public String getUsername() {

View File

@ -15,6 +15,9 @@ import java.nio.file.Path;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -433,7 +436,6 @@ public class StationSettingsController {
settings.setPassword(password.get()); settings.setPassword(password.get());
settings.getPrefixes().clear(); settings.getPrefixes().clear();
settings.getPrefixes().addAll(prefixListView.getItems()); settings.getPrefixes().addAll(prefixListView.getItems());
settings.setScheduleStartDate(scheduleStartDatePicker.getValue());
settings.setUsername(username.get()); settings.setUsername(username.get());
try { try {
@ -447,7 +449,9 @@ public class StationSettingsController {
try { try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime startTime = LocalTime.parse(scheduleStartTimeField.getText(), formatter); LocalTime startTime = LocalTime.parse(scheduleStartTimeField.getText(), formatter);
settings.setScheduleStartTime(startTime); ZonedDateTime scheduleStart = ZonedDateTime.of(scheduleStartDatePicker.getValue(), startTime, ZoneId.systemDefault());
settings.setScheduleStart(scheduleStart.withZoneSameInstant(ZoneOffset.UTC));
} catch (Exception ex) { } catch (Exception ex) {
logger.log(Level.SEVERE, "Error parsing schedule start time", ex); logger.log(Level.SEVERE, "Error parsing schedule start time", ex);
} }
@ -557,9 +561,9 @@ public class StationSettingsController {
password.set(settings.getPassword()); password.set(settings.getPassword());
System.out.println(settings.getPrefixes()); System.out.println(settings.getPrefixes());
prefixListView.getItems().addAll(settings.getPrefixes()); prefixListView.getItems().addAll(settings.getPrefixes());
scheduleStartDate.set(settings.getScheduleStartDate()); scheduleStartDate.set(settings.getScheduleStart().withZoneSameInstant(ZoneId.systemDefault()).toLocalDate());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime startTime = settings.getScheduleStartTime(); LocalTime startTime = settings.getScheduleStart().withZoneSameInstant(ZoneId.systemDefault()).toLocalTime();
if (startTime == null) { if (startTime == null) {
startTime = LocalTime.now(); startTime = LocalTime.now();
} }

View File

@ -16,7 +16,9 @@ import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId;
import javafx.util.Pair; import javafx.util.Pair;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -44,7 +46,7 @@ public class WindowsScheduler {
rootElement.setAttribute("xmlns", "http://schemas.microsoft.com/windows/2004/02/mit/task"); rootElement.setAttribute("xmlns", "http://schemas.microsoft.com/windows/2004/02/mit/task");
doc.appendChild(rootElement); doc.appendChild(rootElement);
LocalDateTime scheduleDateTime = station.getScheduleStartDate().atTime(station.getScheduleStartTime()); LocalDateTime scheduleDateTime = station.getScheduleStart().withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
// RegistrationInfo element // RegistrationInfo element
@ -107,7 +109,8 @@ public class WindowsScheduler {
Element daysOfMonth = doc.createElement("DaysOfMonth"); Element daysOfMonth = doc.createElement("DaysOfMonth");
scheduleByMonth.appendChild(daysOfMonth); scheduleByMonth.appendChild(daysOfMonth);
Element day = doc.createElement("Day"); Element day = doc.createElement("Day");
day.appendChild(doc.createTextNode(String.valueOf(station.getScheduleStartDate().getDayOfMonth()))); LocalDate startDate = station.getScheduleStart().withZoneSameInstant(ZoneId.systemDefault()).toLocalDate();
day.appendChild(doc.createTextNode(String.valueOf(startDate.getDayOfMonth())));
daysOfMonth.appendChild(day); daysOfMonth.appendChild(day);
Element months = doc.createElement("Months"); Element months = doc.createElement("Months");