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.easee.internal.handler;
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.CHANNEL_TYPEPREFIX_RW;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.easee.internal.Utils;
22 import org.openhab.binding.easee.internal.command.EaseeCommand;
23 import org.openhab.binding.easee.internal.config.EaseeConfiguration;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.ChannelGroupUID;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.openhab.core.types.State;
33 import org.slf4j.Logger;
36 * public interface of the {@link EaseeThingHandler}. provides some default implementations which can be used by both
37 * BridgeHandlers and ThingHandlers.
39 * @author Alexander Friese - initial contribution
42 public interface EaseeThingHandler extends ThingHandler, ChannelProvider {
45 * just to avoid usage of static loggers.
52 * method which updates the channels. needs to be implemented by thing types which have channels.
54 * @param values key-value list where key is the channel
56 default void updateChannelStatus(Map<Channel, State> values) {
57 getLogger().debug("updateChannelStatus not implemented/supported by this thing type");
61 * return the bridge's configuration
65 EaseeConfiguration getBridgeConfiguration();
68 * passes a new command o the command queue
70 * @param command to be queued/executed
72 void enqueueCommand(EaseeCommand command);
75 * default implementation to handle commands
81 default void handleCommand(ChannelUID channelUID, Command command) {
82 getLogger().debug("command for {}: {}", channelUID, command);
84 if (command instanceof RefreshType) {
88 String group = channelUID.getGroupId();
89 group = group == null ? "" : group;
90 Channel channel = getChannel(group, channelUID.getIdWithoutGroup());
91 if (channel == null) {
92 // this should not happen
93 getLogger().warn("channel not found: {}", channelUID);
97 String channelType = Utils.getChannelTypeId(channel);
98 if (!channelType.startsWith(CHANNEL_TYPEPREFIX_RW)) {
99 getLogger().info("channel '{}' does not support write access - value to set '{}'",
100 channelUID.getIdWithoutGroup(), command);
101 throw new UnsupportedOperationException(
102 "channel (" + channelUID.getIdWithoutGroup() + ") does not support write access");
105 if (getThing().getStatus() != ThingStatus.ONLINE) {
106 getLogger().debug("Thing is not online, thus no commands will be handled");
109 enqueueCommand(buildEaseeCommand(command, channel));
113 * builds the EaseeCommand which can be send to the webinterface.
115 * @param command the openhab raw command received from the framework
116 * @param channel the channel which belongs to the command.
119 default EaseeCommand buildEaseeCommand(Command command, Channel channel) {
120 throw new UnsupportedOperationException("buildEaseeCommand not implemented/supported by this thing type");
124 * determines the channel for a given groupId and channelId.
126 * @param groupId groupId of the channel
127 * @param channelId channelId of the channel
130 default @Nullable Channel getChannel(String groupId, String channelId) {
131 ThingUID thingUID = this.getThing().getUID();
132 ChannelGroupUID channelGroupUID = new ChannelGroupUID(thingUID, groupId);
133 return getThing().getChannel(new ChannelUID(channelGroupUID, channelId));