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.insteon.internal.handler;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.insteon.internal.InsteonBinding;
26 import org.openhab.binding.insteon.internal.config.InsteonNetworkConfiguration;
27 import org.openhab.binding.insteon.internal.discovery.InsteonDeviceDiscoveryService;
28 import org.openhab.core.io.console.Console;
29 import org.openhab.core.io.transport.serial.SerialPortManager;
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.ThingUID;
35 import org.openhab.core.thing.binding.BaseBridgeHandler;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.State;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link InsteonNetworkHandler} is responsible for handling commands, which are
43 * sent to one of the channels.
45 * @author Rob Nielsen - Initial contribution
48 public class InsteonNetworkHandler extends BaseBridgeHandler {
49 private static final int LOG_DEVICE_STATISTICS_DELAY_IN_SECONDS = 600;
50 private static final int RETRY_DELAY_IN_SECONDS = 30;
51 private static final int SETTLE_TIME_IN_SECONDS = 5;
53 private final Logger logger = LoggerFactory.getLogger(InsteonNetworkHandler.class);
55 private @Nullable InsteonBinding insteonBinding;
56 private @Nullable InsteonDeviceDiscoveryService insteonDeviceDiscoveryService;
57 private @Nullable ScheduledFuture<?> pollingJob = null;
58 private @Nullable ScheduledFuture<?> reconnectJob = null;
59 private @Nullable ScheduledFuture<?> settleJob = null;
60 private long lastInsteonDeviceCreatedTimestamp = 0;
61 private @Nullable SerialPortManager serialPortManager;
62 private Map<String, String> deviceInfo = new ConcurrentHashMap<>();
63 private Map<String, String> channelInfo = new ConcurrentHashMap<>();
65 public static ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
67 public InsteonNetworkHandler(Bridge bridge, @Nullable SerialPortManager serialPortManager) {
69 this.serialPortManager = serialPortManager;
73 public void handleCommand(ChannelUID channelUID, Command command) {
77 public void initialize() {
78 logger.debug("Starting Insteon bridge");
79 InsteonNetworkConfiguration config = getConfigAs(InsteonNetworkConfiguration.class);
81 scheduler.execute(() -> {
82 SerialPortManager serialPortManager = this.serialPortManager;
83 if (serialPortManager == null) {
84 String msg = "Initialization failed, serial port manager is null.";
87 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
91 insteonBinding = new InsteonBinding(this, config, serialPortManager, scheduler);
92 updateStatus(ThingStatus.UNKNOWN);
94 // hold off on starting to poll until devices that already are defined as things are added.
95 // wait SETTLE_TIME_IN_SECONDS to start then check every second afterwards until it has been at
96 // least SETTLE_TIME_IN_SECONDS since last device was created.
97 settleJob = scheduler.scheduleWithFixedDelay(() -> {
98 // check to see if it has been at least SETTLE_TIME_IN_SECONDS since last device was created
99 if (System.currentTimeMillis() - lastInsteonDeviceCreatedTimestamp > SETTLE_TIME_IN_SECONDS * 1000) {
100 // settle time has expired start polling
101 InsteonBinding insteonBinding = this.insteonBinding;
102 if (insteonBinding != null && insteonBinding.startPolling()) {
103 pollingJob = scheduler.scheduleWithFixedDelay(() -> {
104 insteonBinding.logDeviceStatistics();
105 }, 0, LOG_DEVICE_STATISTICS_DELAY_IN_SECONDS, TimeUnit.SECONDS);
107 insteonBinding.setIsActive(true);
109 updateStatus(ThingStatus.ONLINE);
111 String msg = "Initialization failed, unable to start the Insteon bridge with the port '"
112 + config.getPort() + "'.";
115 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
118 ScheduledFuture<?> settleJob = this.settleJob;
119 if (settleJob != null) {
120 settleJob.cancel(false);
122 this.settleJob = null;
124 }, SETTLE_TIME_IN_SECONDS, 1, TimeUnit.SECONDS);
129 public void dispose() {
130 logger.debug("Shutting down Insteon bridge");
132 ScheduledFuture<?> pollingJob = this.pollingJob;
133 if (pollingJob != null) {
134 pollingJob.cancel(true);
135 this.pollingJob = null;
138 ScheduledFuture<?> reconnectJob = this.reconnectJob;
139 if (reconnectJob != null) {
140 reconnectJob.cancel(true);
141 this.reconnectJob = null;
144 ScheduledFuture<?> settleJob = this.settleJob;
145 if (settleJob != null) {
146 settleJob.cancel(true);
147 this.settleJob = null;
150 InsteonBinding insteonBinding = this.insteonBinding;
151 if (insteonBinding != null) {
152 insteonBinding.shutdown();
153 this.insteonBinding = null;
163 public void updateState(ChannelUID channelUID, State state) {
164 super.updateState(channelUID, state);
167 public void bindingDisconnected() {
168 reconnectJob = scheduler.scheduleWithFixedDelay(() -> {
169 InsteonBinding insteonBinding = this.insteonBinding;
170 if (insteonBinding != null && insteonBinding.reconnect()) {
171 updateStatus(ThingStatus.ONLINE);
172 ScheduledFuture<?> reconnectJob = this.reconnectJob;
173 if (reconnectJob != null) {
174 reconnectJob.cancel(false);
176 this.reconnectJob = null;
178 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Port disconnected.");
180 }, 0, RETRY_DELAY_IN_SECONDS, TimeUnit.SECONDS);
183 public void insteonDeviceWasCreated() {
184 lastInsteonDeviceCreatedTimestamp = System.currentTimeMillis();
187 public InsteonBinding getInsteonBinding() {
188 InsteonBinding insteonBinding = this.insteonBinding;
189 if (insteonBinding != null) {
190 return insteonBinding;
192 throw new IllegalArgumentException("insteon binding is null");
196 public void setInsteonDeviceDiscoveryService(InsteonDeviceDiscoveryService insteonDeviceDiscoveryService) {
197 this.insteonDeviceDiscoveryService = insteonDeviceDiscoveryService;
200 public void addMissingDevices(List<String> missing) {
201 scheduler.execute(() -> {
202 InsteonDeviceDiscoveryService insteonDeviceDiscoveryService = this.insteonDeviceDiscoveryService;
203 if (insteonDeviceDiscoveryService != null) {
204 insteonDeviceDiscoveryService.addInsteonDevices(missing, getThing().getUID());
209 public void displayDevices(Console console) {
210 display(console, deviceInfo);
213 public void displayChannels(Console console) {
214 display(console, channelInfo);
217 public void displayLocalDatabase(Console console) {
218 InsteonBinding insteonBinding = this.insteonBinding;
219 if (insteonBinding != null) {
220 Map<String, String> databaseInfo = insteonBinding.getDatabaseInfo();
221 console.println("local database contains " + databaseInfo.size() + " entries");
222 display(console, databaseInfo);
226 public void initialized(ThingUID uid, String msg) {
227 deviceInfo.put(uid.getAsString(), msg);
230 public void disposed(ThingUID uid) {
231 deviceInfo.remove(uid.getAsString());
234 public boolean isChannelLinked(ChannelUID uid) {
235 return channelInfo.containsKey(uid.getAsString());
238 public void linked(ChannelUID uid, String msg) {
239 channelInfo.put(uid.getAsString(), msg);
242 public void unlinked(ChannelUID uid) {
243 channelInfo.remove(uid.getAsString());
246 private void display(Console console, Map<String, String> info) {
247 ArrayList<String> ids = new ArrayList<>(info.keySet());
248 Collections.sort(ids);
249 for (String id : ids) {
250 console.println(info.get(id));