2 * Copyright (c) 2010-2024 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.insteon.internal.device;
15 import java.util.HashMap;
17 import java.util.Map.Entry;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.types.Command;
24 * A simple class which contains the basic info needed to create a device feature.
25 * Here, all handlers are represented as strings. The actual device feature
26 * is then instantiated from the template by calling the build() function.
28 * @author Daniel Pfrommer - Initial contribution
29 * @author Rob Nielsen - Port to openHAB 2 insteon binding
32 public class FeatureTemplate {
34 private String timeout;
35 private boolean isStatus;
36 private @Nullable HandlerEntry dispatcher = null;
37 private @Nullable HandlerEntry pollHandler = null;
38 private @Nullable HandlerEntry defaultMsgHandler = null;
39 private @Nullable HandlerEntry defaultCmdHandler = null;
40 private Map<Integer, HandlerEntry> messageHandlers = new HashMap<>();
41 private Map<Class<? extends Command>, HandlerEntry> commandHandlers = new HashMap<>();
43 public FeatureTemplate(String name, boolean isStatus, String timeout) {
45 this.isStatus = isStatus;
46 this.timeout = timeout;
50 public String getName() {
54 public String getTimeout() {
58 public boolean isStatusFeature() {
62 public @Nullable HandlerEntry getPollHandler() {
66 public @Nullable HandlerEntry getDispatcher() {
70 public @Nullable HandlerEntry getDefaultCommandHandler() {
71 return defaultCmdHandler;
74 public @Nullable HandlerEntry getDefaultMessageHandler() {
75 return defaultMsgHandler;
79 * Retrieves a hashmap of message command code to command handler name
81 * @return a Hashmap from Integer to String representing the command codes and the associated message handlers
83 public Map<Integer, HandlerEntry> getMessageHandlers() {
84 return messageHandlers;
88 * Similar to getMessageHandlers(), but for command handlers
89 * Instead of Integers it uses the class of the Command as a key
91 * @see #getMessageHandlers()
92 * @return a HashMap from Command Classes to CommandHandler names
94 public Map<Class<? extends Command>, HandlerEntry> getCommandHandlers() {
95 return commandHandlers;
100 public void setMessageDispatcher(HandlerEntry he) {
104 public void setPollHandler(HandlerEntry he) {
108 public void setDefaultCommandHandler(HandlerEntry cmd) {
109 defaultCmdHandler = cmd;
112 public void setDefaultMessageHandler(HandlerEntry he) {
113 defaultMsgHandler = he;
117 * Adds a message handler mapped from the command which this handler should be invoked for
118 * to the name of the handler to be created
120 * @param cmd command to be mapped
121 * @param he handler entry to map to
123 public void addMessageHandler(int cmd, HandlerEntry he) {
124 messageHandlers.put(cmd, he);
128 * Adds a command handler mapped from the command class which this handler should be invoke for
129 * to the name of the handler to be created
131 public void addCommandHandler(Class<? extends Command> command, HandlerEntry he) {
132 commandHandlers.put(command, he);
136 * Builds the actual feature
138 * @return the feature which this template describes
140 public DeviceFeature build() {
141 DeviceFeature f = new DeviceFeature(name);
142 f.setStatusFeature(isStatus);
143 f.setTimeout(timeout);
144 HandlerEntry dispatcher = this.dispatcher;
145 if (dispatcher != null) {
146 f.setMessageDispatcher(MessageDispatcher.makeHandler(dispatcher.getName(), dispatcher.getParams(), f));
148 HandlerEntry pollHandler = this.pollHandler;
149 if (pollHandler != null) {
150 f.setPollHandler(PollHandler.makeHandler(pollHandler, f));
152 HandlerEntry defaultCmdHandler = this.defaultCmdHandler;
153 if (defaultCmdHandler != null) {
154 CommandHandler h = CommandHandler.makeHandler(defaultCmdHandler.getName(), defaultCmdHandler.getParams(),
157 f.setDefaultCommandHandler(h);
160 HandlerEntry defaultMsgHandler = this.defaultMsgHandler;
161 if (defaultMsgHandler != null) {
162 MessageHandler h = MessageHandler.makeHandler(defaultMsgHandler.getName(), defaultMsgHandler.getParams(),
165 f.setDefaultMsgHandler(h);
168 for (Entry<Integer, HandlerEntry> mH : messageHandlers.entrySet()) {
169 f.addMessageHandler(mH.getKey(),
170 MessageHandler.makeHandler(mH.getValue().getName(), mH.getValue().getParams(), f));
172 for (Entry<Class<? extends Command>, HandlerEntry> cH : commandHandlers.entrySet()) {
173 f.addCommandHandler(cH.getKey(),
174 CommandHandler.makeHandler(cH.getValue().getName(), cH.getValue().getParams(), f));
180 public String toString() {
181 return getName() + "(" + isStatusFeature() + ")";