Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.openhab.core.io.transport.modbus.ModbusConstants.ValueType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.types.Command;
Expand Down Expand Up @@ -605,7 +604,7 @@ public static String extractStringFromBytes(byte[] bytes, int byteIndex, int len
/**
* Convert command to array of registers using a specific value type
*
* @param command command to be converted. Either OnOffType, OpenClosedType, DecimalType or QuantityType that can be
* @param command command to be converted. Either OnOffType, DecimalType or QuantityType that can be
* converted to dimensionless unit.
* @param type value type to use in conversion
* @return array of registers
Expand All @@ -614,7 +613,7 @@ public static String extractStringFromBytes(byte[] bytes, int byteIndex, int len
*/
public static ModbusRegisterArray commandToRegisters(Command command, ModbusConstants.ValueType type) {
Number numericCommand;
if (command instanceof OnOffType || command instanceof OpenClosedType) {
if (command instanceof OnOffType) {
numericCommand = translateCommand2Boolean(command).get() ? new DecimalType(BigDecimal.ONE)
: DecimalType.ZERO;
} else if (command instanceof DecimalType decimalType) {
Expand All @@ -629,7 +628,7 @@ public static ModbusRegisterArray commandToRegisters(Command command, ModbusCons
numericCommand = qtCommand;
} else {
throw new IllegalArgumentException(String.format(
"Command '%s' of class '%s' cannot be converted to registers. Please use OnOffType, OpenClosedType, DecimalType or dimensionless QuantityType commands.",
"Command '%s' of class '%s' cannot be converted to registers. Please use OnOffType, DecimalType or dimensionless QuantityType commands.",
command, command.getClass().getName()));
}
if (type.getBits() != 16 && type.getBits() != 32 && type.getBits() != 64) {
Expand Down Expand Up @@ -725,8 +724,8 @@ public static ModbusRegisterArray commandToRegisters(Command command, ModbusCons
/**
* Converts command to a boolean
*
* true value is represented by {@link OnOffType#ON}, {@link OpenClosedType#OPEN}.
* false value is represented by {@link OnOffType#OFF}, {@link OpenClosedType#CLOSED}.
* true value is represented by {@link OnOffType#ON}.
* false value is represented by {@link OnOffType#OFF}.
* Furthermore, {@link DecimalType} are converted to boolean true if they are unequal to zero.
*
* @param command to convert to boolean
Expand All @@ -739,12 +738,6 @@ public static Optional<Boolean> translateCommand2Boolean(Command command) {
if (command.equals(OnOffType.OFF)) {
return Optional.of(Boolean.FALSE);
}
if (command.equals(OpenClosedType.OPEN)) {
return Optional.of(Boolean.TRUE);
}
if (command.equals(OpenClosedType.CLOSED)) {
return Optional.of(Boolean.FALSE);
}
if (command instanceof DecimalType) {
return Optional.of(!command.equals(DecimalType.ZERO));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.types.Command;
Expand Down Expand Up @@ -305,10 +304,6 @@ public static Stream<Object> data() {
a("5000000000", FLOAT32_SWAP, 0x02F9, 0x4F95),
// ON/OFF
a(OnOffType.ON, FLOAT32_SWAP, 0x0000, 0x3F80), a(OnOffType.OFF, FLOAT32_SWAP, 0x0000, 0x0000),
// OPEN
a(OpenClosedType.OPEN, FLOAT32_SWAP, 0x0000, 0x3F80), a(OpenClosedType.OPEN, INT16, 1),
// CLOSED
a(OpenClosedType.CLOSED, FLOAT32_SWAP, 0x0000, 0x0000), a(OpenClosedType.CLOSED, INT16, 0x0000),
// QuantityType, dimensionless units are converted to unit of 1. e.g. 500% -> 5
a(QuantityType.valueOf(500, Units.PERCENT), INT16, 0x0005),
// 50% = 0.5 truncated to zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;

/**
* @author Sami Salonen - Initial contribution
Expand Down Expand Up @@ -55,24 +54,12 @@ public void testOn() {
assertThat(actual, is(equalTo(Optional.of(true))));
}

@Test
public void testOpen() {
Optional<Boolean> actual = ModbusBitUtilities.translateCommand2Boolean(OpenClosedType.OPEN);
assertThat(actual, is(equalTo(Optional.of(true))));
}

@Test
public void testOff() {
Optional<Boolean> actual = ModbusBitUtilities.translateCommand2Boolean(OnOffType.OFF);
assertThat(actual, is(equalTo(Optional.of(false))));
}

@Test
public void testClosed() {
Optional<Boolean> actual = ModbusBitUtilities.translateCommand2Boolean(OpenClosedType.CLOSED);
assertThat(actual, is(equalTo(Optional.of(false))));
}

@Test
public void testUnknown() {
Optional<Boolean> actual = ModbusBitUtilities.translateCommand2Boolean(IncreaseDecreaseType.INCREASE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.types.Command;
import org.openhab.core.types.PrimitiveType;
import org.openhab.core.types.State;

Expand All @@ -23,7 +22,7 @@
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public enum OpenClosedType implements PrimitiveType, State, Command {
public enum OpenClosedType implements PrimitiveType, State {
OPEN,
CLOSED;

Expand Down
Loading