Search in sources :

Example 1 with MPDDatabase

use of org.bff.javampd.MPDDatabase in project openhab1-addons by openhab.

the class MpdBinding method executePlayerCommand.

/**
     * Executes the given <code>playerCommandLine</code> on the MPD. The
     * <code>playerCommandLine</code> is split into its properties
     * <code>playerId</code> and <code>playerCommand</code>.
     *
     * @param playerCommandLine the complete commandLine which gets splitted into
     *            its properties.
     */
private void executePlayerCommand(String playerCommandLine, Object commandParams) {
    String[] commandParts = playerCommandLine.split(":");
    String playerId = commandParts[0];
    String playerCommand = commandParts[1];
    MPD daemon = findMPDInstance(playerId);
    if (daemon == null) {
        // we give that player another chance -> try to reconnect
        reconnect(playerId);
    }
    if (daemon != null) {
        PlayerCommandTypeMapping pCommand = null;
        try {
            pCommand = PlayerCommandTypeMapping.fromString(playerCommand);
            MPDPlayer player = daemon.getMPDPlayer();
            MPDPlaylist playlist = daemon.getMPDPlaylist();
            MPDDatabase db = daemon.getMPDDatabase();
            switch(pCommand) {
                case PAUSE:
                    player.pause();
                    break;
                case PLAY:
                    player.play();
                    break;
                case STOP:
                    player.stop();
                    break;
                case VOLUME_INCREASE:
                    player.setVolume(player.getVolume() + VOLUME_CHANGE_SIZE);
                    break;
                case VOLUME_DECREASE:
                    player.setVolume(player.getVolume() - VOLUME_CHANGE_SIZE);
                    break;
                case NEXT:
                    player.playNext();
                    break;
                case PREV:
                    player.playPrev();
                    break;
                case PLAYSONG:
                    logger.debug("Searching for song {}", commandParams);
                    Collection<MPDSong> songs = db.find(ScopeType.TITLE, (String) commandParams);
                    Iterator<MPDSong> it = songs.iterator();
                    if (it.hasNext() == true) {
                        MPDSong song = it.next();
                        logger.debug("Song found: {}", song.getFile());
                        MPDFile file = new MPDFile();
                        file.setPath(song.getFile());
                        playlist.clearPlaylist();
                        playlist.addFileOrDirectory(file);
                    } else {
                        logger.debug("Song not found: {}", commandParams);
                    }
                    break;
                case PLAYSONGID:
                    logger.debug("Play id {}", ((DecimalType) commandParams).intValue());
                    MPDSong song = new MPDSong();
                    // song.setId(Integer.parseInt((String) commandParams));
                    song.setId(((DecimalType) commandParams).intValue());
                    player.playId(song);
                    break;
                case ENABLE:
                case DISABLE:
                    Integer outputId = Integer.valueOf((String) commandParams);
                    MPDAdmin admin = daemon.getMPDAdmin();
                    // internally mpd uses 0-based indexing
                    MPDOutput output = new MPDOutput(outputId - 1);
                    if (pCommand == PlayerCommandTypeMapping.ENABLE) {
                        admin.enableOutput(output);
                    } else {
                        admin.disableOutput(output);
                    }
                    break;
                case VOLUME:
                    logger.debug("Volume adjustment received: '{}' '{}'", pCommand, commandParams);
                    player.setVolume(((PercentType) commandParams).intValue());
                    break;
            }
        } catch (MPDPlayerException pe) {
            logger.error("Error while executing '{}' command: {}", pCommand, pe.getMessage());
        } catch (Exception e) {
            logger.warn("Unknown player command '{}'", playerCommand);
        }
        logger.info("Executed command '{}' for player '{}'", playerCommand, playerId);
    } else {
        logger.warn("Player '{}' was not found or is not connected", playerId);
    }
}
Also used : MPD(org.bff.javampd.MPD) MPDPlayerException(org.bff.javampd.exception.MPDPlayerException) ConfigurationException(org.osgi.service.cm.ConfigurationException) MPDConnectionException(org.bff.javampd.exception.MPDConnectionException) MPDResponseException(org.bff.javampd.exception.MPDResponseException) JobExecutionException(org.quartz.JobExecutionException) MPDPlayerException(org.bff.javampd.exception.MPDPlayerException) SchedulerException(org.quartz.SchedulerException) UnknownHostException(java.net.UnknownHostException) MPDDatabase(org.bff.javampd.MPDDatabase) MPDOutput(org.bff.javampd.MPDOutput) MPDAdmin(org.bff.javampd.MPDAdmin) MPDPlaylist(org.bff.javampd.MPDPlaylist) MPDPlayer(org.bff.javampd.MPDPlayer) MPDFile(org.bff.javampd.MPDFile) MPDSong(org.bff.javampd.objects.MPDSong)

Aggregations

UnknownHostException (java.net.UnknownHostException)1 MPD (org.bff.javampd.MPD)1 MPDAdmin (org.bff.javampd.MPDAdmin)1 MPDDatabase (org.bff.javampd.MPDDatabase)1 MPDFile (org.bff.javampd.MPDFile)1 MPDOutput (org.bff.javampd.MPDOutput)1 MPDPlayer (org.bff.javampd.MPDPlayer)1 MPDPlaylist (org.bff.javampd.MPDPlaylist)1 MPDConnectionException (org.bff.javampd.exception.MPDConnectionException)1 MPDPlayerException (org.bff.javampd.exception.MPDPlayerException)1 MPDResponseException (org.bff.javampd.exception.MPDResponseException)1 MPDSong (org.bff.javampd.objects.MPDSong)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1 JobExecutionException (org.quartz.JobExecutionException)1 SchedulerException (org.quartz.SchedulerException)1