Skip to content

Commit

Permalink
Fixes #1186 (#1191)
Browse files Browse the repository at this point in the history
Mark bridge offline on socket end of stream.

Signed-off-by: Mark Hilbush <mark@hilbush.com>
  • Loading branch information
mhilbush authored and kaikreuzer committed Aug 15, 2016
1 parent 36c4f5d commit 6e29eb7
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 200 deletions.
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

0 comments on commit 6e29eb7

Please sign in to comment.