]> git.basschouten.com Git - openhab-addons.git/blob
da2f3fd7b84fa6be50c1c56b456aea2637ecb026
[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.ArrayList;
16 import java.util.HashMap;
17 import java.util.Map.Entry;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20
21 /**
22  * The DeviceType class holds device type definitions that are read from
23  * an xml file.
24  *
25  * @author Bernd Pfrommer - Initial contribution
26  * @author Rob Nielsen - Port to openHAB 2 insteon binding
27  */
28 @NonNullByDefault
29 @SuppressWarnings("null")
30 public class DeviceType {
31     private String productKey;
32     private String model = "";
33     private String description = "";
34     private HashMap<String, String> features = new HashMap<>();
35     private HashMap<String, FeatureGroup> featureGroups = new HashMap<>();
36
37     /**
38      * Constructor
39      *
40      * @param aProductKey the product key for this device type
41      */
42     public DeviceType(String aProductKey) {
43         productKey = aProductKey;
44     }
45
46     /**
47      * Get supported features
48      *
49      * @return all features that this device type supports
50      */
51     public HashMap<String, String> getFeatures() {
52         return features;
53     }
54
55     /**
56      * Get all feature groups
57      *
58      * @return all feature groups of this device type
59      */
60     public HashMap<String, FeatureGroup> getFeatureGroups() {
61         return featureGroups;
62     }
63
64     /**
65      * Sets the descriptive model string
66      *
67      * @param aModel descriptive model string
68      */
69     public void setModel(String aModel) {
70         model = aModel;
71     }
72
73     /**
74      * Sets free text description
75      *
76      * @param aDesc free text description
77      */
78     public void setDescription(String aDesc) {
79         description = aDesc;
80     }
81
82     /**
83      * Adds feature to this device type
84      *
85      * @param aKey the key (e.g. "switch") under which this feature can be referenced in the item binding config
86      * @param aFeatureName the name (e.g. "GenericSwitch") under which the feature has been defined
87      * @return false if feature was already there
88      */
89     public boolean addFeature(String aKey, String aFeatureName) {
90         if (features.containsKey(aKey)) {
91             return false;
92         }
93         features.put(aKey, aFeatureName);
94         return true;
95     }
96
97     /**
98      * Adds feature group to device type
99      *
100      * @param aKey name of the feature group, which acts as key for lookup later
101      * @param fg feature group to add
102      * @return true if add succeeded, false if group was already there
103      */
104     public boolean addFeatureGroup(String aKey, FeatureGroup fg) {
105         if (features.containsKey(aKey)) {
106             return false;
107         }
108         featureGroups.put(aKey, fg);
109         return true;
110     }
111
112     @Override
113     public String toString() {
114         String s = "pk:" + productKey + "|model:" + model + "|desc:" + description + "|features";
115         for (Entry<String, String> f : features.entrySet()) {
116             s += ":" + f.getKey() + "=" + f.getValue();
117         }
118         s += "|groups";
119         for (Entry<String, FeatureGroup> f : featureGroups.entrySet()) {
120             s += ":" + f.getKey() + "=" + f.getValue();
121         }
122         return s;
123     }
124
125     /**
126      * Class that reflects feature group association
127      *
128      * @author Bernd Pfrommer - Initial contribution
129      */
130     @NonNullByDefault
131     public static class FeatureGroup {
132         private String name;
133         private String type;
134         private ArrayList<String> fgFeatures = new ArrayList<>();
135
136         FeatureGroup(String name, String type) {
137             this.name = name;
138             this.type = type;
139         }
140
141         public void addFeature(String f) {
142             fgFeatures.add(f);
143         }
144
145         public ArrayList<String> getFeatures() {
146             return fgFeatures;
147         }
148
149         public String getName() {
150             return name;
151         }
152
153         public String getType() {
154             return type;
155         }
156
157         @Override
158         public String toString() {
159             String s = "";
160             for (String g : fgFeatures) {
161                 s += g + ",";
162             }
163             return (s.replaceAll(",$", ""));
164         }
165     }
166 }