Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[squeezebox] Fixes reconnect, player status, requestPlayerJob cancellation. #1191

Merged
merged 1 commit into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
package org.openhab.binding.squeezebox.discovery;

import static org.openhab.binding.squeezebox.SqueezeBoxBindingConstants.*;
import static org.openhab.binding.squeezebox.SqueezeBoxBindingConstants.SQUEEZEBOXPLAYER_THING_TYPE;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -29,148 +29,160 @@
/**
* When a {@link SqueezeBoxServerHandler} finds a new SqueezeBox Player we will
* add it to the system.
*
*
* @author Dan Cunningham
* @author Mark Hilbush - added method to cancel request player job, and to set thing properties
*
*/
public class SqueezeBoxPlayerDiscoveryParticipant extends AbstractDiscoveryService
implements SqueezeBoxPlayerEventListener {
private final Logger logger = LoggerFactory
.getLogger(SqueezeBoxPlayerDiscoveryParticipant.class);

private final static int TIMEOUT = 60;
private final static int TTL = 60;

private SqueezeBoxServerHandler squeezeBoxServerHandler;
private ScheduledFuture<?> requestPlayerJob;

/**
* Discovers SqueezeBox Players attached to a SqueezeBox Server
*
* @param squeezeBoxServerHandler
*/
public SqueezeBoxPlayerDiscoveryParticipant(
SqueezeBoxServerHandler squeezeBoxServerHandler) {
super(SqueezeBoxPlayerHandler.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
this.squeezeBoxServerHandler = squeezeBoxServerHandler;
setupRequestPlayerJob();
}

@Override
protected void startScan() {
this.squeezeBoxServerHandler.requestPlayers();
}

@Override
protected void startBackgroundDiscovery() {
this.squeezeBoxServerHandler.requestPlayers();
};

@Override
protected void deactivate() {
super.deactivate();
if (requestPlayerJob != null) {
requestPlayerJob.cancel(true);
requestPlayerJob = null;
}
}

@Override
public void playerAdded(SqueezeBoxPlayer player) {
logger.debug("Player added {} {} ", player.getMacAddress(),
player.getName());
ThingUID bridgeUID = squeezeBoxServerHandler.getThing().getUID();
ThingUID thingUID = new ThingUID(SQUEEZEBOXPLAYER_THING_TYPE,
bridgeUID, player.getMacAddress().replace(":", ""));
Map<String, Object> properties = new HashMap<>(1);
properties.put("mac", player.getMacAddress());
DiscoveryResult discoveryResult = DiscoveryResultBuilder
.create(thingUID).withProperties(properties)
.withBridge(bridgeUID)
.withLabel(player.getName()).build();
thingDiscovered(discoveryResult);

}

/**
* Tells the bridge to request a list of players
*/
private void setupRequestPlayerJob() {
Runnable runnable = new Runnable() {
public void run() {
squeezeBoxServerHandler.requestPlayers();
}
};
requestPlayerJob = scheduler.scheduleWithFixedDelay(runnable, 10, TTL,
TimeUnit.SECONDS);
}

// we can ignore the other events
@Override
public void powerChangeEvent(String mac, boolean power) {
}

@Override
public void modeChangeEvent(String mac, String mode) {
}

@Override
public void volumeChangeEvent(String mac, int volume) {
}

@Override
public void muteChangeEvent(String mac, boolean mute) {
}

@Override
public void currentPlaylistIndexEvent(String mac, int index) {
}

@Override
public void currentPlayingTimeEvent(String mac, int time) {
}

@Override
public void numberPlaylistTracksEvent(String mac, int track) {
}

@Override
public void currentPlaylistShuffleEvent(String mac, int shuffle) {
}

@Override
public void currentPlaylistRepeatEvent(String mac, int repeat) {
}

@Override
public void titleChangeEvent(String mac, String title) {
}

@Override
public void albumChangeEvent(String mac, String album) {
}

@Override
public void artistChangeEvent(String mac, String artist) {
}

@Override
public void coverArtChangeEvent(String mac, String coverArtUrl) {
}

@Override
public void yearChangeEvent(String mac, String year) {
}

@Override
public void genreChangeEvent(String mac, String genre) {
}

@Override
public void remoteTitleChangeEvent(String mac, String title) {
}

@Override
public void irCodeChangeEvent(String mac, String ircode) {
}
implements SqueezeBoxPlayerEventListener {
private final Logger logger = LoggerFactory.getLogger(SqueezeBoxPlayerDiscoveryParticipant.class);

private final static int TIMEOUT = 60;
private final static int TTL = 60;

private SqueezeBoxServerHandler squeezeBoxServerHandler;
private ScheduledFuture<?> requestPlayerJob;

/**
* Discovers SqueezeBox Players attached to a SqueezeBox Server
*
* @param squeezeBoxServerHandler
*/
public SqueezeBoxPlayerDiscoveryParticipant(SqueezeBoxServerHandler squeezeBoxServerHandler) {
super(SqueezeBoxPlayerHandler.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
this.squeezeBoxServerHandler = squeezeBoxServerHandler;
setupRequestPlayerJob();
}

@Override
protected void startScan() {
logger.debug("startScan invoked in SqueezeBoxPlayerDiscoveryParticipant");
this.squeezeBoxServerHandler.requestPlayers();
}

/*
* Allows request player job to be canceled when server handler is removed
*/
public void cancelRequestPlayerJob() {
logger.debug("canceling RequestPlayerJob");
if (requestPlayerJob != null) {
requestPlayerJob.cancel(true);
requestPlayerJob = null;
}
}

@Override
public void playerAdded(SqueezeBoxPlayer player) {
ThingUID bridgeUID = squeezeBoxServerHandler.getThing().getUID();

ThingUID thingUID = new ThingUID(SQUEEZEBOXPLAYER_THING_TYPE, bridgeUID,
player.getMacAddress().replace(":", ""));

if (!playerThingExists(thingUID)) {
logger.debug("player added {} : {} ", player.getMacAddress(), player.getName());

Map<String, Object> properties = new HashMap<>(1);
properties.put("mac", player.getMacAddress());

// Added other properties
properties.put("modelId", player.getModel());
properties.put("name", player.getName());
properties.put("uid", player.getUuid());
properties.put("ip", player.getIpAddr());

DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
.withBridge(bridgeUID).withLabel(player.getName()).build();

thingDiscovered(discoveryResult);
}
}

private boolean playerThingExists(ThingUID newThingUID) {
return squeezeBoxServerHandler.getThingByUID(newThingUID) != null ? true : false;
}

/**
* Tells the bridge to request a list of players
*/
private void setupRequestPlayerJob() {
Runnable runnable = new Runnable() {
@Override
public void run() {
squeezeBoxServerHandler.requestPlayers();
}
};

logger.debug("request player job scheduled to run every {} seconds", TTL);
requestPlayerJob = scheduler.scheduleWithFixedDelay(runnable, 10, TTL, TimeUnit.SECONDS);
}

// we can ignore the other events
@Override
public void powerChangeEvent(String mac, boolean power) {
}

@Override
public void modeChangeEvent(String mac, String mode) {
}

@Override
public void volumeChangeEvent(String mac, int volume) {
}

@Override
public void muteChangeEvent(String mac, boolean mute) {
}

@Override
public void currentPlaylistIndexEvent(String mac, int index) {
}

@Override
public void currentPlayingTimeEvent(String mac, int time) {
}

@Override
public void numberPlaylistTracksEvent(String mac, int track) {
}

@Override
public void currentPlaylistShuffleEvent(String mac, int shuffle) {
}

@Override
public void currentPlaylistRepeatEvent(String mac, int repeat) {
}

@Override
public void titleChangeEvent(String mac, String title) {
}

@Override
public void albumChangeEvent(String mac, String album) {
}

@Override
public void artistChangeEvent(String mac, String artist) {
}

@Override
public void coverArtChangeEvent(String mac, String coverArtUrl) {
}

@Override
public void yearChangeEvent(String mac, String year) {
}

@Override
public void genreChangeEvent(String mac, String genre) {
}

@Override
public void remoteTitleChangeEvent(String mac, String title) {
}

@Override
public void irCodeChangeEvent(String mac, String ircode) {
}
}
Loading