]> git.basschouten.com Git - openhab-addons.git/blob
00e06152d1e4a1d95c85dc5af1620263e154168e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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;
17 import java.util.Map.Entry;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.types.Command;
22
23 /**
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.
27  *
28  * @author Daniel Pfrommer - Initial contribution
29  * @author Rob Nielsen - Port to openHAB 2 insteon binding
30  */
31 @NonNullByDefault
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 Map<Integer, HandlerEntry> messageHandlers = new HashMap<>();
41     private Map<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 Map<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 Map<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         HandlerEntry dispatcher = this.dispatcher;
145         if (dispatcher != null) {
146             f.setMessageDispatcher(MessageDispatcher.makeHandler(dispatcher.getName(), dispatcher.getParams(), f));
147         }
148         HandlerEntry pollHandler = this.pollHandler;
149         if (pollHandler != null) {
150             f.setPollHandler(PollHandler.makeHandler(pollHandler, f));
151         }
152         HandlerEntry defaultCmdHandler = this.defaultCmdHandler;
153         if (defaultCmdHandler != null) {
154             CommandHandler h = CommandHandler.makeHandler(defaultCmdHandler.getName(), defaultCmdHandler.getParams(),
155                     f);
156             if (h != null) {
157                 f.setDefaultCommandHandler(h);
158             }
159         }
160         HandlerEntry defaultMsgHandler = this.defaultMsgHandler;
161         if (defaultMsgHandler != null) {
162             MessageHandler h = MessageHandler.makeHandler(defaultMsgHandler.getName(), defaultMsgHandler.getParams(),
163                     f);
164             if (h != null) {
165                 f.setDefaultMsgHandler(h);
166             }
167         }
168         for (Entry<Integer, HandlerEntry> mH : messageHandlers.entrySet()) {
169             f.addMessageHandler(mH.getKey(),
170                     MessageHandler.makeHandler(mH.getValue().getName(), mH.getValue().getParams(), f));
171         }
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));
175         }
176         return f;
177     }
178
179     @Override
180     public String toString() {
181         return getName() + "(" + isStatusFeature() + ")";
182     }
183 }