From: M Valla <12682715+mvalla@users.noreply.github.com> Date: Sun, 16 Jul 2023 10:42:52 +0000 (+0200) Subject: [openwebnet] add date time synchronization feature for bus_gateway Things (#15115) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=3e0099d6d1554af5cee0dbd4bdf80e3912883f36;p=openhab-addons.git [openwebnet] add date time synchronization feature for bus_gateway Things (#15115) * [openwebnet] fist commit DateTime synch --------- Signed-off-by: Massimo Valla --- diff --git a/bundles/org.openhab.binding.openwebnet/README.md b/bundles/org.openhab.binding.openwebnet/README.md index 4cb7b23b08..b7b311701d 100644 --- a/bundles/org.openhab.binding.openwebnet/README.md +++ b/bundles/org.openhab.binding.openwebnet/README.md @@ -107,6 +107,7 @@ Configuration parameters are: - if the BUS/SCS gateway is configured to accept connections from the openHAB computer IP address, no password should be required - in all other cases, a password must be configured. This includes gateways that have been discovered and added from Inbox: without a password configured they will remain OFFLINE - `discoveryByActivation`: discover BUS devices when they are activated also when a device scan hasn't been started from Inbox (`boolean`, _optional_, default: `false`). See [Discovery by Activation](#discovery-by-activation). +- `dateTimeSynch`: synchronise date and time of slave elements on the SCS BUS using openHAB timestamp (`boolean`, _optional_, default: `false`). Set this parameter to `true` to send time-date synchronisation commands on the BUS when the timestamp received from the gateway differs by more than 1 minute from that of openHAB. Useful if the BUS gateway is not syncronized with Internet time servers and with daylight saving time changes. Alternatively the BUS/SCS Gateway thing can be configured using the `.things` file, see `openwebnet.things` example [below](#full-example). diff --git a/bundles/org.openhab.binding.openwebnet/pom.xml b/bundles/org.openhab.binding.openwebnet/pom.xml index 0511f19dbb..92637aae27 100644 --- a/bundles/org.openhab.binding.openwebnet/pom.xml +++ b/bundles/org.openhab.binding.openwebnet/pom.xml @@ -23,7 +23,7 @@ io.github.openwebnet4j openwebnet4j - 0.9.1 + 0.10.0 compile diff --git a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/OpenWebNetBridgeHandler.java b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/OpenWebNetBridgeHandler.java index 1037599a51..73a3a75f94 100644 --- a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/OpenWebNetBridgeHandler.java +++ b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/OpenWebNetBridgeHandler.java @@ -14,6 +14,8 @@ package org.openhab.binding.openwebnet.internal.handler; import static org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants.*; +import java.time.Duration; +import java.time.ZonedDateTime; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -91,6 +93,8 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement private static final int REFRESH_ALL_CHECK_DELAY_SEC = 20; // Delay to wait to check which devices are // online/offline + private static final int DATETIME_SYNCH_DIFF_SEC = 60; // Difference from BUS date time + private long lastRegisteredDeviceTS = -1; // timestamp when the last device has been associated to the bridge private long refreshAllDevicesDelay = 0; // delay waited before starting all devices refresh @@ -114,6 +118,7 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement private boolean scanIsActive = false; // a device scan has been activated by OpenWebNetDeviceDiscoveryService; private boolean discoveryByActivation; + private boolean dateTimeSynch = false; public OpenWebNetBridgeHandler(Bridge bridge) { super(bridge); @@ -201,8 +206,10 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement passwdMasked = "******"; } discoveryByActivation = busBridgeConfig.getDiscoveryByActivation(); - logger.debug("Creating new BUS gateway with config properties: {}:{}, pwd={}, discoveryByActivation={}", - host, port, passwdMasked, discoveryByActivation); + dateTimeSynch = busBridgeConfig.getDateTimeSynch(); + logger.debug( + "Creating new BUS gateway with config properties: {}:{}, pwd={}, discoveryByActivation={}, dateTimeSynch={}", + host, port, passwdMasked, discoveryByActivation, dateTimeSynch); return new BUSGateway(host, port, passwd); } } @@ -499,7 +506,10 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement } // GATEWAY MANAGEMENT if (msg instanceof GatewayMgmt) { - // noop + GatewayMgmt gwMsg = (GatewayMgmt) msg; + if (dateTimeSynch && GatewayMgmt.DimGatewayMgmt.DATETIME.equals(gwMsg.getDim())) { + checkDateTimeDiff(gwMsg); + } return; } @@ -528,6 +538,31 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement } } + private void checkDateTimeDiff(GatewayMgmt gwMsg) { + try { + ZonedDateTime now = ZonedDateTime.now(); + ZonedDateTime gwTime = GatewayMgmt.parseDateTime(gwMsg); + long diff = Math.abs(Duration.between(now, gwTime).toSeconds()); + if (diff > DATETIME_SYNCH_DIFF_SEC) { + logger.debug("checkDateTimeDiff: difference is more than 60s: {}s", diff); + OpenGateway gw = gateway; + if (gw != null) { + logger.debug("checkDateTimeDiff: synch DateTime to: {}", now); + try { + gw.send(GatewayMgmt.requestSetDateTime(now)); + } catch (OWNException e) { + logger.warn("checkDateTimeDiff: Exception while sending set DateTime command: {}", + e.getMessage()); + } + } + } else { + logger.debug("checkDateTimeDiff: DateTime difference: {}s", diff); + } + } catch (FrameException e) { + logger.warn("checkDateTimeDiff: FrameException while parsing {}", e.getMessage()); + } + } + @Override public void onConnected() { isGatewayConnected = true; diff --git a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/config/OpenWebNetBusBridgeConfig.java b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/config/OpenWebNetBusBridgeConfig.java index fa5d3be72e..0dc66288b7 100644 --- a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/config/OpenWebNetBusBridgeConfig.java +++ b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/config/OpenWebNetBusBridgeConfig.java @@ -30,6 +30,7 @@ public class OpenWebNetBusBridgeConfig { private @Nullable String host; private String passwd = "12345"; private boolean discoveryByActivation = false; + private boolean dateTimeSynch = false; public BigDecimal getPort() { return port; @@ -46,4 +47,8 @@ public class OpenWebNetBusBridgeConfig { public Boolean getDiscoveryByActivation() { return discoveryByActivation; } + + public Boolean getDateTimeSynch() { + return dateTimeSynch; + } } diff --git a/bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/i18n/openwebnet.properties b/bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/i18n/openwebnet.properties index fa5e2daf63..0eca877023 100644 --- a/bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/i18n/openwebnet.properties +++ b/bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/i18n/openwebnet.properties @@ -74,6 +74,8 @@ thing-type.config.openwebnet.bus_dry_contact_ir.where.label = OpenWebNet Address thing-type.config.openwebnet.bus_dry_contact_ir.where.description = Automation Dry Contacts (N=1-201): example N=60 --> where=360. Alarm Dry Contacts and IR sensors (Zone=1-9, N=1-9): example Zone=4, N=5 --> where=345 thing-type.config.openwebnet.bus_energy_meter.where.label = OpenWebNet Address (where) thing-type.config.openwebnet.bus_energy_meter.where.description = Example: 5N with N=[1-255] +thing-type.config.openwebnet.bus_gateway.dateTimeSynch.label = Date Time Synchronisation +thing-type.config.openwebnet.bus_gateway.dateTimeSynch.description = Synchronise date and time of slave elements on the SCS BUS using openHAB timestamp (default: false) thing-type.config.openwebnet.bus_gateway.discoveryByActivation.label = Discovery By Activation thing-type.config.openwebnet.bus_gateway.discoveryByActivation.description = Discover BUS devices when they are activated (also when a device scan is not active) (default: false) thing-type.config.openwebnet.bus_gateway.host.label = Host diff --git a/bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/thing/BusGateway.xml b/bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/thing/BusGateway.xml index d4d3fb9577..1f1f2b282c 100644 --- a/bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/thing/BusGateway.xml +++ b/bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/thing/BusGateway.xml @@ -45,6 +45,12 @@ false + + + Synchronise date and time of slave elements on the SCS BUS using openHAB timestamp (default: false) + false + +