Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[homewizard] Fix issue with missing gas values #11666

Merged
merged 2 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
wborn marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
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