]> git.basschouten.com Git - openhab-addons.git/blob
358c08328b67044fc854f25a8e6017f758566abc
[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.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.insteon.internal.utils.HexUtils;
21
22 /**
23  * The {@link ProductData} represents a device product data
24  *
25  * @author Jeremy Setton - Initial contribution
26  */
27 @NonNullByDefault
28 public class ProductData {
29     public static final int DEVICE_CATEGORY_UNKNOWN = 0xFF;
30     public static final int SUB_CATEGORY_UNKNOWN = 0xFF;
31
32     private int deviceCategory = DEVICE_CATEGORY_UNKNOWN;
33     private int subCategory = SUB_CATEGORY_UNKNOWN;
34     private int productKey = 0;
35     private @Nullable String description;
36     private @Nullable String model;
37     private @Nullable String vendor;
38     private @Nullable String deviceType;
39     private int firstRecord = 0;
40     private int firmware = 0;
41     private int hardware = 0;
42
43     public int getDeviceCategory() {
44         return deviceCategory;
45     }
46
47     public int getSubCategory() {
48         return subCategory;
49     }
50
51     public int getProductKey() {
52         return productKey;
53     }
54
55     public @Nullable String getProductId() {
56         return deviceCategory == DEVICE_CATEGORY_UNKNOWN || subCategory == SUB_CATEGORY_UNKNOWN ? null
57                 : HexUtils.getHexString(deviceCategory) + " " + HexUtils.getHexString(subCategory);
58     }
59
60     public @Nullable String getDescription() {
61         return description;
62     }
63
64     public @Nullable String getModel() {
65         return model;
66     }
67
68     public @Nullable String getVendor() {
69         return vendor;
70     }
71
72     public @Nullable DeviceType getDeviceType() {
73         return DeviceTypeRegistry.getInstance().getDeviceType(deviceType);
74     }
75
76     public int getFirstRecordLocation() {
77         return firstRecord;
78     }
79
80     public int getFirmwareVersion() {
81         return firmware;
82     }
83
84     public int getHardwareVersion() {
85         return hardware;
86     }
87
88     public @Nullable String getLabel() {
89         List<String> properties = new ArrayList<>();
90         if (vendor != null) {
91             properties.add("" + vendor);
92         }
93         if (model != null) {
94             properties.add("" + model);
95         }
96         if (description != null) {
97             properties.add("" + description);
98         }
99         return properties.isEmpty() ? null : String.join(" ", properties);
100     }
101
102     public byte[] getRecordData() {
103         return new byte[] { (byte) deviceCategory, (byte) subCategory, (byte) firmware };
104     }
105
106     public void setDeviceCategory(int deviceCategory) {
107         this.deviceCategory = deviceCategory;
108     }
109
110     public void setSubCategory(int subCategory) {
111         this.subCategory = subCategory;
112     }
113
114     public void setProductKey(int productKey) {
115         this.productKey = productKey;
116     }
117
118     public void setDescription(@Nullable String description) {
119         this.description = description;
120     }
121
122     public void setModel(@Nullable String model) {
123         this.model = model;
124     }
125
126     public void setVendor(@Nullable String vendor) {
127         this.vendor = vendor;
128     }
129
130     public void setDeviceType(@Nullable String deviceType) {
131         this.deviceType = deviceType;
132     }
133
134     public void setDeviceType(DeviceType deviceType) {
135         this.deviceType = deviceType.getName();
136     }
137
138     public void setFirstRecordLocation(int firstRecord) {
139         this.firstRecord = firstRecord;
140     }
141
142     public void setFirmwareVersion(int firmware) {
143         this.firmware = firmware;
144     }
145
146     public void setHardwareVersion(int hardware) {
147         this.hardware = hardware;
148     }
149
150     public boolean update(ProductData productData) {
151         boolean deviceTypeUpdated = false;
152         // update device and sub category if unknown
153         if (deviceCategory == DEVICE_CATEGORY_UNKNOWN && subCategory == SUB_CATEGORY_UNKNOWN) {
154             deviceCategory = productData.deviceCategory;
155             subCategory = productData.subCategory;
156         }
157         // update device type if not defined already
158         if (deviceType == null) {
159             deviceType = productData.deviceType;
160             deviceTypeUpdated = productData.deviceType != null;
161         }
162         // update remaining properties if defined in given product data
163         if (productData.productKey != 0) {
164             productKey = productData.productKey;
165         }
166         if (productData.description != null) {
167             description = productData.description;
168         }
169         if (productData.model != null) {
170             model = productData.model;
171         }
172         if (productData.vendor != null) {
173             vendor = productData.vendor;
174         }
175         if (productData.firstRecord != 0) {
176             firstRecord = productData.firstRecord;
177         }
178         if (productData.firmware != 0) {
179             firmware = productData.firmware;
180         }
181         if (productData.hardware != 0) {
182             hardware = productData.hardware;
183         }
184         return deviceTypeUpdated;
185     }
186
187     @Override
188     public String toString() {
189         List<String> properties = new ArrayList<>();
190         if (deviceCategory != DEVICE_CATEGORY_UNKNOWN) {
191             properties.add("deviceCategory:" + HexUtils.getHexString(deviceCategory));
192         }
193         if (subCategory != SUB_CATEGORY_UNKNOWN) {
194             properties.add("subCategory:" + HexUtils.getHexString(subCategory));
195         }
196         if (productKey != 0) {
197             properties.add("productKey:" + HexUtils.getHexString(productKey, 6));
198         }
199         if (description != null) {
200             properties.add("description:" + description);
201         }
202         if (model != null) {
203             properties.add("model:" + model);
204         }
205         if (vendor != null) {
206             properties.add("vendor:" + vendor);
207         }
208         if (deviceType != null) {
209             properties.add("deviceType:" + deviceType);
210         }
211         if (firstRecord != 0) {
212             properties.add("firstRecord:" + HexUtils.getHexString(firstRecord));
213         }
214         if (firmware != 0) {
215             properties.add("firmwareVersion:" + HexUtils.getHexString(firmware));
216         }
217         if (hardware != 0) {
218             properties.add("hardwareVersion:" + HexUtils.getHexString(hardware));
219         }
220         return properties.isEmpty() ? "undefined product data" : String.join("|", properties);
221     }
222
223     /**
224      * Factory method for creating a ProductData for an Insteon product
225      *
226      * @param deviceCategory the Insteon device category
227      * @param subCategory the Insteon device subcategory
228      * @return the product data
229      */
230     public static ProductData makeInsteonProduct(int deviceCategory, int subCategory) {
231         ProductData productData = new ProductData();
232         productData.setDeviceCategory(deviceCategory);
233         productData.setSubCategory(subCategory);
234         return productData;
235     }
236
237     /**
238      * Factory method for creating a ProductData for an Insteon product
239      *
240      * @param deviceCategory the Insteon device category
241      * @param subCategory the Insteon device subcategory
242      * @param srcData the source product data to use
243      * @return the product data
244      */
245     public static ProductData makeInsteonProduct(int deviceCategory, int subCategory, @Nullable ProductData srcData) {
246         ProductData productData = makeInsteonProduct(deviceCategory, subCategory);
247         if (srcData != null) {
248             productData.update(srcData);
249         }
250         return productData;
251     }
252
253     /**
254      * Factory method for creating a ProductData for a X10 product
255      *
256      * @param deviceType the X10 device type
257      * @return the product data
258      */
259     public static ProductData makeX10Product(String deviceType) {
260         ProductData productData = new ProductData();
261         productData.setDeviceType(deviceType);
262         productData.setDescription(deviceType.replace("_", " "));
263         return productData;
264     }
265 }