Skip to content

Commit

Permalink
Add some speaker audio commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdgeisler committed Nov 28, 2022
1 parent 6bc0abd commit 4aef367
Showing 1 changed file with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import de.dvdgeisler.iot.dirigera.client.api.http.ClientApi;
import de.dvdgeisler.iot.dirigera.client.api.model.device.Device;
import de.dvdgeisler.iot.dirigera.client.api.model.device.speaker.*;
import de.dvdgeisler.iot.dirigera.client.api.model.music.MusicPlayItem;
import de.dvdgeisler.iot.dirigera.client.api.model.music.MusicPlayList;
import de.dvdgeisler.iot.dirigera.client.api.model.music.MusicPlaybackState;
import reactor.core.publisher.Mono;

Expand All @@ -28,12 +30,20 @@ protected boolean isInstance(final Device<?, ?> device) {

public Mono<SpeakerDevice> mute(final SpeakerDevice device) {
return this.assertCapability(device, "isMuted")
.flatMap(d -> this.setStateAttribute(d, createStateAttribute(Optional.of(true), Optional.empty(), Optional.empty())));
.flatMap(d -> this.setStateAttribute(d, createStateAttribute(
Optional.of(true),
Optional.empty(),
Optional.empty(),
Optional.empty())));
}

public Mono<SpeakerDevice> unmute(final SpeakerDevice device) {
return this.assertCapability(device, "isMuted")
.flatMap(d -> this.setStateAttribute(d, createStateAttribute(Optional.of(false), Optional.empty(), Optional.empty())));
.flatMap(d -> this.setStateAttribute(d, createStateAttribute(
Optional.of(false),
Optional.empty(),
Optional.empty(),
Optional.empty())));
}

public Mono<SpeakerDevice> setVolume(final SpeakerDevice device, final Integer volume) {
Expand All @@ -42,12 +52,20 @@ public Mono<SpeakerDevice> setVolume(final SpeakerDevice device, final Integer v
return Mono.error(new IllegalArgumentException("Volume must be between 0 and 100"));

return this.assertCapability(device, "volume")
.flatMap(d -> this.setStateAttribute(d, createStateAttribute(Optional.empty(), Optional.of(volume), Optional.empty())));
.flatMap(d -> this.setStateAttribute(d, createStateAttribute(
Optional.empty(),
Optional.of(volume),
Optional.empty(),
Optional.empty())));
}

public Mono<SpeakerDevice> setPlayback(final SpeakerDevice device, final MusicPlaybackState playbackState) {
return this.assertCapability(device, "playback")
.flatMap(d -> this.setStateAttribute(d, createStateAttribute(Optional.empty(), Optional.empty(), Optional.of(playbackState))));
.flatMap(d -> this.setStateAttribute(d, createStateAttribute(
Optional.empty(),
Optional.empty(),
Optional.of(playbackState),
Optional.empty())));
}

public Mono<SpeakerDevice> play(final SpeakerDevice device) {
Expand All @@ -66,14 +84,43 @@ public Mono<SpeakerDevice> previous(final SpeakerDevice device) {
return this.setPlayback(device, PREVIOUS);
}

private SpeakerStateAttributes createStateAttribute(final Optional<Boolean> isMuted, final Optional<Integer> volume, final Optional<MusicPlaybackState> playback) {
final SpeakerStateAttributes attributes = new SpeakerStateAttributes();
public Mono<SpeakerDevice> setAudio(final SpeakerDevice device, final SpeakerAudioAttributes audio) {
return this.assertCapability(device, "playbackAudio")
.flatMap(d -> this.setStateAttribute(d, createStateAttribute(Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(audio))));
}

isMuted.ifPresent(b -> attributes.isMuted = b);
public Mono<SpeakerDevice> setAudio(final SpeakerDevice device, final MusicPlayItem playItem) {
final SpeakerAudioAttributes audio;

volume.ifPresent(i -> attributes.volume = i);
audio = new SpeakerAudioAttributes();
audio.playItem = playItem;
return this.setAudio(device, audio);
}

public Mono<SpeakerDevice> setNextAudio(final SpeakerDevice device, final MusicPlayItem playItem) {
final SpeakerAudioAttributes audio;

audio = new SpeakerAudioAttributes();
audio.nextPlayItem = playItem;
return this.setAudio(device, audio);
}

public Mono<SpeakerDevice> setNextAudio(final SpeakerDevice device, final MusicPlayList playList) {
final SpeakerAudioAttributes audio;

audio = new SpeakerAudioAttributes();
audio.playList = playList;
return this.setAudio(device, audio);
}

private SpeakerStateAttributes createStateAttribute(final Optional<Boolean> isMuted, final Optional<Integer> volume, final Optional<MusicPlaybackState> playback, final Optional<SpeakerAudioAttributes> audio) {
final SpeakerStateAttributes attributes;

attributes = new SpeakerStateAttributes();
isMuted.ifPresent(b -> attributes.isMuted = b);
volume.ifPresent(i -> attributes.volume = i);
playback.ifPresent(p -> attributes.playback = p);
audio.ifPresent(a -> attributes.playbackAudio = a);

return attributes;
}
Expand Down

0 comments on commit 4aef367

Please sign in to comment.