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