2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.hueemulation.internal.automation;
15 import java.time.Instant;
16 import java.time.LocalDate;
17 import java.time.LocalDateTime;
18 import java.time.ZoneId;
19 import java.time.format.DateTimeFormatter;
20 import java.time.temporal.ChronoField;
21 import java.time.temporal.TemporalAccessor;
24 import org.openhab.core.automation.ModuleHandlerCallback;
25 import org.openhab.core.automation.Trigger;
26 import org.openhab.core.automation.handler.BaseTriggerModuleHandler;
27 import org.openhab.core.automation.handler.TriggerHandlerCallback;
28 import org.openhab.core.scheduler.ScheduledCompletableFuture;
29 import org.openhab.core.scheduler.Scheduler;
30 import org.openhab.core.scheduler.SchedulerRunnable;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Extends the core provided time related module type by an absolute day/time trigger.
37 * It allows to set a date and a time as separate configuration values (easier to manipulate from
38 * other actions / rules etc) and also allows the user to setup a random factor
39 * (presence simulation).
41 * @author David Graeff - Initial contribution
43 public class AbsoluteDateTimeTriggerHandler extends BaseTriggerModuleHandler implements SchedulerRunnable {
45 private final Logger logger = LoggerFactory.getLogger(AbsoluteDateTimeTriggerHandler.class);
47 public static final String MODULE_TYPE_ID = "timer.AbsoluteDateTimeTrigger";
48 public static final String CALLBACK_CONTEXT_NAME = "CALLBACK";
49 public static final String MODULE_CONTEXT_NAME = "MODULE";
51 public static final String CFG_DATE = "date";
52 public static final String CFG_TIME = "time";
53 public static final String CFG_TIME_RND = "randomizeTime";
55 private final Scheduler scheduler;
56 private final Instant dateTime;
57 private ScheduledCompletableFuture<?> schedule;
58 private static final String DATE_FORMAT = "yyyy-MM-dd";
59 private static final String TIME_FORMAT = "HH:mm:ss";
60 private static final String DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
61 private final DateTimeFormatter dateTimeformatter;
63 public AbsoluteDateTimeTriggerHandler(Trigger module, Scheduler scheduler) {
65 this.scheduler = scheduler;
66 dateTimeformatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT);
68 DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
69 DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(TIME_FORMAT);
71 // Take optional date into account
72 String cfgDate = (String) module.getConfiguration().get(CFG_DATE);
73 LocalDateTime dateTime = cfgDate == null || cfgDate.isEmpty() ? LocalDate.now().atStartOfDay()
74 : LocalDateTime.from(dateFormatter.parse(cfgDate));
76 // Take optional time into account
77 String cfgDTime = (String) module.getConfiguration().get(CFG_TIME);
78 if (cfgDTime != null && !cfgDTime.isEmpty()) {
79 TemporalAccessor temporalAccessor = timeFormatter.parse(cfgDTime);
80 dateTime.plusHours(temporalAccessor.getLong(ChronoField.HOUR_OF_DAY));
81 dateTime.plusMinutes(temporalAccessor.getLong(ChronoField.MINUTE_OF_HOUR));
84 this.dateTime = dateTime.atZone(ZoneId.systemDefault()).toInstant();
88 public synchronized void setCallback(ModuleHandlerCallback callback) {
89 super.setCallback(callback);
93 private void scheduleJob() {
94 schedule = scheduler.at(this, dateTime);
95 logger.debug("Scheduled absolute date/time '{}' for trigger '{}'.", dateTimeformatter.format(dateTime),
100 public synchronized void dispose() {
102 if (schedule != null) {
103 schedule.cancel(true);
104 logger.debug("cancelled job for trigger '{}'.", module.getId());
110 ((TriggerHandlerCallback) callback).triggered(module, Map.of());