2 * Copyright (c) 2010-2021 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.hdpowerview.internal.handler;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Map.Entry;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import javax.ws.rs.ProcessingException;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
29 import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
30 import org.openhab.binding.hdpowerview.internal.HubMaintenanceException;
31 import org.openhab.binding.hdpowerview.internal.HubProcessingException;
32 import org.openhab.binding.hdpowerview.internal.api.responses.Scenes;
33 import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene;
34 import org.openhab.binding.hdpowerview.internal.api.responses.Shades;
35 import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
36 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration;
37 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewShadeConfiguration;
38 import org.openhab.core.library.types.OnOffType;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.Channel;
41 import org.openhab.core.thing.ChannelUID;
42 import org.openhab.core.thing.Thing;
43 import org.openhab.core.thing.ThingStatus;
44 import org.openhab.core.thing.ThingStatusDetail;
45 import org.openhab.core.thing.binding.BaseBridgeHandler;
46 import org.openhab.core.thing.binding.ThingHandler;
47 import org.openhab.core.thing.binding.builder.ChannelBuilder;
48 import org.openhab.core.thing.type.ChannelTypeUID;
49 import org.openhab.core.types.Command;
50 import org.openhab.core.types.RefreshType;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import com.google.gson.JsonParseException;
57 * The {@link HDPowerViewHubHandler} is responsible for handling commands, which
58 * are sent to one of the channels.
60 * @author Andy Lintner - Initial contribution
61 * @author Andrew Fiddian-Green - Added support for secondary rail positions
64 public class HDPowerViewHubHandler extends BaseBridgeHandler {
66 private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubHandler.class);
67 private final HttpClient httpClient;
69 private long refreshInterval;
70 private long hardRefreshInterval;
72 private @Nullable HDPowerViewWebTargets webTargets;
73 private @Nullable ScheduledFuture<?> pollFuture;
74 private @Nullable ScheduledFuture<?> hardRefreshFuture;
76 private final ChannelTypeUID sceneChannelTypeUID = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID,
77 HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);
79 public HDPowerViewHubHandler(Bridge bridge, HttpClient httpClient) {
81 this.httpClient = httpClient;
85 public void handleCommand(ChannelUID channelUID, Command command) {
86 if (RefreshType.REFRESH.equals(command)) {
87 requestRefreshShades();
91 Channel channel = getThing().getChannel(channelUID.getId());
92 if (channel != null && sceneChannelTypeUID.equals(channel.getChannelTypeUID())) {
93 if (OnOffType.ON.equals(command)) {
95 HDPowerViewWebTargets webTargets = this.webTargets;
96 if (webTargets == null) {
97 throw new ProcessingException("Web targets not initialized");
99 webTargets.activateScene(Integer.parseInt(channelUID.getId()));
100 } catch (HubMaintenanceException e) {
101 // exceptions are logged in HDPowerViewWebTargets
102 } catch (NumberFormatException | HubProcessingException e) {
103 logger.debug("Unexpected error {}", e.getMessage());
110 public void initialize() {
111 logger.debug("Initializing hub");
112 HDPowerViewHubConfiguration config = getConfigAs(HDPowerViewHubConfiguration.class);
113 String host = config.host;
115 if (host == null || host.isEmpty()) {
116 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Host address must be set");
120 webTargets = new HDPowerViewWebTargets(httpClient, host);
121 refreshInterval = config.refresh;
122 hardRefreshInterval = config.hardRefresh;
126 public @Nullable HDPowerViewWebTargets getWebTargets() {
131 public void handleRemoval() {
132 super.handleRemoval();
137 public void dispose() {
142 private void schedulePoll() {
143 ScheduledFuture<?> future = this.pollFuture;
144 if (future != null) {
145 future.cancel(false);
147 logger.debug("Scheduling poll for 5000ms out, then every {}ms", refreshInterval);
148 this.pollFuture = scheduler.scheduleWithFixedDelay(this::poll, 5000, refreshInterval, TimeUnit.MILLISECONDS);
150 future = this.hardRefreshFuture;
151 if (future != null) {
152 future.cancel(false);
154 if (hardRefreshInterval > 0) {
155 logger.debug("Scheduling hard refresh every {}minutes", hardRefreshInterval);
156 this.hardRefreshFuture = scheduler.scheduleWithFixedDelay(this::requestRefreshShades, 1,
157 hardRefreshInterval, TimeUnit.MINUTES);
161 private synchronized void stopPoll() {
162 ScheduledFuture<?> future = this.pollFuture;
163 if (future != null) {
166 this.pollFuture = null;
168 future = this.hardRefreshFuture;
169 if (future != null) {
172 this.hardRefreshFuture = null;
175 private synchronized void poll() {
177 logger.debug("Polling for state");
180 } catch (JsonParseException e) {
181 logger.warn("Bridge returned a bad JSON response: {}", e.getMessage());
182 } catch (HubProcessingException e) {
183 logger.warn("Error connecting to bridge: {}", e.getMessage());
184 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, e.getMessage());
185 } catch (HubMaintenanceException e) {
186 // exceptions are logged in HDPowerViewWebTargets
190 private void pollShades() throws JsonParseException, HubProcessingException, HubMaintenanceException {
191 HDPowerViewWebTargets webTargets = this.webTargets;
192 if (webTargets == null) {
193 throw new ProcessingException("Web targets not initialized");
196 Shades shades = webTargets.getShades();
197 if (shades == null) {
198 throw new JsonParseException("Missing 'shades' element");
201 List<ShadeData> shadesData = shades.shadeData;
202 if (shadesData == null) {
203 throw new JsonParseException("Missing 'shades.shadeData' element");
206 updateStatus(ThingStatus.ONLINE);
207 logger.debug("Received data for {} shades", shadesData.size());
209 Map<String, ShadeData> idShadeDataMap = getIdShadeDataMap(shadesData);
210 Map<Thing, String> thingIdMap = getThingIdMap();
211 for (Entry<Thing, String> item : thingIdMap.entrySet()) {
212 Thing thing = item.getKey();
213 String shadeId = item.getValue();
214 ShadeData shadeData = idShadeDataMap.get(shadeId);
215 updateShadeThing(shadeId, thing, shadeData);
219 private void updateShadeThing(String shadeId, Thing thing, @Nullable ShadeData shadeData) {
220 HDPowerViewShadeHandler thingHandler = ((HDPowerViewShadeHandler) thing.getHandler());
221 if (thingHandler == null) {
222 logger.debug("Shade '{}' handler not initialized", shadeId);
225 if (shadeData == null) {
226 logger.debug("Shade '{}' has no data in hub", shadeId);
228 logger.debug("Updating shade '{}'", shadeId);
230 thingHandler.onReceiveUpdate(shadeData);
233 private void pollScenes() throws JsonParseException, HubProcessingException, HubMaintenanceException {
234 HDPowerViewWebTargets webTargets = this.webTargets;
235 if (webTargets == null) {
236 throw new ProcessingException("Web targets not initialized");
239 Scenes scenes = webTargets.getScenes();
240 if (scenes == null) {
241 throw new JsonParseException("Missing 'scenes' element");
244 List<Scene> sceneData = scenes.sceneData;
245 if (sceneData == null) {
246 throw new JsonParseException("Missing 'scenes.sceneData' element");
248 logger.debug("Received data for {} scenes", sceneData.size());
250 Map<String, Channel> idChannelMap = getIdChannelMap();
251 for (Scene scene : sceneData) {
252 // remove existing scene channel from the map
253 String sceneId = Integer.toString(scene.id);
254 if (idChannelMap.containsKey(sceneId)) {
255 idChannelMap.remove(sceneId);
256 logger.debug("Keeping channel for existing scene '{}'", sceneId);
258 // create a new scene channel
259 ChannelUID channelUID = new ChannelUID(getThing().getUID(), sceneId);
260 Channel channel = ChannelBuilder.create(channelUID, "Switch").withType(sceneChannelTypeUID)
261 .withLabel(scene.getName()).withDescription("Activates the scene " + scene.getName()).build();
262 updateThing(editThing().withChannel(channel).build());
263 logger.debug("Creating new channel for scene '{}'", sceneId);
267 // remove any previously created channels that no longer exist
268 if (!idChannelMap.isEmpty()) {
269 logger.debug("Removing {} orphan scene channels", idChannelMap.size());
270 List<Channel> allChannels = new ArrayList<>(getThing().getChannels());
271 allChannels.removeAll(idChannelMap.values());
272 updateThing(editThing().withChannels(allChannels).build());
276 private Map<Thing, String> getThingIdMap() {
277 Map<Thing, String> ret = new HashMap<>();
278 for (Thing thing : getThing().getThings()) {
279 String id = thing.getConfiguration().as(HDPowerViewShadeConfiguration.class).id;
280 if (id != null && !id.isEmpty()) {
287 private Map<String, ShadeData> getIdShadeDataMap(List<ShadeData> shadeData) {
288 Map<String, ShadeData> ret = new HashMap<>();
289 for (ShadeData shade : shadeData) {
291 ret.put(Integer.toString(shade.id), shade);
297 private Map<String, Channel> getIdChannelMap() {
298 Map<String, Channel> ret = new HashMap<>();
299 for (Channel channel : getThing().getChannels()) {
300 if (sceneChannelTypeUID.equals(channel.getChannelTypeUID())) {
301 ret.put(channel.getUID().getId(), channel);
307 private void requestRefreshShades() {
308 Map<Thing, String> thingIdMap = getThingIdMap();
309 for (Entry<Thing, String> item : thingIdMap.entrySet()) {
310 Thing thing = item.getKey();
311 ThingHandler handler = thing.getHandler();
312 if (handler instanceof HDPowerViewShadeHandler) {
313 ((HDPowerViewShadeHandler) handler).requestRefreshShade();
315 String shadeId = item.getValue();
316 logger.debug("Shade '{}' handler not initialized", shadeId);