2 * Copyright (c) 2010-2020 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;
20 import java.util.Map.Entry;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.nikohomecontrol.internal.discovery.NikoHomeControlDiscoveryService;
27 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcControllerEvent;
28 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlCommunication;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.thing.binding.BaseBridgeHandler;
35 import org.openhab.core.types.Command;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * {@link NikoHomeControlBridgeHandler} is an abstract class representing a handler to all different interfaces to the
41 * Niko Home Control System. {@link NikoHomeControlBridgeHandler1} or {@link NikoHomeControlBridgeHandler2} should be
42 * used for the respective
43 * version of Niko Home Control.
45 * @author Mark Herwege - Initial Contribution
48 public abstract class NikoHomeControlBridgeHandler extends BaseBridgeHandler implements NhcControllerEvent {
50 private final Logger logger = LoggerFactory.getLogger(NikoHomeControlBridgeHandler.class);
52 protected @NonNullByDefault({}) NikoHomeControlBridgeConfig config;
54 protected @Nullable NikoHomeControlCommunication nhcComm;
56 private volatile @Nullable ScheduledFuture<?> refreshTimer;
58 protected volatile @Nullable NikoHomeControlDiscoveryService nhcDiscovery;
60 public NikoHomeControlBridgeHandler(Bridge nikoHomeControlBridge) {
61 super(nikoHomeControlBridge);
65 public void handleCommand(ChannelUID channelUID, Command command) {
66 // There is nothing to handle in the bridge handler
70 * Create communication object to Niko Home Control IP-interface and start communication.
71 * Trigger discovery when communication setup is successful.
73 protected void startCommunication() {
74 NikoHomeControlCommunication comm = nhcComm;
80 scheduler.submit(() -> {
81 comm.startCommunication();
82 if (!comm.communicationActive()) {
89 updateStatus(ThingStatus.ONLINE);
91 int refreshInterval = config.refresh;
92 setupRefreshTimer(refreshInterval);
94 NikoHomeControlDiscoveryService discovery = nhcDiscovery;
95 if (discovery != null) {
96 discovery.discoverDevices();
98 logger.debug("Niko Home Control: cannot discover devices, discovery service not started");
104 * Schedule future communication refresh.
106 * @param interval_config Time before refresh in minutes.
108 private void setupRefreshTimer(int refreshInterval) {
109 ScheduledFuture<?> timer = refreshTimer;
115 if (refreshInterval == 0) {
119 // This timer will restart the bridge connection periodically
120 logger.debug("Niko Home Control: restart bridge connection every {} min", refreshInterval);
121 refreshTimer = scheduler.scheduleWithFixedDelay(() -> {
122 logger.debug("Niko Home Control: restart communication at scheduled time");
124 NikoHomeControlCommunication comm = nhcComm;
126 comm.restartCommunication();
127 if (!comm.communicationActive()) {
134 updateStatus(ThingStatus.ONLINE);
136 }, refreshInterval, refreshInterval, TimeUnit.MINUTES);
140 * Take bridge offline when error in communication with Niko Home Control IP-interface.
142 protected void bridgeOffline() {
143 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
144 "Niko Home Control: error starting bridge connection");
148 * Put bridge online when error in communication resolved.
150 public void bridgeOnline() {
152 updateStatus(ThingStatus.ONLINE);
156 public void controllerOffline() {
161 public void controllerOnline() {
164 int refreshInterval = config.refresh;
165 if (refreshTimer == null) {
166 setupRefreshTimer(refreshInterval);
171 * Update bridge properties with properties returned from Niko Home Control Controller, so they can be made visible
174 protected abstract void updateProperties();
177 public void dispose() {
178 ScheduledFuture<?> timer = refreshTimer;
184 NikoHomeControlCommunication comm = nhcComm;
186 comm.stopCommunication();
192 public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
193 NikoHomeControlCommunication comm = nhcComm;
194 // if the communication had not been started yet, just dispose and initialize again
196 super.handleConfigurationUpdate(configurationParameters);
200 Configuration configuration = editConfiguration();
201 for (Entry<String, Object> configurationParmeter : configurationParameters.entrySet()) {
202 configuration.put(configurationParmeter.getKey(), configurationParmeter.getValue());
204 updateConfiguration(configuration);
208 scheduler.submit(() -> {
209 comm.restartCommunication();
210 if (!comm.communicationActive()) {
217 updateStatus(ThingStatus.ONLINE);
219 int refreshInterval = config.refresh;
220 setupRefreshTimer(refreshInterval);
225 * Set discovery service handler to be able to start discovery after bridge initialization.
227 * @param nhcDiscovery
229 public void setNhcDiscovery(@Nullable NikoHomeControlDiscoveryService nhcDiscovery) {
230 this.nhcDiscovery = nhcDiscovery;
234 public void alarmEvent(String alarmText) {
235 logger.debug("Niko Home Control: triggering alarm channel with {}", alarmText);
236 triggerChannel(CHANNEL_ALARM, alarmText);
237 updateStatus(ThingStatus.ONLINE);
241 public void noticeEvent(String alarmText) {
242 logger.debug("Niko Home Control: triggering notice channel with {}", alarmText);
243 triggerChannel(CHANNEL_NOTICE, alarmText);
244 updateStatus(ThingStatus.ONLINE);
248 public void updatePropertiesEvent() {
253 * Get the Niko Home Control communication object.
255 * @return Niko Home Control communication object
257 public @Nullable NikoHomeControlCommunication getCommunication() {
262 public @Nullable InetAddress getAddr() {
263 InetAddress addr = null;
265 addr = InetAddress.getByName(config.addr);
266 } catch (UnknownHostException e) {
267 logger.debug("Niko Home Control: Cannot resolve hostname {} to IP adress", config.addr);
273 public int getPort() {
277 protected synchronized void setConfig() {
278 config = getConfig().as(NikoHomeControlBridgeConfig.class);