Search in sources :

Example 6 with StateVariableValue

use of org.teleal.cling.model.state.StateVariableValue in project openhab1-addons by openhab.

the class SonosZonePlayer method executeActionInvocation.

protected void executeActionInvocation(ActionInvocation invocation) {
    if (invocation != null) {
        new ActionCallback.Default(invocation, upnpService.getControlPoint()).run();
        ActionException anException = invocation.getFailure();
        if (anException != null && anException.getMessage() != null) {
            logger.warn(anException.getMessage());
        }
        Map<String, ActionArgumentValue> result = invocation.getOutputMap();
        Map<String, StateVariableValue> mapToProcess = new HashMap<String, StateVariableValue>();
        if (result != null) {
            // only process the variables that have changed value
            for (String variable : result.keySet()) {
                ActionArgumentValue newArgument = result.get(variable);
                StateVariable newVariable = new StateVariable(variable, new StateVariableTypeDetails(newArgument.getDatatype()));
                StateVariableValue newValue = new StateVariableValue(newVariable, newArgument.getValue());
                if (isUpdatedValue(variable, newValue)) {
                    mapToProcess.put(variable, newValue);
                }
            }
            stateMap.putAll(mapToProcess);
            sonosBinding.processVariableMap(device, mapToProcess);
        }
    }
}
Also used : StateVariableValue(org.teleal.cling.model.state.StateVariableValue) ActionCallback(org.teleal.cling.controlpoint.ActionCallback) HashMap(java.util.HashMap) StateVariable(org.teleal.cling.model.meta.StateVariable) StateVariableTypeDetails(org.teleal.cling.model.meta.StateVariableTypeDetails) ActionException(org.teleal.cling.model.action.ActionException) ActionArgumentValue(org.teleal.cling.model.action.ActionArgumentValue)

Example 7 with StateVariableValue

use of org.teleal.cling.model.state.StateVariableValue in project openhab1-addons by openhab.

the class SonosZonePlayer method updateCurrentURIFormatted.

public boolean updateCurrentURIFormatted() {
    if (stateMap != null && isConfigured()) {
        String currentURI = null;
        SonosMetaData currentURIMetaData = null;
        SonosMetaData currentTrack = null;
        if (!isGroupCoordinator()) {
            currentURI = getCoordinator().getCurrentURI();
            currentURIMetaData = getCoordinator().getCurrentURIMetadata();
            currentTrack = getCoordinator().getTrackMetadata();
        } else {
            currentURI = getCurrentURI();
            currentURIMetaData = getCurrentURIMetadata();
            currentTrack = getTrackMetadata();
        }
        if (currentURI != null) {
            String resultString = null;
            String artist = null;
            String album = null;
            String title = null;
            if (currentURI.contains("x-sonosapi-stream")) {
                // TODO: Get partner ID for openhab.org
                String stationID = StringUtils.substringBetween(currentURI, ":s", "?sid");
                StateVariable newVariable = new StateVariable("StationID", new StateVariableTypeDetails(Datatype.Builtin.STRING.getDatatype()));
                StateVariableValue newValue = new StateVariableValue(newVariable, stationID);
                if (this.isUpdatedValue("StationID", newValue) || lastOPMLQuery == null || lastOPMLQuery.plusMinutes(1).isBeforeNow()) {
                    processStateVariableValue(newVariable.getName(), newValue);
                    String url = "http://opml.radiotime.com/Describe.ashx?c=nowplaying&id=" + stationID + "&partnerId=IAeIhU42&serial=" + getMACAddress();
                    String response = HttpUtil.executeUrl("GET", url, SO_TIMEOUT);
                    // logger.debug("RadioTime Response: {}",response);
                    lastOPMLQuery = DateTime.now();
                    List<String> fields = null;
                    try {
                        fields = SonosXMLParser.getRadioTimeFromXML(response);
                    } catch (SAXException e) {
                        logger.error("Could not parse RadioTime from String {}", response);
                    }
                    resultString = new String();
                    if (fields != null && fields.size() > 1) {
                        artist = fields.get(0);
                        title = fields.get(1);
                        Iterator<String> listIterator = fields.listIterator();
                        while (listIterator.hasNext()) {
                            String field = listIterator.next();
                            resultString = resultString + field;
                            if (listIterator.hasNext()) {
                                resultString = resultString + " - ";
                            }
                        }
                    }
                } else {
                    resultString = stateMap.get("CurrentURIFormatted").getValue().toString();
                    title = stateMap.get("CurrentTitle").getValue().toString();
                    artist = stateMap.get("CurrentArtist").getValue().toString();
                }
            } else {
                if (currentTrack != null) {
                    if (currentTrack.getResource().contains("x-rincon-stream")) {
                        title = currentTrack.getTitle();
                        album = " ";
                        artist = " ";
                        resultString = title;
                    } else if (!currentTrack.getResource().contains("x-sonosapi-stream")) {
                        if (currentTrack.getAlbumArtist().equals("")) {
                            resultString = currentTrack.getCreator() + " - " + currentTrack.getAlbum() + " - " + currentTrack.getTitle();
                            artist = currentTrack.getCreator();
                        } else {
                            resultString = currentTrack.getAlbumArtist() + " - " + currentTrack.getAlbum() + " - " + currentTrack.getTitle();
                            artist = currentTrack.getAlbumArtist();
                        }
                        album = currentTrack.getAlbum();
                        title = currentTrack.getTitle();
                        if (album.equals("")) {
                            album = " ";
                        }
                        if (artist.equals("")) {
                            artist = " ";
                        }
                    }
                } else {
                    title = " ";
                    album = " ";
                    artist = " ";
                    resultString = " ";
                }
            }
            StateVariable newVariable = new StateVariable("CurrentURIFormatted", new StateVariableTypeDetails(Datatype.Builtin.STRING.getDatatype()));
            StateVariableValue newValue = new StateVariableValue(newVariable, resultString);
            processStateVariableValue(newVariable.getName(), newValue);
            // update individual variables
            newVariable = new StateVariable("CurrentArtist", new StateVariableTypeDetails(Datatype.Builtin.STRING.getDatatype()));
            if (artist != null) {
                newValue = new StateVariableValue(newVariable, artist);
            } else {
                newValue = new StateVariableValue(newVariable, " ");
            }
            processStateVariableValue(newVariable.getName(), newValue);
            newVariable = new StateVariable("CurrentTitle", new StateVariableTypeDetails(Datatype.Builtin.STRING.getDatatype()));
            if (title != null) {
                newValue = new StateVariableValue(newVariable, title);
            } else {
                newValue = new StateVariableValue(newVariable, " ");
            }
            processStateVariableValue(newVariable.getName(), newValue);
            newVariable = new StateVariable("CurrentAlbum", new StateVariableTypeDetails(Datatype.Builtin.STRING.getDatatype()));
            if (album != null) {
                newValue = new StateVariableValue(newVariable, album);
            } else {
                newValue = new StateVariableValue(newVariable, " ");
            }
            processStateVariableValue(newVariable.getName(), newValue);
            return true;
        }
    }
    return false;
}
Also used : StateVariableValue(org.teleal.cling.model.state.StateVariableValue) StateVariable(org.teleal.cling.model.meta.StateVariable) StateVariableTypeDetails(org.teleal.cling.model.meta.StateVariableTypeDetails) SAXException(org.xml.sax.SAXException)

Example 8 with StateVariableValue

use of org.teleal.cling.model.state.StateVariableValue in project openhab1-addons by openhab.

the class SonosZonePlayer method playFavorite.

/**
     * This will attempt to match the station string with a entry
     * in the favorites list, this supports both single entries and playlists
     * 
     * @param favorite to match
     * @return true if a match was found and played.
     */
public boolean playFavorite(String station) {
    if (isConfigured()) {
        List<SonosEntry> stations = getFavorites();
        SonosEntry theEntry = null;
        // search for the appropriate favorite based on its name (title)
        for (SonosEntry someStation : stations) {
            if (someStation.getTitle().equals(station)) {
                theEntry = someStation;
                break;
            }
        }
        // set the URI of the group coordinator
        if (theEntry != null) {
            SonosZonePlayer coordinator = sonosBinding.getCoordinatorForZonePlayer(this);
            /**
                 * If this is a playlist we need to treat it as such
                 */
            if (theEntry.getResourceMetaData() != null && theEntry.getResourceMetaData().getUpnpClass().equals("object.container.playlistContainer")) {
                coordinator.removeAllTracksFromQueue();
                coordinator.addURIToQueue(theEntry);
                coordinator.setCurrentURI("x-rincon-queue:" + udn.getIdentifierString() + "#0", "");
                if (stateMap != null && isConfigured()) {
                    StateVariableValue firstTrackNumberEnqueued = stateMap.get("FirstTrackNumberEnqueued");
                    if (firstTrackNumberEnqueued != null) {
                        coordinator.seek("TRACK_NR", firstTrackNumberEnqueued.getValue().toString());
                    }
                }
            } else {
                coordinator.setCurrentURI(theEntry);
            }
            coordinator.play();
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
Also used : StateVariableValue(org.teleal.cling.model.state.StateVariableValue)

Example 9 with StateVariableValue

use of org.teleal.cling.model.state.StateVariableValue in project openhab1-addons by openhab.

the class SonosZonePlayer method getCurrentURIMetadata.

public SonosMetaData getCurrentURIMetadata() {
    if (stateMap != null && isConfigured()) {
        StateVariableValue value = stateMap.get("CurrentURIMetaData");
        SonosMetaData currentTrack = null;
        if (value != null) {
            try {
                if (((String) value.getValue()).length() != 0) {
                    currentTrack = SonosXMLParser.getMetaDataFromXML((String) value.getValue());
                }
            } catch (SAXException e) {
                logger.error("Could not parse MetaData from String {}", value.getValue().toString());
            }
            return currentTrack;
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : StateVariableValue(org.teleal.cling.model.state.StateVariableValue) SAXException(org.xml.sax.SAXException)

Aggregations

StateVariableValue (org.teleal.cling.model.state.StateVariableValue)9 SAXException (org.xml.sax.SAXException)4 StateVariable (org.teleal.cling.model.meta.StateVariable)3 StateVariableTypeDetails (org.teleal.cling.model.meta.StateVariableTypeDetails)3 HashMap (java.util.HashMap)1 IllegalClassException (org.apache.commons.lang.IllegalClassException)1 SonosBindingProvider (org.openhab.binding.sonos.SonosBindingProvider)1 SonosCommandType (org.openhab.binding.sonos.SonosCommandType)1 DecimalType (org.openhab.core.library.types.DecimalType)1 OnOffType (org.openhab.core.library.types.OnOffType)1 StringType (org.openhab.core.library.types.StringType)1 Command (org.openhab.core.types.Command)1 State (org.openhab.core.types.State)1 Type (org.openhab.core.types.Type)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1 UpnpService (org.teleal.cling.UpnpService)1 ActionCallback (org.teleal.cling.controlpoint.ActionCallback)1 ActionArgumentValue (org.teleal.cling.model.action.ActionArgumentValue)1 ActionException (org.teleal.cling.model.action.ActionException)1 ActionInvocation (org.teleal.cling.model.action.ActionInvocation)1