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