]> git.basschouten.com Git - openhab-addons.git/blob
22793b69db4c66c2e0aa59fdc392a254d6eac41f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.binding.nikohomecontrol.internal.protocol;
14
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc1.NikoHomeControlCommunication1;
24 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc2.NikoHomeControlCommunication2;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link NikoHomeControlCommunication} class is an abstract class representing the communication objects with the
30  * Niko Home Control System. {@link NikoHomeControlCommunication1} or {@link NikoHomeControlCommunication2} should be
31  * used for the respective version of Niko Home Control.
32  * <ul>
33  * <li>Start and stop communication with the Niko Home Control System.
34  * <li>Read all setup and status information from the Niko Home Control Controller.
35  * <li>Execute Niko Home Control commands.
36  * <li>Listen to events from Niko Home Control.
37  * </ul>
38  *
39  * @author Mark Herwege - Initial Contribution
40  */
41 @NonNullByDefault
42 public abstract class NikoHomeControlCommunication {
43
44     private final Logger logger = LoggerFactory.getLogger(NikoHomeControlCommunication.class);
45
46     protected final Map<String, NhcAction> actions = new ConcurrentHashMap<>();
47     protected final Map<String, NhcThermostat> thermostats = new ConcurrentHashMap<>();
48     protected final Map<String, NhcEnergyMeter> energyMeters = new ConcurrentHashMap<>();
49
50     protected final NhcControllerEvent handler;
51
52     protected final ScheduledExecutorService scheduler;
53
54     // restart attempts
55     private volatile int delay = 0;
56     private volatile int attempt = 0;
57     protected @Nullable ScheduledFuture<?> scheduledRestart = null;
58
59     protected NikoHomeControlCommunication(NhcControllerEvent handler, ScheduledExecutorService scheduler) {
60         this.handler = handler;
61         this.scheduler = scheduler;
62     }
63
64     /**
65      * Start Communication with Niko Home Control system.
66      */
67     public abstract void startCommunication();
68
69     /**
70      * Stop Communication with Niko Home Control system.
71      */
72     public void stopCommunication() {
73         stopScheduledRestart();
74
75         resetCommunication();
76     }
77
78     /**
79      * Stop Communication with Niko Home Control system, but keep reconnection attempts going.
80      */
81     public abstract void resetCommunication();
82
83     protected synchronized void stopScheduledRestart() {
84         ScheduledFuture<?> future = scheduledRestart;
85         if (future != null) {
86             future.cancel(true);
87         }
88         scheduledRestart = null;
89         delay = 0;
90         attempt = 0;
91     }
92
93     /**
94      * Close and restart communication with Niko Home Control system.
95      */
96     public synchronized void restartCommunication() {
97         resetCommunication();
98
99         logger.debug("restart communication from thread {}", Thread.currentThread().getId());
100
101         startCommunication();
102     }
103
104     private synchronized void checkAndRestartCommunication() {
105         restartCommunication();
106
107         // Try again if it didn't succeed
108         if (!communicationActive()) {
109             attempt++;
110             delay = ((attempt <= 5) ? 30 : 60);
111             logger.debug("schedule communication restart in {} seconds", delay);
112             scheduledRestart = scheduler.schedule(this::checkAndRestartCommunication, delay, TimeUnit.SECONDS);
113         } else {
114             stopScheduledRestart();
115         }
116     }
117
118     /**
119      * Close and restart communication with Niko Home Control system. This method will keep doing multiple reconnection
120      * attempts, starting immediately, then 5 times with 30 second intervals and every minute thereafter until the
121      * connection is re-established.
122      */
123     public synchronized void scheduleRestartCommunication() {
124         // Don't do this if we already scheduled to restart
125         if (scheduledRestart == null) {
126             delay = 0;
127             attempt = 0;
128             scheduledRestart = scheduler.schedule(this::checkAndRestartCommunication, 0, TimeUnit.SECONDS);
129         }
130     }
131
132     /**
133      * Method to check if communication with Niko Home Control is active. This method can be blocking for max 5s to wait
134      * for completion of startup.
135      *
136      * @return True if active
137      */
138     public abstract boolean communicationActive();
139
140     /**
141      * Return all actions in the Niko Home Control Controller.
142      *
143      * @return <code>Map&ltString, {@link NhcAction}></code>
144      */
145     public Map<String, NhcAction> getActions() {
146         return actions;
147     }
148
149     /**
150      * Return all thermostats in the Niko Home Control Controller.
151      *
152      * @return <code>Map&ltString, {@link NhcThermostat}></code>
153      */
154     public Map<String, NhcThermostat> getThermostats() {
155         return thermostats;
156     }
157
158     /**
159      * Return all energyMeters meters in the Niko Home Control Controller.
160      *
161      * @return <code>Map&ltString, {@link NhcEnergyMeter}></code>
162      */
163     public Map<String, NhcEnergyMeter> getEnergyMeters() {
164         return energyMeters;
165     }
166
167     /**
168      * Execute an action command by sending it to Niko Home Control.
169      *
170      * @param actionId
171      * @param value
172      */
173     public abstract void executeAction(String actionId, String value);
174
175     /**
176      * Execute a thermostat command by sending it to Niko Home Control.
177      *
178      * @param thermostatId
179      * @param mode
180      */
181     public abstract void executeThermostat(String thermostatId, String mode);
182
183     /**
184      * Execute a thermostat command by sending it to Niko Home Control.
185      *
186      * @param thermostatId
187      * @param overruleTemp
188      * @param overruleTime
189      */
190     public abstract void executeThermostat(String thermostatId, int overruleTemp, int overruleTime);
191
192     /**
193      * Start retrieving energy meter data from Niko Home Control.
194      *
195      */
196     public void startEnergyMeter(String energyMeterId) {
197     };
198
199     /**
200      * Stop retrieving energy meter data from Niko Home Control.
201      *
202      */
203     public void stopEnergyMeter(String energyMeterId) {
204     };
205 }