]> git.basschouten.com Git - openhab-addons.git/blob
28d55dd039b3bfffbd886cf4664fa6e5dc6ab605
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.insteon.internal.device;
14
15 import java.util.HashMap;
16 import java.util.Map.Entry;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.types.Command;
21
22 /**
23  * A simple class which contains the basic info needed to create a device feature.
24  * Here, all handlers are represented as strings. The actual device feature
25  * is then instantiated from the template by calling the build() function.
26  *
27  * @author Daniel Pfrommer - Initial contribution
28  * @author Rob Nielsen - Port to openHAB 2 insteon binding
29  */
30 @NonNullByDefault
31 @SuppressWarnings("null")
32 public class FeatureTemplate {
33     private String name;
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 HashMap<Integer, HandlerEntry> messageHandlers = new HashMap<>();
41     private HashMap<Class<? extends Command>, HandlerEntry> commandHandlers = new HashMap<>();
42
43     public FeatureTemplate(String name, boolean isStatus, String timeout) {
44         this.name = name;
45         this.isStatus = isStatus;
46         this.timeout = timeout;
47     }
48
49     // simple getters
50     public String getName() {
51         return name;
52     }
53
54     public String getTimeout() {
55         return timeout;
56     }
57
58     public boolean isStatusFeature() {
59         return isStatus;
60     }
61
62     public @Nullable HandlerEntry getPollHandler() {
63         return pollHandler;
64     }
65
66     public @Nullable HandlerEntry getDispatcher() {
67         return dispatcher;
68     }
69
70     public @Nullable HandlerEntry getDefaultCommandHandler() {
71         return defaultCmdHandler;
72     }
73
74     public @Nullable HandlerEntry getDefaultMessageHandler() {
75         return defaultMsgHandler;
76     }
77
78     /**
79      * Retrieves a hashmap of message command code to command handler name
80      *
81      * @return a Hashmap from Integer to String representing the command codes and the associated message handlers
82      */
83     public HashMap<Integer, HandlerEntry> getMessageHandlers() {
84         return messageHandlers;
85     }
86
87     /**
88      * Similar to getMessageHandlers(), but for command handlers
89      * Instead of Integers it uses the class of the Command as a key
90      *
91      * @see #getMessageHandlers()
92      * @return a HashMap from Command Classes to CommandHandler names
93      */
94     public HashMap<Class<? extends Command>, HandlerEntry> getCommandHandlers() {
95         return commandHandlers;
96     }
97
98     // simple setters
99
100     public void setMessageDispatcher(HandlerEntry he) {
101         dispatcher = he;
102     }
103
104     public void setPollHandler(HandlerEntry he) {
105         pollHandler = he;
106     }
107
108     public void setDefaultCommandHandler(HandlerEntry cmd) {
109         defaultCmdHandler = cmd;
110     }
111
112     public void setDefaultMessageHandler(HandlerEntry he) {
113         defaultMsgHandler = he;
114     }
115
116     /**
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
119      *
120      * @param cmd command to be mapped
121      * @param he handler entry to map to
122      */
123     public void addMessageHandler(int cmd, HandlerEntry he) {
124         messageHandlers.put(cmd, he);
125     }
126
127     /**
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
130      */
131     public void addCommandHandler(Class<? extends Command> command, HandlerEntry he) {
132         commandHandlers.put(command, he);
133     }
134
135     /**
136      * Builds the actual feature
137      *
138      * @return the feature which this template describes
139      */
140     public DeviceFeature build() {
141         DeviceFeature f = new DeviceFeature(name);
142         f.setStatusFeature(isStatus);
143         f.setTimeout(timeout);
144         if (dispatcher != null) {
145             f.setMessageDispatcher(MessageDispatcher.makeHandler(dispatcher.getName(), dispatcher.getParams(), f));
146         }
147         if (pollHandler != null) {
148             f.setPollHandler(PollHandler.makeHandler(pollHandler, f));
149         }
150         if (defaultCmdHandler != null) {
151             f.setDefaultCommandHandler(
152                     CommandHandler.makeHandler(defaultCmdHandler.getName(), defaultCmdHandler.getParams(), f));
153         }
154         if (defaultMsgHandler != null) {
155             f.setDefaultMsgHandler(
156                     MessageHandler.makeHandler(defaultMsgHandler.getName(), defaultMsgHandler.getParams(), f));
157         }
158         for (Entry<Integer, HandlerEntry> mH : messageHandlers.entrySet()) {
159             f.addMessageHandler(mH.getKey(),
160                     MessageHandler.makeHandler(mH.getValue().getName(), mH.getValue().getParams(), f));
161         }
162         for (Entry<Class<? extends Command>, HandlerEntry> cH : commandHandlers.entrySet()) {
163             f.addCommandHandler(cH.getKey(),
164                     CommandHandler.makeHandler(cH.getValue().getName(), cH.getValue().getParams(), f));
165         }
166         return f;
167     }
168
169     @Override
170     public String toString() {
171         return getName() + "(" + isStatusFeature() + ")";
172     }
173 }