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 String formatedMac = unformatedMac.replaceAll("(.{2})", "$1" + newDivisionChar).substring(0, 17);
90 * unformat MAC-Address replace all division chars
95 public static String unformatMac(String mac) {
96 mac = mac.replace("-", "");
97 mac = mac.replace(":", "");
98 mac = mac.replace(".", "");
103 * HEX-STRING to byte convertion
105 public static byte[] hexStringToByteArray(String s) {
106 int len = s.length();
107 byte[] data = new byte[len / 2];
109 for (int i = 0; i < len; i += 2) {
110 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
112 } catch (Exception e) {
118 * Return Boolean from string
120 * @param s - string to be converted
121 * @param defVal - Default Value
123 public Boolean stringToBool(@Nullable String s, boolean defVal) {
128 return Boolean.parseBoolean(s);
129 } catch (Exception e) {
135 * Return Integer from string
137 * @param s - string to be converted
138 * @param defVal - Default Value
140 public Integer stringToInteger(@Nullable String s, Integer defVal) {
145 return Integer.valueOf(s);
146 } catch (Exception e) {
151 /***********************************
153 ************************************/
155 public static boolean isValidJson(String json) {
157 Gson gson = new Gson();
158 JsonObject jsnObject = gson.fromJson(json, JsonObject.class);
159 return jsnObject != null;
160 } catch (Exception e) {
167 * @param name parameter name
168 * @param defVal - default value;
169 * @return string value
171 public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name, String defVal) {
172 if (jsonObject != null && jsonObject.has(name)) {
173 return jsonObject.get(name).getAsString();
181 * @param name parameter name
182 * @return string value
184 public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name) {
185 return jsonObjectToString(jsonObject, name, "");
190 * @param name parameter name
191 * @param defVal - default value;
192 * @return boolean value
194 public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name, Boolean defVal) {
195 if (jsonObject != null && jsonObject.has(name)) {
196 return jsonObject.get(name).getAsBoolean();
204 * @param name parameter name
205 * @return boolean value
207 public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name) {
208 return jsonObjectToBool(jsonObject, name, false);
213 * @param name parameter name
214 * @param defVal - default value;
215 * @return integer value
217 public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name, Integer defVal) {
218 if (jsonObject != null && jsonObject.has(name)) {
219 return jsonObject.get(name).getAsInt();
227 * @param name parameter name
228 * @return integer value
230 public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name) {
231 return jsonObjectToInt(jsonObject, name, 0);
236 * @param name parameter name
237 * @param defVal - default value;
238 * @return number value
240 public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name, Number defVal) {
241 if (jsonObject != null && jsonObject.has(name)) {
242 return jsonObject.get(name).getAsNumber();
250 * @param name parameter name
251 * @return number value
253 public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name) {
254 return jsonObjectToNumber(jsonObject, name, 0);
257 /************************************
259 ***********************************/
262 * Return OnOffType from bool
266 public static OnOffType getOnOffType(@Nullable Boolean boolVal) {
267 return (boolVal != null ? boolVal ? OnOffType.ON : OnOffType.OFF : OnOffType.OFF);
271 * Return OnOffType from bool
275 public static OnOffType getOnOffType(Integer intVal) {
276 return intVal == 0 ? OnOffType.OFF : OnOffType.ON;
280 * Return StringType from String
284 public static StringType getStringType(@Nullable String strVal) {
285 return new StringType(strVal != null ? strVal : "");
289 * Return DecimalType from Double
293 public static DecimalType getDecimalType(@Nullable Double numVal) {
294 return new DecimalType((numVal != null ? numVal : 0));
298 * Return DecimalType from Integer
302 public static DecimalType getDecimalType(@Nullable Integer numVal) {
303 return new DecimalType((numVal != null ? numVal : 0));
307 * Return DecimalType from Long
311 public static DecimalType getDecimalTypel(@Nullable Long numVal) {
312 return new DecimalType((numVal != null ? numVal : 0));
317 * @param numVal value 0-100
318 * @return PercentType
320 public static PercentType getPercentType(@Nullable Integer numVal) {
321 Integer val = limitVal(numVal, 0, 100);
322 return new PercentType(val);
326 * Return HSBType from integers
328 * @param hue integer hue-color
329 * @param saturation integer saturation
330 * @param brightness integer brightness
333 public static HSBType getHSBType(Integer hue, Integer saturation, Integer brightness) {
334 DecimalType h = new DecimalType(hue);
335 PercentType s = new PercentType(saturation);
336 PercentType b = new PercentType(brightness);
337 return new HSBType(h, s, b);
341 * Return QuantityType with Time
343 * @param numVal Number with value
344 * @param unit TimeUnit (Unit<Time>)
345 * @return QuantityType<Time>
347 public static QuantityType<Time> getTimeType(@Nullable Number numVal, Unit<Time> unit) {
348 return new QuantityType<>((numVal != null ? numVal : 0), unit);
352 * Return QuantityType with Power
354 * @param numVal Number with value
355 * @param unit PowerUnit (Unit<Power>)
356 * @return QuantityType<Power>
358 public static QuantityType<Power> getPowerType(@Nullable Number numVal, Unit<Power> unit) {
359 return new QuantityType<>((numVal != null ? numVal : 0), unit);
363 * Return QuantityType with Energy
365 * @param numVal Number with value
366 * @param unit PowerUnit (Unit<Power>)
367 * @return QuantityType<Energy>
369 public static QuantityType<Energy> getEnergyType(@Nullable Number numVal, Unit<Energy> unit) {
370 return new QuantityType<>((numVal != null ? numVal : 0), unit);