2 * Copyright (c) 2010-2023 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;
20 import java.util.concurrent.ConcurrentHashMap;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.touchwand.internal.config.TouchwandBridgeConfiguration;
26 import org.openhab.binding.touchwand.internal.discovery.TouchWandUnitDiscoveryService;
27 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseBridgeHandler;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.openhab.core.types.Command;
35 import org.osgi.framework.BundleContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link TouchWandBridgeHandler} is responsible for handling commands, which are
41 * sent to one of the channels TouchWand Wanderfullâ„¢ Hub channels .
43 * @author Roie Geron - Initial contribution
46 public class TouchWandBridgeHandler extends BaseBridgeHandler implements TouchWandUnitStatusUpdateListener {
47 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_BRIDGE);
48 private final Logger logger = LoggerFactory.getLogger(TouchWandBridgeHandler.class);
49 private boolean addSecondaryUnits;
50 private @Nullable TouchWandWebSockets touchWandWebSockets;
51 private Map<String, TouchWandUnitUpdateListener> unitUpdateListeners = new ConcurrentHashMap<>();
52 private volatile boolean isRunning = false;
54 public TouchWandRestClient touchWandClient;
56 public TouchWandBridgeHandler(Bridge bridge, HttpClient httpClient, BundleContext bundleContext) {
58 touchWandClient = new TouchWandRestClient(httpClient);
59 touchWandWebSockets = null;
63 public synchronized void initialize() {
66 TouchwandBridgeConfiguration config;
68 updateStatus(ThingStatus.UNKNOWN);
70 config = getConfigAs(TouchwandBridgeConfiguration.class);
72 host = config.ipAddress;
74 addSecondaryUnits = config.addSecondaryUnits;
78 scheduler.execute(() -> {
79 boolean thingReachable = false;
80 String password = config.password;
81 String username = config.username;
82 thingReachable = touchWandClient.connect(username, password, host, port.toString());
84 updateStatus(ThingStatus.ONLINE);
87 TouchWandWebSockets localSockets = touchWandWebSockets = new TouchWandWebSockets(host, port,
89 localSockets.registerListener(this);
90 localSockets.connect();
95 updateStatus(ThingStatus.OFFLINE);
101 public void handleCommand(ChannelUID channelUID, Command command) {
104 public boolean isAddSecondaryControllerUnits() {
105 return addSecondaryUnits;
109 public synchronized void dispose() {
111 TouchWandWebSockets myTouchWandWebSockets = touchWandWebSockets;
112 if (myTouchWandWebSockets != null) {
113 myTouchWandWebSockets.unregisterListener(this);
114 myTouchWandWebSockets.dispose();
118 public synchronized boolean registerUpdateListener(TouchWandUnitUpdateListener listener) {
119 logger.debug("Adding Status update listener for device {}", listener.getId());
120 unitUpdateListeners.put(listener.getId(), listener);
124 public synchronized boolean unregisterUpdateListener(TouchWandUnitUpdateListener listener) {
125 logger.debug("Remove Status update listener for device {}", listener.getId());
126 unitUpdateListeners.remove(listener.getId());
131 public void onDataReceived(TouchWandUnitData unitData) {
132 if (unitUpdateListeners.containsKey(unitData.getId().toString())) {
133 TouchWandUnitUpdateListener updateListener = unitUpdateListeners.get(unitData.getId().toString());
134 updateListener.onItemStatusUpdate(unitData);
139 public Collection<Class<? extends ThingHandlerService>> getServices() {
140 return Set.of(TouchWandUnitDiscoveryService.class);