]> git.basschouten.com Git - openhab-addons.git/blob
21a66ae8bd6370a6825a728fe3bebea083047d7c
[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;
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         return unformatedMac.replaceAll("(.{2})", "$1" + newDivisionChar).substring(0, 17);
86     }
87
88     /**
89      * unformat MAC-Address replace all division chars
90      * 
91      * @param mac
92      * @return
93      */
94     public static String unformatMac(String mac) {
95         mac = mac.replace("-", "");
96         mac = mac.replace(":", "");
97         mac = mac.replace(".", "");
98         return mac;
99     }
100
101     /**
102      * HEX-STRING to byte convertion
103      */
104     public static byte[] hexStringToByteArray(String s) {
105         int len = s.length();
106         byte[] data = new byte[len / 2];
107         try {
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));
110             }
111         } catch (Exception e) {
112         }
113         return data;
114     }
115
116     /**
117      * Return Boolean from string
118      * 
119      * @param s - string to be converted
120      * @param defVal - Default Value
121      */
122     public Boolean stringToBool(@Nullable String s, boolean defVal) {
123         if (s == null) {
124             return defVal;
125         }
126         try {
127             return Boolean.parseBoolean(s);
128         } catch (Exception e) {
129             return defVal;
130         }
131     }
132
133     /**
134      * Return Integer from string
135      * 
136      * @param s - string to be converted
137      * @param defVal - Default Value
138      */
139     public Integer stringToInteger(@Nullable String s, Integer defVal) {
140         if (s == null) {
141             return defVal;
142         }
143         try {
144             return Integer.valueOf(s);
145         } catch (Exception e) {
146             return defVal;
147         }
148     }
149
150     /***********************************
151      * JSON-FORMATER
152      ************************************/
153
154     public static boolean isValidJson(String json) {
155         try {
156             Gson gson = new Gson();
157             JsonObject jsnObject = gson.fromJson(json, JsonObject.class);
158             return jsnObject != null;
159         } catch (Exception e) {
160             return false;
161         }
162     }
163
164     /**
165      * 
166      * @param name parameter name
167      * @param defVal - default value;
168      * @return string value
169      */
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();
173         } else {
174             return defVal;
175         }
176     }
177
178     /**
179      * 
180      * @param name parameter name
181      * @return string value
182      */
183     public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name) {
184         return jsonObjectToString(jsonObject, name, "");
185     }
186
187     /**
188      * 
189      * @param name parameter name
190      * @param defVal - default value;
191      * @return boolean value
192      */
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();
196         } else {
197             return false;
198         }
199     }
200
201     /**
202      * 
203      * @param name parameter name
204      * @return boolean value
205      */
206     public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name) {
207         return jsonObjectToBool(jsonObject, name, false);
208     }
209
210     /**
211      * 
212      * @param name parameter name
213      * @param defVal - default value;
214      * @return integer value
215      */
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();
219         } else {
220             return defVal;
221         }
222     }
223
224     /**
225      * 
226      * @param name parameter name
227      * @return integer value
228      */
229     public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name) {
230         return jsonObjectToInt(jsonObject, name, 0);
231     }
232
233     /**
234      * 
235      * @param name parameter name
236      * @param defVal - default value;
237      * @return number value
238      */
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();
242         } else {
243             return defVal;
244         }
245     }
246
247     /**
248      * 
249      * @param name parameter name
250      * @return number value
251      */
252     public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name) {
253         return jsonObjectToNumber(jsonObject, name, 0);
254     }
255
256     /************************************
257      * TYPE UTILS
258      ***********************************/
259
260     /**
261      * Return OnOffType from bool
262      * 
263      * @param boolVal
264      */
265     public static OnOffType getOnOffType(@Nullable Boolean boolVal) {
266         return boolVal != null ? OnOffType.from(boolVal) : OnOffType.OFF;
267     }
268
269     /**
270      * Return OnOffType from bool
271      * 
272      * @param intVal
273      */
274     public static OnOffType getOnOffType(Integer intVal) {
275         return OnOffType.from(intVal != 0);
276     }
277
278     /**
279      * Return StringType from String
280      * 
281      * @param strVal
282      */
283     public static StringType getStringType(@Nullable String strVal) {
284         return new StringType(strVal != null ? strVal : "");
285     }
286
287     /**
288      * Return DecimalType from Double
289      * 
290      * @param numVal
291      */
292     public static DecimalType getDecimalType(@Nullable Double numVal) {
293         return new DecimalType((numVal != null ? numVal : 0));
294     }
295
296     /**
297      * Return DecimalType from Integer
298      * 
299      * @param numVal
300      */
301     public static DecimalType getDecimalType(@Nullable Integer numVal) {
302         return new DecimalType((numVal != null ? numVal : 0));
303     }
304
305     /**
306      * Return DecimalType from Long
307      * 
308      * @param numVal
309      */
310     public static DecimalType getDecimalTypel(@Nullable Long numVal) {
311         return new DecimalType((numVal != null ? numVal : 0));
312     }
313
314     /**
315      * 
316      * @param numVal value 0-100
317      * @return PercentType
318      */
319     public static PercentType getPercentType(@Nullable Integer numVal) {
320         Integer val = limitVal(numVal, 0, 100);
321         return new PercentType(val);
322     }
323
324     /**
325      * Return HSBType from integers
326      * 
327      * @param hue integer hue-color
328      * @param saturation integer saturation
329      * @param brightness integer brightness
330      * @return HSBType
331      */
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);
337     }
338
339     /**
340      * Return QuantityType with Time
341      * 
342      * @param numVal Number with value
343      * @param unit TimeUnit ({@code Unit<Time>})
344      * @return {@code QuantityType<Time>}
345      */
346     public static QuantityType<Time> getTimeType(@Nullable Number numVal, Unit<Time> unit) {
347         return new QuantityType<>((numVal != null ? numVal : 0), unit);
348     }
349
350     /**
351      * Return QuantityType with Power
352      * 
353      * @param numVal Number with value
354      * @param unit PowerUnit ({@code Unit<Power>})
355      * @return {@code QuantityType<Power>}
356      */
357     public static QuantityType<Power> getPowerType(@Nullable Number numVal, Unit<Power> unit) {
358         return new QuantityType<>((numVal != null ? numVal : 0), unit);
359     }
360
361     /**
362      * Return QuantityType with Energy
363      * 
364      * @param numVal Number with value
365      * @param unit PowerUnit ({@code Unit<Power>})
366      * @return {@code QuantityType<Energy>}
367      */
368     public static QuantityType<Energy> getEnergyType(@Nullable Number numVal, Unit<Energy> unit) {
369         return new QuantityType<>((numVal != null ? numVal : 0), unit);
370     }
371 }