]> git.basschouten.com Git - openhab-addons.git/blob
58f069fd9076892181ba8304c8c7ad1fb559ebbf
[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.ArrayList;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Map.Entry;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * The DeviceType class holds device type definitions that are read from
24  * an xml file.
25  *
26  * @author Bernd Pfrommer - Initial contribution
27  * @author Rob Nielsen - Port to openHAB 2 insteon binding
28  */
29 @NonNullByDefault
30 public class DeviceType {
31     private String productKey;
32     private String model = "";
33     private String description = "";
34     private Map<String, String> features = new HashMap<>();
35     private Map<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 Map<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 Map<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     public static class FeatureGroup {
131         private String name;
132         private String type;
133         private ArrayList<String> fgFeatures = new ArrayList<>();
134
135         FeatureGroup(String name, String type) {
136             this.name = name;
137             this.type = type;
138         }
139
140         public void addFeature(String f) {
141             fgFeatures.add(f);
142         }
143
144         public ArrayList<String> getFeatures() {
145             return fgFeatures;
146         }
147
148         public String getName() {
149             return name;
150         }
151
152         public String getType() {
153             return type;
154         }
155
156         @Override
157         public String toString() {
158             String s = "";
159             for (String g : fgFeatures) {
160                 s += g + ",";
161             }
162             return (s.replaceAll(",$", ""));
163         }
164     }
165 }