]> git.basschouten.com Git - openhab-addons.git/blob
72db563011c2db9417ab3358939c9ea2c8d0bdd1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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;
14
15 import javax.measure.Unit;
16 import javax.measure.quantity.Energy;
17 import javax.measure.quantity.Power;
18 import javax.measure.quantity.Time;
19
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;
28
29 import com.google.gson.Gson;
30 import com.google.gson.JsonObject;
31
32 /**
33  * {@link TapoUtils} TapoUtils -
34  * Utility Helper Functions
35  *
36  * @author Christian Wild - Initial Initial contribution
37  */
38 @NonNullByDefault
39 public class TapoUtils {
40
41     /************************************
42      * CALCULATION UTILS
43      ***********************************/
44     /**
45      * Limit Value between limits
46      * 
47      * @param value Integer
48      * @param lowerLimit
49      * @param upperLimit
50      * @return
51      */
52     public static Integer limitVal(@Nullable Integer value, Integer lowerLimit, Integer upperLimit) {
53         if (value == null || value < lowerLimit) {
54             return lowerLimit;
55         } else if (value > upperLimit) {
56             return upperLimit;
57         }
58         return value;
59     }
60
61     /************************************
62      * FORMAT UTILS
63      ***********************************/
64     /**
65      * return value or default val if it's null
66      * 
67      * @param <T> Type of value
68      * @param value value
69      * @param defaultValue defaut value
70      * @return
71      */
72     public static <T> T getValueOrDefault(@Nullable T value, T defaultValue) {
73         return value == null ? defaultValue : value;
74     }
75
76     /**
77      * Format MAC-Address replacing old division chars and add new one
78      * 
79      * @param mac unformated mac-Address
80      * @param newDivisionChar new division char (e.g. ":","-" )
81      * @return new formated mac-Address
82      */
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);
86         return formatedMac;
87     }
88
89     /**
90      * unformat MAC-Address replace all division chars
91      * 
92      * @param mac
93      * @return
94      */
95     public static String unformatMac(String mac) {
96         mac = mac.replace("-", "");
97         mac = mac.replace(":", "");
98         mac = mac.replace(".", "");
99         return mac;
100     }
101
102     /**
103      * HEX-STRING to byte convertion
104      */
105     public static byte[] hexStringToByteArray(String s) {
106         int len = s.length();
107         byte[] data = new byte[len / 2];
108         try {
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));
111             }
112         } catch (Exception e) {
113         }
114         return data;
115     }
116
117     /**
118      * Return Boolean from string
119      * 
120      * @param s - string to be converted
121      * @param defVal - Default Value
122      */
123     public Boolean stringToBool(@Nullable String s, boolean defVal) {
124         if (s == null) {
125             return defVal;
126         }
127         try {
128             return Boolean.parseBoolean(s);
129         } catch (Exception e) {
130             return defVal;
131         }
132     }
133
134     /**
135      * Return Integer from string
136      * 
137      * @param s - string to be converted
138      * @param defVal - Default Value
139      */
140     public Integer stringToInteger(@Nullable String s, Integer defVal) {
141         if (s == null) {
142             return defVal;
143         }
144         try {
145             return Integer.valueOf(s);
146         } catch (Exception e) {
147             return defVal;
148         }
149     }
150
151     /***********************************
152      * JSON-FORMATER
153      ************************************/
154
155     public static boolean isValidJson(String json) {
156         try {
157             Gson gson = new Gson();
158             JsonObject jsnObject = gson.fromJson(json, JsonObject.class);
159             return jsnObject != null;
160         } catch (Exception e) {
161             return false;
162         }
163     }
164
165     /**
166      * 
167      * @param name parameter name
168      * @param defVal - default value;
169      * @return string value
170      */
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();
174         } else {
175             return defVal;
176         }
177     }
178
179     /**
180      * 
181      * @param name parameter name
182      * @return string value
183      */
184     public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name) {
185         return jsonObjectToString(jsonObject, name, "");
186     }
187
188     /**
189      * 
190      * @param name parameter name
191      * @param defVal - default value;
192      * @return boolean value
193      */
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();
197         } else {
198             return false;
199         }
200     }
201
202     /**
203      * 
204      * @param name parameter name
205      * @return boolean value
206      */
207     public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name) {
208         return jsonObjectToBool(jsonObject, name, false);
209     }
210
211     /**
212      * 
213      * @param name parameter name
214      * @param defVal - default value;
215      * @return integer value
216      */
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();
220         } else {
221             return defVal;
222         }
223     }
224
225     /**
226      * 
227      * @param name parameter name
228      * @return integer value
229      */
230     public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name) {
231         return jsonObjectToInt(jsonObject, name, 0);
232     }
233
234     /**
235      * 
236      * @param name parameter name
237      * @param defVal - default value;
238      * @return number value
239      */
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();
243         } else {
244             return defVal;
245         }
246     }
247
248     /**
249      * 
250      * @param name parameter name
251      * @return number value
252      */
253     public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name) {
254         return jsonObjectToNumber(jsonObject, name, 0);
255     }
256
257     /************************************
258      * TYPE UTILS
259      ***********************************/
260
261     /**
262      * Return OnOffType from bool
263      * 
264      * @param boolVal
265      */
266     public static OnOffType getOnOffType(@Nullable Boolean boolVal) {
267         return (boolVal != null ? boolVal ? OnOffType.ON : OnOffType.OFF : OnOffType.OFF);
268     }
269
270     /**
271      * Return OnOffType from bool
272      * 
273      * @param boolVal
274      */
275     public static OnOffType getOnOffType(Integer intVal) {
276         return intVal == 0 ? OnOffType.OFF : OnOffType.ON;
277     }
278
279     /**
280      * Return StringType from String
281      * 
282      * @param strVal
283      */
284     public static StringType getStringType(@Nullable String strVal) {
285         return new StringType(strVal != null ? strVal : "");
286     }
287
288     /**
289      * Return DecimalType from Double
290      * 
291      * @param numVal
292      */
293     public static DecimalType getDecimalType(@Nullable Double numVal) {
294         return new DecimalType((numVal != null ? numVal : 0));
295     }
296
297     /**
298      * Return DecimalType from Integer
299      * 
300      * @param numVal
301      */
302     public static DecimalType getDecimalType(@Nullable Integer numVal) {
303         return new DecimalType((numVal != null ? numVal : 0));
304     }
305
306     /**
307      * Return DecimalType from Long
308      * 
309      * @param numVal
310      */
311     public static DecimalType getDecimalTypel(@Nullable Long numVal) {
312         return new DecimalType((numVal != null ? numVal : 0));
313     }
314
315     /**
316      * 
317      * @param numVal value 0-100
318      * @return PercentType
319      */
320     public static PercentType getPercentType(@Nullable Integer numVal) {
321         Integer val = limitVal(numVal, 0, 100);
322         return new PercentType(val);
323     }
324
325     /**
326      * Return HSBType from integers
327      * 
328      * @param hue integer hue-color
329      * @param saturation integer saturation
330      * @param brightness integer brightness
331      * @return HSBType
332      */
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);
338     }
339
340     /**
341      * Return QuantityType with Time
342      * 
343      * @param numVal Number with value
344      * @param unit TimeUnit (Unit<Time>)
345      * @return QuantityType<Time>
346      */
347     public static QuantityType<Time> getTimeType(@Nullable Number numVal, Unit<Time> unit) {
348         return new QuantityType<>((numVal != null ? numVal : 0), unit);
349     }
350
351     /**
352      * Return QuantityType with Power
353      * 
354      * @param numVal Number with value
355      * @param unit PowerUnit (Unit<Power>)
356      * @return QuantityType<Power>
357      */
358     public static QuantityType<Power> getPowerType(@Nullable Number numVal, Unit<Power> unit) {
359         return new QuantityType<>((numVal != null ? numVal : 0), unit);
360     }
361
362     /**
363      * Return QuantityType with Energy
364      * 
365      * @param numVal Number with value
366      * @param unit PowerUnit (Unit<Power>)
367      * @return QuantityType<Energy>
368      */
369     public static QuantityType<Energy> getEnergyType(@Nullable Number numVal, Unit<Energy> unit) {
370         return new QuantityType<>((numVal != null ? numVal : 0), unit);
371     }
372 }