]> git.basschouten.com Git - openhab-addons.git/blob
604c175325607e0081003aca97661a41cea83ccf
[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.tapocontrol.internal.helpers.utils;
14
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
16 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
17
18 import java.util.Objects;
19
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;
24
25 /**
26  * {@link TapoUtils} TapoUtils -
27  * Utility Helper Functions
28  *
29  * @author Christian Wild - Initial Initial contribution
30  */
31 @NonNullByDefault
32 public class TapoUtils {
33
34     /************************************
35      * CALCULATION UTILS
36      ***********************************/
37     /**
38      * Limit Value between limits
39      * 
40      * @param value Integer
41      * @param lowerLimit
42      * @param upperLimit
43      * @return
44      */
45     public static Integer limitVal(@Nullable Integer value, Integer lowerLimit, Integer upperLimit) {
46         if (value == null || value < lowerLimit) {
47             return lowerLimit;
48         } else if (value > upperLimit) {
49             return upperLimit;
50         }
51         return value;
52     }
53
54     /************************************
55      * FORMAT UTILS
56      ***********************************/
57     /**
58      * return value or default val if it's null
59      * 
60      * @param <T> Type of value
61      * @param value value
62      * @param defaultValue defaut value
63      * @return
64      */
65     public static <T> T getValueOrDefault(@Nullable T value, T defaultValue) {
66         return value == null ? defaultValue : value;
67     }
68
69     /**
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
72      * 
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
77      * @return
78      */
79     public static <T> T compareValuesAgainstComparator(@Nullable T val1, @Nullable T val2, T comparator) {
80         if (val1 == null && val2 == null) {
81             return comparator;
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);
86         } else {
87             return Objects.requireNonNull(val2);
88         }
89     }
90
91     /**
92      * Format MAC-Address replacing old division chars and add new one
93      * 
94      * @param mac unformated mac-Address
95      * @param newDivisionChar new division char (e.g. ":","-" )
96      * @return new formated mac-Address
97      */
98     public static String formatMac(String mac, char newDivisionChar) {
99         String unformatedMac = unformatMac(mac);
100         return unformatedMac.replaceAll("(.{2})", "$1" + newDivisionChar).substring(0, 17);
101     }
102
103     /**
104      * unformat MAC-Address replace all division chars
105      * 
106      * @param mac string with mac address
107      * @return mac address without any division chars
108      */
109     public static String unformatMac(String mac) {
110         mac = mac.replace("-", "");
111         mac = mac.replace(":", "");
112         mac = mac.replace(".", "");
113         return mac;
114     }
115
116     /**
117      * Get DeviceModel from String - Formats different spellings in model-strings
118      * 
119      * @param device JsonObject with deviceData
120      * @return String with DeviceModel
121      */
122     public static String getDeviceModel(TapoDiscoveryResult device) {
123         return getDeviceModel(device.deviceModel());
124     }
125
126     /**
127      * Get DeviceModel from String - Formats different spellings in model-strings
128      * 
129      * @param deviceModel String to find model from
130      * @return String with DeviceModel
131      */
132     public static String getDeviceModel(String deviceModel) {
133         try {
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);
140             return deviceModel;
141         } catch (Exception e) {
142             return "";
143         }
144     }
145
146     /**
147      * GET DEVICE LABEL
148      * 
149      * @param device JsonObject with deviceData
150      * @return String with DeviceLabel
151      */
152     public static String getDeviceLabel(TapoDiscoveryResult device) {
153         try {
154             String deviceLabel = "";
155             String deviceModel = getDeviceModel(device);
156             String alias = device.alias();
157             ThingTypeUID deviceUID = new ThingTypeUID(BINDING_ID, deviceModel);
158
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;
177             }
178             if (alias.length() > 0) {
179                 return String.format("%s %s %s (%s)", DEVICE_VENDOR, deviceModel, deviceLabel, alias);
180             }
181             return String.format("%s %s %s", DEVICE_VENDOR, deviceModel, deviceLabel);
182         } catch (Exception e) {
183             return "";
184         }
185     }
186 }