Search in sources :

Example 1 with OutputChangeListener

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

Aggregations

UnknownHostException (java.net.UnknownHostException)1 MPD (org.bff.javampd.MPD)1 OutputChangeEvent (org.bff.javampd.events.OutputChangeEvent)1 OutputChangeListener (org.bff.javampd.events.OutputChangeListener)1 MPDConnectionException (org.bff.javampd.exception.MPDConnectionException)1 MPDStandAloneMonitor (org.bff.javampd.monitor.MPDStandAloneMonitor)1