[squeezebox] Map server connection state to thing status. (#10778)

If a player goes offline (it e.g. lost power or network connection),
mark it as such in the thing status.

Signed-off-by: Danny Baumann <dannybaumann@web.de>
This commit is contained in:
maniac103 2021-06-25 13:06:16 +02:00 committed by GitHub
parent 6403e03cea
commit ba2a33947d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 6 deletions

View File

@ -211,4 +211,8 @@ public class SqueezeBoxPlayerDiscoveryParticipant extends AbstractDiscoveryServi
@Override
public void buttonsChangeEvent(String mac, String likeCommand, String unlikeCommand) {
}
@Override
public void connectedStateChangeEvent(String mac, boolean connected) {
}
}

View File

@ -220,4 +220,8 @@ public final class SqueezeBoxNotificationListener implements SqueezeBoxPlayerEve
@Override
public void buttonsChangeEvent(String mac, String likeCommand, String unlikeCommand) {
}
@Override
public void connectedStateChangeEvent(String mac, boolean connected) {
}
}

View File

@ -82,4 +82,6 @@ public interface SqueezeBoxPlayerEventListener {
void sourceChangeEvent(String mac, String source);
void buttonsChangeEvent(String mac, String likeCommand, String unlikeCommand);
void connectedStateChangeEvent(String mac, boolean connected);
}

View File

@ -123,6 +123,7 @@ public class SqueezeBoxPlayerHandler extends BaseThingHandler implements Squeeze
private String likeCommand;
private String unlikeCommand;
private boolean connected = false;
/**
* Creates SqueezeBox Player Handler
@ -141,24 +142,31 @@ public class SqueezeBoxPlayerHandler extends BaseThingHandler implements Squeeze
public void initialize() {
mac = getConfig().as(SqueezeBoxPlayerConfig.class).mac;
timeCounter();
updateBridgeStatus();
updateThingStatus();
logger.debug("player thing {} initialized with mac {}", getThing().getUID(), mac);
if (squeezeBoxServerHandler != null) {
// ensure we get an up-to-date connection state
squeezeBoxServerHandler.requestPlayers();
}
}
@Override
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
updateBridgeStatus();
updateThingStatus();
}
private void updateBridgeStatus() {
private void updateThingStatus() {
Thing bridge = getBridge();
if (bridge != null) {
squeezeBoxServerHandler = (SqueezeBoxServerHandler) bridge.getHandler();
ThingStatus bridgeStatus = bridge.getStatus();
if (bridgeStatus == ThingStatus.ONLINE && getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
} else if (bridgeStatus == ThingStatus.OFFLINE) {
if (bridgeStatus == ThingStatus.OFFLINE) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
} else if (!this.connected) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE);
} else if (bridgeStatus == ThingStatus.ONLINE && getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
}
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge not found");
@ -527,6 +535,14 @@ public class SqueezeBoxPlayerHandler extends BaseThingHandler implements Squeeze
}
}
@Override
public void connectedStateChangeEvent(String mac, boolean connected) {
if (isMe(mac)) {
this.connected = connected;
updateThingStatus();
}
}
@Override
public void updateFavoritesListEvent(List<Favorite> favorites) {
logger.trace("Player {} updating favorites list with {} favorites", mac, favorites.size());

View File

@ -30,6 +30,7 @@ import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -535,6 +536,8 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
}
private void handlePlayersList(String message) {
final Set<String> connectedPlayers = new HashSet<>();
// Split out players
String[] playersList = message.split("playerindex\\S*\\s");
for (String playerParams : playersList) {
@ -571,6 +574,10 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
player.setName(parameter.substring(parameter.indexOf(":") + 1));
} else if (parameter.startsWith("model:")) {
player.setModel(parameter.substring(parameter.indexOf(":") + 1));
} else if (parameter.startsWith("connected:")) {
if ("1".equals(parameter.substring(parameter.indexOf(":") + 1))) {
connectedPlayers.add(macAddress);
}
}
}
@ -587,6 +594,11 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
sendCommand(player.getMacAddress() + " status - 1 subscribe:10 tags:yagJlNKjc");
}
}
for (final SqueezeBoxPlayer player : players.values()) {
final String mac = player.getMacAddress();
final boolean connected = connectedPlayers.contains(mac);
updatePlayer(listener -> listener.connectedStateChangeEvent(mac, connected));
}
}
private void handlePlayerUpdate(String message) {
@ -601,6 +613,9 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
// get the message type
String messageType = messageParts[1];
switch (messageType) {
case "client":
handleClientMessage(mac, messageParts);
break;
case "status":
handleStatusMessage(mac, messageParts);
break;
@ -662,6 +677,26 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
}
}
private void handleClientMessage(final String mac, String[] messageParts) {
if (messageParts.length < 3) {
return;
}
String action = messageParts[2];
final boolean connected;
if ("new".equals(action) || "reconnect".equals(action)) {
connected = true;
} else if ("disconnect".equals(action) || "forget".equals(action)) {
connected = false;
} else {
logger.trace("Unhandled client message type '{}'", Arrays.toString(messageParts));
return;
}
updatePlayer(listener -> listener.connectedStateChangeEvent(mac, connected));
}
private void handleStatusMessage(final String mac, String[] messageParts) {
String remoteTitle = "", artist = "", album = "", genre = "", year = "";
boolean coverart = false;