2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mycroft.internal.channels;
15 import java.util.Arrays;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.mycroft.internal.MycroftBindingConstants;
20 import org.openhab.binding.mycroft.internal.MycroftHandler;
21 import org.openhab.binding.mycroft.internal.api.MessageType;
22 import org.openhab.binding.mycroft.internal.api.dto.BaseMessage;
23 import org.openhab.binding.mycroft.internal.api.dto.MessageAudioNext;
24 import org.openhab.binding.mycroft.internal.api.dto.MessageAudioPause;
25 import org.openhab.binding.mycroft.internal.api.dto.MessageAudioPlay;
26 import org.openhab.binding.mycroft.internal.api.dto.MessageAudioPrev;
27 import org.openhab.binding.mycroft.internal.api.dto.MessageAudioResume;
28 import org.openhab.core.library.types.NextPreviousType;
29 import org.openhab.core.library.types.PlayPauseType;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.State;
34 * This channel handles the Mycroft capability to act as a music player
35 * (depending on common play music skills installed)
37 * @author Gwendal Roulleau - Initial contribution
40 public class AudioPlayerChannel extends MycroftChannel<State> {
42 public AudioPlayerChannel(MycroftHandler handler) {
43 super(handler, MycroftBindingConstants.PLAYER_CHANNEL);
47 protected List<MessageType> getMessageToListenTo() {
48 return Arrays.asList(MessageType.mycroft_audio_service_prev, MessageType.mycroft_audio_service_next,
49 MessageType.mycroft_audio_service_pause, MessageType.mycroft_audio_service_resume,
50 MessageType.mycroft_audio_service_play, MessageType.mycroft_audio_service_stop,
51 MessageType.mycroft_audio_service_track_info, MessageType.mycroft_audio_service_track_info_reply);
55 public void messageReceived(BaseMessage message) {
56 switch (message.type) {
57 case mycroft_audio_service_pause:
58 case mycroft_audio_service_stop:
59 updateMyState(PlayPauseType.PAUSE);
61 case mycroft_audio_service_play:
62 case mycroft_audio_service_resume:
63 updateMyState(PlayPauseType.PLAY);
71 public void handleCommand(Command command) {
72 if (command instanceof PlayPauseType) {
73 if (((PlayPauseType) command) == PlayPauseType.PAUSE) {
74 if (handler.sendMessage(new MessageAudioPause())) {
75 updateMyState(PlayPauseType.PAUSE);
78 if (((PlayPauseType) command) == PlayPauseType.PLAY) {
79 handler.sendMessage(new MessageAudioPlay());
80 if (handler.sendMessage(new MessageAudioResume())) {
81 updateMyState(PlayPauseType.PLAY);
85 if (command instanceof NextPreviousType) {
86 if (((NextPreviousType) command) == NextPreviousType.NEXT) {
87 if (handler.sendMessage(new MessageAudioNext())) {
88 updateMyState(PlayPauseType.PLAY);
91 if (((NextPreviousType) command) == NextPreviousType.PREVIOUS) {
92 if (handler.sendMessage(new MessageAudioPrev())) {
93 updateMyState(PlayPauseType.PLAY);