]> git.basschouten.com Git - openhab-addons.git/blob
9cd41581d43f4b33e9e008f3fe8e9a2e0db47298
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 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;
55
56     public TouchWandRestClient touchWandClient;
57
58     public TouchWandBridgeHandler(Bridge bridge, HttpClient httpClient, BundleContext bundleContext) {
59         super(bridge);
60         touchWandClient = new TouchWandRestClient(httpClient);
61         touchWandWebSockets = null;
62     }
63
64     @Override
65     public synchronized void initialize() {
66         String host;
67         Integer port;
68         TouchwandBridgeConfiguration config;
69
70         updateStatus(ThingStatus.UNKNOWN);
71
72         config = getConfigAs(TouchwandBridgeConfiguration.class);
73
74         host = config.ipAddress;
75         port = config.port;
76         statusRefreshRateSec = config.statusrefresh;
77         addSecondaryUnits = config.addSecondaryUnits;
78
79         isRunning = true;
80
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());
86             if (thingReachable) {
87                 updateStatus(ThingStatus.ONLINE);
88                 synchronized (this) {
89                     if (isRunning) {
90                         TouchWandWebSockets localSockets = touchWandWebSockets = new TouchWandWebSockets(host,
91                                 scheduler);
92                         localSockets.registerListener(this);
93                         localSockets.connect();
94                     }
95                 }
96
97             } else {
98                 updateStatus(ThingStatus.OFFLINE);
99             }
100         });
101     }
102
103     @Override
104     public void handleCommand(ChannelUID channelUID, Command command) {
105     }
106
107     public boolean isAddSecondaryControllerUnits() {
108         return addSecondaryUnits;
109     }
110
111     public int getStatusRefreshTime() {
112         return statusRefreshRateSec;
113     }
114
115     @Override
116     public synchronized void dispose() {
117         isRunning = false;
118         TouchWandWebSockets myTouchWandWebSockets = touchWandWebSockets;
119         if (myTouchWandWebSockets != null) {
120             myTouchWandWebSockets.unregisterListener(this);
121             myTouchWandWebSockets.dispose();
122         }
123     }
124
125     public synchronized boolean registerUpdateListener(TouchWandUnitUpdateListener listener) {
126         logger.debug("Adding Status update listener for device {}", listener.getId());
127         unitUpdateListeners.put(listener.getId(), listener);
128         return true;
129     }
130
131     public synchronized boolean unregisterUpdateListener(TouchWandUnitUpdateListener listener) {
132         logger.debug("Remove Status update listener for device {}", listener.getId());
133         unitUpdateListeners.remove(listener.getId());
134         return true;
135     }
136
137     @Override
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);
142         }
143     }
144
145     @Override
146     public Collection<Class<? extends ThingHandlerService>> getServices() {
147         return Collections.singleton(TouchWandUnitDiscoveryService.class);
148     }
149 }