[Freeboxos] npe when updating inactive Player (#15813)

* Correcting npe
* Added status update

---------

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2023-11-04 10:28:46 +01:00 committed by GitHub
parent 1ca0f643a0
commit c7ce929aa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 20 deletions

View File

@ -197,21 +197,30 @@ public class PlayerManager extends ListableRest<PlayerManager.Player, PlayerMana
public static record TvContext(@Nullable Channel channel, @Nullable PlayerDetails player) { public static record TvContext(@Nullable Channel channel, @Nullable PlayerDetails player) {
} }
private final Map<Integer, String> subPaths = new HashMap<>(); private final Map<Integer, @Nullable String> subPaths = new HashMap<>();
public PlayerManager(FreeboxOsSession session) throws FreeboxException { public PlayerManager(FreeboxOsSession session) throws FreeboxException {
super(session, LoginManager.Permission.PLAYER, PlayerResponse.class, super(session, LoginManager.Permission.PLAYER, PlayerResponse.class,
session.getUriBuilder().path(THING_PLAYER)); session.getUriBuilder().path(THING_PLAYER));
}
private @Nullable String getSubPath(int id) throws FreeboxException {
String subPath = subPaths.get(id);
if (subPath != null) {
return subPath;
}
getDevices().stream().filter(Player::apiAvailable).forEach(player -> { getDevices().stream().filter(Player::apiAvailable).forEach(player -> {
String baseUrl = player.baseUrl(); String baseUrl = player.baseUrl();
if (baseUrl != null) { if (baseUrl != null) {
subPaths.put(player.id, baseUrl); subPaths.put(player.id, baseUrl);
} }
}); });
return subPaths.get(id);
} }
public Status getPlayerStatus(int id) throws FreeboxException { public @Nullable Status getPlayerStatus(int id) throws FreeboxException {
return getSingle(StatusResponse.class, subPaths.get(id), STATUS_PATH); String subPath = getSubPath(id);
return subPath != null ? getSingle(StatusResponse.class, subPath, STATUS_PATH) : null;
} }
// The player API does not allow to directly request a given player like others api parts // The player API does not allow to directly request a given player like others api parts
@ -220,8 +229,9 @@ public class PlayerManager extends ListableRest<PlayerManager.Player, PlayerMana
return getDevices().stream().filter(player -> player.id == id).findFirst().orElse(null); return getDevices().stream().filter(player -> player.id == id).findFirst().orElse(null);
} }
public Configuration getConfig(int id) throws FreeboxException { public @Nullable Configuration getConfig(int id) throws FreeboxException {
return getSingle(ConfigurationResponse.class, subPaths.get(id), SYSTEM_PATH); String subPath = getSubPath(id);
return subPath != null ? getSingle(ConfigurationResponse.class, subPath, SYSTEM_PATH) : null;
} }
public void sendKey(String ip, String code, String key, boolean longPress, int count) { public void sendKey(String ip, String code, String key, boolean longPress, int count) {
@ -240,7 +250,12 @@ public class PlayerManager extends ListableRest<PlayerManager.Player, PlayerMana
} }
} }
public void reboot(int id) throws FreeboxException { public boolean reboot(int id) throws FreeboxException {
post(subPaths.get(id), SYSTEM_PATH, REBOOT_ACTION); String subPath = getSubPath(id);
if (subPath != null) {
post(subPath, SYSTEM_PATH, REBOOT_ACTION);
return true;
}
return false;
} }
} }

View File

@ -35,8 +35,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* The {@link ActivePlayerHandler} is responsible for handling everything associated to Freebox Player with api * The {@link ActivePlayerHandler} is responsible for handling everything associated to Freebox Player
* capabilities. * with api capabilities.
* *
* @author Gaël L'hopital - Initial contribution * @author Gaël L'hopital - Initial contribution
*/ */
@ -59,24 +59,35 @@ public class ActivePlayerHandler extends PlayerHandler implements FreeDeviceIntf
Player player = getManager(PlayerManager.class).getDevice(getClientId()); Player player = getManager(PlayerManager.class).getDevice(getClientId());
if (player.reachable()) { if (player.reachable()) {
Configuration config = getManager(PlayerManager.class).getConfig(player.id()); Configuration config = getManager(PlayerManager.class).getConfig(player.id());
if (config != null) {
properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial()); properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial());
properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion()); properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion());
} }
} }
}
@Override @Override
protected void internalPoll() throws FreeboxException { protected void internalPoll() throws FreeboxException {
super.internalPoll(); super.internalPoll();
if (thing.getStatus().equals(ThingStatus.ONLINE)) { if (thing.getStatus().equals(ThingStatus.ONLINE)) {
Player player = getManager(PlayerManager.class).getDevice(getClientId());
updateStatus(player.reachable() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
if (player.reachable()) {
Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId()); Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId());
if (status != null) {
updateChannelString(PLAYER_STATUS, PLAYER_STATUS, status.powerState().name()); updateChannelString(PLAYER_STATUS, PLAYER_STATUS, status.powerState().name());
ForegroundApp foreground = status.foregroundApp(); ForegroundApp foreground = status.foregroundApp();
if (foreground != null) { if (foreground != null) {
updateChannelString(PLAYER_STATUS, PACKAGE, foreground._package()); updateChannelString(PLAYER_STATUS, PACKAGE, foreground._package());
} }
}
Configuration config = getManager(PlayerManager.class).getConfig(getClientId()); Configuration config = getManager(PlayerManager.class).getConfig(getClientId());
if (config != null) {
uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion()); uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion());
} else {
uptime = 0;
}
}
updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND); updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND);
} }
} }
@ -84,7 +95,9 @@ public class ActivePlayerHandler extends PlayerHandler implements FreeDeviceIntf
public void reboot() { public void reboot() {
processReboot(() -> { processReboot(() -> {
try { try {
getManager(PlayerManager.class).reboot(getClientId()); if (!getManager(PlayerManager.class).reboot(getClientId())) {
logger.warn("Unable to reboot the player - probably not reachable");
}
} catch (FreeboxException e) { } catch (FreeboxException e) {
logger.warn("Error rebooting: {}", e.getMessage()); logger.warn("Error rebooting: {}", e.getMessage());
} }