[modbus] Replace apache commons lang usage with plain java (#10002)
* [modbus] fix item creation in tests Signed-off-by: Sami Salonen <ssalonen@gmail.com>
This commit is contained in:
parent
0f8f72dd2e
commit
d5e83e395b
@ -17,7 +17,6 @@ import java.util.Optional;
|
|||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.binding.modbus.internal.AtomicStampedValue;
|
import org.openhab.binding.modbus.internal.AtomicStampedValue;
|
||||||
@ -179,7 +178,7 @@ public class ModbusPollerThingHandler extends BaseBridgeHandler {
|
|||||||
private final Logger logger = LoggerFactory.getLogger(ModbusPollerThingHandler.class);
|
private final Logger logger = LoggerFactory.getLogger(ModbusPollerThingHandler.class);
|
||||||
|
|
||||||
private final static List<String> SORTED_READ_FUNCTION_CODES = ModbusBindingConstantsInternal.READ_FUNCTION_CODES
|
private final static List<String> SORTED_READ_FUNCTION_CODES = ModbusBindingConstantsInternal.READ_FUNCTION_CODES
|
||||||
.keySet().stream().sorted().collect(Collectors.toList());
|
.keySet().stream().sorted().collect(Collectors.toUnmodifiableList());
|
||||||
|
|
||||||
private @NonNullByDefault({}) ModbusPollerConfiguration config;
|
private @NonNullByDefault({}) ModbusPollerConfiguration config;
|
||||||
private long cacheMillis;
|
private long cacheMillis;
|
||||||
@ -246,7 +245,7 @@ public class ModbusPollerThingHandler extends BaseBridgeHandler {
|
|||||||
if (!ModbusBindingConstantsInternal.READ_FUNCTION_CODES.containsKey(type)) {
|
if (!ModbusBindingConstantsInternal.READ_FUNCTION_CODES.containsKey(type)) {
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
|
||||||
String.format("No function code found for type='%s'. Was expecting one of: %s", type,
|
String.format("No function code found for type='%s'. Was expecting one of: %s", type,
|
||||||
StringUtils.join(SORTED_READ_FUNCTION_CODES, ", ")));
|
String.join(", ", SORTED_READ_FUNCTION_CODES)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
functionCode = ModbusBindingConstantsInternal.READ_FUNCTION_CODES.get(type);
|
functionCode = ModbusBindingConstantsInternal.READ_FUNCTION_CODES.get(type);
|
||||||
|
|||||||
@ -17,11 +17,15 @@ import static org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.apache.commons.lang.NotImplementedException;
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.binding.modbus.handler.EndpointNotInitializedException;
|
import org.openhab.binding.modbus.handler.EndpointNotInitializedException;
|
||||||
@ -291,7 +295,7 @@ public class ModbusDataThingHandler extends BaseThingHandler {
|
|||||||
// Should not happen! This method is not called in case configuration errors and writeType is validated
|
// Should not happen! This method is not called in case configuration errors and writeType is validated
|
||||||
// already in initialization (validateAndParseWriteParameters).
|
// already in initialization (validateAndParseWriteParameters).
|
||||||
// We keep this here for future-proofing the code (new writeType values)
|
// We keep this here for future-proofing the code (new writeType values)
|
||||||
throw new NotImplementedException(String.format(
|
throw new IllegalStateException(String.format(
|
||||||
"writeType does not equal %s or %s and thus configuration is invalid. Should not end up this far with configuration error.",
|
"writeType does not equal %s or %s and thus configuration is invalid. Should not end up this far with configuration error.",
|
||||||
WRITE_TYPE_COIL, WRITE_TYPE_HOLDING));
|
WRITE_TYPE_COIL, WRITE_TYPE_HOLDING));
|
||||||
}
|
}
|
||||||
@ -433,8 +437,8 @@ public class ModbusDataThingHandler extends BaseThingHandler {
|
|||||||
ModbusReadFunctionCode functionCode = this.functionCode;
|
ModbusReadFunctionCode functionCode = this.functionCode;
|
||||||
boolean readingDiscreteOrCoil = functionCode == ModbusReadFunctionCode.READ_COILS
|
boolean readingDiscreteOrCoil = functionCode == ModbusReadFunctionCode.READ_COILS
|
||||||
|| functionCode == ModbusReadFunctionCode.READ_INPUT_DISCRETES;
|
|| functionCode == ModbusReadFunctionCode.READ_INPUT_DISCRETES;
|
||||||
boolean readStartMissing = StringUtils.isBlank(config.getReadStart());
|
boolean readStartMissing = config.getReadStart() == null || config.getReadStart().isBlank();
|
||||||
boolean readValueTypeMissing = StringUtils.isBlank(config.getReadValueType());
|
boolean readValueTypeMissing = config.getReadValueType() == null || config.getReadValueType().isBlank();
|
||||||
|
|
||||||
if (childOfEndpoint && readRequest == null) {
|
if (childOfEndpoint && readRequest == null) {
|
||||||
if (!readStartMissing || !readValueTypeMissing) {
|
if (!readStartMissing || !readValueTypeMissing) {
|
||||||
@ -503,10 +507,10 @@ public class ModbusDataThingHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void validateAndParseWriteParameters(ModbusDataConfiguration config) throws ModbusConfigurationException {
|
private void validateAndParseWriteParameters(ModbusDataConfiguration config) throws ModbusConfigurationException {
|
||||||
boolean writeTypeMissing = StringUtils.isBlank(config.getWriteType());
|
boolean writeTypeMissing = config.getWriteType() == null || config.getWriteType().isBlank();
|
||||||
boolean writeStartMissing = StringUtils.isBlank(config.getWriteStart());
|
boolean writeStartMissing = config.getWriteStart() == null || config.getWriteStart().isBlank();
|
||||||
boolean writeValueTypeMissing = StringUtils.isBlank(config.getWriteValueType());
|
boolean writeValueTypeMissing = config.getWriteValueType() == null || config.getWriteValueType().isBlank();
|
||||||
boolean writeTransformationMissing = StringUtils.isBlank(config.getWriteTransform());
|
boolean writeTransformationMissing = config.getWriteTransform() == null || config.getWriteTransform().isBlank();
|
||||||
writeTransformation = new Transformation(config.getWriteTransform());
|
writeTransformation = new Transformation(config.getWriteTransform());
|
||||||
boolean writingCoil = WRITE_TYPE_COIL.equals(config.getWriteType());
|
boolean writingCoil = WRITE_TYPE_COIL.equals(config.getWriteType());
|
||||||
writeParametersHavingTransformationOnly = (writeTypeMissing && writeStartMissing && writeValueTypeMissing
|
writeParametersHavingTransformationOnly = (writeTypeMissing && writeStartMissing && writeValueTypeMissing
|
||||||
@ -865,8 +869,8 @@ public class ModbusDataThingHandler extends BaseThingHandler {
|
|||||||
localReadTransformation.isIdentityTransform() ? "<identity>" : localReadTransformation);
|
localReadTransformation.isIdentityTransform() ? "<identity>" : localReadTransformation);
|
||||||
states.put(channelUID, transformedState);
|
states.put(channelUID, transformedState);
|
||||||
} else {
|
} else {
|
||||||
String types = StringUtils.join(acceptedDataTypes.stream().map(cls -> cls.getSimpleName()).toArray(),
|
String types = String.join(", ",
|
||||||
", ");
|
acceptedDataTypes.stream().map(cls -> cls.getSimpleName()).toArray(String[]::new));
|
||||||
logger.warn(
|
logger.warn(
|
||||||
"Channel {} will not be updated since transformation was unsuccessful. Channel is expecting the following data types [{}]. Input data: number value {} (value type '{}' taken into account) and bool value {}. Transformation: {}",
|
"Channel {} will not be updated since transformation was unsuccessful. Channel is expecting the following data types [{}]. Input data: number value {} (value type '{}' taken into account) and bool value {}. Transformation: {}",
|
||||||
channelId, types, numericState, readValueType, boolValue,
|
channelId, types, numericState, readValueType, boolValue,
|
||||||
|
|||||||
@ -31,7 +31,6 @@ import java.util.concurrent.ScheduledFuture;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
|
||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -60,7 +59,6 @@ import org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint;
|
|||||||
import org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint;
|
import org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint;
|
||||||
import org.openhab.core.items.GenericItem;
|
import org.openhab.core.items.GenericItem;
|
||||||
import org.openhab.core.items.Item;
|
import org.openhab.core.items.Item;
|
||||||
import org.openhab.core.library.items.DateTimeItem;
|
|
||||||
import org.openhab.core.library.types.DecimalType;
|
import org.openhab.core.library.types.DecimalType;
|
||||||
import org.openhab.core.library.types.OnOffType;
|
import org.openhab.core.library.types.OnOffType;
|
||||||
import org.openhab.core.library.types.OpenClosedType;
|
import org.openhab.core.library.types.OpenClosedType;
|
||||||
@ -191,6 +189,7 @@ public class ModbusDataHandlerTest extends AbstractModbusOSGiTest {
|
|||||||
Map<String, ChannelUID> toBeLinked = new HashMap<>();
|
Map<String, ChannelUID> toBeLinked = new HashMap<>();
|
||||||
for (Entry<String, String> entry : CHANNEL_TO_ACCEPTED_TYPE.entrySet()) {
|
for (Entry<String, String> entry : CHANNEL_TO_ACCEPTED_TYPE.entrySet()) {
|
||||||
String channelId = entry.getKey();
|
String channelId = entry.getKey();
|
||||||
|
// accepted item type
|
||||||
String channelAcceptedType = entry.getValue();
|
String channelAcceptedType = entry.getValue();
|
||||||
ChannelUID channelUID = new ChannelUID(thingUID, channelId);
|
ChannelUID channelUID = new ChannelUID(thingUID, channelId);
|
||||||
builder = builder.withChannel(ChannelBuilder.create(channelUID, channelAcceptedType).build());
|
builder = builder.withChannel(ChannelBuilder.create(channelUID, channelAcceptedType).build());
|
||||||
@ -199,11 +198,7 @@ public class ModbusDataHandlerTest extends AbstractModbusOSGiTest {
|
|||||||
// Create item of correct type and link it to channel
|
// Create item of correct type and link it to channel
|
||||||
String itemName = getItemName(channelUID);
|
String itemName = getItemName(channelUID);
|
||||||
final GenericItem item;
|
final GenericItem item;
|
||||||
if (channelId.startsWith("last") || channelId.equals("datetime")) {
|
item = coreItemFactory.createItem(channelAcceptedType, itemName);
|
||||||
item = new DateTimeItem(itemName);
|
|
||||||
} else {
|
|
||||||
item = coreItemFactory.createItem(StringUtils.capitalize(channelId), itemName);
|
|
||||||
}
|
|
||||||
assertThat(String.format("Could not determine correct item type for %s", channelId), item,
|
assertThat(String.format("Could not determine correct item type for %s", channelId), item,
|
||||||
is(notNullValue()));
|
is(notNullValue()));
|
||||||
assertNotNull(item);
|
assertNotNull(item);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user