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.ArrayList;
16 import java.util.List;
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;
27 * A helper method for channel handling
29 * @author Gwendal Roulleau - Initial contribution
32 public abstract class MycroftChannel<T extends State>
33 implements ChannelCommandHandler, MycroftMessageListener<BaseMessage> {
35 private ChannelUID channelUID;
36 protected MycroftHandler handler;
38 public MycroftChannel(MycroftHandler handler, String channelUIDPart) {
39 this.handler = handler;
40 this.channelUID = new ChannelUID(handler.getThing().getUID(), channelUIDPart);
43 public final ChannelUID getChannelUID() {
47 protected final void updateMyState(T state) {
48 handler.updateMyChannel(this, state);
51 public final void registerListeners() {
52 for (MessageType messageType : getMessageToListenTo()) {
53 handler.registerMessageListener(messageType, this);
57 protected List<MessageType> getMessageToListenTo() {
58 return new ArrayList<>();
61 public final void unregisterListeners() {
62 for (MessageType messageType : getMessageToListenTo()) {
63 handler.unregisterMessageListener(messageType, this);
68 public void messageReceived(BaseMessage message) {