Search in sources :

Example 1 with SonosMetaData

use of org.eclipse.smarthome.binding.sonos.internal.SonosMetaData in project smarthome by eclipse.

the class ZonePlayerHandler method handleRadioStream.

/**
 * Does a chain of predefined actions when a Notification sound is played by
 * {@link ZonePlayerHandler#playNotificationSoundURI(Command)} in case
 * radio streaming is currently loaded
 *
 * @param currentStreamURI - the currently loaded stream's URI
 * @param notificationURL - the notification url in the format of //host/folder/filename.mp3
 * @param coordinator - {@link ZonePlayerHandler} coordinator for the SONOS device(s)
 */
private void handleRadioStream(String currentStreamURI, Command notificationURL, ZonePlayerHandler coordinator) {
    String nextAction = coordinator.getTransportState();
    SonosMetaData track = coordinator.getTrackMetadata();
    SonosMetaData currentURI = coordinator.getCurrentURIMetadata();
    if (track != null && currentURI != null) {
        handleNotificationSound(notificationURL, coordinator);
        coordinator.setCurrentURI(new SonosEntry("", currentURI.getTitle(), "", "", track.getAlbumArtUri(), "", currentURI.getUpnpClass(), currentStreamURI));
        restoreLastTransportState(coordinator, nextAction);
    }
}
Also used : SonosMetaData(org.eclipse.smarthome.binding.sonos.internal.SonosMetaData) SonosEntry(org.eclipse.smarthome.binding.sonos.internal.SonosEntry)

Example 2 with SonosMetaData

use of org.eclipse.smarthome.binding.sonos.internal.SonosMetaData in project smarthome by eclipse.

the class ZonePlayerHandler method updateMediaInformation.

protected void updateMediaInformation() {
    String currentURI = getCurrentURI();
    SonosMetaData currentTrack = getTrackMetadata();
    SonosMetaData currentUriMetaData = getCurrentURIMetadata();
    String artist = null;
    String album = null;
    String title = null;
    String resultString = null;
    String stationID = null;
    boolean needsUpdating = false;
    // if currentURI == null, we do nothing
    if (currentURI != null) {
        if (currentURI.isEmpty()) {
            // Reset data
            needsUpdating = true;
        } else if (isPlayingStream(currentURI)) {
            // Radio stream (tune-in)
            boolean opmlUrlSucceeded = false;
            stationID = StringUtils.substringBetween(currentURI, ":s", "?sid");
            if (opmlUrl != null) {
                String mac = getMACAddress();
                if (stationID != null && !stationID.isEmpty() && mac != null && !mac.isEmpty()) {
                    String url = opmlUrl;
                    url = StringUtils.replace(url, "%id", stationID);
                    url = StringUtils.replace(url, "%serial", mac);
                    String response = null;
                    try {
                        response = HttpUtil.executeUrl("GET", url, SOCKET_TIMEOUT);
                    } catch (IOException e) {
                        logger.debug("Request to device failed: {}", e);
                    }
                    if (response != null) {
                        List<String> fields = SonosXMLParser.getRadioTimeFromXML(response);
                        if (fields != null && fields.size() > 0) {
                            opmlUrlSucceeded = true;
                            resultString = new String();
                            // radio name should be first field
                            title = fields.get(0);
                            Iterator<String> listIterator = fields.listIterator();
                            while (listIterator.hasNext()) {
                                String field = listIterator.next();
                                resultString = resultString + field;
                                if (listIterator.hasNext()) {
                                    resultString = resultString + " - ";
                                }
                            }
                            needsUpdating = true;
                        }
                    }
                }
            }
            if (!opmlUrlSucceeded) {
                if (currentUriMetaData != null) {
                    title = currentUriMetaData.getTitle();
                    if ((currentTrack == null) || (currentTrack.getStreamContent() == null) || currentTrack.getStreamContent().isEmpty()) {
                        resultString = title;
                    } else {
                        resultString = title + " - " + currentTrack.getStreamContent();
                    }
                    needsUpdating = true;
                }
            }
        } else if (isPlayingLineIn(currentURI)) {
            if (currentTrack != null) {
                title = currentTrack.getTitle();
                resultString = title;
                needsUpdating = true;
            }
        } else if (isPlayingRadio(currentURI) || (!currentURI.contains("x-rincon-mp3") && !currentURI.contains("x-sonosapi"))) {
            // isPlayingRadio(currentURI) is true for Google Play Music radio or Apple Music radio
            if (currentTrack != null) {
                artist = !currentTrack.getAlbumArtist().isEmpty() ? currentTrack.getAlbumArtist() : currentTrack.getCreator();
                album = currentTrack.getAlbum();
                title = currentTrack.getTitle();
                resultString = artist + " - " + album + " - " + title;
                needsUpdating = true;
            }
        }
    }
    String albumArtURI = (currentTrack != null && currentTrack.getAlbumArtUri() != null && !currentTrack.getAlbumArtUri().isEmpty()) ? currentTrack.getAlbumArtUri() : "";
    ZonePlayerHandler handlerForImageUpdate = null;
    for (String member : getZoneGroupMembers()) {
        try {
            ZonePlayerHandler memberHandler = getHandlerByName(member);
            if (memberHandler != null && ThingStatus.ONLINE.equals(memberHandler.getThing().getStatus())) {
                if (memberHandler.isLinked(CURRENTALBUMART) && hasValueChanged(albumArtURI, memberHandler.stateMap.get("CurrentAlbumArtURI"))) {
                    handlerForImageUpdate = memberHandler;
                }
                memberHandler.onValueReceived("CurrentTuneInStationId", (stationID != null) ? stationID : "", "AVTransport");
                if (needsUpdating) {
                    memberHandler.onValueReceived("CurrentArtist", (artist != null) ? artist : "", "AVTransport");
                    memberHandler.onValueReceived("CurrentAlbum", (album != null) ? album : "", "AVTransport");
                    memberHandler.onValueReceived("CurrentTitle", (title != null) ? title : "", "AVTransport");
                    memberHandler.onValueReceived("CurrentURIFormatted", (resultString != null) ? resultString : "", "AVTransport");
                    memberHandler.onValueReceived("CurrentAlbumArtURI", albumArtURI, "AVTransport");
                }
            }
        } catch (IllegalStateException e) {
            logger.debug("Cannot update media data for group member ({})", e.getMessage());
        }
    }
    if (needsUpdating && handlerForImageUpdate != null) {
        handlerForImageUpdate.updateAlbumArtChannel(true);
    }
}
Also used : SonosMetaData(org.eclipse.smarthome.binding.sonos.internal.SonosMetaData) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 3 with SonosMetaData

use of org.eclipse.smarthome.binding.sonos.internal.SonosMetaData in project smarthome by eclipse.

the class ZonePlayerHandler method saveState.

/**
 * Save the state (track, position etc) of the Sonos Zone player.
 *
 * @return true if no error occurred.
 */
protected void saveState() {
    synchronized (stateLock) {
        savedState = new SonosZonePlayerState();
        String currentURI = getCurrentURI();
        savedState.transportState = getTransportState();
        savedState.volume = getVolume();
        if (currentURI != null) {
            if (isPlayingStream(currentURI) || isPlayingRadio(currentURI)) {
                // we are streaming music, like tune-in radio or Google Play Music radio
                SonosMetaData track = getTrackMetadata();
                SonosMetaData current = getCurrentURIMetadata();
                if (track != null && current != null) {
                    savedState.entry = new SonosEntry("", current.getTitle(), "", "", track.getAlbumArtUri(), "", current.getUpnpClass(), currentURI);
                }
            } else if (currentURI.contains(GROUP_URI)) {
                // we are a slave to some coordinator
                savedState.entry = new SonosEntry("", "", "", "", "", "", "", currentURI);
            } else if (isPlayingLineIn(currentURI)) {
                // we are streaming from the Line In connection
                savedState.entry = new SonosEntry("", "", "", "", "", "", "", currentURI);
            } else if (isPlayingQueue(currentURI)) {
                // we are playing something that sits in the queue
                SonosMetaData queued = getEnqueuedTransportURIMetaData();
                if (queued != null) {
                    savedState.track = getCurrenTrackNr();
                    if (queued.getUpnpClass().contains("object.container.playlistContainer")) {
                        // we are playing a real 'saved' playlist
                        List<SonosEntry> playLists = getPlayLists();
                        for (SonosEntry someList : playLists) {
                            if (someList.getTitle().equals(queued.getTitle())) {
                                savedState.entry = new SonosEntry(someList.getId(), someList.getTitle(), someList.getParentId(), "", "", "", someList.getUpnpClass(), someList.getRes());
                                break;
                            }
                        }
                    } else if (queued.getUpnpClass().contains("object.container")) {
                        // we are playing some other sort of
                        // 'container' - we will save that to a
                        // playlist for our convenience
                        logger.debug("Save State for a container of type {}", queued.getUpnpClass());
                        // save the playlist
                        String existingList = "";
                        List<SonosEntry> playLists = getPlayLists();
                        for (SonosEntry someList : playLists) {
                            if (someList.getTitle().equals(ESH_PREFIX + getUDN())) {
                                existingList = someList.getId();
                                break;
                            }
                        }
                        saveQueue(ESH_PREFIX + getUDN(), existingList);
                        // get all the playlists and a ref to our
                        // saved list
                        playLists = getPlayLists();
                        for (SonosEntry someList : playLists) {
                            if (someList.getTitle().equals(ESH_PREFIX + getUDN())) {
                                savedState.entry = new SonosEntry(someList.getId(), someList.getTitle(), someList.getParentId(), "", "", "", someList.getUpnpClass(), someList.getRes());
                                break;
                            }
                        }
                    }
                } else {
                    savedState.entry = new SonosEntry("", "", "", "", "", "", "", QUEUE_URI + getUDN() + "#0");
                }
            }
            savedState.relTime = getPosition();
        } else {
            savedState.entry = null;
        }
    }
}
Also used : SonosMetaData(org.eclipse.smarthome.binding.sonos.internal.SonosMetaData) SonosZonePlayerState(org.eclipse.smarthome.binding.sonos.internal.SonosZonePlayerState) SonosEntry(org.eclipse.smarthome.binding.sonos.internal.SonosEntry) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

SonosMetaData (org.eclipse.smarthome.binding.sonos.internal.SonosMetaData)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SonosEntry (org.eclipse.smarthome.binding.sonos.internal.SonosEntry)2 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 SonosZonePlayerState (org.eclipse.smarthome.binding.sonos.internal.SonosZonePlayerState)1