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