]> git.basschouten.com Git - openhab-addons.git/blob
3f7edf8a16b8f29c36dd5ec00d6019fd2d9ea6f0
[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.easee.internal.handler;
14
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.CHANNEL_TYPEPREFIX_RW;
16
17 import java.util.Map;
18
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;
34
35 /**
36  * public interface of the {@link EaseeThingHandler}. provides some default implementations which can be used by both
37  * BridgeHandlers and ThingHandlers.
38  *
39  * @author Alexander Friese - initial contribution
40  */
41 @NonNullByDefault
42 public interface EaseeThingHandler extends ThingHandler, ChannelProvider {
43
44     /**
45      * just to avoid usage of static loggers.
46      *
47      * @return
48      */
49     Logger getLogger();
50
51     /**
52      * method which updates the channels. needs to be implemented by thing types which have channels.
53      *
54      * @param values key-value list where key is the channel
55      */
56     default void updateChannelStatus(Map<Channel, State> values) {
57         getLogger().debug("updateChannelStatus not implemented/supported by this thing type");
58     }
59
60     /**
61      * return the bridge's configuration
62      *
63      * @return
64      */
65     EaseeConfiguration getBridgeConfiguration();
66
67     /**
68      * passes a new command o the command queue
69      *
70      * @param command to be queued/executed
71      */
72     void enqueueCommand(EaseeCommand command);
73
74     /**
75      * default implementation to handle commands
76      *
77      * @param channelUID
78      * @param command
79      */
80     @Override
81     default void handleCommand(ChannelUID channelUID, Command command) {
82         getLogger().debug("command for {}: {}", channelUID, command);
83
84         if (command instanceof RefreshType) {
85             return;
86         }
87
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);
94             return;
95         }
96
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");
103         }
104
105         if (getThing().getStatus() != ThingStatus.ONLINE) {
106             getLogger().debug("Thing is not online, thus no commands will be handled");
107             return;
108         }
109         enqueueCommand(buildEaseeCommand(command, channel));
110     }
111
112     /**
113      * builds the EaseeCommand which can be send to the webinterface.
114      *
115      * @param command the openhab raw command received from the framework
116      * @param channel the channel which belongs to the command.
117      * @return
118      */
119     default EaseeCommand buildEaseeCommand(Command command, Channel channel) {
120         throw new UnsupportedOperationException("buildEaseeCommand not implemented/supported by this thing type");
121     }
122
123     /**
124      * determines the channel for a given groupId and channelId.
125      *
126      * @param groupId groupId of the channel
127      * @param channelId channelId of the channel
128      */
129     @Override
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));
134     }
135 }