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.squeezebox.internal.handler;
15 import static org.openhab.binding.squeezebox.internal.SqueezeBoxBindingConstants.*;
18 import java.net.URISyntaxException;
19 import java.time.Duration;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
26 import java.util.concurrent.ScheduledFuture;
27 import java.util.concurrent.TimeUnit;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.openhab.binding.squeezebox.internal.SqueezeBoxStateDescriptionOptionsProvider;
31 import org.openhab.binding.squeezebox.internal.config.SqueezeBoxPlayerConfig;
32 import org.openhab.binding.squeezebox.internal.model.Favorite;
33 import org.openhab.binding.squeezebox.internal.utils.SqueezeBoxTimeoutException;
34 import org.openhab.core.cache.ExpiringCacheMap;
35 import org.openhab.core.io.net.http.HttpUtil;
36 import org.openhab.core.library.types.DecimalType;
37 import org.openhab.core.library.types.IncreaseDecreaseType;
38 import org.openhab.core.library.types.NextPreviousType;
39 import org.openhab.core.library.types.OnOffType;
40 import org.openhab.core.library.types.PercentType;
41 import org.openhab.core.library.types.PlayPauseType;
42 import org.openhab.core.library.types.RawType;
43 import org.openhab.core.library.types.RewindFastforwardType;
44 import org.openhab.core.library.types.StringType;
45 import org.openhab.core.thing.ChannelUID;
46 import org.openhab.core.thing.Thing;
47 import org.openhab.core.thing.ThingStatus;
48 import org.openhab.core.thing.ThingStatusDetail;
49 import org.openhab.core.thing.ThingStatusInfo;
50 import org.openhab.core.thing.ThingTypeUID;
51 import org.openhab.core.thing.binding.BaseThingHandler;
52 import org.openhab.core.types.Command;
53 import org.openhab.core.types.RefreshType;
54 import org.openhab.core.types.State;
55 import org.openhab.core.types.StateOption;
56 import org.openhab.core.types.UnDefType;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * The {@link SqueezeBoxPlayerHandler} is responsible for handling states, which
62 * are sent to/from channels.
64 * @author Dan Cunningham - Initial contribution
65 * @author Mark Hilbush - Improved handling of player status, prevent REFRESH from causing exception
66 * @author Mark Hilbush - Implement AudioSink and notifications
67 * @author Mark Hilbush - Added duration channel
68 * @author Patrik Gfeller - Timeout for TTS messages increased from 30 to 90s.
69 * @author Mark Hilbush - Get favorites from server and play favorite
70 * @author Mark Hilbush - Convert sound notification volume from channel to config parameter
71 * @author Mark Hilbush - Add like/unlike functionality
73 public class SqueezeBoxPlayerHandler extends BaseThingHandler implements SqueezeBoxPlayerEventListener {
74 private final Logger logger = LoggerFactory.getLogger(SqueezeBoxPlayerHandler.class);
76 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
77 .singleton(SQUEEZEBOXPLAYER_THING_TYPE);
80 * We need to remember some states to change offsets in volume, time index,
83 protected Map<String, State> stateMap = Collections.synchronizedMap(new HashMap<>());
86 * Keeps current track time
88 private ScheduledFuture<?> timeCounterJob;
91 * Local reference to our bridge
93 private SqueezeBoxServerHandler squeezeBoxServerHandler;
96 * Our mac address, needed everywhere
101 * The server sends us the current time on play/pause/stop events, we
102 * increment it locally from there on
104 private int currentTime = 0;
107 * Our we playing something right now or not, need to keep current track
110 private boolean playing;
113 * Separate volume level for notifications
115 private Integer notificationSoundVolume = null;
117 private String callbackUrl;
119 private SqueezeBoxStateDescriptionOptionsProvider stateDescriptionProvider;
121 private static final ExpiringCacheMap<String, RawType> IMAGE_CACHE = new ExpiringCacheMap<>(
122 TimeUnit.MINUTES.toMillis(15)); // 15min
124 private String likeCommand;
125 private String unlikeCommand;
128 * Creates SqueezeBox Player Handler
131 * @param stateDescriptionProvider
133 public SqueezeBoxPlayerHandler(@NonNull Thing thing, String callbackUrl,
134 SqueezeBoxStateDescriptionOptionsProvider stateDescriptionProvider) {
136 this.callbackUrl = callbackUrl;
137 this.stateDescriptionProvider = stateDescriptionProvider;
141 public void initialize() {
142 mac = getConfig().as(SqueezeBoxPlayerConfig.class).mac;
144 updateBridgeStatus();
145 logger.debug("player thing {} initialized with mac {}", getThing().getUID(), mac);
149 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
150 updateBridgeStatus();
153 private void updateBridgeStatus() {
154 Thing bridge = getBridge();
155 if (bridge != null) {
156 squeezeBoxServerHandler = (SqueezeBoxServerHandler) bridge.getHandler();
157 ThingStatus bridgeStatus = bridge.getStatus();
158 if (bridgeStatus == ThingStatus.ONLINE && getThing().getStatus() != ThingStatus.ONLINE) {
159 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
160 } else if (bridgeStatus == ThingStatus.OFFLINE) {
161 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
164 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge not found");
169 public void dispose() {
170 // stop our duration counter
171 if (timeCounterJob != null && !timeCounterJob.isCancelled()) {
172 timeCounterJob.cancel(true);
173 timeCounterJob = null;
176 if (squeezeBoxServerHandler != null) {
177 squeezeBoxServerHandler.removePlayerCache(mac);
179 logger.debug("player thing {} disposed for mac {}", getThing().getUID(), mac);
184 public void handleCommand(ChannelUID channelUID, Command command) {
185 if (squeezeBoxServerHandler == null) {
186 logger.debug("Player {} has no server configured, ignoring command: {}", getThing().getUID(), command);
189 // Some of the code below is not designed to handle REFRESH, only reply to channels where cached values exist
190 if (command == RefreshType.REFRESH) {
191 String channelID = channelUID.getId();
192 State newState = stateMap.get(channelID);
193 if (newState != null) {
194 updateState(channelID, newState);
199 switch (channelUID.getIdWithoutGroup()) {
201 if (command.equals(OnOffType.ON)) {
202 squeezeBoxServerHandler.powerOn(mac);
204 squeezeBoxServerHandler.powerOff(mac);
208 if (command.equals(OnOffType.ON)) {
209 squeezeBoxServerHandler.mute(mac);
211 squeezeBoxServerHandler.unMute(mac);
215 if (command.equals(OnOffType.ON)) {
216 squeezeBoxServerHandler.stop(mac);
217 } else if (command.equals(OnOffType.OFF)) {
218 squeezeBoxServerHandler.play(mac);
221 case CHANNEL_PLAY_PAUSE:
222 if (command.equals(OnOffType.ON)) {
223 squeezeBoxServerHandler.play(mac);
224 } else if (command.equals(OnOffType.OFF)) {
225 squeezeBoxServerHandler.pause(mac);
229 if (command.equals(OnOffType.ON)) {
230 squeezeBoxServerHandler.prev(mac);
234 if (command.equals(OnOffType.ON)) {
235 squeezeBoxServerHandler.next(mac);
239 if (command instanceof PercentType) {
240 squeezeBoxServerHandler.setVolume(mac, ((PercentType) command).intValue());
241 } else if (command.equals(IncreaseDecreaseType.INCREASE)) {
242 squeezeBoxServerHandler.volumeUp(mac, currentVolume());
243 } else if (command.equals(IncreaseDecreaseType.DECREASE)) {
244 squeezeBoxServerHandler.volumeDown(mac, currentVolume());
245 } else if (command.equals(OnOffType.OFF)) {
246 squeezeBoxServerHandler.mute(mac);
247 } else if (command.equals(OnOffType.ON)) {
248 squeezeBoxServerHandler.unMute(mac);
251 case CHANNEL_CONTROL:
252 if (command instanceof PlayPauseType) {
253 if (command.equals(PlayPauseType.PLAY)) {
254 squeezeBoxServerHandler.play(mac);
255 } else if (command.equals(PlayPauseType.PAUSE)) {
256 squeezeBoxServerHandler.pause(mac);
259 if (command instanceof NextPreviousType) {
260 if (command.equals(NextPreviousType.NEXT)) {
261 squeezeBoxServerHandler.next(mac);
262 } else if (command.equals(NextPreviousType.PREVIOUS)) {
263 squeezeBoxServerHandler.prev(mac);
266 if (command instanceof RewindFastforwardType) {
267 if (command.equals(RewindFastforwardType.REWIND)) {
268 squeezeBoxServerHandler.setPlayingTime(mac, currentPlayingTime() - 5);
269 } else if (command.equals(RewindFastforwardType.FASTFORWARD)) {
270 squeezeBoxServerHandler.setPlayingTime(mac, currentPlayingTime() + 5);
275 squeezeBoxServerHandler.playUrl(mac, command.toString());
278 if (command.toString().isBlank()) {
279 squeezeBoxServerHandler.unSyncPlayer(mac);
281 squeezeBoxServerHandler.syncPlayer(mac, command.toString());
285 if (command.equals(OnOffType.ON)) {
286 squeezeBoxServerHandler.unSyncPlayer(mac);
289 case CHANNEL_PLAYLIST_INDEX:
290 squeezeBoxServerHandler.playPlaylistItem(mac, ((DecimalType) command).intValue());
292 case CHANNEL_CURRENT_PLAYING_TIME:
293 squeezeBoxServerHandler.setPlayingTime(mac, ((DecimalType) command).intValue());
295 case CHANNEL_CURRENT_PLAYLIST_SHUFFLE:
296 squeezeBoxServerHandler.setShuffleMode(mac, ((DecimalType) command).intValue());
298 case CHANNEL_CURRENT_PLAYLIST_REPEAT:
299 squeezeBoxServerHandler.setRepeatMode(mac, ((DecimalType) command).intValue());
301 case CHANNEL_FAVORITES_PLAY:
302 squeezeBoxServerHandler.playFavorite(mac, command.toString());
305 if (command.equals(OnOffType.ON)) {
306 squeezeBoxServerHandler.rate(mac, likeCommand);
307 } else if (command.equals(OnOffType.OFF)) {
308 squeezeBoxServerHandler.rate(mac, unlikeCommand);
312 if (command instanceof DecimalType) {
313 Duration sleepDuration = Duration.ofMinutes(((DecimalType) command).longValue());
314 if (sleepDuration.isNegative() || sleepDuration.compareTo(Duration.ofDays(1)) > 0) {
315 logger.debug("Sleep timer of {} minutes must be >= 0 and <= 1 day", sleepDuration.toMinutes());
318 squeezeBoxServerHandler.sleep(mac, sleepDuration);
327 public void playerAdded(SqueezeBoxPlayer player) {
328 // Player properties are saved in SqueezeBoxPlayerDiscoveryParticipant
332 public void powerChangeEvent(String mac, boolean power) {
333 updateChannel(mac, CHANNEL_POWER, power ? OnOffType.ON : OnOffType.OFF);
334 if (!power && isMe(mac)) {
340 public synchronized void modeChangeEvent(String mac, String mode) {
341 updateChannel(mac, CHANNEL_CONTROL, "play".equals(mode) ? PlayPauseType.PLAY : PlayPauseType.PAUSE);
342 updateChannel(mac, CHANNEL_PLAY_PAUSE, "play".equals(mode) ? OnOffType.ON : OnOffType.OFF);
343 updateChannel(mac, CHANNEL_STOP, "stop".equals(mode) ? OnOffType.ON : OnOffType.OFF);
345 playing = "play".equalsIgnoreCase(mode);
350 public void sourceChangeEvent(String mac, String source) {
351 updateChannel(mac, CHANNEL_SOURCE, StringType.valueOf(source));
355 public void absoluteVolumeChangeEvent(String mac, int volume) {
356 int newVolume = volume;
357 newVolume = Math.min(100, newVolume);
358 newVolume = Math.max(0, newVolume);
359 updateChannel(mac, CHANNEL_VOLUME, new PercentType(newVolume));
363 public void relativeVolumeChangeEvent(String mac, int volumeChange) {
364 int newVolume = currentVolume() + volumeChange;
365 newVolume = Math.min(100, newVolume);
366 newVolume = Math.max(0, newVolume);
367 updateChannel(mac, CHANNEL_VOLUME, new PercentType(newVolume));
370 logger.trace("Volume changed [{}] for player {}. New volume: {}", volumeChange, mac, newVolume);
375 public void muteChangeEvent(String mac, boolean mute) {
376 updateChannel(mac, CHANNEL_MUTE, mute ? OnOffType.ON : OnOffType.OFF);
380 public void currentPlaylistIndexEvent(String mac, int index) {
381 updateChannel(mac, CHANNEL_PLAYLIST_INDEX, new DecimalType(index));
385 public void currentPlayingTimeEvent(String mac, int time) {
386 updateChannel(mac, CHANNEL_CURRENT_PLAYING_TIME, new DecimalType(time));
393 public void durationEvent(String mac, int duration) {
394 if (getThing().getChannel(CHANNEL_DURATION) == null) {
395 logger.debug("Channel 'duration' does not exist. Delete and readd player thing to pick up channel.");
398 updateChannel(mac, CHANNEL_DURATION, new DecimalType(duration));
402 public void numberPlaylistTracksEvent(String mac, int track) {
403 updateChannel(mac, CHANNEL_NUMBER_PLAYLIST_TRACKS, new DecimalType(track));
407 public void currentPlaylistShuffleEvent(String mac, int shuffle) {
408 updateChannel(mac, CHANNEL_CURRENT_PLAYLIST_SHUFFLE, new DecimalType(shuffle));
412 public void currentPlaylistRepeatEvent(String mac, int repeat) {
413 updateChannel(mac, CHANNEL_CURRENT_PLAYLIST_REPEAT, new DecimalType(repeat));
417 public void titleChangeEvent(String mac, String title) {
418 updateChannel(mac, CHANNEL_TITLE, new StringType(title));
422 public void albumChangeEvent(String mac, String album) {
423 updateChannel(mac, CHANNEL_ALBUM, new StringType(album));
427 public void artistChangeEvent(String mac, String artist) {
428 updateChannel(mac, CHANNEL_ARTIST, new StringType(artist));
432 public void coverArtChangeEvent(String mac, String coverArtUrl) {
433 updateChannel(mac, CHANNEL_COVERART_DATA, createImage(downloadImage(mac, coverArtUrl)));
437 * Download and cache the image data from an URL.
439 * @param url The URL of the image to be downloaded.
440 * @return A RawType object containing the image, null if the content type could not be found or the content type is
443 private RawType downloadImage(String mac, String url) {
444 // Only get the image if this is my PlayerHandler instance
446 if (url != null && !url.isEmpty()) {
447 String sanitizedUrl = sanitizeUrl(url);
448 RawType image = IMAGE_CACHE.putIfAbsentAndGet(url, () -> {
449 logger.debug("Trying to download the content of URL {}", sanitizedUrl);
451 return HttpUtil.downloadImage(url);
452 } catch (IllegalArgumentException e) {
453 logger.debug("IllegalArgumentException when downloading image from {}", sanitizedUrl, e);
458 logger.debug("Failed to download the content of URL {}", sanitizedUrl);
469 * Replaces the password in the URL, if present
471 private String sanitizeUrl(String url) {
472 String sanitizedUrl = url;
474 URI uri = new URI(url);
475 String userInfo = uri.getUserInfo();
476 if (userInfo != null) {
477 String[] userInfoParts = userInfo.split(":");
478 if (userInfoParts.length == 2) {
479 sanitizedUrl = url.replace(userInfoParts[1], "**********");
482 } catch (URISyntaxException e) {
483 // Just return what was passed in
489 * Wrap the given RawType and return it as {@link State} or return {@link UnDefType#UNDEF} if the RawType is null.
491 private State createImage(RawType image) {
493 return UnDefType.UNDEF;
500 public void yearChangeEvent(String mac, String year) {
501 updateChannel(mac, CHANNEL_YEAR, new StringType(year));
505 public void genreChangeEvent(String mac, String genre) {
506 updateChannel(mac, CHANNEL_GENRE, new StringType(genre));
510 public void remoteTitleChangeEvent(String mac, String title) {
511 updateChannel(mac, CHANNEL_REMOTE_TITLE, new StringType(title));
515 public void irCodeChangeEvent(String mac, String ircode) {
517 postCommand(CHANNEL_IRCODE, new StringType(ircode));
522 public void buttonsChangeEvent(String mac, String likeCommand, String unlikeCommand) {
524 this.likeCommand = likeCommand;
525 this.unlikeCommand = unlikeCommand;
526 logger.trace("Player {} got a button change event: like='{}' unlike='{}'", mac, likeCommand, unlikeCommand);
531 public void updateFavoritesListEvent(List<Favorite> favorites) {
532 logger.trace("Player {} updating favorites list with {} favorites", mac, favorites.size());
533 List<StateOption> options = new ArrayList<>();
534 for (Favorite favorite : favorites) {
535 options.add(new StateOption(favorite.shortId, favorite.name));
537 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_FAVORITES_PLAY), options);
541 * Update a channel if the mac matches our own
547 private void updateChannel(String mac, String channelID, State state) {
549 State prevState = stateMap.put(channelID, state);
550 if (prevState == null || !prevState.equals(state)) {
551 logger.trace("Updating channel {} for thing {} with mac {} to state {}", channelID, getThing().getUID(),
553 updateState(channelID, state);
559 * Helper methods to get the current state of the player
563 int currentVolume() {
564 if (stateMap.containsKey(CHANNEL_VOLUME)) {
565 return ((DecimalType) stateMap.get(CHANNEL_VOLUME)).intValue();
571 int currentPlayingTime() {
572 if (stateMap.containsKey(CHANNEL_CURRENT_PLAYING_TIME)) {
573 return ((DecimalType) stateMap.get(CHANNEL_CURRENT_PLAYING_TIME)).intValue();
579 int currentNumberPlaylistTracks() {
580 if (stateMap.containsKey(CHANNEL_NUMBER_PLAYLIST_TRACKS)) {
581 return ((DecimalType) stateMap.get(CHANNEL_NUMBER_PLAYLIST_TRACKS)).intValue();
587 int currentPlaylistIndex() {
588 if (stateMap.containsKey(CHANNEL_PLAYLIST_INDEX)) {
589 return ((DecimalType) stateMap.get(CHANNEL_PLAYLIST_INDEX)).intValue();
595 boolean currentPower() {
596 if (stateMap.containsKey(CHANNEL_POWER)) {
597 return (stateMap.get(CHANNEL_POWER).equals(OnOffType.ON) ? true : false);
603 boolean currentStop() {
604 if (stateMap.containsKey(CHANNEL_STOP)) {
605 return (stateMap.get(CHANNEL_STOP).equals(OnOffType.ON) ? true : false);
611 boolean currentControl() {
612 if (stateMap.containsKey(CHANNEL_CONTROL)) {
613 return (stateMap.get(CHANNEL_CONTROL).equals(PlayPauseType.PLAY) ? true : false);
619 boolean currentMute() {
620 if (stateMap.containsKey(CHANNEL_MUTE)) {
621 return (stateMap.get(CHANNEL_MUTE).equals(OnOffType.ON) ? true : false);
627 int currentShuffle() {
628 if (stateMap.containsKey(CHANNEL_CURRENT_PLAYLIST_SHUFFLE)) {
629 return ((DecimalType) stateMap.get(CHANNEL_CURRENT_PLAYLIST_SHUFFLE)).intValue();
635 int currentRepeat() {
636 if (stateMap.containsKey(CHANNEL_CURRENT_PLAYLIST_REPEAT)) {
637 return ((DecimalType) stateMap.get(CHANNEL_CURRENT_PLAYLIST_REPEAT)).intValue();
644 * Ticks away when in a play state to keep current track time
646 private void timeCounter() {
647 timeCounterJob = scheduler.scheduleWithFixedDelay(() -> {
649 updateChannel(mac, CHANNEL_CURRENT_PLAYING_TIME, new DecimalType(currentTime++));
651 }, 0, 1, TimeUnit.SECONDS);
654 private boolean isMe(String mac) {
655 return mac.equals(this.mac);
659 * Returns our server handler if set
663 public SqueezeBoxServerHandler getSqueezeBoxServerHandler() {
664 return this.squeezeBoxServerHandler;
668 * Returns the MAC address for this player
672 public String getMac() {
677 * Give the notification player access to the notification timeout
679 public int getNotificationTimeout() {
680 return getConfigAs(SqueezeBoxPlayerConfig.class).notificationTimeout;
684 * Used by the AudioSink to get the volume level that should be used for the notification.
685 * Priority for determining volume is:
686 * - volume is provided in the say/playSound actions
687 * - volume is contained in the player thing's configuration
688 * - current player volume setting
690 public PercentType getNotificationSoundVolume() {
691 // Get the notification sound volume from this player thing's configuration
692 Integer configNotificationSoundVolume = getConfigAs(SqueezeBoxPlayerConfig.class).notificationVolume;
694 // Determine which volume to use
695 Integer currentNotificationSoundVolume;
696 if (notificationSoundVolume != null) {
697 currentNotificationSoundVolume = notificationSoundVolume;
698 } else if (configNotificationSoundVolume != null) {
699 currentNotificationSoundVolume = configNotificationSoundVolume;
701 currentNotificationSoundVolume = Integer.valueOf(currentVolume());
703 return new PercentType(currentNotificationSoundVolume.intValue());
707 * Used by the AudioSink to set the volume level that should be used to play the notification
709 public void setNotificationSoundVolume(PercentType newNotificationSoundVolume) {
710 if (newNotificationSoundVolume != null) {
711 notificationSoundVolume = Integer.valueOf(newNotificationSoundVolume.intValue());
716 * Play the notification.
718 public void playNotificationSoundURI(StringType uri) {
719 logger.debug("Play notification sound on player {} at URI {}", mac, uri);
721 try (SqueezeBoxNotificationPlayer notificationPlayer = new SqueezeBoxNotificationPlayer(this,
722 squeezeBoxServerHandler, uri)) {
723 notificationPlayer.play();
724 } catch (InterruptedException e) {
725 logger.warn("Notification playback was interrupted", e);
726 } catch (SqueezeBoxTimeoutException e) {
727 logger.debug("SqueezeBoxTimeoutException during notification: {}", e.getMessage());
729 notificationSoundVolume = null;
734 * Return the IP and port of the OH2 web server
736 public String getHostAndPort() {