]> git.basschouten.com Git - openhab-addons.git/blob
721a9a2aec0b664f04f8465ae205347c8718313a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.hueemulation.internal.automation;
14
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;
22 import java.util.Map;
23
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;
33
34 /**
35  * Extends the core provided time related module type by an absolute day/time trigger.
36  * <p>
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).
40  *
41  * @author David Graeff - Initial contribution
42  */
43 public class AbsoluteDateTimeTriggerHandler extends BaseTriggerModuleHandler implements SchedulerRunnable {
44
45     private final Logger logger = LoggerFactory.getLogger(AbsoluteDateTimeTriggerHandler.class);
46
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";
50
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";
54
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;
62
63     public AbsoluteDateTimeTriggerHandler(Trigger module, Scheduler scheduler) {
64         super(module);
65         this.scheduler = scheduler;
66         dateTimeformatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT);
67
68         DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
69         DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(TIME_FORMAT);
70
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));
75
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));
82         }
83
84         this.dateTime = dateTime.atZone(ZoneId.systemDefault()).toInstant();
85     }
86
87     @Override
88     public synchronized void setCallback(ModuleHandlerCallback callback) {
89         super.setCallback(callback);
90         scheduleJob();
91     }
92
93     private void scheduleJob() {
94         schedule = scheduler.at(this, dateTime);
95         logger.debug("Scheduled absolute date/time '{}' for trigger '{}'.", dateTimeformatter.format(dateTime),
96                 module.getId());
97     }
98
99     @Override
100     public synchronized void dispose() {
101         super.dispose();
102         if (schedule != null) {
103             schedule.cancel(true);
104             logger.debug("cancelled job for trigger '{}'.", module.getId());
105         }
106     }
107
108     @Override
109     public void run() {
110         ((TriggerHandlerCallback) callback).triggered(module, Map.of());
111         schedule = null;
112     }
113 }