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