[openwebnet] add date time synchronization feature for bus_gateway Things (#15115)
* [openwebnet] fist commit DateTime synch --------- Signed-off-by: Massimo Valla <mvcode00@gmail.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -45,6 +45,12 @@
|
||||
<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>
|
||||
|
||||
Reference in New Issue
Block a user