]> git.basschouten.com Git - openhab-addons.git/blob
f7b6045e1d16501458f594097f0aa4000cbfc4f1
[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.MessageRecognizerLoopUtterance;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.types.Command;
26
27 /**
28  * This channel handle the full utterance send or received by Mycroft, before any intent recognition
29  *
30  * @author Gwendal Roulleau - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 public class UtteranceChannel extends MycroftChannel<StringType> {
35
36     public UtteranceChannel(MycroftHandler handler) {
37         super(handler, MycroftBindingConstants.UTTERANCE_CHANNEL);
38     }
39
40     @Override
41     protected List<MessageType> getMessageToListenTo() {
42         return Arrays.asList(MessageType.recognizer_loop__utterance);
43     }
44
45     @Override
46     public void messageReceived(BaseMessage message) {
47         if (message.type == MessageType.recognizer_loop__utterance) {
48             List<String> utterances = ((MessageRecognizerLoopUtterance) message).data.utterances;
49             if (!utterances.isEmpty()) {
50                 updateMyState(new StringType(utterances.get(0)));
51             }
52         }
53     }
54
55     @Override
56     public void handleCommand(Command command) {
57         if (command instanceof StringType) {
58             if (handler.sendMessage(new MessageRecognizerLoopUtterance(command.toFullString()))) {
59                 updateMyState(new StringType(command.toFullString()));
60             }
61         }
62     }
63 }