Skip to content

Commit

Permalink
Support offset in DateTimeTrigger (#4271)
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng authored Sep 8, 2024
1 parent 3e912ec commit 69dc832
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* based on a {@link org.openhab.core.library.types.DateTimeType} stored in an item
*
* @author Jan N. Klug - Initial contribution
* @author Jimmy Tanagra - Add offset support
*/
@NonNullByDefault
public class DateTimeTriggerHandler extends BaseTriggerModuleHandler
Expand All @@ -60,6 +61,7 @@ public class DateTimeTriggerHandler extends BaseTriggerModuleHandler
public static final String MODULE_TYPE_ID = "timer.DateTimeTrigger";
public static final String CONFIG_ITEM_NAME = "itemName";
public static final String CONFIG_TIME_ONLY = "timeOnly";
public static final String CONFIG_OFFSET = "offset";

private static final DateTimeFormatter CRON_FORMATTER = DateTimeFormatter.ofPattern("s m H d M * uuuu");
private static final DateTimeFormatter CRON_TIMEONLY_FORMATTER = DateTimeFormatter.ofPattern("s m H * * * *");
Expand All @@ -71,6 +73,7 @@ public class DateTimeTriggerHandler extends BaseTriggerModuleHandler
private final @Nullable EventFilter eventFilter;
private String cronExpression = CronAdjuster.REBOOT;
private Boolean timeOnly = false;
private Long offset = 0L;

private @Nullable ScheduledCompletableFuture<?> schedule;
private @Nullable ServiceRegistration<?> eventSubscriberRegistration;
Expand All @@ -88,6 +91,7 @@ public DateTimeTriggerHandler(Trigger module, CronScheduler scheduler, ItemRegis
this.eventFilter = new TopicPrefixEventFilter("openhab/items/" + itemName + "/");
this.timeOnly = ConfigParser.valueAsOrElse(module.getConfiguration().get(CONFIG_TIME_ONLY), Boolean.class,
false);
this.offset = ConfigParser.valueAsOrElse(module.getConfiguration().get(CONFIG_OFFSET), Long.class, 0L);
eventSubscriberRegistration = bundleContext.registerService(EventSubscriber.class.getName(), this, null);
try {
process(itemRegistry.getItem(itemName).getState());
Expand Down Expand Up @@ -153,8 +157,8 @@ private synchronized void startScheduler() {
cancelScheduler();
if (!CronAdjuster.REBOOT.equals(cronExpression)) {
schedule = scheduler.schedule(this, cronExpression);
logger.debug("Scheduled cron job '{}' for trigger '{}'.", module.getConfiguration().get(CONFIG_ITEM_NAME),
module.getId());
logger.debug("Scheduled cron job '{}' from item '{}' for trigger '{}'.", cronExpression,
module.getConfiguration().get(CONFIG_ITEM_NAME), module.getId());
}
}

Expand All @@ -174,6 +178,7 @@ private void process(Type value) {
} else if (value instanceof DateTimeType dateTimeType) {
boolean itemIsTimeOnly = dateTimeType.toString().startsWith("1970-01-01T");
cronExpression = dateTimeType.getZonedDateTime().withZoneSameInstant(ZoneId.systemDefault())
.plusSeconds(offset.longValue())
.format(timeOnly || itemIsTimeOnly ? CRON_TIMEONLY_FORMATTER : CRON_FORMATTER);
startScheduler();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
"value": "false"
}
]
},
{
"name": "offset",
"type": "INTEGER",
"label": "Offset",
"description": "The offset in seconds to add to the time of the item."
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ module-type.timer.DateTimeTrigger.config.timeOnly.label = Time only
module-type.timer.DateTimeTrigger.config.timeOnly.description = Specifies whether only the time of the item should be compared or the date and time.
module-type.timer.DateTimeTrigger.config.timeOnly.option.true = Yes
module-type.timer.DateTimeTrigger.config.timeOnly.option.false = No
module-type.timer.DateTimeTrigger.config.offset.label = Offset
module-type.timer.DateTimeTrigger.config.offset.description = The offset in seconds to add to the time of the item.

# timer.DayOfWeekCondition

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,30 @@ public void testDifferentTimeZone() {

verify(mockScheduler).schedule(eq(handler), eq("0 0 0 11 8 * 2022"));
}

@Test
public void testOffsetPositive() {
ZonedDateTime zdt = ZonedDateTime.of(2024, 6, 7, 0, 0, 0, 0, ZoneId.systemDefault());
item.setState(new DateTimeType(zdt));
when(mockTrigger.getConfiguration())
.thenReturn(new Configuration(Map.ofEntries(entry(DateTimeTriggerHandler.CONFIG_ITEM_NAME, ITEM_NAME),
entry(DateTimeTriggerHandler.CONFIG_OFFSET, 10))));
DateTimeTriggerHandler handler = new DateTimeTriggerHandler(mockTrigger, mockScheduler, mockItemRegistry,
mockBundleContext);

verify(mockScheduler).schedule(eq(handler), eq("10 0 0 7 6 * 2024"));
}

@Test
public void testOffsetNegative() {
ZonedDateTime zdt = ZonedDateTime.of(2024, 6, 7, 0, 0, 0, 0, ZoneId.systemDefault());
item.setState(new DateTimeType(zdt));
when(mockTrigger.getConfiguration())
.thenReturn(new Configuration(Map.ofEntries(entry(DateTimeTriggerHandler.CONFIG_ITEM_NAME, ITEM_NAME),
entry(DateTimeTriggerHandler.CONFIG_OFFSET, -10))));
DateTimeTriggerHandler handler = new DateTimeTriggerHandler(mockTrigger, mockScheduler, mockItemRegistry,
mockBundleContext);

verify(mockScheduler).schedule(eq(handler), eq("50 59 23 6 6 * 2024"));
}
}

0 comments on commit 69dc832

Please sign in to comment.