From e13d6e44218693d1d4953a224594725a4f0a0456 Mon Sep 17 00:00:00 2001 From: Fabian Wolter Date: Wed, 2 Dec 2020 17:55:18 +0100 Subject: [PATCH] Refactor float/BigDecimal Signed-off-by: Fabian Wolter --- .../openhab/binding/modbus/sbc/internal/ALD1Handler.java | 5 ++--- .../binding/modbus/sbc/internal/ALD1Registers.java | 8 +++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.modbus.sbc/src/main/java/org/openhab/binding/modbus/sbc/internal/ALD1Handler.java b/bundles/org.openhab.binding.modbus.sbc/src/main/java/org/openhab/binding/modbus/sbc/internal/ALD1Handler.java index 79d7e44cb98dd..781a05393bbc9 100644 --- a/bundles/org.openhab.binding.modbus.sbc/src/main/java/org/openhab/binding/modbus/sbc/internal/ALD1Handler.java +++ b/bundles/org.openhab.binding.modbus.sbc/src/main/java/org/openhab/binding/modbus/sbc/internal/ALD1Handler.java @@ -14,7 +14,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.QuantityType; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; @@ -82,8 +81,8 @@ private void readSuccessful(AsyncModbusReadResult result) { int index = channel.getRegisterAddress() - FIRST_READ_REGISTER; ModbusBitUtilities.extractStateFromRegisters(registers, index, channel.getType()) - .map(DecimalType::floatValue) - .map(v -> QuantityType.valueOf(v * channel.getMultiplier(), channel.getUnit())) + .map(d -> d.toBigDecimal().multiply(channel.getMultiplier())) + .map(bigDecimal -> new QuantityType<>(bigDecimal, channel.getUnit())) .ifPresent(v -> updateState(createChannelUid(channel), v)); } }); diff --git a/bundles/org.openhab.binding.modbus.sbc/src/main/java/org/openhab/binding/modbus/sbc/internal/ALD1Registers.java b/bundles/org.openhab.binding.modbus.sbc/src/main/java/org/openhab/binding/modbus/sbc/internal/ALD1Registers.java index 1ab52b4e692b0..d43c20a27627a 100644 --- a/bundles/org.openhab.binding.modbus.sbc/src/main/java/org/openhab/binding/modbus/sbc/internal/ALD1Registers.java +++ b/bundles/org.openhab.binding.modbus.sbc/src/main/java/org/openhab/binding/modbus/sbc/internal/ALD1Registers.java @@ -14,6 +14,8 @@ import static org.openhab.io.transport.modbus.ModbusConstants.ValueType.*; +import java.math.BigDecimal; + import javax.measure.Unit; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -37,13 +39,13 @@ public enum ALD1Registers { REACTIVE_POWER(10, 39, INT16, SmartHomeUnits.VAR), POWER_FACTOR(0.01f, 40, UINT16, SmartHomeUnits.ONE); - private float multiplier; + private BigDecimal multiplier; private int registerAddress; private ModbusConstants.ValueType type; private Unit unit; private ALD1Registers(float multiplier, int registerAddress, ValueType type, Unit unit) { - this.multiplier = multiplier; + this.multiplier = new BigDecimal(multiplier); this.registerAddress = registerAddress; this.type = type; this.unit = unit; @@ -53,7 +55,7 @@ public Unit getUnit() { return unit; } - public float getMultiplier() { + public BigDecimal getMultiplier() { return multiplier; }