]> git.basschouten.com Git - openhab-addons.git/blob
47a835a5ec7fe6c7bc879c30ade31aee792f1694
[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.lutron.internal.keypadconfig;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.LinkedList;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.lutron.internal.KeypadComponent;
23 import org.openhab.binding.lutron.internal.discovery.project.ComponentType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Abstract base class for keypad configuration definition classes
29  *
30  * @author Bob Adair - Initial contribution
31  */
32 @NonNullByDefault
33 public abstract class KeypadConfig {
34     private final Logger logger = LoggerFactory.getLogger(KeypadConfig.class);
35
36     protected final HashMap<String, @Nullable List<KeypadComponent>> modelData = new HashMap<>();
37
38     public abstract boolean isCCI(int id);
39
40     public abstract boolean isButton(int id);
41
42     public abstract boolean isLed(int id);
43
44     /**
45      * Get a list of all {@link KeypadComponent}s for the specified keypad model
46      *
47      * @param model The keypad model for which to return components.
48      * @return List of components. Will be empty if no components match.
49      */
50     public List<KeypadComponent> getComponents(String model) {
51         return getComponents(model, null);
52     }
53
54     /**
55      * Get a list of {@link KeypadComponent}s of the specified type for the specified keypad model
56      *
57      * @param model The keypad model for which to return components.
58      * @param type The component type to include, or null for all components.
59      * @return List of components. Will be empty if no components match.
60      */
61     public List<KeypadComponent> getComponents(String model, @Nullable ComponentType type) {
62         List<KeypadComponent> filteredList = new LinkedList<>();
63         List<KeypadComponent> cList = modelData.get(model);
64         if (cList == null) {
65             logger.debug("Keypad components lookup using invalid keypad model: {}", model);
66             return filteredList;
67         } else if (type == null) {
68             return cList;
69         } else {
70             for (KeypadComponent i : cList) {
71                 if (i.type() == type) {
72                     filteredList.add(i);
73                 }
74             }
75             return filteredList;
76         }
77     }
78
79     /**
80      * Get a list of all component IDs for the specified keypad model
81      *
82      * @param model The keypad model for which to return component IDs.
83      * @return List of component IDs. Will be empty if no components match.
84      */
85     public @Nullable List<Integer> getComponentIds(String model) {
86         return getComponentIds(model, null);
87     }
88
89     /**
90      * Get a list of component IDs of the specified type for the specified keypad model
91      *
92      * @param model The keypad model for which to return component IDs.
93      * @param type The component type to include, or null for all components.
94      * @return List of component IDs. Will be empty if no components match.
95      */
96     public List<Integer> getComponentIds(String model, @Nullable ComponentType type) {
97         List<Integer> idList = new LinkedList<>();
98         List<KeypadComponent> cList = modelData.get(model);
99         if (cList == null) {
100             logger.debug("Keypad component IDs lookup using invalid keypad model: {}", model);
101         } else {
102             for (KeypadComponent i : cList) {
103                 if (type == null || i.type() == type) {
104                     idList.add(i.id());
105                 }
106             }
107         }
108         return idList;
109     }
110
111     /**
112      * Determine keypad model from list of button component IDs
113      *
114      * @param buttonIds List of button component IDs for a keypad. Must be in ascending order.
115      * @return String containing the keypad model, or null if no models match.
116      */
117     public @Nullable String determineModelFromComponentIds(List<Integer> buttonIds) {
118         for (String k : modelData.keySet()) {
119             List<Integer> modelButtonIds = getComponentIds(k, ComponentType.BUTTON);
120             Collections.sort(modelButtonIds); // make sure button IDs are in ascending order for comparison
121             if (modelButtonIds.equals(buttonIds)) {
122                 return k;
123             }
124         }
125         return null;
126     }
127
128     /**
129      * Utility routine to concatenate multiple lists of {@link KeypadComponent}s
130      *
131      * @param lists Lists to concatenate
132      * @return Concatenated list
133      */
134     @SafeVarargs
135     protected static final List<KeypadComponent> combinedList(final List<KeypadComponent>... lists) {
136         List<KeypadComponent> newlist = new LinkedList<>();
137         for (List<KeypadComponent> list : lists) {
138             newlist.addAll(list);
139         }
140         return newlist;
141     }
142 }