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.handler;
15 import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.*;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.Collection;
21 import java.util.Map.Entry;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.nikohomecontrol.internal.discovery.NikoHomeControlDiscoveryService;
29 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcControllerEvent;
30 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlCommunication;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.binding.BaseBridgeHandler;
37 import org.openhab.core.thing.binding.ThingHandlerService;
38 import org.openhab.core.types.Command;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * {@link NikoHomeControlBridgeHandler} is an abstract class representing a handler to all different interfaces to the
44 * Niko Home Control System. {@link NikoHomeControlBridgeHandler1} or {@link NikoHomeControlBridgeHandler2} should be
45 * used for the respective version of Niko Home Control.
47 * @author Mark Herwege - Initial Contribution
50 public abstract class NikoHomeControlBridgeHandler extends BaseBridgeHandler implements NhcControllerEvent {
52 private final Logger logger = LoggerFactory.getLogger(NikoHomeControlBridgeHandler.class);
54 protected @Nullable NikoHomeControlCommunication nhcComm;
56 private volatile @Nullable ScheduledFuture<?> refreshTimer;
58 public NikoHomeControlBridgeHandler(Bridge nikoHomeControlBridge) {
59 super(nikoHomeControlBridge);
63 public void handleCommand(ChannelUID channelUID, Command command) {
64 // There is nothing to handle in the bridge handler
68 * Create communication object to Niko Home Control IP-interface and start communication.
69 * Trigger discovery when communication setup is successful.
71 protected void startCommunication() {
72 NikoHomeControlCommunication comm = nhcComm;
78 updateStatus(ThingStatus.UNKNOWN);
80 scheduler.submit(() -> {
81 comm.startCommunication();
83 int refreshInterval = getConfig().as(NikoHomeControlBridgeConfig.class).refresh;
84 setupRefreshTimer(refreshInterval);
86 if (!comm.communicationActive()) {
93 updateStatus(ThingStatus.ONLINE);
98 * Schedule future communication refresh.
100 * @param refreshInterval Time before refresh in minutes.
102 private void setupRefreshTimer(int refreshInterval) {
103 ScheduledFuture<?> timer = refreshTimer;
109 if (refreshInterval == 0) {
113 // This timer will restart the bridge connection periodically
114 logger.debug("restart bridge connection every {} min", refreshInterval);
115 refreshTimer = scheduler.scheduleWithFixedDelay(() -> {
116 logger.debug("restart communication at scheduled time");
118 NikoHomeControlCommunication comm = nhcComm;
120 comm.restartCommunication();
121 if (!comm.communicationActive()) {
128 updateStatus(ThingStatus.ONLINE);
130 }, refreshInterval, refreshInterval, TimeUnit.MINUTES);
134 * Take bridge offline when error in communication with Niko Home Control IP-interface.
136 protected void bridgeOffline() {
137 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
138 "@text/offline.communication-error");
142 * Put bridge online when error in communication resolved.
144 public void bridgeOnline() {
146 updateStatus(ThingStatus.ONLINE);
150 public void controllerOffline(String message) {
151 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, message);
155 public void controllerOnline() {
160 * Update bridge properties with properties returned from Niko Home Control Controller.
162 protected abstract void updateProperties();
165 public void dispose() {
166 ScheduledFuture<?> timer = refreshTimer;
172 NikoHomeControlCommunication comm = nhcComm;
174 comm.stopCommunication();
181 public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
182 NikoHomeControlCommunication comm = nhcComm;
183 // if the communication had not been started yet, just dispose and initialize again
185 super.handleConfigurationUpdate(configurationParameters);
189 Configuration configuration = editConfiguration();
190 for (Entry<String, Object> configurationParameter : configurationParameters.entrySet()) {
191 configuration.put(configurationParameter.getKey(), configurationParameter.getValue());
193 updateConfiguration(configuration);
195 scheduler.submit(() -> {
196 comm.restartCommunication();
197 if (!comm.communicationActive()) {
204 updateStatus(ThingStatus.ONLINE);
206 int refreshInterval = getConfig().as(NikoHomeControlBridgeConfig.class).refresh;
207 setupRefreshTimer(refreshInterval);
212 public void alarmEvent(String alarmText) {
213 logger.debug("triggering alarm channel with {}", alarmText);
214 triggerChannel(CHANNEL_ALARM, alarmText);
215 updateStatus(ThingStatus.ONLINE);
219 public void noticeEvent(String alarmText) {
220 logger.debug("triggering notice channel with {}", alarmText);
221 triggerChannel(CHANNEL_NOTICE, alarmText);
222 updateStatus(ThingStatus.ONLINE);
226 public void updatePropertiesEvent() {
231 * Get the Niko Home Control communication object.
233 * @return Niko Home Control communication object
235 public @Nullable NikoHomeControlCommunication getCommunication() {
240 public @Nullable InetAddress getAddr() {
241 InetAddress addr = null;
242 NikoHomeControlBridgeConfig config = getConfig().as(NikoHomeControlBridgeConfig.class);
244 addr = InetAddress.getByName(config.addr);
245 } catch (UnknownHostException e) {
246 logger.debug("Cannot resolve hostname {} to IP adress", config.addr);
252 public int getPort() {
253 return getConfig().as(NikoHomeControlBridgeConfig.class).port;
257 public Collection<Class<? extends ThingHandlerService>> getServices() {
258 return Set.of(NikoHomeControlDiscoveryService.class);