2 * Copyright (c) 2010-2022 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.Time;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.HSBType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.PercentType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.types.StringType;
27 import com.google.gson.Gson;
28 import com.google.gson.JsonObject;
31 * {@link TapoUtils} TapoUtils -
32 * Utility Helper Functions
34 * @author Christian Wild - Initial Initial contribution
37 public class TapoUtils {
39 /************************************
41 ***********************************/
43 * Limit Value between limits
45 * @param value Integer
50 public static Integer limitVal(@Nullable Integer value, Integer lowerLimit, Integer upperLimit) {
51 if (value == null || value < lowerLimit) {
53 } else if (value > upperLimit) {
59 /************************************
61 ***********************************/
63 * return value or default val if it's null
65 * @param <T> Type of value
67 * @param defaultValue defaut value
70 public static <T> T getValueOrDefault(@Nullable T value, T defaultValue) {
71 return value == null ? defaultValue : value;
75 * Format MAC-Address replacing old division chars and add new one
77 * @param mac unformated mac-Address
78 * @param newDivisionChar new division char (e.g. ":","-" )
79 * @return new formated mac-Address
81 public static String formatMac(String mac, char newDivisionChar) {
82 String unformatedMac = unformatMac(mac);
83 String formatedMac = unformatedMac.replaceAll("(.{2})", "$1" + newDivisionChar).substring(0, 17);
88 * unformat MAC-Address replace all division chars
93 public static String unformatMac(String mac) {
94 mac = mac.replace("-", "");
95 mac = mac.replace(":", "");
96 mac = mac.replace(".", "");
101 * HEX-STRING to byte convertion
103 public static byte[] hexStringToByteArray(String s) {
104 int len = s.length();
105 byte[] data = new byte[len / 2];
107 for (int i = 0; i < len; i += 2) {
108 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
110 } catch (Exception e) {
116 * Return Boolean from string
118 * @param s - string to be converted
119 * @param defVal - Default Value
121 public Boolean stringToBool(@Nullable String s, boolean defVal) {
126 return Boolean.parseBoolean(s);
127 } catch (Exception e) {
133 * Return Integer from string
135 * @param s - string to be converted
136 * @param defVal - Default Value
138 public Integer stringToInteger(@Nullable String s, Integer defVal) {
143 return Integer.valueOf(s);
144 } catch (Exception e) {
149 /***********************************
151 ************************************/
153 public static boolean isValidJson(String json) {
155 Gson gson = new Gson();
156 JsonObject jsnObject = gson.fromJson(json, JsonObject.class);
157 return jsnObject != null;
158 } catch (Exception e) {
165 * @param name parameter name
166 * @param defVal - default value;
167 * @return string value
169 public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name, String defVal) {
170 if (jsonObject != null && jsonObject.has(name)) {
171 return jsonObject.get(name).getAsString();
179 * @param name parameter name
180 * @return string value
182 public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name) {
183 return jsonObjectToString(jsonObject, name, "");
188 * @param name parameter name
189 * @param defVal - default value;
190 * @return boolean value
192 public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name, Boolean defVal) {
193 if (jsonObject != null && jsonObject.has(name)) {
194 return jsonObject.get(name).getAsBoolean();
202 * @param name parameter name
203 * @return boolean value
205 public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name) {
206 return jsonObjectToBool(jsonObject, name, false);
211 * @param name parameter name
212 * @param defVal - default value;
213 * @return integer value
215 public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name, Integer defVal) {
216 if (jsonObject != null && jsonObject.has(name)) {
217 return jsonObject.get(name).getAsInt();
225 * @param name parameter name
226 * @return integer value
228 public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name) {
229 return jsonObjectToInt(jsonObject, name, 0);
234 * @param name parameter name
235 * @param defVal - default value;
236 * @return number value
238 public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name, Number defVal) {
239 if (jsonObject != null && jsonObject.has(name)) {
240 return jsonObject.get(name).getAsNumber();
248 * @param name parameter name
249 * @return number value
251 public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name) {
252 return jsonObjectToNumber(jsonObject, name, 0);
255 /************************************
257 ***********************************/
260 * Return OnOffType from bool
264 public static OnOffType getOnOffType(@Nullable Boolean boolVal) {
265 return (boolVal != null ? boolVal ? OnOffType.ON : OnOffType.OFF : OnOffType.OFF);
269 * Return OnOffType from bool
273 public static OnOffType getOnOffType(Integer intVal) {
274 return intVal == 0 ? OnOffType.OFF : OnOffType.ON;
278 * Return StringType from String
282 public static StringType getStringType(@Nullable String strVal) {
283 return new StringType(strVal != null ? strVal : "");
287 * Return DecimalType from Double
291 public static DecimalType getDecimalType(@Nullable Double numVal) {
292 return new DecimalType((numVal != null ? numVal : 0));
296 * Return DecimalType from Integer
300 public static DecimalType getDecimalType(@Nullable Integer numVal) {
301 return new DecimalType((numVal != null ? numVal : 0));
305 * Return DecimalType from Long
309 public static DecimalType getDecimalTypel(@Nullable Long numVal) {
310 return new DecimalType((numVal != null ? numVal : 0));
315 * @param numVal value 0-100
316 * @return PercentType
318 public static PercentType getPercentType(@Nullable Integer numVal) {
319 Integer val = limitVal(numVal, 0, 100);
320 return new PercentType(val);
324 * Return HSBType from integers
326 * @param hue integer hue-color
327 * @param saturation integer saturation
328 * @param brightness integer brightness
331 public static HSBType getHSBType(Integer hue, Integer saturation, Integer brightness) {
332 DecimalType h = new DecimalType(hue);
333 PercentType s = new PercentType(saturation);
334 PercentType b = new PercentType(brightness);
335 return new HSBType(h, s, b);
339 * Return QuantityType with Time
341 * @param numVal Number with value
342 * @param unit TimeUnit (Unit<Time>)
343 * @return QuantityTime<Time>
345 public static QuantityType<Time> getQuantityType(@Nullable Number numVal, Unit<Time> unit) {
346 return new QuantityType<>((numVal != null ? numVal : 0), unit);