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.touchwand.internal;
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.THING_TYPE_BRIDGE;
17 import java.util.Collection;
18 import java.util.Collections;
21 import java.util.concurrent.ConcurrentHashMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.openhab.binding.touchwand.internal.config.TouchwandBridgeConfiguration;
27 import org.openhab.binding.touchwand.internal.discovery.TouchWandUnitDiscoveryService;
28 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.binding.BaseBridgeHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.openhab.core.types.Command;
36 import org.osgi.framework.BundleContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link TouchWandBridgeHandler} is responsible for handling commands, which are
42 * sent to one of the channels TouchWand Wanderfullâ„¢ Hub channels .
44 * @author Roie Geron - Initial contribution
47 public class TouchWandBridgeHandler extends BaseBridgeHandler implements TouchWandUnitStatusUpdateListener {
48 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE);
49 private final Logger logger = LoggerFactory.getLogger(TouchWandBridgeHandler.class);
50 private int statusRefreshRateSec;
51 private boolean addSecondaryUnits;
52 private @Nullable TouchWandWebSockets touchWandWebSockets;
53 private Map<String, TouchWandUnitUpdateListener> unitUpdateListeners = new ConcurrentHashMap<>();
54 private volatile boolean isRunning = false;
56 public TouchWandRestClient touchWandClient;
58 public TouchWandBridgeHandler(Bridge bridge, HttpClient httpClient, BundleContext bundleContext) {
60 touchWandClient = new TouchWandRestClient(httpClient);
61 touchWandWebSockets = null;
65 public synchronized void initialize() {
68 TouchwandBridgeConfiguration config;
70 updateStatus(ThingStatus.UNKNOWN);
72 config = getConfigAs(TouchwandBridgeConfiguration.class);
74 host = config.ipAddress;
76 statusRefreshRateSec = config.statusrefresh;
77 addSecondaryUnits = config.addSecondaryUnits;
81 scheduler.execute(() -> {
82 boolean thingReachable = false;
83 String password = config.password;
84 String username = config.username;
85 thingReachable = touchWandClient.connect(username, password, host, port.toString());
87 updateStatus(ThingStatus.ONLINE);
90 TouchWandWebSockets localSockets = touchWandWebSockets = new TouchWandWebSockets(host,
92 localSockets.registerListener(this);
93 localSockets.connect();
98 updateStatus(ThingStatus.OFFLINE);
104 public void handleCommand(ChannelUID channelUID, Command command) {
107 public boolean isAddSecondaryControllerUnits() {
108 return addSecondaryUnits;
111 public int getStatusRefreshTime() {
112 return statusRefreshRateSec;
116 public synchronized void dispose() {
118 TouchWandWebSockets myTouchWandWebSockets = touchWandWebSockets;
119 if (myTouchWandWebSockets != null) {
120 myTouchWandWebSockets.unregisterListener(this);
121 myTouchWandWebSockets.dispose();
125 public synchronized boolean registerUpdateListener(TouchWandUnitUpdateListener listener) {
126 logger.debug("Adding Status update listener for device {}", listener.getId());
127 unitUpdateListeners.put(listener.getId(), listener);
131 public synchronized boolean unregisterUpdateListener(TouchWandUnitUpdateListener listener) {
132 logger.debug("Remove Status update listener for device {}", listener.getId());
133 unitUpdateListeners.remove(listener.getId());
138 public void onDataReceived(TouchWandUnitData unitData) {
139 if (unitUpdateListeners.containsKey(unitData.getId().toString())) {
140 TouchWandUnitUpdateListener updateListener = unitUpdateListeners.get(unitData.getId().toString());
141 updateListener.onItemStatusUpdate(unitData);
146 public Collection<Class<? extends ThingHandlerService>> getServices() {
147 return Collections.singleton(TouchWandUnitDiscoveryService.class);