]> git.basschouten.com Git - openhab-addons.git/commitdiff
[openwebnet] add date time synchronization feature for bus_gateway Things (#15115)
authorM Valla <12682715+mvalla@users.noreply.github.com>
Sun, 16 Jul 2023 10:42:52 +0000 (12:42 +0200)
committerGitHub <noreply@github.com>
Sun, 16 Jul 2023 10:42:52 +0000 (12:42 +0200)
* [openwebnet] fist commit DateTime synch

---------

Signed-off-by: Massimo Valla <mvcode00@gmail.com>
bundles/org.openhab.binding.openwebnet/README.md
bundles/org.openhab.binding.openwebnet/pom.xml
bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/OpenWebNetBridgeHandler.java
bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/handler/config/OpenWebNetBusBridgeConfig.java
bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/i18n/openwebnet.properties
bundles/org.openhab.binding.openwebnet/src/main/resources/OH-INF/thing/BusGateway.xml

index 4cb7b23b08aaa2a8a710c7674592359158321c19..b7b311701dc7912b7a0d8abbf7233f0b5324c47e 100644 (file)
@@ -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).
 
index 0511f19dbbf8eb58407153e7d875af4869b3dcef..92637aae27fe6302d5755371ae9bd6b7c62943ab 100644 (file)
@@ -23,7 +23,7 @@
     <dependency>
       <groupId>io.github.openwebnet4j</groupId>
       <artifactId>openwebnet4j</artifactId>
-      <version>0.9.1</version>
+      <version>0.10.0</version>
       <scope>compile</scope>
     </dependency>
 
index 1037599a51a5d369f02b7a62204adabe88d991c0..73a3a75f94b3c606c1b27493ce7eafea2e0d0f3e 100644 (file)
@@ -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;
index fa5d3be72e416f707d3763d9679d2a776b73477f..0dc66288b73216e4aa820cdde1b034624b042a0a 100644 (file)
@@ -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;
+    }
 }
index fa5e2daf63fecf3bc2c58def483f9b03a502aa13..0eca8770231fd3bcdf66ae6a0952735e78e4d178 100644 (file)
@@ -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
index d4d3fb9577ae56c39d3552279f69f56a12a42ea8..1f1f2b282c49a059bf304dd304e216afc91cae3f 100644 (file)
                                <default>false</default>
                        </parameter>
 
+                       <parameter name="dateTimeSynch" type="boolean">
+                               <label>Date Time Synchronisation</label>
+                               <description>Synchronise date and time of slave elements on the SCS BUS using openHAB timestamp (default: false)</description>
+                               <default>false</default>
+                       </parameter>
+
                </config-description>
 
        </bridge-type>