Skip to content

Commit

Permalink
Fix online status, implement playuri channel handling and refactor pl… (
Browse files Browse the repository at this point in the history
openhab#1408)

* Fix online status, implement playuri channel handling and refactor play media code.

Signed-off-by: Daniel Walters <[email protected]>
  • Loading branch information
tavalin authored and kaikreuzer committed Nov 11, 2016
1 parent bf84a49 commit 4dd754f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ChromecastBindingConstants {
// channel IDs
public static final String CHANNEL_CONTROL = "control";
public static final String CHANNEL_VOLUME = "volume";
public static final String CHANNEL_PLAY_URI = "playuri";

// config parameters
public static final String HOST = "ipAddress";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.smarthome.core.audio.UnsupportedAudioFormatException;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.library.types.PlayPauseType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
Expand All @@ -47,10 +48,10 @@
*
* @author Markus Rathgeb - Original author
* @author Kai Kreuzer - Initial contribution as openHAB add-on
* @author Daniel Walters - Online status fix, handle playuri channel and refactor play media code
*
*/
public class ChromecastHandler extends BaseThingHandler
implements ChromeCastSpontaneousEventListener, AudioSink {
public class ChromecastHandler extends BaseThingHandler implements ChromeCastSpontaneousEventListener, AudioSink {

private static final String MEDIA_PLAYER = "CC1AD845";

Expand Down Expand Up @@ -114,6 +115,8 @@ private void scheduleConnect(final boolean immediate) {
futureConnect = scheduler.schedule(() -> {
try {
chromecast.connect();
// assume device is online as we no longer get notified
updateStatus(ThingStatus.ONLINE);
} catch (final Exception e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
scheduleConnect(false);
Expand Down Expand Up @@ -166,12 +169,21 @@ public void handleCommand(final ChannelUID channelUID, final Command command) {
case ChromecastBindingConstants.CHANNEL_VOLUME:
handleVolume(command);
break;
case ChromecastBindingConstants.CHANNEL_PLAY_URI:
handlePlayUri(command);
break;
default:
logger.debug("Received command {} for unknown channel: {}", command, channelUID);
break;
}
}

private void handlePlayUri(Command command) {
if (command instanceof StringType) {
playMedia(null, null, command.toString(), null);
}
}

private void handleControl(final Command command) {
try {
if (command instanceof PlayPauseType) {
Expand Down Expand Up @@ -274,26 +286,25 @@ public void process(AudioStream audioStream) throws UnsupportedAudioFormatExcept
return;
}
}
String mimeType = audioStream.getFormat().getCodec() == AudioFormat.CODEC_MP3 ? "audio/mpeg" : "audio/wav";
playMedia("Notification", null, url, mimeType);
}

private void playMedia(String title, String imgUrl, String url, String mimeType) {
try {
final Status status = chromecast.getStatus();
if (chromecast.isAppAvailable(MEDIA_PLAYER)) {
if (!status.isAppRunning(MEDIA_PLAYER)) {
if (!chromecast.isAppRunning(MEDIA_PLAYER)) {
final Application app = chromecast.launchApp(MEDIA_PLAYER);
logger.debug("Application launched: {}", app);
}
if (url == null) {
// stop whatever stream is currently playing
chromecast.pause();
} else {
String mimeType = audioStream.getFormat().getCodec() == AudioFormat.CODEC_MP3 ? "audio/mpeg"
: "audio/wav";
chromecast.load("Notification", null, url.toString(), mimeType);
if (url != null) {
chromecast.load(title, imgUrl, url, mimeType);
}
} else {
logger.error("Missing media player app - cannot process audio stream.");
logger.error("Missing media player app - cannot process media.");
}
} catch (final IOException e) {
logger.debug("Failed playing audio stream: {}", e.getMessage());
logger.debug("Failed playing media: {}", e.getMessage());
}
}

Expand Down

0 comments on commit 4dd754f

Please sign in to comment.