Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugwise] Prevent possible chomp bug #15339

Merged
merged 11 commits into from
Nov 4, 2023
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 @@ -76,10 +76,6 @@ public static DeviceType getDeviceType(ThingTypeUID uid) {
}
}

public static String lowerCamelToUpperUnderscore(String text) {
return text.replaceAll("([a-z])([A-Z]+)", "$1_$2").toUpperCase();
}

public static <T extends Comparable<T>> T minComparable(T first, T second) {
return first.compareTo(second) <= 0 ? first : second;
}
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.protocol.field.MACAddress;
import org.openhab.binding.plugwise.internal.util.StringUtil;
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()));
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
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(StringUtil.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.protocol.field.MACAddress;
import org.openhab.binding.plugwise.internal.protocol.field.Sensitivity;
import org.openhab.binding.plugwise.internal.util.StringUtil;
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(StringUtil.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.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
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.binding.plugwise.internal.util.StringUtil;
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 = Optional.ofNullable(StringUtils.capitalizeByUnderscore(NONE.name())).orElse("");
private String boundaryAction = Optional.ofNullable(StringUtils.capitalizeByUnderscore(OFF_BELOW_ON_ABOVE.name()))
.orElse("");
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(StringUtil.lowerCamelToUpperUnderscore(boundaryType));
}

public BoundaryAction getBoundaryAction() {
return BoundaryAction.valueOf(lowerCamelToUpperUnderscore(boundaryAction));
return BoundaryAction.valueOf(StringUtil.lowerCamelToUpperUnderscore(boundaryAction));
}

public Temperature getTemperatureBoundaryMin() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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.util;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Utility class for sharing string utility methods.
*
* @author Leo Siepel - Initial contribution
*/
@NonNullByDefault
public final class StringUtil {
wborn marked this conversation as resolved.
Show resolved Hide resolved

public static String lowerCamelToUpperUnderscore(String text) {
return text.replaceAll("([a-z])([A-Z]+)", "$1_$2").toUpperCase();
}
}

This file was deleted.