[homewizard] Fix issue with missing gas values (#11666)

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>
This commit is contained in:
Daniël van Os 2021-12-01 21:53:11 +01:00 committed by GitHub
parent 2f035b53b6
commit 7e6a54baab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 19 deletions

View File

@ -13,6 +13,9 @@
package org.openhab.binding.homewizard.internal; package org.openhab.binding.homewizard.internal;
import java.io.IOException; 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.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -29,6 +32,8 @@ import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.FieldNamingPolicy; import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -43,6 +48,7 @@ import com.google.gson.GsonBuilder;
@NonNullByDefault @NonNullByDefault
public class HomeWizardHandler extends BaseThingHandler { 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) private final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create(); .create();
@ -171,30 +177,40 @@ public class HomeWizardHandler extends BaseThingHandler {
updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L3, updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L3,
new QuantityType<>(payload.getActivePowerL3W(), Units.WATT)); new QuantityType<>(payload.getActivePowerL3W(), Units.WATT));
// 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();
if (dtv > 0) {
updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS, updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS,
new QuantityType<>(payload.getTotalGasM3(), SIUnits.CUBIC_METRE)); new QuantityType<>(payload.getTotalGasM3(), SIUnits.CUBIC_METRE));
// 210119164000 // 210119164000
long dtv = payload.getGasTimestamp(); int seconds = (int) (dtv % 100);
long seconds = dtv % 100;
dtv /= 100; dtv /= 100;
long minutes = dtv % 100; int minutes = (int) (dtv % 100);
dtv /= 100; dtv /= 100;
long hours = dtv % 100; int hours = (int) (dtv % 100);
dtv /= 100; dtv /= 100;
long day = dtv % 100; int day = (int) (dtv % 100);
dtv /= 100; dtv /= 100;
long month = dtv % 100; int month = (int) (dtv % 100);
dtv /= 100; dtv /= 100;
long year = dtv + 2000; // Where (When?) have I seen this before? int year = (int) (dtv + 2000);
DateTimeType dtt = DateTimeType try {
.valueOf(String.format("%04d-%02d-%02dT%02d:%02d:%02d", year, month, day, hours, minutes, seconds)); DateTimeType dtt = new DateTimeType(
ZonedDateTime.of(year, month, day, hours, minutes, seconds, 0, ZoneId.systemDefault()));
updateState(HomeWizardBindingConstants.CHANNEL_GAS_TIMESTAMP, dtt); 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());
}
}
} }
} }

View File

@ -43,7 +43,7 @@ public class P1Payload {
private double activePowerL2W; private double activePowerL2W;
private double activePowerL3W; private double activePowerL3W;
private double totalGasM3; private double totalGasM3;
private long gasTimestamp; private long gasTimestamp = 0;
/** /**
* Getter for the smart meter version * Getter for the smart meter version