use of org.bff.javampd.exception.MPDConnectionException 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);
}
}
use of org.bff.javampd.exception.MPDConnectionException 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());
}
}
Aggregations