Skip to content

Commit

Permalink
Fix logging, locking and documentation
Browse files Browse the repository at this point in the history
Fixed typo in the documentation
Added lock when using ocnfig data
Increased timeout for get and no longer dumping timeout exception to the log anymore
  • Loading branch information
Daniel-42 committed Jan 19, 2021
1 parent b023a73 commit 07da14a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 51 deletions.
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.homewizard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Note that update rate of the P1 Meter itself depends on the telegrams it receive
|-----------------------|---------------|--------------------------------------------------------------------------------------------|
| total_power_import_t1 | Number:Power | The most recently reported total imported power in KWh by counter 1 |
| total_power_import_t2 | Number:Power | The most recently reported total imported power in KWh by counter 2 |
| total_power_import_t1 | Number:Power | The most recently reported total exported power in KWh by counter 1 |
| total_power_export_t1 | Number:Power | The most recently reported total exported power in KWh by counter 1 |
| total_power_export_t2 | Number:Power | The most recently reported total exported power in KWh by counter 2 |
| active_power | Number:Power | The current net total power in W. It will be below 0 if power is currently being exported. |
| active_power_l1 | Number:Power | The current net total power in W for phase 1. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,66 +201,74 @@ private void startPolling() {
* The actual polling loop
*/
private void pollingCode() {
final String query;
final String result;

// get a local copy of the configuration parameters
try {
result = HttpUtil.executeUrl("GET", apiURL, 500);
configurationLock.lock();
query = apiURL;
} catch (Exception e) {
return;
} finally {
configurationLock.unlock();
}

if (result.trim().isEmpty()) {
logger.warn("P1 Wi-Fi meter API at URI {} returned empty result", apiURL);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"P1 Meter API returned empty status");
return;
}
try {
result = HttpUtil.executeUrl("GET", query, 750);
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Unable to query P1 Meter");
return;
}

P1Payload payload = gson.fromJson(result, P1Payload.class);
if (payload != null) {
if (payload.getMeter_model() == "") {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Results from API seem empty");
return;
}
if (result.trim().isEmpty()) {
logger.warn("P1 Wi-Fi meter API at URI {} returned empty result", apiURL);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"P1 Meter API returned empty status");
return;
}

updateStatus(ThingStatus.ONLINE);
P1Payload payload = gson.fromJson(result, P1Payload.class);
if (payload != null) {
if (payload.getMeter_model() == "") {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Results from API seem empty");
return;
}

if (meterModel != payload.getMeter_model()) {
meterModel = payload.getMeter_model();
updateProperty(HomeWizardBindingConstants.PROPERTY_METER_MODEL, meterModel);
}
updateStatus(ThingStatus.ONLINE);

if (meterVersion != payload.getSmr_version()) {
meterVersion = payload.getSmr_version();
updateProperty(HomeWizardBindingConstants.PROPERTY_METER_VERSION,
String.format("%d", meterVersion));
}
if (meterModel != payload.getMeter_model()) {
meterModel = payload.getMeter_model();
updateProperty(HomeWizardBindingConstants.PROPERTY_METER_MODEL, meterModel);
}

updateState(HomeWizardBindingConstants.CHANNEL_POWER_IMPORT_T1,
new QuantityType<>(payload.getTotal_power_import_t1_kwh(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_POWER_IMPORT_T2,
new QuantityType<>(payload.getTotal_power_import_t2_kwh(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_POWER_EXPORT_T1,
new QuantityType<>(payload.getTotal_power_export_t1_kwh(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_POWER_EXPORT_T2,
new QuantityType<>(payload.getTotal_power_export_t2_kwh(), Units.WATT));

updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER,
new QuantityType<>(payload.getActive_power_w(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L1,
new QuantityType<>(payload.getActive_power_l1_w(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L2,
new QuantityType<>(payload.getActive_power_l2_w(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L3,
new QuantityType<>(payload.getActive_power_l3_w(), Units.WATT));

updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS,
new QuantityType<>(payload.getTotal_gas_m3(), Units.ONE)); // could convert, 1m^3 = 1000 liters
updateState(HomeWizardBindingConstants.CHANNEL_GAS_TIMESTAMP,
new QuantityType<>(payload.getGas_timestamp(), Units.SECOND));
if (meterVersion != payload.getSmr_version()) {
meterVersion = payload.getSmr_version();
updateProperty(HomeWizardBindingConstants.PROPERTY_METER_VERSION, String.format("%d", meterVersion));
}

} catch (IOException e) {
logger.warn("IO Exception when querying P1 Wi-Fi meter API", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Unable to query P1 Meter");
return;
updateState(HomeWizardBindingConstants.CHANNEL_POWER_IMPORT_T1,
new QuantityType<>(payload.getTotal_power_import_t1_kwh(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_POWER_IMPORT_T2,
new QuantityType<>(payload.getTotal_power_import_t2_kwh(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_POWER_EXPORT_T1,
new QuantityType<>(payload.getTotal_power_export_t1_kwh(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_POWER_EXPORT_T2,
new QuantityType<>(payload.getTotal_power_export_t2_kwh(), Units.WATT));

updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER,
new QuantityType<>(payload.getActive_power_w(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L1,
new QuantityType<>(payload.getActive_power_l1_w(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L2,
new QuantityType<>(payload.getActive_power_l2_w(), Units.WATT));
updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER_L3,
new QuantityType<>(payload.getActive_power_l3_w(), Units.WATT));

updateState(HomeWizardBindingConstants.CHANNEL_TOTAL_GAS,
new QuantityType<>(payload.getTotal_gas_m3(), Units.ONE)); // could convert, 1m^3 = 1000 liters
updateState(HomeWizardBindingConstants.CHANNEL_GAS_TIMESTAMP,
new QuantityType<>(payload.getGas_timestamp(), Units.SECOND));
}
}
}

0 comments on commit 07da14a

Please sign in to comment.