From 07da14ade36500eb62e9fec5268498490e8e1f96 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 19 Jan 2021 14:06:46 +0100 Subject: [PATCH] Fix logging, locking and documentation 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 --- .../org.openhab.binding.homewizard/README.md | 2 +- .../internal/HomeWizardHandler.java | 108 ++++++++++-------- 2 files changed, 59 insertions(+), 51 deletions(-) diff --git a/bundles/org.openhab.binding.homewizard/README.md b/bundles/org.openhab.binding.homewizard/README.md index a4f4765b7e8e9..818f046225ca6 100644 --- a/bundles/org.openhab.binding.homewizard/README.md +++ b/bundles/org.openhab.binding.homewizard/README.md @@ -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. | diff --git a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardHandler.java b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardHandler.java index 574661009919c..aa1659ccf64dd 100644 --- a/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardHandler.java +++ b/bundles/org.openhab.binding.homewizard/src/main/java/org/openhab/binding/homewizard/internal/HomeWizardHandler.java @@ -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)); } } }