Skip to content

Commit

Permalink
Reintroducing missing capability to send keys to player.
Browse files Browse the repository at this point in the history
Solving an NPE

Signed-off-by: clinique <gael@lhopital.org>
  • Loading branch information
clinique committed Apr 5, 2022
1 parent 24df596 commit 7fa9bcf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void closeSession() {
}
}

<F, T extends Response<F>> @Nullable F execute(URI uri, HttpMethod method, Class<T> clazz,
public <F, T extends Response<F>> @Nullable F execute(URI uri, HttpMethod method, Class<T> clazz,
@Nullable Object aPayload) throws FreeboxException {
boolean retryAuth = sessionToken() != null;
return execute(uri, method, clazz, retryAuth, 3, aPayload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ private URI buildUri(String... pathElements) {
return assemblePath(getUriBuilder(), pathElements).build();
}

@SuppressWarnings("null")
protected <F, T extends Response<List<F>>> List<F> getList(Class<T> clazz, String... pathElements)
throws FreeboxException {
// GetList may return null object because API does not return anything for empty lists
Expand Down Expand Up @@ -110,6 +109,6 @@ protected void post(String... pathElements) throws FreeboxException {
return session.execute(buildUri(pathElements), PUT, clazz, payload);
}

private class GenericResponse extends Response<Object> {
public class GenericResponse extends Response<Object> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void initialize() {
super.initialize();
Map<String, String> 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<AudioSink>) bundleContext.registerService(AudioSink.class.getName(),
new AirMediaSink(this, audioHTTPServer, ohIP, bundleContext, "", upnpName), new Hashtable<>());
}
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,16 @@ private void fetchConnectionStatus() throws FreeboxException {
QuantityType<DataTransferRate> 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<DataTransferRate> 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),
Expand Down

0 comments on commit 7fa9bcf

Please sign in to comment.