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.harmonyhub.internal.handler;
15 import static org.openhab.binding.harmonyhub.internal.HarmonyHubBindingConstants.*;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.LinkedList;
21 import java.util.List;
23 import java.util.concurrent.CompletableFuture;
24 import java.util.concurrent.CopyOnWriteArrayList;
25 import java.util.concurrent.ScheduledFuture;
26 import java.util.concurrent.TimeUnit;
28 import org.apache.commons.lang.StringUtils;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.openhab.binding.harmonyhub.internal.HarmonyHubHandlerFactory;
32 import org.openhab.binding.harmonyhub.internal.config.HarmonyHubConfig;
33 import org.openhab.core.config.core.Configuration;
34 import org.openhab.core.library.types.DecimalType;
35 import org.openhab.core.library.types.NextPreviousType;
36 import org.openhab.core.library.types.PlayPauseType;
37 import org.openhab.core.library.types.RewindFastforwardType;
38 import org.openhab.core.library.types.StringType;
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.ThingStatus;
43 import org.openhab.core.thing.ThingStatusDetail;
44 import org.openhab.core.thing.ThingTypeUID;
45 import org.openhab.core.thing.binding.BaseBridgeHandler;
46 import org.openhab.core.thing.binding.builder.BridgeBuilder;
47 import org.openhab.core.thing.binding.builder.ChannelBuilder;
48 import org.openhab.core.thing.type.ChannelType;
49 import org.openhab.core.thing.type.ChannelTypeBuilder;
50 import org.openhab.core.thing.type.ChannelTypeUID;
51 import org.openhab.core.types.Command;
52 import org.openhab.core.types.RefreshType;
53 import org.openhab.core.types.StateDescriptionFragmentBuilder;
54 import org.openhab.core.types.StateOption;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import com.digitaldan.harmony.HarmonyClient;
59 import com.digitaldan.harmony.HarmonyClientListener;
60 import com.digitaldan.harmony.config.Activity;
61 import com.digitaldan.harmony.config.Activity.Status;
62 import com.digitaldan.harmony.config.HarmonyConfig;
65 * The {@link HarmonyHubHandler} is responsible for handling commands for Harmony Hubs, which are
66 * sent to one of the channels.
68 * @author Dan Cunningham - Initial contribution
69 * @author Pawel Pieczul - added support for hub status changes
70 * @author Wouter Born - Add null annotations
73 public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClientListener {
75 private final Logger logger = LoggerFactory.getLogger(HarmonyHubHandler.class);
77 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(HARMONY_HUB_THING_TYPE);
79 private static final Comparator<Activity> ACTIVITY_COMPERATOR = Comparator.comparing(Activity::getActivityOrder,
80 Comparator.nullsFirst(Integer::compareTo));
82 private static final int RETRY_TIME = 60;
83 private static final int HEARTBEAT_INTERVAL = 30;
84 // Websocket will timeout after 60 seconds, pick a sensible max under this,
85 private static final int HEARTBEAT_INTERVAL_MAX = 50;
86 private List<HubStatusListener> listeners = new CopyOnWriteArrayList<>();
87 private final HarmonyHubHandlerFactory factory;
88 private @NonNullByDefault({}) HarmonyHubConfig config;
89 private final HarmonyClient client;
90 private @Nullable ScheduledFuture<?> retryJob;
91 private @Nullable ScheduledFuture<?> heartBeatJob;
93 private int heartBeatInterval;
95 public HarmonyHubHandler(Bridge bridge, HarmonyHubHandlerFactory factory) {
97 this.factory = factory;
98 client = new HarmonyClient(factory.getHttpClient());
99 client.addListener(this);
103 public void handleCommand(ChannelUID channelUID, Command command) {
104 logger.trace("Handling command '{}' for {}", command, channelUID);
106 if (getThing().getStatus() != ThingStatus.ONLINE) {
107 logger.debug("Hub is offline, ignoring command {} for channel {}", command, channelUID);
111 if (command instanceof RefreshType) {
112 client.getCurrentActivity().thenAccept(activity -> {
113 updateState(activity);
118 Channel channel = getThing().getChannel(channelUID.getId());
119 if (channel == null) {
120 logger.warn("No such channel for UID {}", channelUID);
124 switch (channel.getUID().getId()) {
125 case CHANNEL_CURRENT_ACTIVITY:
126 if (command instanceof DecimalType) {
128 client.startActivity(((DecimalType) command).intValue());
129 } catch (Exception e) {
130 logger.warn("Could not start activity", e);
135 int actId = Integer.parseInt(command.toString());
136 client.startActivity(actId);
137 } catch (NumberFormatException ignored) {
138 client.startActivityByName(command.toString());
140 } catch (IllegalArgumentException e) {
141 logger.warn("Activity '{}' is not known by the hub, ignoring it.", command);
142 } catch (Exception e) {
143 logger.warn("Could not start activity", e);
147 case CHANNEL_BUTTON_PRESS:
148 client.pressButtonCurrentActivity(command.toString());
152 if (command instanceof PlayPauseType) {
153 if (command == PlayPauseType.PLAY) {
155 } else if (command == PlayPauseType.PAUSE) {
158 } else if (command instanceof NextPreviousType) {
159 if (command == NextPreviousType.NEXT) {
161 } else if (command == NextPreviousType.PREVIOUS) {
162 cmd = "SkipBackward";
164 } else if (command instanceof RewindFastforwardType) {
165 if (command == RewindFastforwardType.FASTFORWARD) {
167 } else if (command == RewindFastforwardType.REWIND) {
172 client.pressButtonCurrentActivity(cmd);
174 logger.warn("Unknown player type {}", command);
178 logger.warn("Unknown channel id {}", channel.getUID().getId());
183 public void initialize() {
184 config = getConfigAs(HarmonyHubConfig.class);
186 updateStatus(ThingStatus.UNKNOWN);
187 retryJob = scheduler.schedule(this::connect, 0, TimeUnit.SECONDS);
191 public void dispose() {
195 factory.removeChannelTypesForThing(getThing().getUID());
199 protected void updateStatus(ThingStatus status, ThingStatusDetail detail, @Nullable String comment) {
200 super.updateStatus(status, detail, comment);
201 logger.debug("Updating listeners with status {}", status);
202 for (HubStatusListener listener : listeners) {
203 listener.hubStatusChanged(status);
208 public void channelLinked(ChannelUID channelUID) {
209 client.getCurrentActivity().thenAccept((activity) -> {
210 updateState(channelUID, new StringType(activity.getLabel()));
215 public void hubDisconnected(@Nullable String reason) {
216 if (getThing().getStatus() == ThingStatus.ONLINE) {
217 setOfflineAndReconnect(String.format("Could not connect: %s", reason));
222 public void hubConnected() {
223 heartBeatJob = scheduler.scheduleWithFixedDelay(() -> {
226 } catch (Exception e) {
227 logger.debug("heartbeat failed", e);
228 setOfflineAndReconnect("Hearbeat failed");
230 }, heartBeatInterval, heartBeatInterval, TimeUnit.SECONDS);
231 updateStatus(ThingStatus.ONLINE);
232 getConfigFuture().thenAcceptAsync(harmonyConfig -> updateCurrentActivityChannel(harmonyConfig), scheduler)
233 .exceptionally(e -> {
234 setOfflineAndReconnect("Getting config failed: " + e.getMessage());
237 client.getCurrentActivity().thenAccept(activity -> {
238 updateState(activity);
243 public void activityStatusChanged(@Nullable Activity activity, @Nullable Status status) {
244 updateActivityStatus(activity, status);
248 public void activityStarted(@Nullable Activity activity) {
249 updateState(activity);
253 * Starts the connection process
255 private synchronized void connect() {
258 heartBeatInterval = Math.min(config.heartBeatInterval > 0 ? config.heartBeatInterval : HEARTBEAT_INTERVAL,
259 HEARTBEAT_INTERVAL_MAX);
261 String host = config.host;
263 // earlier versions required a name and used network discovery to find the hub and retrieve the host,
264 // this section is to not break that and also update older configurations to use the host configuration
265 // option instead of name
266 if (StringUtils.isBlank(host)) {
267 host = getThing().getProperties().get(HUB_PROPERTY_HOST);
268 if (StringUtils.isNotBlank(host)) {
269 Configuration genericConfig = getConfig();
270 genericConfig.put(HUB_PROPERTY_HOST, host);
271 updateConfiguration(genericConfig);
273 logger.debug("host not configured");
274 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "host not configured");
280 logger.debug("Connecting: host {}", host);
281 client.connect(host);
282 } catch (Exception e) {
283 logger.debug("Could not connect to HarmonyHub at {}", host, e);
284 setOfflineAndReconnect("Could not connect: " + e.getMessage());
288 private void disconnectFromHub() {
289 ScheduledFuture<?> localHeartBeatJob = heartBeatJob;
290 if (localHeartBeatJob != null && !localHeartBeatJob.isDone()) {
291 localHeartBeatJob.cancel(false);
296 private void setOfflineAndReconnect(String error) {
298 retryJob = scheduler.schedule(this::connect, RETRY_TIME, TimeUnit.SECONDS);
299 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, error);
302 private void cancelRetry() {
303 ScheduledFuture<?> localRetryJob = retryJob;
304 if (localRetryJob != null && !localRetryJob.isDone()) {
305 localRetryJob.cancel(false);
309 private void updateState(@Nullable Activity activity) {
310 if (activity != null) {
311 logger.debug("Updating current activity to {}", activity.getLabel());
312 updateState(new ChannelUID(getThing().getUID(), CHANNEL_CURRENT_ACTIVITY),
313 new StringType(activity.getLabel()));
317 private void updateActivityStatus(@Nullable Activity activity, @Nullable Status status) {
318 if (activity == null) {
319 logger.debug("Cannot update activity status of {} with activity that is null", getThing().getUID());
321 } else if (status == null) {
322 logger.debug("Cannot update activity status of {} with status that is null", getThing().getUID());
326 logger.debug("Received {} activity status for {}", status, activity.getLabel());
328 case ACTIVITY_IS_STARTING:
329 triggerChannel(CHANNEL_ACTIVITY_STARTING_TRIGGER, getEventName(activity));
331 case ACTIVITY_IS_STARTED:
333 // hub is off is received with power-off activity
334 triggerChannel(CHANNEL_ACTIVITY_STARTED_TRIGGER, getEventName(activity));
336 case HUB_IS_TURNING_OFF:
337 // hub is turning off is received for current activity, we will translate it into activity starting
338 // trigger of power-off activity (with ID=-1)
339 getConfigFuture().thenAccept(config -> {
340 if (config != null) {
341 Activity powerOff = config.getActivityById(-1);
342 if (powerOff != null) {
343 triggerChannel(CHANNEL_ACTIVITY_STARTING_TRIGGER, getEventName(powerOff));
346 }).exceptionally(e -> {
347 setOfflineAndReconnect("Getting config failed: " + e.getMessage());
356 private String getEventName(Activity activity) {
357 return activity.getLabel().replaceAll("[^A-Za-z0-9]", "_");
361 * Updates the current activity channel with the available activities as option states.
363 private void updateCurrentActivityChannel(@Nullable HarmonyConfig config) {
364 ChannelTypeUID channelTypeUID = new ChannelTypeUID(getThing().getUID() + ":" + CHANNEL_CURRENT_ACTIVITY);
366 if (config == null) {
367 logger.debug("Cannot update {} when HarmonyConfig is null", channelTypeUID);
371 logger.debug("Updating {}", channelTypeUID);
373 List<Activity> activities = config.getActivities();
374 // sort our activities in order
375 Collections.sort(activities, ACTIVITY_COMPERATOR);
377 // add our activities as channel state options
378 List<StateOption> states = new LinkedList<>();
379 for (Activity activity : activities) {
380 states.add(new StateOption(activity.getLabel(), activity.getLabel()));
383 ChannelType channelType = ChannelTypeBuilder.state(channelTypeUID, "Current Activity", "String")
384 .withDescription("Current activity for " + getThing().getLabel())
385 .withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withPattern("%s")
386 .withReadOnly(false).withOptions(states).build())
389 factory.addChannelType(channelType);
391 Channel channel = ChannelBuilder.create(new ChannelUID(getThing().getUID(), CHANNEL_CURRENT_ACTIVITY), "String")
392 .withType(channelTypeUID).build();
394 // replace existing currentActivity with updated one
395 List<Channel> newChannels = new ArrayList<>();
396 for (Channel c : getThing().getChannels()) {
397 if (!c.getUID().equals(channel.getUID())) {
401 newChannels.add(channel);
403 BridgeBuilder thingBuilder = editThing();
404 thingBuilder.withChannels(newChannels);
405 updateThing(thingBuilder.build());
409 * Sends a button press to a device
414 public void pressButton(int device, String button) {
415 client.pressButton(device, button);
419 * Sends a button press to a device
424 public void pressButton(String device, String button) {
425 client.pressButton(device, button);
428 public CompletableFuture<@Nullable HarmonyConfig> getConfigFuture() {
429 return client.getConfig();
433 * Adds a HubConnectedListener
437 public void addHubStatusListener(HubStatusListener listener) {
438 listeners.add(listener);
439 listener.hubStatusChanged(getThing().getStatus());
443 * Removes a HubConnectedListener
447 public void removeHubStatusListener(HubStatusListener listener) {
448 listeners.remove(listener);