]> git.basschouten.com Git - openhab-addons.git/blob
be3a8e62b45426531c767ca3a62acd7229584275
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mycroft.internal.channels;
14
15 import java.util.Arrays;
16 import java.util.List;
17
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;
32
33 /**
34  * This channel handles the Mycroft capability to act as a music player
35  * (depending on common play music skills installed)
36  *
37  * @author Gwendal Roulleau - Initial contribution
38  */
39 @NonNullByDefault
40 public class AudioPlayerChannel extends MycroftChannel<State> {
41
42     public AudioPlayerChannel(MycroftHandler handler) {
43         super(handler, MycroftBindingConstants.PLAYER_CHANNEL);
44     }
45
46     @Override
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);
52     }
53
54     @Override
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);
60                 break;
61             case mycroft_audio_service_play:
62             case mycroft_audio_service_resume:
63                 updateMyState(PlayPauseType.PLAY);
64                 break;
65             default:
66                 break;
67         }
68     }
69
70     @Override
71     public void handleCommand(Command command) {
72         if (command instanceof PlayPauseType playPauseCommand) {
73             if (playPauseCommand == PlayPauseType.PAUSE) {
74                 if (handler.sendMessage(new MessageAudioPause())) {
75                     updateMyState(PlayPauseType.PAUSE);
76                 }
77             }
78             if (playPauseCommand == PlayPauseType.PLAY) {
79                 handler.sendMessage(new MessageAudioPlay());
80                 if (handler.sendMessage(new MessageAudioResume())) {
81                     updateMyState(PlayPauseType.PLAY);
82                 }
83             }
84         }
85         if (command instanceof NextPreviousType nextPreviousCommand) {
86             if (nextPreviousCommand == NextPreviousType.NEXT) {
87                 if (handler.sendMessage(new MessageAudioNext())) {
88                     updateMyState(PlayPauseType.PLAY);
89                 }
90             }
91             if (nextPreviousCommand == NextPreviousType.PREVIOUS) {
92                 if (handler.sendMessage(new MessageAudioPrev())) {
93                     updateMyState(PlayPauseType.PLAY);
94                 }
95             }
96         }
97     }
98 }