Search in sources :

Example 1 with MPD

use of org.bff.javampd.MPD 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)

Example 2 with MPD

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

the class MpdBinding method determineSongChange.

private void determineSongChange(String playerId) {
    MPD daemon = findMPDInstance(playerId);
    if (daemon == null) {
        // we give that player another chance -> try to reconnect
        reconnect(playerId);
    }
    if (daemon != null) {
        try {
            MPDPlayer player = daemon.getMPDPlayer();
            // get the song object here
            MPDSong curSong = player.getCurrentSong();
            MPDSong curSongCache = songInfoCache.get(playerId);
            if (!songsEqual(curSong, curSongCache)) {
                // song is different (or not in cache), update cache
                songInfoCache.put(playerId, curSong);
                // action the song change&notification
                songChanged(playerId, curSong);
            }
        } catch (MPDPlayerException pe) {
            logger.error("Error while updating player status for player '{}': {}", playerId, pe.getMessage());
        } catch (Exception e) {
            logger.warn("Failed to communicate with player '{}'", 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) MPDPlayer(org.bff.javampd.MPDPlayer) 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) MPDSong(org.bff.javampd.objects.MPDSong)

Example 3 with MPD

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

the class MpdBinding method determinePlayStateChange.

private void determinePlayStateChange(String playerId) {
    MPD daemon = findMPDInstance(playerId);
    if (daemon == null) {
        // we give that player another chance -> try to reconnect
        reconnect(playerId);
    }
    if (daemon != null) {
        try {
            MPDPlayer player = daemon.getMPDPlayer();
            // get the song object here
            PlayerStatus ps = player.getStatus();
            PlayerStatus curPs = playerStatusCache.get(playerId);
            if (curPs != null) {
                if (ps != curPs) {
                    logger.debug("Play state of player '{}' changed", playerId);
                    playerStatusCache.put(playerId, ps);
                    PlayerCommandTypeMapping reportTo;
                    OnOffType reportStatus;
                    // trigger song update
                    if (ps.equals(PlayerStatus.STATUS_PAUSED) || ps.equals(PlayerStatus.STATUS_STOPPED)) {
                        // stopped
                        reportTo = PlayerCommandTypeMapping.STOP;
                        reportStatus = OnOffType.OFF;
                    } else {
                        // playing
                        reportTo = PlayerCommandTypeMapping.PLAY;
                        reportStatus = OnOffType.ON;
                    }
                    broadcastPlayerStateChange(playerId, reportTo, reportStatus);
                } else {
                // nothing, same state
                }
            } else {
                playerStatusCache.put(playerId, ps);
            }
        } catch (MPDPlayerException pe) {
            logger.error("Error while updating player status for player '{}': {}", playerId, pe.getMessage());
        } catch (Exception e) {
            logger.warn("Failed to communicate with player '{}'", playerId);
        }
    } else {
        logger.warn("Player '{}' was not found or is not connected", playerId);
    }
}
Also used : MPD(org.bff.javampd.MPD) OnOffType(org.openhab.core.library.types.OnOffType) PlayerStatus(org.bff.javampd.MPDPlayer.PlayerStatus) MPDPlayerException(org.bff.javampd.exception.MPDPlayerException) MPDPlayer(org.bff.javampd.MPDPlayer) 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)

Example 4 with MPD

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

the class MpdBinding method connect.

/**
     * Connects the player <code>playerId</code> to the given <code>host</code>
     * and <code>port</code> and registers this binding as MPD-Event listener.
     *
     * @param playerId
     * @param host
     * @param port
     */
private void connect(final String playerId) {
    MpdPlayerConfig config = null;
    try {
        config = playerConfigCache.get(playerId);
        if (config != null && config.instance == null) {
            MPD mpd = new MPD(config.host, config.port, config.password, CONNECTION_TIMEOUT);
            MPDStandAloneMonitor mpdStandAloneMonitor = new MPDStandAloneMonitor(mpd, 500);
            mpdStandAloneMonitor.addVolumeChangeListener(this);
            mpdStandAloneMonitor.addPlayerChangeListener(this);
            mpdStandAloneMonitor.addTrackPositionChangeListener(this);
            // 'this' glue for the inner anon instance
            final MpdBinding self = this;
            mpdStandAloneMonitor.addOutputChangeListener(new OutputChangeListener() {

                @Override
                public void outputChanged(OutputChangeEvent e) {
                    // We have to 'wrap' the OutputChangeEvent listener
                    // callback and add the playerId so we know which
                    // player generated the event. There's not enough
                    // info on just the OutputChangeEvent to derive
                    // the source player. This 'workaround' is necessary
                    // to support output control on multiple MPD players.
                    self.outputChanged(playerId, e);
                }
            });
            Thread monitorThread = new Thread(mpdStandAloneMonitor, "MPD Monitor (player:" + playerId + ")");
            monitorThread.start();
            config.instance = mpd;
            config.monitor = mpdStandAloneMonitor;
            logger.debug("Connected to player '{}' with config {}", playerId, config);
        }
    } catch (MPDConnectionException ce) {
        logger.error("Error connecting to player '{}' with config {}", playerId, config, ce);
    } catch (UnknownHostException uhe) {
        logger.error("Wrong connection details for player '{}'", playerId, uhe);
    }
}
Also used : MPD(org.bff.javampd.MPD) MPDConnectionException(org.bff.javampd.exception.MPDConnectionException) OutputChangeEvent(org.bff.javampd.events.OutputChangeEvent) UnknownHostException(java.net.UnknownHostException) OutputChangeListener(org.bff.javampd.events.OutputChangeListener) MPDStandAloneMonitor(org.bff.javampd.monitor.MPDStandAloneMonitor)

Example 5 with MPD

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

the class MpdBinding method disconnect.

/**
     * Disconnects the player <code>playerId</code>
     *
     * @param playerId the id of the player to disconnect
     */
private void disconnect(String playerId) {
    try {
        MpdPlayerConfig playerConfig = playerConfigCache.get(playerId);
        MPDStandAloneMonitor monitor = playerConfig.monitor;
        if (monitor != null) {
            monitor.stop();
        }
        MPD mpd = playerConfig.instance;
        if (mpd != null) {
            mpd.close();
        }
    } catch (MPDConnectionException ce) {
        logger.warn("Couldn't disconnect player '{}'", playerId);
    } catch (MPDResponseException re) {
        logger.warn("Received response error: {}", re.getLocalizedMessage());
    }
}
Also used : MPD(org.bff.javampd.MPD) MPDConnectionException(org.bff.javampd.exception.MPDConnectionException) MPDStandAloneMonitor(org.bff.javampd.monitor.MPDStandAloneMonitor) MPDResponseException(org.bff.javampd.exception.MPDResponseException)

Aggregations

MPD (org.bff.javampd.MPD)5 MPDConnectionException (org.bff.javampd.exception.MPDConnectionException)5 UnknownHostException (java.net.UnknownHostException)4 MPDResponseException (org.bff.javampd.exception.MPDResponseException)4 MPDPlayer (org.bff.javampd.MPDPlayer)3 MPDPlayerException (org.bff.javampd.exception.MPDPlayerException)3 ConfigurationException (org.osgi.service.cm.ConfigurationException)3 JobExecutionException (org.quartz.JobExecutionException)3 SchedulerException (org.quartz.SchedulerException)3 MPDStandAloneMonitor (org.bff.javampd.monitor.MPDStandAloneMonitor)2 MPDSong (org.bff.javampd.objects.MPDSong)2 MPDAdmin (org.bff.javampd.MPDAdmin)1 MPDDatabase (org.bff.javampd.MPDDatabase)1 MPDFile (org.bff.javampd.MPDFile)1 MPDOutput (org.bff.javampd.MPDOutput)1 PlayerStatus (org.bff.javampd.MPDPlayer.PlayerStatus)1 MPDPlaylist (org.bff.javampd.MPDPlaylist)1 OutputChangeEvent (org.bff.javampd.events.OutputChangeEvent)1 OutputChangeListener (org.bff.javampd.events.OutputChangeListener)1 OnOffType (org.openhab.core.library.types.OnOffType)1