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.tapocontrol.internal.helpers.utils;
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
16 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
18 import java.util.Objects;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.tapocontrol.internal.discovery.dto.TapoDiscoveryResult;
23 import org.openhab.core.thing.ThingTypeUID;
26 * {@link TapoUtils} TapoUtils -
27 * Utility Helper Functions
29 * @author Christian Wild - Initial Initial contribution
32 public class TapoUtils {
34 /************************************
36 ***********************************/
38 * Limit Value between limits
40 * @param value Integer
45 public static Integer limitVal(@Nullable Integer value, Integer lowerLimit, Integer upperLimit) {
46 if (value == null || value < lowerLimit) {
48 } else if (value > upperLimit) {
54 /************************************
56 ***********************************/
58 * return value or default val if it's null
60 * @param <T> Type of value
62 * @param defaultValue defaut value
65 public static <T> T getValueOrDefault(@Nullable T value, T defaultValue) {
66 return value == null ? defaultValue : value;
70 * compare tow values against an comparator and return the other one
71 * if both are null, comparator will be returned - if both have values val2 will be returned
73 * @param <T> Type of return value
74 * @param val1 fist value to campare - will be returned if val2 is null or matches comparator
75 * @param val2 second value to compare - will be returned if val1 is null or matches comparator
76 * @param comparator compared values with this
79 public static <T> T compareValuesAgainstComparator(@Nullable T val1, @Nullable T val2, T comparator) {
80 if (val1 == null && val2 == null) {
82 } else if (val1 != null && (val2 == null || val2.equals(comparator))) {
83 return Objects.requireNonNull(val1);
84 } else if (val1 == null || val1.equals(comparator)) {
85 return Objects.requireNonNull(val2);
87 return Objects.requireNonNull(val2);
92 * Format MAC-Address replacing old division chars and add new one
94 * @param mac unformated mac-Address
95 * @param newDivisionChar new division char (e.g. ":","-" )
96 * @return new formated mac-Address
98 public static String formatMac(String mac, char newDivisionChar) {
99 String unformatedMac = unformatMac(mac);
100 return unformatedMac.replaceAll("(.{2})", "$1" + newDivisionChar).substring(0, 17);
104 * unformat MAC-Address replace all division chars
106 * @param mac string with mac address
107 * @return mac address without any division chars
109 public static String unformatMac(String mac) {
110 mac = mac.replace("-", "");
111 mac = mac.replace(":", "");
112 mac = mac.replace(".", "");
117 * Get DeviceModel from String - Formats different spellings in model-strings
119 * @param device JsonObject with deviceData
120 * @return String with DeviceModel
122 public static String getDeviceModel(TapoDiscoveryResult device) {
123 return getDeviceModel(device.deviceModel());
127 * Get DeviceModel from String - Formats different spellings in model-strings
129 * @param deviceModel String to find model from
130 * @return String with DeviceModel
132 public static String getDeviceModel(String deviceModel) {
134 deviceModel = deviceModel.replaceAll("\\(.*\\)", ""); // replace (DE)
135 deviceModel = deviceModel.replace("Tapo", "");
136 deviceModel = deviceModel.replace("Series", "");
137 deviceModel = deviceModel.trim();
138 deviceModel = deviceModel.replace(" ", "_");
139 deviceModel = deviceModel.substring(0, 4);
141 } catch (Exception e) {
149 * @param device JsonObject with deviceData
150 * @return String with DeviceLabel
152 public static String getDeviceLabel(TapoDiscoveryResult device) {
154 String deviceLabel = "";
155 String deviceModel = getDeviceModel(device);
156 String alias = device.alias();
157 ThingTypeUID deviceUID = new ThingTypeUID(BINDING_ID, deviceModel);
159 if (SUPPORTED_HUB_UIDS.contains(deviceUID)) {
160 deviceLabel = DEVICE_DESCRIPTION_HUB;
161 } else if (SUPPORTED_SOCKET_UIDS.contains(deviceUID)) {
162 deviceLabel = DEVICE_DESCRIPTION_SOCKET;
163 } else if (SUPPORTED_SOCKET_STRIP_UIDS.contains(deviceUID)) {
164 deviceLabel = DEVICE_DESCRIPTION_SOCKET_STRIP;
165 } else if (SUPPORTED_WHITE_BULB_UIDS.contains(deviceUID)) {
166 deviceLabel = DEVICE_DESCRIPTION_WHITE_BULB;
167 } else if (SUPPORTED_COLOR_BULB_UIDS.contains(deviceUID)) {
168 deviceLabel = DEVICE_DESCRIPTION_COLOR_BULB;
169 } else if (SUPPORTED_LIGHT_STRIP_UIDS.contains(deviceUID)) {
170 deviceLabel = DEVICE_DESCRIPTION_LIGHTSTRIP;
171 } else if (SUPPORTED_SMART_CONTACTS.contains(deviceUID)) {
172 deviceLabel = DEVICE_DESCRIPTION_SMART_CONTACT;
173 } else if (SUPPORTED_MOTION_SENSORS.contains(deviceUID)) {
174 deviceLabel = DEVICE_DESCRIPTION_MOTION_SENSOR;
175 } else if (SUPPORTED_WHEATHER_SENSORS.contains(deviceUID)) {
176 deviceLabel = DEVICE_DESCRIPTION_TEMP_SENSOR;
178 if (alias.length() > 0) {
179 return String.format("%s %s %s (%s)", DEVICE_VENDOR, deviceModel, deviceLabel, alias);
181 return String.format("%s %s %s", DEVICE_VENDOR, deviceModel, deviceLabel);
182 } catch (Exception e) {