]> git.basschouten.com Git - openhab-addons.git/blob
b8ad08575ea43c646527b244d79c229dff14e146
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
23 import org.openhab.core.automation.ModuleHandlerCallback;
24 import org.openhab.core.automation.Trigger;
25 import org.openhab.core.automation.handler.BaseTriggerModuleHandler;
26 import org.openhab.core.automation.handler.TriggerHandlerCallback;
27 import org.openhab.core.scheduler.ScheduledCompletableFuture;
28 import org.openhab.core.scheduler.Scheduler;
29 import org.openhab.core.scheduler.SchedulerRunnable;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Extends the core provided time related module type by an absolute day/time trigger.
35  * <p>
36  * It allows to set a date and a time as separate configuration values (easier to manipulate from
37  * other actions / rules etc) and also allows the user to setup a random factor
38  * (presence simulation).
39  *
40  * @author David Graeff - Initial contribution
41  */
42 public class AbsoluteDateTimeTriggerHandler extends BaseTriggerModuleHandler implements SchedulerRunnable {
43
44     private final Logger logger = LoggerFactory.getLogger(AbsoluteDateTimeTriggerHandler.class);
45
46     public static final String MODULE_TYPE_ID = "timer.AbsoluteDateTimeTrigger";
47     public static final String CALLBACK_CONTEXT_NAME = "CALLBACK";
48     public static final String MODULE_CONTEXT_NAME = "MODULE";
49
50     public static final String CFG_DATE = "date";
51     public static final String CFG_TIME = "time";
52     public static final String CFG_TIME_RND = "randomizeTime";
53
54     private final Scheduler scheduler;
55     private final Instant dateTime;
56     private ScheduledCompletableFuture<?> schedule;
57     private static final String DATE_FORMAT = "yyyy-MM-dd";
58     private static final String TIME_FORMAT = "HH:mm:ss";
59     private static final String DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
60     private final DateTimeFormatter dateTimeformatter;
61
62     public AbsoluteDateTimeTriggerHandler(Trigger module, Scheduler scheduler) {
63         super(module);
64         this.scheduler = scheduler;
65         dateTimeformatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT);
66
67         DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
68         DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(TIME_FORMAT);
69
70         // Take optional date into account
71         String cfgDate = (String) module.getConfiguration().get(CFG_DATE);
72         LocalDateTime dateTime = cfgDate == null || cfgDate.isEmpty() ? LocalDate.now().atStartOfDay()
73                 : LocalDateTime.from(dateFormatter.parse(cfgDate));
74
75         // Take optional time into account
76         String cfgDTime = (String) module.getConfiguration().get(CFG_TIME);
77         if (cfgDTime != null && !cfgDTime.isEmpty()) {
78             TemporalAccessor temporalAccessor = timeFormatter.parse(cfgDTime);
79             dateTime.plusHours(temporalAccessor.getLong(ChronoField.HOUR_OF_DAY));
80             dateTime.plusMinutes(temporalAccessor.getLong(ChronoField.MINUTE_OF_HOUR));
81         }
82
83         this.dateTime = dateTime.atZone(ZoneId.systemDefault()).toInstant();
84     }
85
86     @Override
87     public synchronized void setCallback(ModuleHandlerCallback callback) {
88         super.setCallback(callback);
89         scheduleJob();
90     }
91
92     private void scheduleJob() {
93         schedule = scheduler.at(this, dateTime);
94         logger.debug("Scheduled absolute date/time '{}' for trigger '{}'.", dateTimeformatter.format(dateTime),
95                 module.getId());
96     }
97
98     @Override
99     public synchronized void dispose() {
100         super.dispose();
101         if (schedule != null) {
102             schedule.cancel(true);
103             logger.debug("cancelled job for trigger '{}'.", module.getId());
104         }
105     }
106
107     @Override
108     public void run() {
109         ((TriggerHandlerCallback) callback).triggered(module, null);
110         schedule = null;
111     }
112 }