Skip to content

Commit

Permalink
Handle specific cosem values that can either have a unit or not
Browse files Browse the repository at this point in the history
It appears some Luxembourg meters have a unit in the cosem value, while other meters don't report this value. This changes makes it possible to handle both cases.
  • Loading branch information
Hilbrand committed Sep 2, 2020
1 parent 85d3946 commit bb0b8f5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
Expand Up @@ -26,13 +26,21 @@
@NonNullByDefault
class CosemDecimal extends CosemValueDescriptor<DecimalType> {

public static final CosemDecimal INSTANCE = new CosemDecimal();
public static final CosemDecimal INSTANCE = new CosemDecimal(false);
public static final CosemDecimal INSTANCE_WITH_UNITS = new CosemDecimal(true);

private CosemDecimal() {
/**
* If true it can be the input contains a unit. In that case the unit will be stripped before parsing.
*/
private final boolean expectUnit;

private CosemDecimal(boolean expectUnit) {
this.expectUnit = expectUnit;
}

public CosemDecimal(String ohChannelId) {
super(ohChannelId);
this.expectUnit = false;
}

/**
Expand All @@ -45,7 +53,15 @@ public CosemDecimal(String ohChannelId) {
@Override
protected DecimalType getStateValue(String cosemValue) throws ParseException {
try {
return new DecimalType(cosemValue);
final String value;

if (expectUnit) {
final int sep = cosemValue.indexOf('*');
value = sep > 0 ? cosemValue.substring(0, sep) : cosemValue;
} else {
value = cosemValue;
}
return new DecimalType(value);
} catch (NumberFormatException nfe) {
throw new ParseException("Failed to parse value '" + cosemValue + "' as integer", 0);
}
Expand Down
Expand Up @@ -143,9 +143,9 @@ public enum CosemObjectType {
EMETER_TOTAL_IMPORTED_ENERGY_REGISTER_Q(new OBISIdentifier(1, null, 3, 8, 0, null), CosemQuantity.KILO_VAR_HOUR),
EMETER_TOTAL_EXPORTED_ENERGY_REGISTER_Q(new OBISIdentifier(1, null, 4, 8, 0, null), CosemQuantity.KILO_VAR_HOUR),
// The actual reactive's and threshold have no unit in the data and therefore are not quantity types.
EMETER_ACTUAL_REACTIVE_DELIVERY(new OBISIdentifier(1, 0, 3, 7, 0, null), CosemDecimal.INSTANCE),
EMETER_ACTUAL_REACTIVE_PRODUCTION(new OBISIdentifier(1, 0, 4, 7, 0, null), CosemDecimal.INSTANCE),
EMETER_ACTIVE_THRESHOLD_SMAX(new OBISIdentifier(0, 0, 17, 0, 0, null, true), CosemDecimal.INSTANCE),
EMETER_ACTUAL_REACTIVE_DELIVERY(new OBISIdentifier(1, 0, 3, 7, 0, null), CosemDecimal.INSTANCE_WITH_UNITS),
EMETER_ACTUAL_REACTIVE_PRODUCTION(new OBISIdentifier(1, 0, 4, 7, 0, null), CosemDecimal.INSTANCE_WITH_UNITS),
EMETER_ACTIVE_THRESHOLD_SMAX(new OBISIdentifier(0, 0, 17, 0, 0, null, true), CosemDecimal.INSTANCE_WITH_UNITS),
EMETER_INSTANT_REACTIVE_POWER_DELIVERY_L1(new OBISIdentifier(1, 0, 23, 7, 0, null), CosemQuantity.KILO_VAR),
EMETER_INSTANT_REACTIVE_POWER_DELIVERY_L2(new OBISIdentifier(1, 0, 43, 7, 0, null), CosemQuantity.KILO_VAR),
EMETER_INSTANT_REACTIVE_POWER_DELIVERY_L3(new OBISIdentifier(1, 0, 63, 7, 0, null), CosemQuantity.KILO_VAR),
Expand Down
Expand Up @@ -47,6 +47,7 @@ public static final List<Object[]> data() {
{ "Landis_Gyr_ZCF110", 25, },
{ "Sagemcom_XS210", 41, },
{ "smarty", 28, },
{ "smarty_with_units", 23, },
});
}
// @formatter:on
Expand Down
Expand Up @@ -59,6 +59,7 @@ public static final List<Object[]> data() {
{ "Landis_Gyr_ZCF110", EnumSet.of( DEVICE_V4, ELECTRICITY_V4_2, M3_V5_0)},
{ "Sagemcom_XS210", EnumSet.of( DEVICE_V4, ELECTRICITY_V4_2)},
{ "smarty", EnumSet.of( DEVICE_V5, ELECTRICITY_SMARTY_V1_0)},
{ "smarty_with_units", EnumSet.of( DEVICE_V5, ELECTRICITY_SMARTY_V1_0, M3_V4)},
});
}
// @formatter:on
Expand Down
@@ -0,0 +1,25 @@
/Lux5\253663629_D

1-3:0.2.8(42)
0-0:1.0.0(180130102122W)
0-0:42.0.0(53414731303330373030313134303034)
1-0:1.8.0(012345.000*kWh)
1-0:2.8.0(000000.123*kWh)
1-0:3.8.0(000012.345*kvarh)
1-0:4.8.0(001234.567*kvarh)
1-0:1.7.0(01.234*kW)
1-0:2.7.0(00.000*kW)
1-0:3.7.0(00.000*kvar)
1-0:4.7.0(00.000*kvar)
0-0:17.0.0(12.300*kVA)
0-0:96.3.10(1)
0-0:96.13.0()
0-0:96.13.2()
0-0:96.13.3()
0-0:96.13.4()
0-0:96.13.5()
0-1:24.1.0(003)
0-1:96.1.0(1234567890ABCDEF1234567890ABCD)
0-1:24.2.1(200606201530S)(01234.567*m3)
0-1:24.4.0(0)
!3076

0 comments on commit bb0b8f5

Please sign in to comment.