diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/PlayerManager.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/PlayerManager.java index 0326c1e0ff8b4..31256516c750d 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/PlayerManager.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/PlayerManager.java @@ -197,21 +197,30 @@ private static record Channel(@SerializedName("bouquetId") long bouquetId, public static record TvContext(@Nullable Channel channel, @Nullable PlayerDetails player) { } - private final Map subPaths = new HashMap<>(); + private final Map subPaths = new HashMap<>(); public PlayerManager(FreeboxOsSession session) throws FreeboxException { super(session, LoginManager.Permission.PLAYER, PlayerResponse.class, 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 -> { String baseUrl = player.baseUrl(); if (baseUrl != null) { subPaths.put(player.id, baseUrl); } }); + return subPaths.get(id); } - public Status getPlayerStatus(int id) throws FreeboxException { - return getSingle(StatusResponse.class, subPaths.get(id), STATUS_PATH); + public @Nullable Status getPlayerStatus(int id) throws FreeboxException { + 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 @@ -220,8 +229,9 @@ public Player getDevice(int id) throws FreeboxException { return getDevices().stream().filter(player -> player.id == id).findFirst().orElse(null); } - public Configuration getConfig(int id) throws FreeboxException { - return getSingle(ConfigurationResponse.class, subPaths.get(id), SYSTEM_PATH); + public @Nullable Configuration getConfig(int id) throws FreeboxException { + 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) { @@ -240,7 +250,12 @@ public void sendKey(String ip, String code, String key, boolean longPress, int c } } - public void reboot(int id) throws FreeboxException { - post(subPaths.get(id), SYSTEM_PATH, REBOOT_ACTION); + public boolean reboot(int id) throws FreeboxException { + String subPath = getSubPath(id); + if (subPath != null) { + post(subPath, SYSTEM_PATH, REBOOT_ACTION); + return true; + } + return false; } } diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ActivePlayerHandler.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ActivePlayerHandler.java index 52c8063664735..63a02eb4b64dc 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ActivePlayerHandler.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ActivePlayerHandler.java @@ -35,8 +35,8 @@ import org.slf4j.LoggerFactory; /** - * The {@link ActivePlayerHandler} is responsible for handling everything associated to Freebox Player with api - * capabilities. + * The {@link ActivePlayerHandler} is responsible for handling everything associated to Freebox Player + * with api capabilities. * * @author Gaƫl L'hopital - Initial contribution */ @@ -59,8 +59,10 @@ void initializeProperties(Map properties) throws FreeboxExceptio Player player = getManager(PlayerManager.class).getDevice(getClientId()); if (player.reachable()) { Configuration config = getManager(PlayerManager.class).getConfig(player.id()); - properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial()); - properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion()); + if (config != null) { + properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.serial()); + properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.firmwareVersion()); + } } } @@ -68,15 +70,24 @@ void initializeProperties(Map properties) throws FreeboxExceptio protected void internalPoll() throws FreeboxException { super.internalPoll(); if (thing.getStatus().equals(ThingStatus.ONLINE)) { - Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId()); - updateChannelString(PLAYER_STATUS, PLAYER_STATUS, status.powerState().name()); - ForegroundApp foreground = status.foregroundApp(); - if (foreground != null) { - updateChannelString(PLAYER_STATUS, PACKAGE, foreground._package()); + Player player = getManager(PlayerManager.class).getDevice(getClientId()); + updateStatus(player.reachable() ? ThingStatus.ONLINE : ThingStatus.OFFLINE); + if (player.reachable()) { + Status status = getManager(PlayerManager.class).getPlayerStatus(getClientId()); + if (status != null) { + updateChannelString(PLAYER_STATUS, PLAYER_STATUS, status.powerState().name()); + ForegroundApp foreground = status.foregroundApp(); + if (foreground != null) { + updateChannelString(PLAYER_STATUS, PACKAGE, foreground._package()); + } + } + Configuration config = getManager(PlayerManager.class).getConfig(getClientId()); + if (config != null) { + uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion()); + } else { + uptime = 0; + } } - Configuration config = getManager(PlayerManager.class).getConfig(getClientId()); - - uptime = checkUptimeAndFirmware(config.uptimeVal(), uptime, config.firmwareVersion()); updateChannelQuantity(SYS_INFO, UPTIME, uptime, Units.SECOND); } } @@ -84,7 +95,9 @@ protected void internalPoll() throws FreeboxException { public void reboot() { processReboot(() -> { 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) { logger.warn("Error rebooting: {}", e.getMessage()); }