Skip to content

Commit

Permalink
New feature ToastControl:Toast and MediaControl:PlayState
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Prehn committed Nov 29, 2015
1 parent aa9596c commit 71c339f
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openhab.binding.connectsdk.internal.bridges.MediaControlForward;
import org.openhab.binding.connectsdk.internal.bridges.MediaControlPause;
import org.openhab.binding.connectsdk.internal.bridges.MediaControlPlay;
import org.openhab.binding.connectsdk.internal.bridges.MediaControlPlayState;
import org.openhab.binding.connectsdk.internal.bridges.MediaControlRewind;
import org.openhab.binding.connectsdk.internal.bridges.MediaControlStop;
import org.openhab.binding.connectsdk.internal.bridges.OpenhabConnectSDKPropertyBridge;
Expand All @@ -33,6 +34,7 @@
import org.openhab.binding.connectsdk.internal.bridges.TVControlDown;
import org.openhab.binding.connectsdk.internal.bridges.TVControlProgram;
import org.openhab.binding.connectsdk.internal.bridges.TVControlUp;
import org.openhab.binding.connectsdk.internal.bridges.ToastControlToast;
import org.openhab.binding.connectsdk.internal.bridges.VolumeControlDown;
import org.openhab.binding.connectsdk.internal.bridges.VolumeControlMute;
import org.openhab.binding.connectsdk.internal.bridges.VolumeControlUp;
Expand Down Expand Up @@ -68,7 +70,8 @@ public class ConnectSDKBinding extends AbstractBinding<ConnectSDKBindingProvider
new VolumeControlVolume(), new VolumeControlMute(), new VolumeControlUp(), new VolumeControlDown(),
new TVControlChannel(), new TVControlUp(), new TVControlDown(), new VolumeControlUpDown(), new TVControlChannelName(), new TVControlProgram(),
new PowerControlPower(), new ExternalInputControlInput(),
new MediaControlForward(), new MediaControlPause(), new MediaControlPlay(), new MediaControlRewind(), new MediaControlStop()
new MediaControlForward(), new MediaControlPause(), new MediaControlPlay(), new MediaControlRewind(), new MediaControlStop(), new MediaControlPlayState(),
new ToastControlToast()

};

Expand Down Expand Up @@ -265,4 +268,5 @@ public void onError(ServiceCommandError error) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,48 @@
public abstract class AbstractOpenhabConnectSDKPropertyBridge<T> implements OpenhabConnectSDKPropertyBridge {
private static final Logger logger = LoggerFactory.getLogger(AbstractOpenhabConnectSDKPropertyBridge.class);

private Map<String, ServiceSubscription<T>> subscriptions = new ConcurrentHashMap<String, ServiceSubscription<T>>();
private Map<String, ServiceSubscription<T>> subscriptions;

private synchronized Map<String, ServiceSubscription<T>> getSubscriptions() {
if (subscriptions == null) {
subscriptions = new ConcurrentHashMap<String, ServiceSubscription<T>>();
}
return subscriptions;
}

@Override
public final void updateSubscription(final ConnectableDevice device,
final Collection<ConnectSDKBindingProvider> providers, final EventPublisher eventPublisher) { // here
removeAnySubscription(device);

ServiceSubscription<T> listener = getSubscription(device, providers, eventPublisher);
if (listener != null) {
logger.debug("Subscribed {}:{} listener on IP: {}", getItemClass(), getItemProperty(), device.getIpAddress());
subscriptions.put(device.getIpAddress(), listener);
if (listener != null) {
logger.debug("Subscribed {}:{} listener on IP: {}", getItemClass(), getItemProperty(),
device.getIpAddress());
getSubscriptions().put(device.getIpAddress(), listener);
}
}

/**
* Creates a subscription instance for this device.
*
* @param device
* @param providers
* @param eventPublisher
* @return instance or <code>null</code> if no subscription is possible or required
*/
protected abstract ServiceSubscription<T> getSubscription(final ConnectableDevice device, final Collection<ConnectSDKBindingProvider> providers, final EventPublisher eventPublisher);
protected abstract ServiceSubscription<T> getSubscription(final ConnectableDevice device,
final Collection<ConnectSDKBindingProvider> providers, final EventPublisher eventPublisher);

@Override
public final void removeAnySubscription(final ConnectableDevice device) { // here
ServiceSubscription<T> l = subscriptions.remove(device.getIpAddress());
if (l != null) {
l.unsubscribe();
logger.debug("Unsubscribed {}:{} listener on IP: {}", getItemClass(), getItemProperty(),
device.getIpAddress());
public final synchronized void removeAnySubscription(final ConnectableDevice device) { // here
if (subscriptions != null) {
ServiceSubscription<T> l = subscriptions.remove(device.getIpAddress());
if (l != null) {
l.unsubscribe();
logger.debug("Unsubscribed {}:{} listener on IP: {}", getItemClass(), getItemProperty(),
device.getIpAddress());
}
}
}

Expand All @@ -65,19 +77,19 @@ public final void removeAnySubscription(final ConnectableDevice device) { // her
protected boolean matchClassAndProperty(String clazz, String property) {
return getItemClass().equals(clazz) && getItemProperty().equals(property);
}

protected <O> ResponseListener<O> createDefaultResponseListener() {
return new ResponseListener<O>() {

@Override
public void onError(ServiceCommandError error) {
logger.error("Error setting {}:{}: {}.", getItemClass(), getItemProperty(),error.getMessage());
logger.error("Error setting {}:{}: {}.", getItemClass(), getItemProperty(), error.getMessage());

}

@Override
public void onSuccess(O object) {
logger.debug("Successfully set {}:{}: {}.", getItemClass(), getItemProperty(),object);
logger.debug("Successfully set {}:{}: {}.", getItemClass(), getItemProperty(), object);

}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void onReceiveCommand(final ConnectableDevice d, final String clazz, fina
control.getExternalInputList(new ExternalInputListListener() {
@Override
public void onError(ServiceCommandError error) {
logger.error("error requesting channel list: {}.", error.getMessage());
logger.error("error requesting external input list: {}.", error.getMessage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.openhab.binding.connectsdk.internal.bridges;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;

import org.openhab.binding.connectsdk.ConnectSDKBindingProvider;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.library.types.StringType;
import org.openhab.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.connectsdk.device.ConnectableDevice;
import com.connectsdk.service.capability.MediaControl;
import com.connectsdk.service.capability.MediaControl.PlayStateListener;
import com.connectsdk.service.capability.MediaControl.PlayStateStatus;
import com.connectsdk.service.command.ServiceCommandError;
import com.connectsdk.service.command.ServiceSubscription;
// TODO: somehow this does not work with LG 60UF7709 spotify app or netflix
public class MediaControlPlayState extends AbstractOpenhabConnectSDKPropertyBridge<PlayStateListener> {
private static final Logger logger = LoggerFactory.getLogger(MediaControlPlayState.class);
@Override
protected String getItemClass() {
return "MediaControl";
}

@Override
protected String getItemProperty() {
return "playState";
}

private MediaControl getControl(final ConnectableDevice device) {
return device.getCapability(MediaControl.class);
}

@Override
public void onReceiveCommand(final ConnectableDevice d, final String clazz, final String property, Command command) {

}

@Override
protected ServiceSubscription<PlayStateListener> getSubscription(final ConnectableDevice device,
final Collection<ConnectSDKBindingProvider> providers, final EventPublisher eventPublisher) {
if (device.hasCapability(MediaControl.PlayState_Subscribe)) {
return getControl(device).subscribePlayState(new PlayStateListener() {

@Override
public void onError(ServiceCommandError error) {
logger.error("error: {} {} {}", error.getCode(), error.getPayload(), error.getMessage());
}

@Override
public void onSuccess(PlayStateStatus status) {
for (ConnectSDKBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
try {
if (matchClassAndProperty(provider.getClassForItem(itemName),provider.getPropertyForItem(itemName))
&& device.getIpAddress().equals(
InetAddress.getByName(provider.getDeviceForItem(itemName))
.getHostAddress())) {
if (eventPublisher != null) {
eventPublisher.postUpdate(itemName,
new StringType(status.name()));
}
}
} catch (UnknownHostException e) {
logger.error("Failed to resolve {} to IP address. Skipping update on item {}.",
device, itemName);
}
}
}
}
});
} else return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.openhab.binding.connectsdk.internal.bridges;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.Collection;

import javax.imageio.ImageIO;

import org.openhab.binding.connectsdk.ConnectSDKBindingProvider;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.connectsdk.device.ConnectableDevice;
import com.connectsdk.service.capability.ToastControl;
import com.connectsdk.service.command.ServiceSubscription;

public class ToastControlToast extends AbstractOpenhabConnectSDKPropertyBridge<Void> {
private static final Logger logger = LoggerFactory.getLogger(ToastControlToast.class);

@Override
protected String getItemClass() {
return "ToastControl";
}

@Override
protected String getItemProperty() {
return "toast";
}

private ToastControl getControl(final ConnectableDevice device) {
return device.getCapability(ToastControl.class);
}

@Override
public void onReceiveCommand(final ConnectableDevice d, final String clazz, final String property, Command command) {
if (matchClassAndProperty(clazz, property)
&& d.hasCapabilities(ToastControl.Show_Toast)) {
final String value = command.toString();
final ToastControl control = getControl(d);
try {
BufferedImage bi = ImageIO.read(getClass().getResource("/openhab-logo-square.png"));
ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStream b64 = Base64.getEncoder().wrap(os);
ImageIO.write(bi, "png", b64);
control.showToast(value, os.toString("UTF-8"), "png", createDefaultResponseListener() );
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
}
}
}

@Override
protected ServiceSubscription<Void> getSubscription(final ConnectableDevice device,
final Collection<ConnectSDKBindingProvider> providers, final EventPublisher eventPublisher) {
return null;
}

}

0 comments on commit 71c339f

Please sign in to comment.