2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.insteon.internal.device;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.insteon.internal.utils.HexUtils;
23 * The {@link ProductData} represents a device product data
25 * @author Jeremy Setton - Initial contribution
28 public class ProductData {
29 public static final int DEVICE_CATEGORY_UNKNOWN = 0xFF;
30 public static final int SUB_CATEGORY_UNKNOWN = 0xFF;
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;
43 public int getDeviceCategory() {
44 return deviceCategory;
47 public int getSubCategory() {
51 public int getProductKey() {
55 public @Nullable String getProductId() {
56 return deviceCategory == DEVICE_CATEGORY_UNKNOWN || subCategory == SUB_CATEGORY_UNKNOWN ? null
57 : HexUtils.getHexString(deviceCategory) + " " + HexUtils.getHexString(subCategory);
60 public @Nullable String getDescription() {
64 public @Nullable String getModel() {
68 public @Nullable String getVendor() {
72 public @Nullable DeviceType getDeviceType() {
73 return DeviceTypeRegistry.getInstance().getDeviceType(deviceType);
76 public int getFirstRecordLocation() {
80 public int getFirmwareVersion() {
84 public int getHardwareVersion() {
88 public @Nullable String getLabel() {
89 List<String> properties = new ArrayList<>();
91 properties.add("" + vendor);
94 properties.add("" + model);
96 if (description != null) {
97 properties.add("" + description);
99 return properties.isEmpty() ? null : String.join(" ", properties);
102 public byte[] getRecordData() {
103 return new byte[] { (byte) deviceCategory, (byte) subCategory, (byte) firmware };
106 public void setDeviceCategory(int deviceCategory) {
107 this.deviceCategory = deviceCategory;
110 public void setSubCategory(int subCategory) {
111 this.subCategory = subCategory;
114 public void setProductKey(int productKey) {
115 this.productKey = productKey;
118 public void setDescription(@Nullable String description) {
119 this.description = description;
122 public void setModel(@Nullable String model) {
126 public void setVendor(@Nullable String vendor) {
127 this.vendor = vendor;
130 public void setDeviceType(@Nullable String deviceType) {
131 this.deviceType = deviceType;
134 public void setDeviceType(DeviceType deviceType) {
135 this.deviceType = deviceType.getName();
138 public void setFirstRecordLocation(int firstRecord) {
139 this.firstRecord = firstRecord;
142 public void setFirmwareVersion(int firmware) {
143 this.firmware = firmware;
146 public void setHardwareVersion(int hardware) {
147 this.hardware = hardware;
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;
157 // update device type if not defined already
158 if (deviceType == null) {
159 deviceType = productData.deviceType;
160 deviceTypeUpdated = productData.deviceType != null;
162 // update remaining properties if defined in given product data
163 if (productData.productKey != 0) {
164 productKey = productData.productKey;
166 if (productData.description != null) {
167 description = productData.description;
169 if (productData.model != null) {
170 model = productData.model;
172 if (productData.vendor != null) {
173 vendor = productData.vendor;
175 if (productData.firstRecord != 0) {
176 firstRecord = productData.firstRecord;
178 if (productData.firmware != 0) {
179 firmware = productData.firmware;
181 if (productData.hardware != 0) {
182 hardware = productData.hardware;
184 return deviceTypeUpdated;
188 public String toString() {
189 List<String> properties = new ArrayList<>();
190 if (deviceCategory != DEVICE_CATEGORY_UNKNOWN) {
191 properties.add("deviceCategory:" + HexUtils.getHexString(deviceCategory));
193 if (subCategory != SUB_CATEGORY_UNKNOWN) {
194 properties.add("subCategory:" + HexUtils.getHexString(subCategory));
196 if (productKey != 0) {
197 properties.add("productKey:" + HexUtils.getHexString(productKey, 6));
199 if (description != null) {
200 properties.add("description:" + description);
203 properties.add("model:" + model);
205 if (vendor != null) {
206 properties.add("vendor:" + vendor);
208 if (deviceType != null) {
209 properties.add("deviceType:" + deviceType);
211 if (firstRecord != 0) {
212 properties.add("firstRecord:" + HexUtils.getHexString(firstRecord));
215 properties.add("firmwareVersion:" + HexUtils.getHexString(firmware));
218 properties.add("hardwareVersion:" + HexUtils.getHexString(hardware));
220 return properties.isEmpty() ? "undefined product data" : String.join("|", properties);
224 * Factory method for creating a ProductData for an Insteon product
226 * @param deviceCategory the Insteon device category
227 * @param subCategory the Insteon device subcategory
228 * @return the product data
230 public static ProductData makeInsteonProduct(int deviceCategory, int subCategory) {
231 ProductData productData = new ProductData();
232 productData.setDeviceCategory(deviceCategory);
233 productData.setSubCategory(subCategory);
238 * Factory method for creating a ProductData for an Insteon product
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
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);
254 * Factory method for creating a ProductData for a X10 product
256 * @param deviceType the X10 device type
257 * @return the product data
259 public static ProductData makeX10Product(String deviceType) {
260 ProductData productData = new ProductData();
261 productData.setDeviceType(deviceType);
262 productData.setDescription(deviceType.replace("_", " "));