[plugwise] Prevent possible chomp bug (#15339)
* Improve chomp * Adapt to core Stringutils * Improve minor null check Signed-off-by: Leo Siepel <leosiepel@gmail.com>pull/15843/head
parent
6efa3d792c
commit
38a9612b6c
|
@ -30,6 +30,7 @@ import org.openhab.binding.plugwise.internal.protocol.field.MessageType;
|
|||
import org.openhab.core.io.transport.serial.SerialPort;
|
||||
import org.openhab.core.io.transport.serial.SerialPortEvent;
|
||||
import org.openhab.core.io.transport.serial.SerialPortEventListener;
|
||||
import org.openhab.core.util.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -93,7 +94,7 @@ public class PlugwiseMessageProcessor implements SerialPortEventListener {
|
|||
*/
|
||||
private void parseAndQueue(ByteBuffer readBuffer) {
|
||||
String response = new String(readBuffer.array(), 0, readBuffer.limit());
|
||||
response = response.replace("\r", "").replace("\n", "");
|
||||
response = StringUtils.chomp(response);
|
||||
|
||||
Matcher matcher = RESPONSE_PATTERN.matcher(response);
|
||||
|
||||
|
|
|
@ -99,22 +99,6 @@ public final class PlugwiseUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static String upperUnderscoreToLowerCamel(String text) {
|
||||
final String delimiter = "_";
|
||||
StringBuilder upperCamelBuilder = new StringBuilder(text.length());
|
||||
for (String str : text.split(delimiter)) {
|
||||
if (upperCamelBuilder.isEmpty() && !str.isEmpty()) {
|
||||
upperCamelBuilder.append(str.substring(0, 1).toLowerCase());
|
||||
} else if (!str.isEmpty()) {
|
||||
upperCamelBuilder.append(str.substring(0, 1).toUpperCase());
|
||||
}
|
||||
if (str.length() > 1) {
|
||||
upperCamelBuilder.append(str.substring(1).toLowerCase());
|
||||
}
|
||||
}
|
||||
return upperCamelBuilder.toString();
|
||||
}
|
||||
|
||||
public static boolean updateProperties(Map<String, String> properties, InformationResponseMessage message) {
|
||||
boolean update = false;
|
||||
|
||||
|
|
|
@ -12,13 +12,15 @@
|
|||
*/
|
||||
package org.openhab.binding.plugwise.internal.config;
|
||||
|
||||
import static org.openhab.binding.plugwise.internal.PlugwiseUtils.*;
|
||||
import static org.openhab.binding.plugwise.internal.config.PlugwiseRelayConfig.PowerStateChanging.COMMAND_SWITCHING;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.binding.plugwise.internal.PlugwiseUtils;
|
||||
import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
|
||||
import org.openhab.core.util.StringUtils;
|
||||
|
||||
/**
|
||||
* The {@link PlugwiseRelayConfig} class represents the configuration for a Plugwise relay device (Circle, Circle+,
|
||||
|
@ -36,7 +38,8 @@ public class PlugwiseRelayConfig {
|
|||
}
|
||||
|
||||
private String macAddress = "";
|
||||
private String powerStateChanging = upperUnderscoreToLowerCamel(COMMAND_SWITCHING.name());
|
||||
private String powerStateChanging = Objects
|
||||
.requireNonNull(StringUtils.capitalizeByUnderscore(COMMAND_SWITCHING.name()));
|
||||
private boolean suppliesPower = false;
|
||||
private int measurementInterval = 60; // minutes
|
||||
private boolean temporarilyNotInNetwork = false;
|
||||
|
@ -47,7 +50,7 @@ public class PlugwiseRelayConfig {
|
|||
}
|
||||
|
||||
public PowerStateChanging getPowerStateChanging() {
|
||||
return PowerStateChanging.valueOf(lowerCamelToUpperUnderscore(powerStateChanging));
|
||||
return PowerStateChanging.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(powerStateChanging));
|
||||
}
|
||||
|
||||
public boolean isSuppliesPower() {
|
||||
|
|
|
@ -12,14 +12,16 @@
|
|||
*/
|
||||
package org.openhab.binding.plugwise.internal.config;
|
||||
|
||||
import static org.openhab.binding.plugwise.internal.PlugwiseUtils.*;
|
||||
import static org.openhab.binding.plugwise.internal.protocol.field.Sensitivity.MEDIUM;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.binding.plugwise.internal.PlugwiseUtils;
|
||||
import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
|
||||
import org.openhab.binding.plugwise.internal.protocol.field.Sensitivity;
|
||||
import org.openhab.core.util.StringUtils;
|
||||
|
||||
/**
|
||||
* The {@link PlugwiseScanConfig} class represents the configuration for a Plugwise Scan.
|
||||
|
@ -30,7 +32,7 @@ import org.openhab.binding.plugwise.internal.protocol.field.Sensitivity;
|
|||
public class PlugwiseScanConfig {
|
||||
|
||||
private String macAddress = "";
|
||||
private String sensitivity = upperUnderscoreToLowerCamel(MEDIUM.name());
|
||||
private String sensitivity = Objects.requireNonNull(StringUtils.capitalizeByUnderscore(MEDIUM.name()));
|
||||
private int switchOffDelay = 5; // minutes
|
||||
private boolean daylightOverride = false;
|
||||
private int wakeupInterval = 1440; // minutes (1 day)
|
||||
|
@ -43,7 +45,7 @@ public class PlugwiseScanConfig {
|
|||
}
|
||||
|
||||
public Sensitivity getSensitivity() {
|
||||
return Sensitivity.valueOf(lowerCamelToUpperUnderscore(sensitivity));
|
||||
return Sensitivity.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(sensitivity));
|
||||
}
|
||||
|
||||
public Duration getSwitchOffDelay() {
|
||||
|
|
|
@ -12,18 +12,20 @@
|
|||
*/
|
||||
package org.openhab.binding.plugwise.internal.config;
|
||||
|
||||
import static org.openhab.binding.plugwise.internal.PlugwiseUtils.*;
|
||||
import static org.openhab.binding.plugwise.internal.protocol.field.BoundaryAction.OFF_BELOW_ON_ABOVE;
|
||||
import static org.openhab.binding.plugwise.internal.protocol.field.BoundaryType.NONE;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.binding.plugwise.internal.PlugwiseUtils;
|
||||
import org.openhab.binding.plugwise.internal.protocol.field.BoundaryAction;
|
||||
import org.openhab.binding.plugwise.internal.protocol.field.BoundaryType;
|
||||
import org.openhab.binding.plugwise.internal.protocol.field.Humidity;
|
||||
import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
|
||||
import org.openhab.binding.plugwise.internal.protocol.field.Temperature;
|
||||
import org.openhab.core.util.StringUtils;
|
||||
|
||||
/**
|
||||
* The {@link PlugwiseScanConfig} class represents the configuration for a Plugwise Sense.
|
||||
|
@ -35,8 +37,9 @@ public class PlugwiseSenseConfig {
|
|||
|
||||
private String macAddress = "";
|
||||
private int measurementInterval = 15; // minutes
|
||||
private String boundaryType = upperUnderscoreToLowerCamel(NONE.name());
|
||||
private String boundaryAction = upperUnderscoreToLowerCamel(OFF_BELOW_ON_ABOVE.name());
|
||||
private String boundaryType = Objects.requireNonNull(StringUtils.capitalizeByUnderscore(NONE.name()));
|
||||
private String boundaryAction = Objects
|
||||
.requireNonNull(StringUtils.capitalizeByUnderscore(OFF_BELOW_ON_ABOVE.name()));
|
||||
private int temperatureBoundaryMin = 15; // degrees Celsius
|
||||
private int temperatureBoundaryMax = 25; // degrees Celsius
|
||||
private int humidityBoundaryMin = 45; // relative humidity (RH)
|
||||
|
@ -54,11 +57,11 @@ public class PlugwiseSenseConfig {
|
|||
}
|
||||
|
||||
public BoundaryType getBoundaryType() {
|
||||
return BoundaryType.valueOf(lowerCamelToUpperUnderscore(boundaryType));
|
||||
return BoundaryType.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(boundaryType));
|
||||
}
|
||||
|
||||
public BoundaryAction getBoundaryAction() {
|
||||
return BoundaryAction.valueOf(lowerCamelToUpperUnderscore(boundaryAction));
|
||||
return BoundaryAction.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(boundaryAction));
|
||||
}
|
||||
|
||||
public Temperature getTemperatureBoundaryMin() {
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
|
||||
package org.openhab.binding.plugwise.internal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The {@link PlugwiseUtilsTest} class tests some static string utility methods
|
||||
*
|
||||
* @author Leo Siepel - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class PlugwiseUtilsTest {
|
||||
|
||||
@Test
|
||||
public void upperUnderscoreToLowerCamelBaseTest() {
|
||||
assertEquals("nodelimiterpresent", PlugwiseUtils.upperUnderscoreToLowerCamel("NoDelimiterPresent"));
|
||||
assertEquals("delimiterIsPresent", PlugwiseUtils.upperUnderscoreToLowerCamel("DeliMiter_iS_PreSent"));
|
||||
assertEquals("doubleDelimitersDouble", PlugwiseUtils.upperUnderscoreToLowerCamel("DOUBLE__DELIMITERS__DOUBLE"));
|
||||
assertEquals("helloWorld", PlugwiseUtils.upperUnderscoreToLowerCamel("HELLO_WORLD"));
|
||||
assertEquals("", PlugwiseUtils.upperUnderscoreToLowerCamel(""));
|
||||
assertEquals("", PlugwiseUtils.upperUnderscoreToLowerCamel("_"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue