2 * Copyright (c) 2010-2023 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;
15 import javax.measure.Unit;
16 import javax.measure.quantity.Energy;
17 import javax.measure.quantity.Power;
18 import javax.measure.quantity.Time;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.HSBType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.types.StringType;
29 import com.google.gson.Gson;
30 import com.google.gson.JsonObject;
33 * {@link TapoUtils} TapoUtils -
34 * Utility Helper Functions
36 * @author Christian Wild - Initial Initial contribution
39 public class TapoUtils {
41 /************************************
43 ***********************************/
45 * Limit Value between limits
47 * @param value Integer
52 public static Integer limitVal(@Nullable Integer value, Integer lowerLimit, Integer upperLimit) {
53 if (value == null || value < lowerLimit) {
55 } else if (value > upperLimit) {
61 /************************************
63 ***********************************/
65 * return value or default val if it's null
67 * @param <T> Type of value
69 * @param defaultValue defaut value
72 public static <T> T getValueOrDefault(@Nullable T value, T defaultValue) {
73 return value == null ? defaultValue : value;
77 * Format MAC-Address replacing old division chars and add new one
79 * @param mac unformated mac-Address
80 * @param newDivisionChar new division char (e.g. ":","-" )
81 * @return new formated mac-Address
83 public static String formatMac(String mac, char newDivisionChar) {
84 String unformatedMac = unformatMac(mac);
85 return unformatedMac.replaceAll("(.{2})", "$1" + newDivisionChar).substring(0, 17);
89 * unformat MAC-Address replace all division chars
94 public static String unformatMac(String mac) {
95 mac = mac.replace("-", "");
96 mac = mac.replace(":", "");
97 mac = mac.replace(".", "");
102 * HEX-STRING to byte convertion
104 public static byte[] hexStringToByteArray(String s) {
105 int len = s.length();
106 byte[] data = new byte[len / 2];
108 for (int i = 0; i < len; i += 2) {
109 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
111 } catch (Exception e) {
117 * Return Boolean from string
119 * @param s - string to be converted
120 * @param defVal - Default Value
122 public Boolean stringToBool(@Nullable String s, boolean defVal) {
127 return Boolean.parseBoolean(s);
128 } catch (Exception e) {
134 * Return Integer from string
136 * @param s - string to be converted
137 * @param defVal - Default Value
139 public Integer stringToInteger(@Nullable String s, Integer defVal) {
144 return Integer.valueOf(s);
145 } catch (Exception e) {
150 /***********************************
152 ************************************/
154 public static boolean isValidJson(String json) {
156 Gson gson = new Gson();
157 JsonObject jsnObject = gson.fromJson(json, JsonObject.class);
158 return jsnObject != null;
159 } catch (Exception e) {
166 * @param name parameter name
167 * @param defVal - default value;
168 * @return string value
170 public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name, String defVal) {
171 if (jsonObject != null && jsonObject.has(name)) {
172 return jsonObject.get(name).getAsString();
180 * @param name parameter name
181 * @return string value
183 public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name) {
184 return jsonObjectToString(jsonObject, name, "");
189 * @param name parameter name
190 * @param defVal - default value;
191 * @return boolean value
193 public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name, Boolean defVal) {
194 if (jsonObject != null && jsonObject.has(name)) {
195 return jsonObject.get(name).getAsBoolean();
203 * @param name parameter name
204 * @return boolean value
206 public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name) {
207 return jsonObjectToBool(jsonObject, name, false);
212 * @param name parameter name
213 * @param defVal - default value;
214 * @return integer value
216 public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name, Integer defVal) {
217 if (jsonObject != null && jsonObject.has(name)) {
218 return jsonObject.get(name).getAsInt();
226 * @param name parameter name
227 * @return integer value
229 public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name) {
230 return jsonObjectToInt(jsonObject, name, 0);
235 * @param name parameter name
236 * @param defVal - default value;
237 * @return number value
239 public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name, Number defVal) {
240 if (jsonObject != null && jsonObject.has(name)) {
241 return jsonObject.get(name).getAsNumber();
249 * @param name parameter name
250 * @return number value
252 public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name) {
253 return jsonObjectToNumber(jsonObject, name, 0);
256 /************************************
258 ***********************************/
261 * Return OnOffType from bool
265 public static OnOffType getOnOffType(@Nullable Boolean boolVal) {
266 return boolVal != null ? OnOffType.from(boolVal) : OnOffType.OFF;
270 * Return OnOffType from bool
274 public static OnOffType getOnOffType(Integer intVal) {
275 return OnOffType.from(intVal != 0);
279 * Return StringType from String
283 public static StringType getStringType(@Nullable String strVal) {
284 return new StringType(strVal != null ? strVal : "");
288 * Return DecimalType from Double
292 public static DecimalType getDecimalType(@Nullable Double numVal) {
293 return new DecimalType((numVal != null ? numVal : 0));
297 * Return DecimalType from Integer
301 public static DecimalType getDecimalType(@Nullable Integer numVal) {
302 return new DecimalType((numVal != null ? numVal : 0));
306 * Return DecimalType from Long
310 public static DecimalType getDecimalTypel(@Nullable Long numVal) {
311 return new DecimalType((numVal != null ? numVal : 0));
316 * @param numVal value 0-100
317 * @return PercentType
319 public static PercentType getPercentType(@Nullable Integer numVal) {
320 Integer val = limitVal(numVal, 0, 100);
321 return new PercentType(val);
325 * Return HSBType from integers
327 * @param hue integer hue-color
328 * @param saturation integer saturation
329 * @param brightness integer brightness
332 public static HSBType getHSBType(Integer hue, Integer saturation, Integer brightness) {
333 DecimalType h = new DecimalType(hue);
334 PercentType s = new PercentType(saturation);
335 PercentType b = new PercentType(brightness);
336 return new HSBType(h, s, b);
340 * Return QuantityType with Time
342 * @param numVal Number with value
343 * @param unit TimeUnit ({@code Unit<Time>})
344 * @return {@code QuantityType<Time>}
346 public static QuantityType<Time> getTimeType(@Nullable Number numVal, Unit<Time> unit) {
347 return new QuantityType<>((numVal != null ? numVal : 0), unit);
351 * Return QuantityType with Power
353 * @param numVal Number with value
354 * @param unit PowerUnit ({@code Unit<Power>})
355 * @return {@code QuantityType<Power>}
357 public static QuantityType<Power> getPowerType(@Nullable Number numVal, Unit<Power> unit) {
358 return new QuantityType<>((numVal != null ? numVal : 0), unit);
362 * Return QuantityType with Energy
364 * @param numVal Number with value
365 * @param unit PowerUnit ({@code Unit<Power>})
366 * @return {@code QuantityType<Energy>}
368 public static QuantityType<Energy> getEnergyType(@Nullable Number numVal, Unit<Energy> unit) {
369 return new QuantityType<>((numVal != null ? numVal : 0), unit);