Skip to content

Commit

Permalink
[plugwise] Prevent possible chomp bug (openhab#15339)
Browse files Browse the repository at this point in the history
* Improve chomp
* Adapt to core Stringutils
* Improve minor null check

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
  • Loading branch information
lsiepel committed Nov 4, 2023
1 parent 6efa3d7 commit 38a9612
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;

Expand Down Expand Up @@ -93,7 +94,7 @@ public PlugwiseMessageProcessor(PlugwiseCommunicationContext context) {
*/
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,6 @@ public static void stopBackgroundThread(@Nullable Thread thread) {
}
}

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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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+,
Expand All @@ -36,7 +38,8 @@ public enum PowerStateChanging {
}

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;
Expand All @@ -47,7 +50,7 @@ public MACAddress getMACAddress() {
}

public PowerStateChanging getPowerStateChanging() {
return PowerStateChanging.valueOf(lowerCamelToUpperUnderscore(powerStateChanging));
return PowerStateChanging.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(powerStateChanging));
}

public boolean isSuppliesPower() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,7 +32,7 @@
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)
Expand All @@ -43,7 +45,7 @@ public MACAddress getMACAddress() {
}

public Sensitivity getSensitivity() {
return Sensitivity.valueOf(lowerCamelToUpperUnderscore(sensitivity));
return Sensitivity.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(sensitivity));
}

public Duration getSwitchOffDelay() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -54,11 +57,11 @@ public Duration getMeasurementInterval() {
}

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() {
Expand Down

This file was deleted.

0 comments on commit 38a9612

Please sign in to comment.