From 7fa9bcfa835b44fd7499886f713de66a9ad409a0 Mon Sep 17 00:00:00 2001 From: clinique Date: Tue, 5 Apr 2022 15:46:25 +0200 Subject: [PATCH] Reintroducing missing capability to send keys to player. Solving an NPE Signed-off-by: clinique --- .../internal/api/player/PlayerManager.java | 23 +++++++++++++++++++ .../internal/api/rest/FreeboxOsSession.java | 2 +- .../internal/api/rest/RestManager.java | 3 +-- .../internal/handler/FreeDeviceHandler.java | 6 ++--- .../internal/handler/PlayerHandler.java | 19 ++++----------- .../internal/handler/ServerHandler.java | 9 +++++--- 6 files changed, 38 insertions(+), 24 deletions(-) diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/player/PlayerManager.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/player/PlayerManager.java index e2f4668253e2d..3fcca3a78bacc 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/player/PlayerManager.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/player/PlayerManager.java @@ -15,7 +15,11 @@ import java.util.HashMap; import java.util.Map; +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.UriBuilderException; + import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jetty.http.HttpMethod; import org.openhab.binding.freeboxos.internal.api.FreeboxException; import org.openhab.binding.freeboxos.internal.api.login.Session.Permission; import org.openhab.binding.freeboxos.internal.api.player.Player.PlayerResponse; @@ -60,7 +64,26 @@ public DeviceConfig getConfig(int id) throws FreeboxException { throw new FreeboxException("Player config is null"); } + public void sendKey(String ip, String code, String key, boolean longPress, int count) { + UriBuilder uriBuilder = UriBuilder.fromPath("pub").scheme("http").host(ip).path("remote_control"); + uriBuilder.queryParam("code", code).queryParam("key", key); + if (longPress) { + uriBuilder.queryParam("long", true); + } + if (count > 1) { + uriBuilder.queryParam("repeat", count); + } + try { + session.execute(uriBuilder.build(), HttpMethod.GET, GenericResponse.class, null); + } catch (IllegalArgumentException | UriBuilderException e) { + // This call does not answer anything, we can safely ignore + } catch (FreeboxException ignore) { + // This call does not answer anything, we can safely ignore + } + } + public void reboot(int id) throws FreeboxException { post(subPaths.get(id), SYSTEM_SUB_PATH, REBOOT_SUB_PATH); } + } diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeboxOsSession.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeboxOsSession.java index e072f124aa082..2bdd09da94ae3 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeboxOsSession.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeboxOsSession.java @@ -134,7 +134,7 @@ public void closeSession() { } } - > @Nullable F execute(URI uri, HttpMethod method, Class clazz, + public > @Nullable F execute(URI uri, HttpMethod method, Class clazz, @Nullable Object aPayload) throws FreeboxException { boolean retryAuth = sessionToken() != null; return execute(uri, method, clazz, retryAuth, 3, aPayload); diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/RestManager.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/RestManager.java index 074b628cf50a8..39b30610667e2 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/RestManager.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/RestManager.java @@ -71,7 +71,6 @@ private URI buildUri(String... pathElements) { return assemblePath(getUriBuilder(), pathElements).build(); } - @SuppressWarnings("null") protected >> List getList(Class clazz, String... pathElements) throws FreeboxException { // GetList may return null object because API does not return anything for empty lists @@ -110,6 +109,6 @@ protected void post(String... pathElements) throws FreeboxException { return session.execute(buildUri(pathElements), PUT, clazz, payload); } - private class GenericResponse extends Response { + public class GenericResponse extends Response { } } diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeDeviceHandler.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeDeviceHandler.java index 9377a52fdb210..80ddfff1ec7c0 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeDeviceHandler.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeDeviceHandler.java @@ -100,7 +100,7 @@ public void initialize() { super.initialize(); Map properties = editProperties(); String upnpName = properties.get(NameSource.UPNP.name()); - if (upnpName != null && Boolean.parseBoolean(properties.get(MediaType.AUDIO.name()))) { + if (upnpName != null && Boolean.parseBoolean(properties.get(MediaType.AUDIO.name())) && reg == null) { reg = (ServiceRegistration) bundleContext.registerService(AudioSink.class.getName(), new AirMediaSink(this, audioHTTPServer, ohIP, bundleContext, "", upnpName), new Hashtable<>()); } @@ -161,9 +161,9 @@ public void reboot() { try { internalCallReboot(); triggerChannel(new ChannelUID(getThing().getUID(), SYS_INFO, BOX_EVENT), "reboot_requested"); - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.DUTY_CYCLE, "System rebooting, will wait 2 minutes."); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.DUTY_CYCLE, "System rebooting, will wait 3 minutes."); stopRefreshJob(); - scheduler.schedule(this::initialize, 2, TimeUnit.MINUTES); + scheduler.schedule(this::initialize, 3, TimeUnit.MINUTES); } catch (FreeboxException e) { logger.warn("Error rebooting device : {}", e.getMessage()); } diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/PlayerHandler.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/PlayerHandler.java index 840e2564e4108..01a025b76134a 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/PlayerHandler.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/PlayerHandler.java @@ -21,8 +21,6 @@ import java.util.Map; import java.util.Optional; -import javax.ws.rs.core.UriBuilder; - import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.freeboxos.internal.action.PlayerActions; @@ -102,20 +100,11 @@ public void sendKey(String key, boolean longPress, int count) { } else if (VALID_REMOTE_KEYS.contains(aKey)) { String remoteCode = (String) getConfig().get(PlayerConfiguration.REMOTE_CODE); if (remoteCode != null) { - UriBuilder uriBuilder = UriBuilder.fromPath("pub").scheme("http").host(ip).path("remote_control"); - uriBuilder.queryParam("code", remoteCode).queryParam("key", aKey); - if (longPress) { - uriBuilder.queryParam("long", true); - } - if (count > 1) { - uriBuilder.queryParam("repeat", count); + try { + getManager(PlayerManager.class).sendKey(ip, remoteCode, aKey, longPress, count); + } catch (FreeboxException e) { + logger.info("Error sending key", e.getMessage()); } - // try { - // TODO : s Correct this - // getApi().execute(uriBuilder.build(), HttpMethod.GET, null, null, false); - // } catch (FreeboxException e) { - // logger.warn("Error calling Player url : {}", e.getMessage()); - // } } else { logger.warn("A remote code must be configured in the on the player thing."); } diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ServerHandler.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ServerHandler.java index d4cd163a62071..64e938b2a7a34 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ServerHandler.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ServerHandler.java @@ -111,13 +111,16 @@ private void fetchConnectionStatus() throws FreeboxException { QuantityType rateUp = new QuantityType<>(connectionStatus.getRateUp() * 8, Units.BIT_PER_SECOND); updateChannelQuantity(CONNECTION_STATUS, RATE_UP, rateUp, KILOBIT_PER_SECOND); - updateChannelQuantity(CONNECTION_STATUS, PCT_BW_UP, rateUp.multiply(HUNDRED).divide(bandwidthUp), + updateChannelQuantity(CONNECTION_STATUS, PCT_BW_UP, + !bandwidthUp.equals(QuantityType.ZERO) ? rateUp.multiply(HUNDRED).divide(bandwidthUp) + : QuantityType.ZERO, Units.PERCENT); - QuantityType rateDown = new QuantityType<>(connectionStatus.getRateDown() * 8, Units.BIT_PER_SECOND); updateChannelQuantity(CONNECTION_STATUS, RATE_DOWN, rateDown, KILOBIT_PER_SECOND); - updateChannelQuantity(CONNECTION_STATUS, PCT_BW_DOWN, rateDown.multiply(HUNDRED).divide(bandwidthDown), + updateChannelQuantity(CONNECTION_STATUS, PCT_BW_DOWN, + !bandwidthDown.equals(QuantityType.ZERO) ? rateDown.multiply(HUNDRED).divide(bandwidthDown) + : QuantityType.ZERO, Units.PERCENT); updateChannelQuantity(CONNECTION_STATUS, BYTES_UP, new QuantityType<>(connectionStatus.getBytesUp(), OCTET),