forked from openhab/openhab1-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New feature ToastControl:Toast and MediaControl:PlayState
- Loading branch information
Sebastian Prehn
committed
Nov 29, 2015
1 parent
aa9596c
commit 71c339f
Showing
5 changed files
with
173 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
.../src/main/java/org/openhab/binding/connectsdk/internal/bridges/MediaControlPlayState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...tsdk/src/main/java/org/openhab/binding/connectsdk/internal/bridges/ToastControlToast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |