Skip to content

Commit

Permalink
Refactor float/BigDecimal
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Wolter <[email protected]>
  • Loading branch information
fwolter committed Dec 2, 2020
1 parent b88761e commit e13d6e4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
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.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -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));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -53,7 +55,7 @@ public Unit<?> getUnit() {
return unit;
}

public float getMultiplier() {
public BigDecimal getMultiplier() {
return multiplier;
}

Expand Down

0 comments on commit e13d6e4

Please sign in to comment.