Skip to content

Commit

Permalink
[homewizard] Fix issue with missing gas values (#11639)
Browse files Browse the repository at this point in the history
This commit fixes a crash that happens when the smart meter does not provide gas values. The crash was caused by the empty timestamp.

Signed-off-by: Daniël van Os <[email protected]>
  • Loading branch information
Daniel-42 committed Dec 1, 2021
1 parent 1417b9c commit 4ba7179
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
Expand All @@ -43,6 +45,7 @@
@NonNullByDefault
public class HomeWizardHandler extends BaseThingHandler {

private final Logger logger = LoggerFactory.getLogger(HomeWizardHandler.class);
private final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();

Expand Down Expand Up @@ -171,30 +174,41 @@ private void pollingCode() {
updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L3,
new QuantityType<>(payload.getActivePowerL3W(), Units.WATT));

updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS,
new QuantityType<>(payload.getTotalGasM3(), SIUnits.CUBIC_METRE));

// 210119164000
// If no data from the gas meter is present, the json value will be null, which means gson ignores it,
// leaving the value in the payload object at 0.
long dtv = payload.getGasTimestamp();
long seconds = dtv % 100;

dtv /= 100;
long minutes = dtv % 100;

dtv /= 100;
long hours = dtv % 100;

dtv /= 100;
long day = dtv % 100;

dtv /= 100;
long month = dtv % 100;

dtv /= 100;
long year = dtv + 2000; // Where (When?) have I seen this before?

DateTimeType dtt = DateTimeType
.valueOf(String.format("%04d-%02d-%02dT%02d:%02d:%02d", year, month, day, hours, minutes, seconds));
updateState(HomeWizardBindingConstants.CHANNEL_GAS_TIMESTAMP, dtt);
if (dtv > 0) {
updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS,
new QuantityType<>(payload.getTotalGasM3(), SIUnits.CUBIC_METRE));

// 210119164000
long seconds = dtv % 100;

dtv /= 100;
long minutes = dtv % 100;

dtv /= 100;
long hours = dtv % 100;

dtv /= 100;
long day = dtv % 100;

dtv /= 100;
long month = dtv % 100;

dtv /= 100;
long year = dtv + 2000;

String dateString = String.format("%04d-%02d-%02dT%02d:%02d:%02d", year, month, day, hours, minutes,
seconds);
try {
DateTimeType dtt = DateTimeType.valueOf(dateString);
updateState(HomeWizardBindingConstants.CHANNEL_GAS_TIMESTAMP, dtt);
updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS,
new QuantityType<>(payload.getTotalGasM3(), SIUnits.CUBIC_METRE));
} catch (java.time.format.DateTimeParseException e) {
logger.warn("Unable to parse Gas timestamp {} / {}", payload.getGasTimestamp(), dateString);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class P1Payload {
private double activePowerL2W;
private double activePowerL3W;
private double totalGasM3;
private long gasTimestamp;
private long gasTimestamp = 0;

/**
* Getter for the smart meter version
Expand Down

0 comments on commit 4ba7179

Please sign in to comment.