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;
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 boolean addSecondaryUnits;
51 private @Nullable TouchWandWebSockets touchWandWebSockets;
52 private Map<String, TouchWandUnitUpdateListener> unitUpdateListeners = new ConcurrentHashMap<>();
53 private volatile boolean isRunning = false;
55 public TouchWandRestClient touchWandClient;
57 public TouchWandBridgeHandler(Bridge bridge, HttpClient httpClient, BundleContext bundleContext) {
59 touchWandClient = new TouchWandRestClient(httpClient);
60 touchWandWebSockets = null;
64 public synchronized void initialize() {
67 TouchwandBridgeConfiguration config;
69 updateStatus(ThingStatus.UNKNOWN);
71 config = getConfigAs(TouchwandBridgeConfiguration.class);
73 host = config.ipAddress;
75 addSecondaryUnits = config.addSecondaryUnits;
79 scheduler.execute(() -> {
80 boolean thingReachable = false;
81 String password = config.password;
82 String username = config.username;
83 thingReachable = touchWandClient.connect(username, password, host, port.toString());
85 updateStatus(ThingStatus.ONLINE);
88 TouchWandWebSockets localSockets = touchWandWebSockets = new TouchWandWebSockets(host, port,
90 localSockets.registerListener(this);
91 localSockets.connect();
96 updateStatus(ThingStatus.OFFLINE);
102 public void handleCommand(ChannelUID channelUID, Command command) {
105 public boolean isAddSecondaryControllerUnits() {
106 return addSecondaryUnits;
110 public synchronized void dispose() {
112 TouchWandWebSockets myTouchWandWebSockets = touchWandWebSockets;
113 if (myTouchWandWebSockets != null) {
114 myTouchWandWebSockets.unregisterListener(this);
115 myTouchWandWebSockets.dispose();
119 public synchronized boolean registerUpdateListener(TouchWandUnitUpdateListener listener) {
120 logger.debug("Adding Status update listener for device {}", listener.getId());
121 unitUpdateListeners.put(listener.getId(), listener);
125 public synchronized boolean unregisterUpdateListener(TouchWandUnitUpdateListener listener) {
126 logger.debug("Remove Status update listener for device {}", listener.getId());
127 unitUpdateListeners.remove(listener.getId());
132 public void onDataReceived(TouchWandUnitData unitData) {
133 if (unitUpdateListeners.containsKey(unitData.getId().toString())) {
134 TouchWandUnitUpdateListener updateListener = unitUpdateListeners.get(unitData.getId().toString());
135 updateListener.onItemStatusUpdate(unitData);
140 public Collection<Class<? extends ThingHandlerService>> getServices() {
141 return Collections.singleton(TouchWandUnitDiscoveryService.class);