]> git.basschouten.com Git - openhab-addons.git/blob
fb86148f9e5d4031c1b85b45234bdc20a74e235a
[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.plugwise.internal;
14
15 import static org.openhab.binding.plugwise.internal.PlugwiseBindingConstants.*;
16 import static org.openhab.binding.plugwise.internal.protocol.field.DeviceType.*;
17
18 import java.time.LocalDateTime;
19 import java.time.ZoneId;
20 import java.time.format.DateTimeFormatter;
21 import java.util.Map;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.plugwise.internal.protocol.InformationResponseMessage;
26 import org.openhab.binding.plugwise.internal.protocol.field.DeviceType;
27 import org.openhab.core.library.types.DateTimeType;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30
31 /**
32  * Utility class for sharing utility methods between objects.
33  *
34  * @author Wouter Born - Initial contribution
35  */
36 @NonNullByDefault
37 public final class PlugwiseUtils {
38
39     private PlugwiseUtils() {
40         // Hidden utility class constructor
41     }
42
43     public static DeviceType getDeviceType(ThingTypeUID uid) {
44         if (THING_TYPE_CIRCLE.equals(uid)) {
45             return CIRCLE;
46         } else if (THING_TYPE_CIRCLE_PLUS.equals(uid)) {
47             return CIRCLE_PLUS;
48         } else if (THING_TYPE_SCAN.equals(uid)) {
49             return SCAN;
50         } else if (THING_TYPE_SENSE.equals(uid)) {
51             return SENSE;
52         } else if (THING_TYPE_STEALTH.equals(uid)) {
53             return STEALTH;
54         } else if (THING_TYPE_SWITCH.equals(uid)) {
55             return SWITCH;
56         } else {
57             return UNKNOWN;
58         }
59     }
60
61     public static @Nullable ThingTypeUID getThingTypeUID(DeviceType deviceType) {
62         if (deviceType == CIRCLE) {
63             return THING_TYPE_CIRCLE;
64         } else if (deviceType == CIRCLE_PLUS) {
65             return THING_TYPE_CIRCLE_PLUS;
66         } else if (deviceType == SCAN) {
67             return THING_TYPE_SCAN;
68         } else if (deviceType == SENSE) {
69             return THING_TYPE_SENSE;
70         } else if (deviceType == STEALTH) {
71             return THING_TYPE_STEALTH;
72         } else if (deviceType == SWITCH) {
73             return THING_TYPE_SWITCH;
74         } else {
75             return null;
76         }
77     }
78
79     public static String lowerCamelToUpperUnderscore(String text) {
80         return text.replaceAll("([a-z])([A-Z]+)", "$1_$2").toUpperCase();
81     }
82
83     public static <T extends Comparable<T>> T minComparable(T first, T second) {
84         return first.compareTo(second) <= 0 ? first : second;
85     }
86
87     public static DateTimeType newDateTimeType(LocalDateTime localDateTime) {
88         return new DateTimeType(localDateTime.atZone(ZoneId.systemDefault()));
89     }
90
91     public static void stopBackgroundThread(@Nullable Thread thread) {
92         if (thread != null) {
93             thread.interrupt();
94             try {
95                 thread.join();
96             } catch (InterruptedException e) {
97                 Thread.interrupted();
98             }
99         }
100     }
101
102     public static String upperUnderscoreToLowerCamel(String text) {
103         final String delimiter = "_";
104         StringBuilder upperCamelBuilder = new StringBuilder(text.length());
105         for (String str : text.split(delimiter)) {
106             if (upperCamelBuilder.isEmpty() && str.length() > 0) {
107                 upperCamelBuilder.append(str.substring(0, 1).toLowerCase());
108             } else if (str.length() > 0) {
109                 upperCamelBuilder.append(str.substring(0, 1).toUpperCase());
110             }
111             if (str.length() > 1) {
112                 upperCamelBuilder.append(str.substring(1).toLowerCase());
113             }
114         }
115         return upperCamelBuilder.toString();
116     }
117
118     public static boolean updateProperties(Map<String, String> properties, InformationResponseMessage message) {
119         boolean update = false;
120
121         // Update firmware version property
122         String oldFirmware = properties.get(Thing.PROPERTY_FIRMWARE_VERSION);
123         String newFirmware = DateTimeFormatter.ISO_LOCAL_DATE.format(message.getFirmwareVersion());
124         if (oldFirmware == null || !oldFirmware.equals(newFirmware)) {
125             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, newFirmware);
126             update = true;
127         }
128
129         // Update hardware version property
130         String oldHardware = properties.get(Thing.PROPERTY_HARDWARE_VERSION);
131         String newHardware = message.getHardwareVersion();
132         if (oldHardware == null || !oldHardware.equals(newHardware)) {
133             properties.put(Thing.PROPERTY_HARDWARE_VERSION, newHardware);
134             update = true;
135         }
136
137         // Update hertz property for devices with a relay
138         if (message.getDeviceType().isRelayDevice()) {
139             String oldHertz = properties.get(PlugwiseBindingConstants.PROPERTY_HERTZ);
140             String newHertz = Integer.toString(message.getHertz());
141             if (oldHertz == null || !oldHertz.equals(newHertz)) {
142                 properties.put(PlugwiseBindingConstants.PROPERTY_HERTZ, newHertz);
143                 update = true;
144             }
145         }
146
147         // Update MAC address property
148         String oldMACAddress = properties.get(PlugwiseBindingConstants.PROPERTY_MAC_ADDRESS);
149         String newMACAddress = message.getMACAddress().toString();
150         if (oldMACAddress == null || !oldMACAddress.equals(newMACAddress)) {
151             properties.put(PlugwiseBindingConstants.PROPERTY_MAC_ADDRESS, newMACAddress);
152             update = true;
153         }
154
155         return update;
156     }
157 }