]> git.basschouten.com Git - openhab-addons.git/blob
400fdb6062584cfb5c5cb8eb839b56e7e7af10ad
[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.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.mycroft.internal.MycroftBindingConstants;
21 import org.openhab.binding.mycroft.internal.MycroftHandler;
22 import org.openhab.binding.mycroft.internal.api.MessageType;
23 import org.openhab.binding.mycroft.internal.api.dto.BaseMessage;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.types.Command;
26
27 /**
28  * The channel responsible for sending/receiving raw message
29  *
30  * @author Gwendal Roulleau - Initial contribution
31  */
32 @NonNullByDefault
33 public class FullMessageChannel extends MycroftChannel<StringType> {
34
35     private List<String> messageTypesList = new ArrayList<>();
36
37     public FullMessageChannel(MycroftHandler handler, String messageTypesList) {
38         super(handler, MycroftBindingConstants.FULL_MESSAGE_CHANNEL);
39         for (String messageType : messageTypesList.split(",")) {
40             this.messageTypesList.add(messageType.trim());
41         }
42     }
43
44     @Override
45     public List<MessageType> getMessageToListenTo() {
46         return Arrays.asList(MessageType.any);
47     }
48
49     @Override
50     public void messageReceived(BaseMessage message) {
51         if (messageTypesList.contains(message.type.getMessageTypeName())) {
52             updateMyState(new StringType(message.message));
53         }
54     }
55
56     @Override
57     public void handleCommand(Command command) {
58         if (command instanceof StringType) {
59             if (handler.sendMessage(command.toFullString())) {
60                 updateMyState(new StringType(command.toFullString()));
61             }
62         }
63     }
64 }