]> git.basschouten.com Git - openhab-addons.git/blob
da6253a2c0a85563525bd6edd4b9810ca5415f9d
[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.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.mycroft.internal.MycroftHandler;
20 import org.openhab.binding.mycroft.internal.api.MessageType;
21 import org.openhab.binding.mycroft.internal.api.MycroftMessageListener;
22 import org.openhab.binding.mycroft.internal.api.dto.BaseMessage;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.types.State;
25
26 /**
27  * A helper method for channel handling
28  *
29  * @author Gwendal Roulleau - Initial contribution
30  */
31 @NonNullByDefault
32 public abstract class MycroftChannel<T extends State>
33         implements ChannelCommandHandler, MycroftMessageListener<BaseMessage> {
34
35     private ChannelUID channelUID;
36     protected MycroftHandler handler;
37
38     public MycroftChannel(MycroftHandler handler, String channelUIDPart) {
39         this.handler = handler;
40         this.channelUID = new ChannelUID(handler.getThing().getUID(), channelUIDPart);
41     }
42
43     public final ChannelUID getChannelUID() {
44         return channelUID;
45     }
46
47     protected final void updateMyState(T state) {
48         handler.updateMyChannel(this, state);
49     }
50
51     public final void registerListeners() {
52         for (MessageType messageType : getMessageToListenTo()) {
53             handler.registerMessageListener(messageType, this);
54         }
55     }
56
57     protected List<MessageType> getMessageToListenTo() {
58         return new ArrayList<>();
59     }
60
61     public final void unregisterListeners() {
62         for (MessageType messageType : getMessageToListenTo()) {
63             handler.unregisterMessageListener(messageType, this);
64         }
65     }
66
67     @Override
68     public void messageReceived(BaseMessage message) {
69     }
70 }