2 * Copyright (c) 2010-2022 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.binding.nikohomecontrol.internal.protocol;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
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;
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.
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.
39 * @author Mark Herwege - Initial Contribution
42 public abstract class NikoHomeControlCommunication {
44 private final Logger logger = LoggerFactory.getLogger(NikoHomeControlCommunication.class);
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<>();
50 protected final NhcControllerEvent handler;
52 protected final ScheduledExecutorService scheduler;
55 private volatile int delay = 0;
56 private volatile int attempt = 0;
57 protected @Nullable ScheduledFuture<?> scheduledRestart = null;
59 protected NikoHomeControlCommunication(NhcControllerEvent handler, ScheduledExecutorService scheduler) {
60 this.handler = handler;
61 this.scheduler = scheduler;
65 * Start Communication with Niko Home Control system.
67 public abstract void startCommunication();
70 * Stop Communication with Niko Home Control system.
72 public void stopCommunication() {
73 stopScheduledRestart();
79 * Stop Communication with Niko Home Control system, but keep reconnection attempts going.
81 public abstract void resetCommunication();
83 protected synchronized void stopScheduledRestart() {
84 ScheduledFuture<?> future = scheduledRestart;
88 scheduledRestart = null;
94 * Close and restart communication with Niko Home Control system.
96 public synchronized void restartCommunication() {
99 logger.debug("restart communication from thread {}", Thread.currentThread().getId());
101 startCommunication();
104 private synchronized void checkAndRestartCommunication() {
105 restartCommunication();
107 // Try again if it didn't succeed
108 if (!communicationActive()) {
110 delay = ((attempt <= 5) ? 30 : 60);
111 logger.debug("schedule communication restart in {} seconds", delay);
112 scheduledRestart = scheduler.schedule(this::checkAndRestartCommunication, delay, TimeUnit.SECONDS);
114 stopScheduledRestart();
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.
123 public synchronized void scheduleRestartCommunication() {
124 // Don't do this if we already scheduled to restart
125 if (scheduledRestart == null) {
128 scheduledRestart = scheduler.schedule(this::checkAndRestartCommunication, 0, TimeUnit.SECONDS);
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.
136 * @return True if active
138 public abstract boolean communicationActive();
141 * Return all actions in the Niko Home Control Controller.
143 * @return <code>Map<String, {@link NhcAction}></code>
145 public Map<String, NhcAction> getActions() {
150 * Return all thermostats in the Niko Home Control Controller.
152 * @return <code>Map<String, {@link NhcThermostat}></code>
154 public Map<String, NhcThermostat> getThermostats() {
159 * Return all energyMeters meters in the Niko Home Control Controller.
161 * @return <code>Map<String, {@link NhcEnergyMeter}></code>
163 public Map<String, NhcEnergyMeter> getEnergyMeters() {
168 * Execute an action command by sending it to Niko Home Control.
173 public abstract void executeAction(String actionId, String value);
176 * Execute a thermostat command by sending it to Niko Home Control.
178 * @param thermostatId
181 public abstract void executeThermostat(String thermostatId, String mode);
184 * Execute a thermostat command by sending it to Niko Home Control.
186 * @param thermostatId
187 * @param overruleTemp
188 * @param overruleTime
190 public abstract void executeThermostat(String thermostatId, int overruleTemp, int overruleTime);
193 * Start retrieving energy meter data from Niko Home Control.
196 public void startEnergyMeter(String energyMeterId) {
200 * Stop retrieving energy meter data from Niko Home Control.
203 public void stopEnergyMeter(String energyMeterId) {