]> git.basschouten.com Git - openhab-addons.git/blob
7fd25e855e4f87f80240223b69ac65890c197974
[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.shelly.internal.util;
14
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16
17 import java.math.BigDecimal;
18 import java.math.RoundingMode;
19 import java.net.URLEncoder;
20 import java.nio.charset.StandardCharsets;
21 import java.time.DateTimeException;
22 import java.time.Instant;
23 import java.time.LocalDateTime;
24 import java.time.ZoneId;
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
27
28 import javax.measure.Unit;
29
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.openhab.binding.shelly.internal.api.ShellyApiException;
33 import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile;
34 import org.openhab.core.library.types.DateTimeType;
35 import org.openhab.core.library.types.DecimalType;
36 import org.openhab.core.library.types.OnOffType;
37 import org.openhab.core.library.types.PercentType;
38 import org.openhab.core.library.types.QuantityType;
39 import org.openhab.core.library.types.StringType;
40 import org.openhab.core.types.Command;
41 import org.openhab.core.types.State;
42 import org.openhab.core.types.UnDefType;
43
44 import com.google.gson.Gson;
45 import com.google.gson.JsonSyntaxException;
46
47 /**
48  * {@link ShellyUtils} provides general utility functions
49  *
50  * @author Markus Michels - Initial contribution
51  */
52 @NonNullByDefault
53 public class ShellyUtils {
54     private static final String PRE = "Unable to create object of type ";
55     public static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern(DateTimeType.DATE_PATTERN);
56
57     public static <T> T fromJson(Gson gson, @Nullable String json, Class<T> classOfT) throws ShellyApiException {
58         @Nullable
59         T o = fromJson(gson, json, classOfT, true);
60         if (o == null) {
61             throw new ShellyApiException("Unable to create JSON object");
62         }
63         return o;
64     }
65
66     public static @Nullable <T> T fromJson(Gson gson, @Nullable String json, Class<T> classOfT, boolean exceptionOnNull)
67             throws ShellyApiException {
68         String className = substringAfter(classOfT.getName(), "$");
69
70         if (json == null) {
71             if (exceptionOnNull) {
72                 throw new IllegalArgumentException(PRE + className + ": json is null!");
73             } else {
74                 return null;
75             }
76         }
77
78         if (classOfT.isInstance(json)) {
79             return wrap(classOfT).cast(json);
80         } else if (json.isEmpty()) { // update GSON might return null
81             throw new ShellyApiException(PRE + className + "from empty JSON");
82         } else {
83             try {
84                 @Nullable
85                 T obj = gson.fromJson(json, classOfT);
86                 if ((obj == null) && exceptionOnNull) { // new in OH3: fromJson may return null
87                     throw new ShellyApiException(PRE + className + "from JSON: " + json);
88                 }
89                 return obj;
90             } catch (JsonSyntaxException e) {
91                 throw new ShellyApiException(PRE + className + "from JSON (syntax/format error): " + json, e);
92             } catch (RuntimeException e) {
93                 throw new ShellyApiException(PRE + className + "from JSON: " + json, e);
94             }
95         }
96     }
97
98     @SuppressWarnings("unchecked")
99     private static <T> Class<T> wrap(Class<T> type) {
100         if (type == int.class) {
101             return (Class<T>) Integer.class;
102         }
103         if (type == float.class) {
104             return (Class<T>) Float.class;
105         }
106         if (type == byte.class) {
107             return (Class<T>) Byte.class;
108         }
109         if (type == double.class) {
110             return (Class<T>) Double.class;
111         }
112         if (type == long.class) {
113             return (Class<T>) Long.class;
114         }
115         if (type == char.class) {
116             return (Class<T>) Character.class;
117         }
118         if (type == boolean.class) {
119             return (Class<T>) Boolean.class;
120         }
121         if (type == short.class) {
122             return (Class<T>) Short.class;
123         }
124         if (type == void.class) {
125             return (Class<T>) Void.class;
126         }
127         return type;
128     }
129
130     public static String mkChannelId(String group, String channel) {
131         return group + "#" + channel;
132     }
133
134     public static String getString(@Nullable String value) {
135         return value != null ? value : "";
136     }
137
138     public static String substringBefore(@Nullable String string, String pattern) {
139         if (string != null) {
140             int pos = string.indexOf(pattern);
141             if (pos > 0) {
142                 return string.substring(0, pos);
143             }
144         }
145         return "";
146     }
147
148     public static String substringBeforeLast(@Nullable String string, String pattern) {
149         if (string != null) {
150             int pos = string.lastIndexOf(pattern);
151             if (pos > 0) {
152                 return string.substring(0, pos);
153             }
154         }
155         return "";
156     }
157
158     public static String substringAfter(@Nullable String string, String pattern) {
159         if (string != null) {
160             int pos = string.indexOf(pattern);
161             if (pos != -1) {
162                 return string.substring(pos + pattern.length());
163             }
164         }
165         return "";
166     }
167
168     public static String substringAfterLast(@Nullable String string, String pattern) {
169         if (string == null) {
170             return "";
171         }
172         int pos = string.lastIndexOf(pattern);
173         if (pos != -1) {
174             return string.substring(pos + pattern.length());
175         }
176         return string;
177     }
178
179     public static String substringBetween(@Nullable String string, String begin, String end) {
180         if (string != null) {
181             int s = string.indexOf(begin);
182             if (s != -1) {
183                 // The end tag might be included before the start tag, e.g.
184                 // when using "http://" and ":" to get the IP from http://192.168.1.1:8081/xxx
185                 // therefore make it 2 steps
186                 String result = string.substring(s + begin.length());
187                 return substringBefore(result, end);
188             }
189         }
190         return "";
191     }
192
193     public static String getMessage(Exception e) {
194         String message = e.getMessage();
195         return message != null ? message : "";
196     }
197
198     public static Integer getInteger(@Nullable Integer value) {
199         return (value != null ? (Integer) value : 0);
200     }
201
202     public static Long getLong(@Nullable Long value) {
203         return (value != null ? (Long) value : 0);
204     }
205
206     public static Double getDouble(@Nullable Double value) {
207         return (value != null ? (Double) value : 0);
208     }
209
210     public static Boolean getBool(@Nullable Boolean value) {
211         return (value != null ? (Boolean) value : false);
212     }
213
214     // as State
215
216     public static StringType getStringType(@Nullable String value) {
217         return new StringType(value != null ? value : "");
218     }
219
220     public static DecimalType getDecimal(@Nullable Double value) {
221         return new DecimalType((value != null ? value : 0));
222     }
223
224     public static DecimalType getDecimal(@Nullable Integer value) {
225         return new DecimalType((value != null ? value : 0));
226     }
227
228     public static DecimalType getDecimal(@Nullable Long value) {
229         return new DecimalType((value != null ? value : 0));
230     }
231
232     public static Double getNumber(Command command) throws IllegalArgumentException {
233         if (command instanceof DecimalType) {
234             return ((DecimalType) command).doubleValue();
235         }
236         if (command instanceof QuantityType) {
237             return ((QuantityType<?>) command).doubleValue();
238         }
239         throw new IllegalArgumentException("Unable to convert number");
240     }
241
242     public static OnOffType getOnOff(@Nullable Boolean value) {
243         return (value != null ? value ? OnOffType.ON : OnOffType.OFF : OnOffType.OFF);
244     }
245
246     public static OnOffType getOnOff(int value) {
247         return value == 0 ? OnOffType.OFF : OnOffType.ON;
248     }
249
250     public static State toQuantityType(@Nullable Double value, int digits, Unit<?> unit) {
251         if (value == null) {
252             return UnDefType.NULL;
253         }
254         BigDecimal bd = new BigDecimal(value.doubleValue());
255         return toQuantityType(bd.setScale(digits, RoundingMode.HALF_UP), unit);
256     }
257
258     public static State toQuantityType(@Nullable Number value, Unit<?> unit) {
259         return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
260     }
261
262     public static State toQuantityType(@Nullable PercentType value, Unit<?> unit) {
263         return value == null ? UnDefType.NULL : toQuantityType(value.toBigDecimal(), unit);
264     }
265
266     public static void validateRange(String name, Integer value, int min, int max) {
267         if ((value < min) || (value > max)) {
268             throw new IllegalArgumentException("Value " + name + " is out of range (" + min + "-" + max + ")");
269         }
270     }
271
272     public static String urlEncode(String input) {
273         return URLEncoder.encode(input, StandardCharsets.UTF_8);
274     }
275
276     public static Long now() {
277         return System.currentTimeMillis() / 1000L;
278     }
279
280     public static DateTimeType getTimestamp() {
281         return new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochSecond(now()), ZoneId.systemDefault()));
282     }
283
284     public static DateTimeType getTimestamp(String zone, long timestamp) {
285         try {
286             if (timestamp == 0) {
287                 throw new IllegalArgumentException("Timestamp value 0 is invalid");
288             }
289             ZoneId zoneId = !zone.isEmpty() ? ZoneId.of(zone) : ZoneId.systemDefault();
290             ZonedDateTime zdt = LocalDateTime.now().atZone(zoneId);
291             int delta = zdt.getOffset().getTotalSeconds();
292             return new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp - delta), zoneId));
293         } catch (DateTimeException e) {
294             // Unable to convert device's timezone, use system one
295             return getTimestamp();
296         }
297     }
298
299     public static String getTimestamp(DateTimeType dt) {
300         return dt.getZonedDateTime().toString().replace('T', ' ').replace('-', '/');
301     }
302
303     public static String convertTimestamp(long ts) {
304         if (ts == 0) {
305             return "";
306         }
307         String time = DATE_TIME.format(ZonedDateTime.ofInstant(Instant.ofEpochSecond(ts), ZoneId.systemDefault()));
308         return time.replace('T', ' ').replace('-', '/');
309     }
310
311     public static Integer getLightIdFromGroup(String groupName) {
312         if (groupName.startsWith(CHANNEL_GROUP_LIGHT_CHANNEL)) {
313             return Integer.parseInt(substringAfter(groupName, CHANNEL_GROUP_LIGHT_CHANNEL)) - 1;
314         }
315         return 0; // only 1 light, e.g. bulb or rgbw2 in color mode
316     }
317
318     public static String buildControlGroupName(ShellyDeviceProfile profile, Integer channelId) {
319         return profile.isBulb || profile.isDuo || profile.inColor ? CHANNEL_GROUP_LIGHT_CONTROL
320                 : CHANNEL_GROUP_LIGHT_CHANNEL + channelId.toString();
321     }
322
323     public static String buildWhiteGroupName(ShellyDeviceProfile profile, Integer channelId) {
324         return profile.isBulb || profile.isDuo ? CHANNEL_GROUP_WHITE_CONTROL
325                 : CHANNEL_GROUP_LIGHT_CHANNEL + channelId.toString();
326     }
327
328     public static DecimalType mapSignalStrength(int dbm) {
329         int strength = -1;
330         if (dbm > -60) {
331             strength = 4;
332         } else if (dbm > -70) {
333             strength = 3;
334         } else if (dbm > -80) {
335             strength = 2;
336         } else if (dbm > -90) {
337             strength = 1;
338         } else {
339             strength = 0;
340         }
341         return new DecimalType(strength);
342     }
343 }