Skip to content

Commit

Permalink
[homewizard] Fix issue with missing gas values (openhab#11666)
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 <daniel@supercell.nl>
  • Loading branch information
Daniel-42 authored and andan67 committed Nov 5, 2022
1 parent 3c13a28 commit 87fbbb3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
package org.openhab.binding.homewizard.internal;

import java.io.IOException;
import java.time.DateTimeException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

Expand All @@ -29,6 +32,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 +48,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 +177,40 @@ 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;
if (dtv > 0) {
updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS,
new QuantityType<>(payload.getTotalGasM3(), SIUnits.CUBIC_METRE));

// 210119164000
int seconds = (int) (dtv % 100);

dtv /= 100;
long minutes = dtv % 100;
dtv /= 100;
int minutes = (int) (dtv % 100);

dtv /= 100;
long hours = dtv % 100;
dtv /= 100;
int hours = (int) (dtv % 100);

dtv /= 100;
long day = dtv % 100;
dtv /= 100;
int day = (int) (dtv % 100);

dtv /= 100;
long month = dtv % 100;
dtv /= 100;
int month = (int) (dtv % 100);

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

DateTimeType dtt = DateTimeType
.valueOf(String.format("%04d-%02d-%02dT%02d:%02d:%02d", year, month, day, hours, minutes, seconds));
updateState(HomeWizardBindingConstants.CHANNEL_GAS_TIMESTAMP, dtt);
try {
DateTimeType dtt = new DateTimeType(
ZonedDateTime.of(year, month, day, hours, minutes, seconds, 0, ZoneId.systemDefault()));
updateState(HomeWizardBindingConstants.CHANNEL_GAS_TIMESTAMP, dtt);
updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS,
new QuantityType<>(payload.getTotalGasM3(), SIUnits.CUBIC_METRE));
} catch (DateTimeException e) {
logger.warn("Unable to parse Gas timestamp: {}", payload.getGasTimestamp());
}
}
}
}
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 87fbbb3

Please sign in to comment.