Add period to Windows scheduled task

This commit is contained in:
Nathan McRae 2025-01-29 20:11:37 -08:00
parent c7fb317e05
commit b9d95acd04

View File

@ -12,6 +12,8 @@ import java.nio.file.Path;
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;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
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;
@ -39,12 +41,15 @@ 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());
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
// RegistrationInfo element // RegistrationInfo element
Element registrationInfo = doc.createElement("RegistrationInfo"); Element registrationInfo = doc.createElement("RegistrationInfo");
rootElement.appendChild(registrationInfo); rootElement.appendChild(registrationInfo);
Element date = doc.createElement("Date"); Element date = doc.createElement("Date");
date.appendChild(doc.createTextNode("2025-01-22T22:09:01.6765321")); date.appendChild(doc.createTextNode(scheduleDateTime.format(dateFormatter)));
registrationInfo.appendChild(date); registrationInfo.appendChild(date);
Element author = doc.createElement("Author"); Element author = doc.createElement("Author");
@ -63,7 +68,7 @@ public class WindowsScheduler {
triggers.appendChild(calendarTrigger); triggers.appendChild(calendarTrigger);
Element startBoundary = doc.createElement("StartBoundary"); Element startBoundary = doc.createElement("StartBoundary");
startBoundary.appendChild(doc.createTextNode("2025-01-22T22:08:06")); startBoundary.appendChild(doc.createTextNode(scheduleDateTime.format(dateFormatter)));
calendarTrigger.appendChild(startBoundary); calendarTrigger.appendChild(startBoundary);
Element enabled = doc.createElement("Enabled"); Element enabled = doc.createElement("Enabled");
@ -74,12 +79,46 @@ public class WindowsScheduler {
randomDelay.appendChild(doc.createTextNode("PT1H")); randomDelay.appendChild(doc.createTextNode("PT1H"));
calendarTrigger.appendChild(randomDelay); calendarTrigger.appendChild(randomDelay);
Element scheduleByDay = doc.createElement("ScheduleByDay"); switch(station.getMessagePeriod()) {
calendarTrigger.appendChild(scheduleByDay); case DAILY:
{
Element scheduleByDay = doc.createElement("ScheduleByDay");
calendarTrigger.appendChild(scheduleByDay);
Element daysInterval = doc.createElement("DaysInterval");
daysInterval.appendChild(doc.createTextNode("1"));
scheduleByDay.appendChild(daysInterval);
}
break;
case WEEKLY:
{
Element scheduleByDay = doc.createElement("ScheduleByDay");
calendarTrigger.appendChild(scheduleByDay);
Element daysInterval = doc.createElement("DaysInterval");
daysInterval.appendChild(doc.createTextNode("7"));
scheduleByDay.appendChild(daysInterval);
}
break;
case MONTHLY:
Element scheduleByMonth = doc.createElement("ScheduleByMonth");
calendarTrigger.appendChild(scheduleByMonth);
Element daysOfMonth = doc.createElement("DaysOfMonth");
scheduleByMonth.appendChild(daysOfMonth);
Element day = doc.createElement("Day");
day.appendChild(doc.createTextNode(String.valueOf(station.getScheduleStartDate().getDayOfMonth())));
daysOfMonth.appendChild(day);
Element daysInterval = doc.createElement("DaysInterval"); Element months = doc.createElement("Months");
daysInterval.appendChild(doc.createTextNode("1")); String[] monthNames = {
scheduleByDay.appendChild(daysInterval); "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
for (String monthName : monthNames) {
Element month = doc.createElement(monthName);
months.appendChild(month);
}
scheduleByMonth.appendChild(months);
break;
}
// Principals element // Principals element
Element principals = doc.createElement("Principals"); Element principals = doc.createElement("Principals");