]> git.basschouten.com Git - openhab-addons.git/blob
b84b3c12db206f68e269691401784bdc1ecf64d1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.touchwand.internal;
14
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.THING_TYPE_BRIDGE;
16
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.concurrent.ConcurrentHashMap;
22
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;
39
40 /**
41  * The {@link TouchWandBridgeHandler} is responsible for handling commands, which are
42  * sent to one of the channels TouchWand Wanderfullâ„¢ Hub channels .
43  *
44  * @author Roie Geron - Initial contribution
45  */
46 @NonNullByDefault
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;
54
55     public TouchWandRestClient touchWandClient;
56
57     public TouchWandBridgeHandler(Bridge bridge, HttpClient httpClient, BundleContext bundleContext) {
58         super(bridge);
59         touchWandClient = new TouchWandRestClient(httpClient);
60         touchWandWebSockets = null;
61     }
62
63     @Override
64     public synchronized void initialize() {
65         String host;
66         Integer port;
67         TouchwandBridgeConfiguration config;
68
69         updateStatus(ThingStatus.UNKNOWN);
70
71         config = getConfigAs(TouchwandBridgeConfiguration.class);
72
73         host = config.ipAddress;
74         port = config.port;
75         addSecondaryUnits = config.addSecondaryUnits;
76
77         isRunning = true;
78
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());
84             if (thingReachable) {
85                 updateStatus(ThingStatus.ONLINE);
86                 synchronized (this) {
87                     if (isRunning) {
88                         TouchWandWebSockets localSockets = touchWandWebSockets = new TouchWandWebSockets(host, port,
89                                 scheduler);
90                         localSockets.registerListener(this);
91                         localSockets.connect();
92                     }
93                 }
94
95             } else {
96                 updateStatus(ThingStatus.OFFLINE);
97             }
98         });
99     }
100
101     @Override
102     public void handleCommand(ChannelUID channelUID, Command command) {
103     }
104
105     public boolean isAddSecondaryControllerUnits() {
106         return addSecondaryUnits;
107     }
108
109     @Override
110     public synchronized void dispose() {
111         isRunning = false;
112         TouchWandWebSockets myTouchWandWebSockets = touchWandWebSockets;
113         if (myTouchWandWebSockets != null) {
114             myTouchWandWebSockets.unregisterListener(this);
115             myTouchWandWebSockets.dispose();
116         }
117     }
118
119     public synchronized boolean registerUpdateListener(TouchWandUnitUpdateListener listener) {
120         logger.debug("Adding Status update listener for device {}", listener.getId());
121         unitUpdateListeners.put(listener.getId(), listener);
122         return true;
123     }
124
125     public synchronized boolean unregisterUpdateListener(TouchWandUnitUpdateListener listener) {
126         logger.debug("Remove Status update listener for device {}", listener.getId());
127         unitUpdateListeners.remove(listener.getId());
128         return true;
129     }
130
131     @Override
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);
136         }
137     }
138
139     @Override
140     public Collection<Class<? extends ThingHandlerService>> getServices() {
141         return Collections.singleton(TouchWandUnitDiscoveryService.class);
142     }
143 }