added migrated 2.x add-ons
Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
32
bundles/org.openhab.binding.ntp/.classpath
Normal file
32
bundles/org.openhab.binding.ntp/.classpath
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
23
bundles/org.openhab.binding.ntp/.project
Normal file
23
bundles/org.openhab.binding.ntp/.project
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>org.openhab.binding.ntp</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
13
bundles/org.openhab.binding.ntp/NOTICE
Normal file
13
bundles/org.openhab.binding.ntp/NOTICE
Normal file
@@ -0,0 +1,13 @@
|
||||
This content is produced and maintained by the openHAB project.
|
||||
|
||||
* Project home: https://www.openhab.org
|
||||
|
||||
== Declared Project Licenses
|
||||
|
||||
This program and the accompanying materials are made available under the terms
|
||||
of the Eclipse Public License 2.0 which is available at
|
||||
https://www.eclipse.org/legal/epl-2.0/.
|
||||
|
||||
== Source Code
|
||||
|
||||
https://github.com/openhab/openhab-addons
|
||||
53
bundles/org.openhab.binding.ntp/README.md
Normal file
53
bundles/org.openhab.binding.ntp/README.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# NTP Binding
|
||||
|
||||
The NTP binding is used for displaying the local date and time based update from an NTP server.
|
||||
|
||||
## Supported Things
|
||||
|
||||
This binding supports one ThingType: ntp
|
||||
|
||||
## Discovery
|
||||
|
||||
Discovery is used to place one default item in the inbox as a convenient way to add a Thing for the local time.
|
||||
|
||||
## Binding Configuration
|
||||
|
||||
The binding has no configuration options, all configuration is done at Thing level.
|
||||
|
||||
## Thing Configuration
|
||||
|
||||
The thing has a few configuration options:
|
||||
|
||||
| Option | Description |
|
||||
|-----------------|--------------------------------------------------- |
|
||||
| hostname | The NTP server hostname, e.g. nl.pool.ntp.org |
|
||||
| refreshInterval | Interval that new time updates are posted to the eventbus in seconds. Default is 60s. |
|
||||
| refreshNtp | Number of updates between querying the NTP server (e.g. with refreshinterval = 60 (seconds) and refreshNtp = 30 the NTP server is queried each half hour). Default is 30. |
|
||||
| serverPort | The port that the NTP server could use. Default is 123. |
|
||||
| timeZone | The configured timezone. Can be left blank for using the timezone defined as openHAB configuration setting (or default system timezone if not defined). |
|
||||
|
||||
|
||||
## Channels
|
||||
|
||||
The ntp binding has two channels:
|
||||
|
||||
* `dateTime` which provides the data in a dateTime type
|
||||
* `string` which provides the data in a string type. The string channel can be configured with the formatting of the date & time. This also allows proper representation of timezones other than the java machine default one.
|
||||
|
||||
See the [java documentation](https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html) for the detailed information on the formatting
|
||||
|
||||
|
||||
|
||||
## Full Example
|
||||
|
||||
Things:
|
||||
|
||||
```
|
||||
ntp:ntp:demo [ hostname="nl.pool.ntp.org", refreshInterval=60, refreshNtp=30 ]
|
||||
```
|
||||
|
||||
Items:
|
||||
|
||||
```
|
||||
DateTime Date "Date [%1$tA, %1$td.%1$tm.%1$tY %1$tH:%1$tM]" { channel="ntp:ntp:demo:dateTime" }
|
||||
```
|
||||
17
bundles/org.openhab.binding.ntp/pom.xml
Normal file
17
bundles/org.openhab.binding.ntp/pom.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.openhab.addons.bundles</groupId>
|
||||
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
|
||||
<version>3.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>org.openhab.binding.ntp</artifactId>
|
||||
|
||||
<name>openHAB Add-ons :: Bundles :: NTP Binding</name>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<features name="org.openhab.binding.ntp-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
|
||||
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>
|
||||
|
||||
<feature name="openhab-binding-ntp" description="NTP Binding" version="${project.version}">
|
||||
<feature>openhab-runtime-base</feature>
|
||||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.ntp/${project.version}</bundle>
|
||||
</feature>
|
||||
</features>
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.ntp.internal;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.core.thing.ThingTypeUID;
|
||||
|
||||
/**
|
||||
* The {@link ntpBinding} class defines common constants, which are used across
|
||||
* the whole binding.
|
||||
*
|
||||
* @author Marcel Verpaalen - Initial contribution
|
||||
*
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class NtpBindingConstants {
|
||||
|
||||
public static final String BINDING_ID = "ntp";
|
||||
|
||||
// List of all Thing Type UIDs
|
||||
public static final ThingTypeUID THING_TYPE_NTP = new ThingTypeUID(BINDING_ID, "ntp");
|
||||
|
||||
// List of all Channel ids
|
||||
public static final String CHANNEL_DATE_TIME = "dateTime";
|
||||
public static final String CHANNEL_STRING = "string";
|
||||
|
||||
// Custom Properties
|
||||
public static final String PROPERTY_NTP_SERVER_HOST = "hostname";
|
||||
public static final String PROPERTY_REFRESH_INTERVAL = "refreshInterval";
|
||||
public static final String PROPERTY_REFRESH_NTP = "refreshNtp";
|
||||
public static final String PROPERTY_TIMEZONE = "timeZone";
|
||||
public static final String PROPERTY_LOCALE = "locale";
|
||||
public static final String PROPERTY_DATE_TIME_FORMAT = "DateTimeFormat";
|
||||
public static final String PROPERTY_NTP_SERVER_PORT = "serverPort";
|
||||
|
||||
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_NTP);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.ntp.internal;
|
||||
|
||||
import static org.openhab.binding.ntp.internal.NtpBindingConstants.*;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.ntp.internal.handler.NtpHandler;
|
||||
import org.openhab.core.i18n.TimeZoneProvider;
|
||||
import org.openhab.core.thing.Thing;
|
||||
import org.openhab.core.thing.ThingTypeUID;
|
||||
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
|
||||
import org.openhab.core.thing.binding.ThingHandler;
|
||||
import org.openhab.core.thing.binding.ThingHandlerFactory;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
|
||||
/**
|
||||
* The {@link NtpHandlerFactory} is responsible for creating things and thing
|
||||
* handlers.
|
||||
*
|
||||
* @author Marcel Verpaalen - Initial contribution
|
||||
* @author Markus Rathgeb - Add locale provider support
|
||||
*/
|
||||
@NonNullByDefault
|
||||
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.ntp")
|
||||
public class NtpHandlerFactory extends BaseThingHandlerFactory {
|
||||
|
||||
private final TimeZoneProvider timeZoneProvider;
|
||||
|
||||
@Activate
|
||||
public NtpHandlerFactory(final @Reference TimeZoneProvider timeZoneProvider) {
|
||||
this.timeZoneProvider = timeZoneProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
|
||||
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable ThingHandler createHandler(Thing thing) {
|
||||
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
|
||||
|
||||
if (THING_TYPE_NTP.equals(thingTypeUID)) {
|
||||
return new NtpHandler(thing, timeZoneProvider);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.ntp.internal.config;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
* The {@link NtpStringChannelConfiguration} is responsible for holding
|
||||
* the configuration settings for the channel "string"
|
||||
*
|
||||
* @author Laurent Garnier - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class NtpStringChannelConfiguration {
|
||||
|
||||
public String DateTimeFormat = "yyyy-MM-dd HH:mm:ss z";
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.ntp.internal.config;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* The {@link NtpThingConfiguration} is responsible for holding
|
||||
* the thing configuration settings
|
||||
*
|
||||
* @author Laurent Garnier - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class NtpThingConfiguration {
|
||||
|
||||
public String hostname = "0.pool.ntp.org";
|
||||
public int refreshInterval = 60;
|
||||
public int refreshNtp = 30;
|
||||
public int serverPort = 123;
|
||||
public @Nullable String timeZone;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.ntp.internal.discovery;
|
||||
|
||||
import static org.openhab.binding.ntp.internal.NtpBindingConstants.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.core.config.discovery.AbstractDiscoveryService;
|
||||
import org.openhab.core.config.discovery.DiscoveryResult;
|
||||
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
|
||||
import org.openhab.core.config.discovery.DiscoveryService;
|
||||
import org.openhab.core.i18n.LocaleProvider;
|
||||
import org.openhab.core.i18n.TimeZoneProvider;
|
||||
import org.openhab.core.i18n.TranslationProvider;
|
||||
import org.openhab.core.thing.ThingUID;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
|
||||
/**
|
||||
*
|
||||
* The {@link NtpDiscovery} is used to add a ntp Thing for the local time in the discovery inbox
|
||||
* *
|
||||
*
|
||||
* @author Marcel Verpaalen - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
@Component(service = DiscoveryService.class, immediate = true, configurationPid = "discovery.ntp")
|
||||
public class NtpDiscovery extends AbstractDiscoveryService {
|
||||
|
||||
private final TimeZoneProvider timeZoneProvider;
|
||||
|
||||
@Activate
|
||||
public NtpDiscovery(final @Reference LocaleProvider localeProvider,
|
||||
final @Reference TranslationProvider i18nProvider, final @Reference TimeZoneProvider timeZoneProvider,
|
||||
@Nullable Map<String, @Nullable Object> configProperties) throws IllegalArgumentException {
|
||||
super(SUPPORTED_THING_TYPES_UIDS, 2);
|
||||
this.localeProvider = localeProvider;
|
||||
this.i18nProvider = i18nProvider;
|
||||
this.timeZoneProvider = timeZoneProvider;
|
||||
activate(configProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void startBackgroundDiscovery() {
|
||||
scheduler.schedule(() -> {
|
||||
discoverNtp();
|
||||
}, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void startScan() {
|
||||
discoverNtp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a ntp Thing for the local time in the discovery inbox
|
||||
*/
|
||||
private void discoverNtp() {
|
||||
Map<String, Object> properties = new HashMap<>(4);
|
||||
properties.put(PROPERTY_TIMEZONE, timeZoneProvider.getTimeZone().getId());
|
||||
ThingUID uid = new ThingUID(THING_TYPE_NTP, "local");
|
||||
DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("Local Time")
|
||||
.build();
|
||||
thingDiscovered(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.ntp.internal.handler;
|
||||
|
||||
import static org.openhab.binding.ntp.internal.NtpBindingConstants.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.time.DateTimeException;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.net.ntp.NTPUDPClient;
|
||||
import org.apache.commons.net.ntp.TimeInfo;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.ntp.internal.config.NtpStringChannelConfiguration;
|
||||
import org.openhab.binding.ntp.internal.config.NtpThingConfiguration;
|
||||
import org.openhab.core.i18n.TimeZoneProvider;
|
||||
import org.openhab.core.library.types.DateTimeType;
|
||||
import org.openhab.core.library.types.StringType;
|
||||
import org.openhab.core.thing.Channel;
|
||||
import org.openhab.core.thing.ChannelUID;
|
||||
import org.openhab.core.thing.Thing;
|
||||
import org.openhab.core.thing.ThingStatus;
|
||||
import org.openhab.core.thing.ThingStatusDetail;
|
||||
import org.openhab.core.thing.binding.BaseThingHandler;
|
||||
import org.openhab.core.types.Command;
|
||||
import org.openhab.core.types.RefreshType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The NTP Refresh Service polls the configured timeserver with a configurable
|
||||
* interval and posts a new event of type ({@link DateTimeType}.
|
||||
*
|
||||
* The {@link NtpHandler} is responsible for handling commands, which are sent
|
||||
* to one of the channels.
|
||||
*
|
||||
* @author Marcel Verpaalen - Initial contribution OH2 ntp binding
|
||||
* @author Thomas.Eichstaedt-Engelen - OH1 ntp binding (getTime routine)
|
||||
* @author Markus Rathgeb - Add locale provider
|
||||
* @author Erdoan Hadzhiyusein - Adapted the class to work with the new DateTimeType
|
||||
* @author Laurent Garnier - null annotations, TimeZoneProvider, configuration settings cleanup
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class NtpHandler extends BaseThingHandler {
|
||||
|
||||
/** timeout for requests to the NTP server */
|
||||
private static final int NTP_TIMEOUT = 30000;
|
||||
|
||||
public static final String DATE_PATTERN_WITH_TZ = "yyyy-MM-dd HH:mm:ss z";
|
||||
private static final DateTimeFormatter DATE_FORMATTER_WITH_TZ = DateTimeFormatter.ofPattern(DATE_PATTERN_WITH_TZ);
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(NtpHandler.class);
|
||||
|
||||
private final TimeZoneProvider timeZoneProvider;
|
||||
|
||||
/** for publish purposes */
|
||||
private DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern(DATE_PATTERN_WITH_TZ);
|
||||
|
||||
private NtpThingConfiguration configuration = new NtpThingConfiguration();
|
||||
|
||||
private @Nullable ScheduledFuture<?> refreshJob;
|
||||
|
||||
private @Nullable ZoneId timeZoneId;
|
||||
|
||||
/** NTP refresh counter */
|
||||
private int refreshNtpCount = 0;
|
||||
/** NTP system time delta */
|
||||
private long timeOffset;
|
||||
|
||||
public NtpHandler(final Thing thing, final TimeZoneProvider timeZoneProvider) {
|
||||
super(thing);
|
||||
this.timeZoneProvider = timeZoneProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommand(ChannelUID channelUID, Command command) {
|
||||
if (command == RefreshType.REFRESH) {
|
||||
logger.debug("Refreshing channel '{}' for '{}'.", channelUID.getId(), getThing().getUID());
|
||||
refreshTimeDate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
logger.debug("Initializing NTP handler for '{}'.", getThing().getUID());
|
||||
|
||||
configuration = getConfigAs(NtpThingConfiguration.class);
|
||||
|
||||
refreshNtpCount = 0;
|
||||
|
||||
if (configuration.timeZone != null) {
|
||||
logger.debug("{} with timezone '{}' set in configuration setting '{}'", getThing().getUID(),
|
||||
configuration.timeZone, PROPERTY_TIMEZONE);
|
||||
try {
|
||||
timeZoneId = ZoneId.of(configuration.timeZone);
|
||||
} catch (DateTimeException e) {
|
||||
timeZoneId = null;
|
||||
logger.debug("{} using default timezone '{}', because configuration setting '{}' is invalid: {}",
|
||||
getThing().getUID(), timeZoneProvider.getTimeZone(), PROPERTY_TIMEZONE, e.getMessage());
|
||||
}
|
||||
} else {
|
||||
timeZoneId = null;
|
||||
logger.debug("{} using default timezone '{}', because configuration setting '{}' is null.",
|
||||
getThing().getUID(), timeZoneProvider.getTimeZone(), PROPERTY_TIMEZONE);
|
||||
}
|
||||
ZoneId zoneId = timeZoneId != null ? timeZoneId : timeZoneProvider.getTimeZone();
|
||||
|
||||
Channel stringChannel = getThing().getChannel(CHANNEL_STRING);
|
||||
if (stringChannel != null) {
|
||||
String dateTimeFormatString = stringChannel.getConfiguration()
|
||||
.as(NtpStringChannelConfiguration.class).DateTimeFormat;
|
||||
if (!dateTimeFormatString.isEmpty()) {
|
||||
logger.debug("Date format set in config for channel '{}': {}", CHANNEL_STRING, dateTimeFormatString);
|
||||
try {
|
||||
dateTimeFormat = DateTimeFormatter.ofPattern(dateTimeFormatString);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
logger.debug("Invalid date format set in config for channel '{}'. Using default format. ({})",
|
||||
CHANNEL_STRING, ex.getMessage());
|
||||
dateTimeFormat = DateTimeFormatter.ofPattern(DATE_PATTERN_WITH_TZ);
|
||||
}
|
||||
} else {
|
||||
logger.debug("No date format set in config for channel '{}'. Using default format.", CHANNEL_STRING);
|
||||
dateTimeFormat = DateTimeFormatter.ofPattern(DATE_PATTERN_WITH_TZ);
|
||||
}
|
||||
} else {
|
||||
logger.debug("Missing channel: '{}'", CHANNEL_STRING);
|
||||
}
|
||||
dateTimeFormat.withZone(zoneId);
|
||||
|
||||
logger.debug(
|
||||
"Initialized NTP handler '{}' with configuration: host '{}', port {}, refresh interval {}, refresh frequency {}, timezone {}.",
|
||||
getThing().getUID(), configuration.hostname, configuration.serverPort, configuration.refreshInterval,
|
||||
configuration.refreshNtp, zoneId);
|
||||
|
||||
refreshJob = scheduler.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
refreshTimeDate();
|
||||
} catch (Exception e) {
|
||||
logger.debug("Exception occurred during refresh: {}", e.getMessage(), e);
|
||||
}
|
||||
}, 0, configuration.refreshInterval, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
logger.debug("Disposing NTP handler for '{}'.", getThing().getUID());
|
||||
ScheduledFuture<?> job = refreshJob;
|
||||
if (job != null) {
|
||||
job.cancel(true);
|
||||
}
|
||||
refreshJob = null;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
private synchronized void refreshTimeDate() {
|
||||
long networkTimeInMillis;
|
||||
if (refreshNtpCount <= 0) {
|
||||
networkTimeInMillis = getTime(configuration.hostname, configuration.serverPort);
|
||||
timeOffset = networkTimeInMillis - System.currentTimeMillis();
|
||||
logger.debug("{} delta system time: {}", getThing().getUID(), timeOffset);
|
||||
refreshNtpCount = configuration.refreshNtp;
|
||||
} else {
|
||||
networkTimeInMillis = System.currentTimeMillis() + timeOffset;
|
||||
refreshNtpCount--;
|
||||
}
|
||||
|
||||
ZoneId zoneId = timeZoneId != null ? timeZoneId : timeZoneProvider.getTimeZone();
|
||||
ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(networkTimeInMillis), zoneId);
|
||||
updateState(CHANNEL_DATE_TIME, new DateTimeType(zoned));
|
||||
dateTimeFormat.withZone(zoneId);
|
||||
updateState(CHANNEL_STRING, new StringType(dateTimeFormat.format(zoned)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries the given timeserver <code>hostname</code> and returns the time
|
||||
* in milliseconds.
|
||||
*
|
||||
* @param hostname the timeserver hostname to query
|
||||
* @param port the timeserver port to query
|
||||
* @return the time in milliseconds or the current time of the system if an
|
||||
* error occurs.
|
||||
*/
|
||||
private long getTime(String hostname, int port) {
|
||||
try {
|
||||
NTPUDPClient timeClient = new NTPUDPClient();
|
||||
timeClient.setDefaultTimeout(NTP_TIMEOUT);
|
||||
InetAddress inetAddress = InetAddress.getByName(hostname);
|
||||
TimeInfo timeInfo = timeClient.getTime(inetAddress, port);
|
||||
timeInfo.computeDetails();
|
||||
|
||||
long serverMillis = timeInfo.getReturnTime() + timeInfo.getOffset();
|
||||
ZoneId zoneId = timeZoneId != null ? timeZoneId : timeZoneProvider.getTimeZone();
|
||||
ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(serverMillis), zoneId);
|
||||
logger.debug("{} Got time update from host '{}': {}.", getThing().getUID(), hostname,
|
||||
zoned.format(DATE_FORMATTER_WITH_TZ));
|
||||
updateStatus(ThingStatus.ONLINE);
|
||||
return serverMillis;
|
||||
} catch (UnknownHostException uhe) {
|
||||
logger.debug(
|
||||
"{} The given hostname '{}' of the timeserver is unknown -> returning current sytem time instead. ({})",
|
||||
getThing().getUID(), hostname, uhe.getMessage());
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
||||
"@text/offline.comm-error-unknown-host [\"" + hostname + "\"]");
|
||||
} catch (IOException ioe) {
|
||||
logger.debug(
|
||||
"{} Couldn't establish network connection to host '{}' -> returning current sytem time instead. ({})",
|
||||
getThing().getUID(), hostname, ioe.getMessage());
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
||||
"@text/offline.comm-error-connection [\"" + hostname + "\"]");
|
||||
}
|
||||
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<binding:binding id="ntp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:binding="https://openhab.org/schemas/binding/v1.0.0"
|
||||
xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd">
|
||||
|
||||
<name>NTP Binding</name>
|
||||
<description>The NTP Binding polls the configured timeserver and post the current date and time.</description>
|
||||
<author>Marcel Verpaalen</author>
|
||||
|
||||
</binding:binding>
|
||||
@@ -0,0 +1,6 @@
|
||||
# Thing status descriptions
|
||||
offline.comm-error-unknown-host = The timeserver hostname {0} is unknown -> returning current sytem time instead.
|
||||
offline.comm-error-connection = The network connection to the timeserver {0} cannot be established -> returning current sytem time instead.
|
||||
|
||||
# Discovery result
|
||||
discovery.ntp.ntp.local.label = Local Time
|
||||
@@ -0,0 +1,36 @@
|
||||
# binding
|
||||
binding.ntp.name = NTP Binding
|
||||
binding.ntp.description = Dieses Binding fragt das aktuelle Datum und die aktuelle Zeit bei einem Zeitserver ab.
|
||||
|
||||
# thing types
|
||||
thing-type.ntp.ntp.label = NTP-Server
|
||||
thing-type.ntp.ntp.description = NTP-Server zur Abfrage des Datums und der Zeit.
|
||||
|
||||
# thing type configuration
|
||||
thing-type.config.ntp.ntp.hostname.label = IP-Adresse
|
||||
thing-type.config.ntp.ntp.hostname.description = IP-Adresse oder Hostname des Zeitservers.
|
||||
thing-type.config.ntp.ntp.refreshInterval.label = Abfrageintervall
|
||||
thing-type.config.ntp.ntp.refreshInterval.description = Intervall zur Abfrage des Zeitservers (in Sekunden).
|
||||
thing-type.config.ntp.ntp.refreshNtp.label = Abfragefrequenz
|
||||
thing-type.config.ntp.ntp.refreshNtp.description = Anzahl der Aktualisierungen bevor eine Anfrage an den Zeitserver gestellt wird.
|
||||
thing-type.config.ntp.ntp.serverPort.label = Port
|
||||
thing-type.config.ntp.ntp.serverPort.description = Port des Zeitservers.
|
||||
thing-type.config.ntp.ntp.timeZone.label = Zeitzone
|
||||
thing-type.config.ntp.ntp.timeZone.description = Zeitzone des Systems.
|
||||
|
||||
# channel types
|
||||
channel-type.ntp.dateTime-channel.label = Datum und Zeit
|
||||
channel-type.ntp.dateTime-channel.description = Zeigt das Datum und die Zeit des Zeitservers an.
|
||||
channel-type.ntp.string-channel.label = Datum und Zeit
|
||||
channel-type.ntp.string-channel.description = Zeigt das formatierte Datum und die formatierte Zeit des Zeitservers an.
|
||||
|
||||
# channel type configuration
|
||||
channel-type.config.ntp.string-channel.DateTimeFormat.label = Datumsformat
|
||||
channel-type.config.ntp.string-channel.DateTimeFormat.description = Format für die Anzeige des Datum und der Zeit.
|
||||
|
||||
# Thing status descriptions
|
||||
offline.comm-error-unknown-host = Zeitserver {0} ist unbekannt. Systemzeit wird zurückgegeben.
|
||||
offline.comm-error-connection = Verbindung zum Zeitserver {0} kann nicht aufgebaut werden. Systemzeit wird zurückgegeben.
|
||||
|
||||
# Discovery result
|
||||
discovery.ntp.ntp.local.label = Lokale Zeit
|
||||
@@ -0,0 +1,36 @@
|
||||
# binding
|
||||
binding.ntp.name = Extension NTP
|
||||
binding.ntp.description = L'extension NTP interroge le serveur de temps configuré et fournit la date et l'heure actuelles.
|
||||
|
||||
# thing types
|
||||
thing-type.ntp.ntp.label = Serveur NTP
|
||||
thing-type.ntp.ntp.description = Un serveur de temps fournissant la date et l'heure actuelles.
|
||||
|
||||
# thing type configuration
|
||||
thing-type.config.ntp.ntp.hostname.label = Nom d'hôte du serveur
|
||||
thing-type.config.ntp.ntp.hostname.description = Le nom d'hôte du serveur de temps.
|
||||
thing-type.config.ntp.ntp.refreshInterval.label = Fréquence de rafraîchissement
|
||||
thing-type.config.ntp.ntp.refreshInterval.description = La fréquence en secondes de mise à jour de l'heure.
|
||||
thing-type.config.ntp.ntp.refreshNtp.label = Fréquence de rafraîchissement NTP
|
||||
thing-type.config.ntp.ntp.refreshNtp.description = Le nombre de mises à jour avant d'interroger le serveur de temps.
|
||||
thing-type.config.ntp.ntp.serverPort.label = Port du serveur
|
||||
thing-type.config.ntp.ntp.serverPort.description = Le port que le serveur de temps peut utiliser.
|
||||
thing-type.config.ntp.ntp.timeZone.label = Fuseau horaire
|
||||
thing-type.config.ntp.ntp.timeZone.description = Le fuseau horaire sélectionné.
|
||||
|
||||
# channel types
|
||||
channel-type.ntp.dateTime-channel.label = Date heure
|
||||
channel-type.ntp.dateTime-channel.description = Date et heure mises à jour à partir du serveur NTP.
|
||||
channel-type.ntp.string-channel.label = Date heure
|
||||
channel-type.ntp.string-channel.description = Date et heure mises à jour à partir du serveur NTP.
|
||||
|
||||
# channel type configuration
|
||||
channel-type.config.ntp.string-channel.DateTimeFormat.label = Format date et heure
|
||||
channel-type.config.ntp.string-channel.DateTimeFormat.description = Format utilisé pour la présentation de la date et de l'heure.
|
||||
|
||||
# Thing status descriptions
|
||||
offline.comm-error-unknown-host = Le nom d''hôte {0} du serveur de temps est inconnu -> renvoi de l''heure courante système à la place.
|
||||
offline.comm-error-connection = La connexion réseau avec le serveur de temps {0} ne peut pas être établie -> renvoi de l''heure courante système à la place.
|
||||
|
||||
# Discovery result
|
||||
discovery.ntp.ntp.local.label = Heure locale
|
||||
@@ -0,0 +1,36 @@
|
||||
# binding
|
||||
binding.ntp.name = NTP ba\u011flant\u0131s\u0131
|
||||
binding.ntp.description = NTP ba\u011flant\u0131s\u0131 bir zaman sunucusundan geçerli tarih ve saati sorgular.
|
||||
|
||||
# thing types
|
||||
thing-type.ntp.ntp.label = NTP sunucusu
|
||||
thing-type.ntp.ntp.description = Tarih ve zaman sorgulanacak sunucu
|
||||
|
||||
# thing type configuration
|
||||
thing-type.config.ntp.ntp.hostname.label = \u0130P adresi
|
||||
thing-type.config.ntp.ntp.hostname.description = Zaman sunucusunun ip adresi veya ismi
|
||||
thing-type.config.ntp.ntp.refreshInterval.label = Yoklama aral\u0131\u011f\u0131
|
||||
thing-type.config.ntp.ntp.refreshInterval.description = Zaman sunucunu yoklama aral\u0131\u011f\u0131 (saniye olarak)
|
||||
thing-type.config.ntp.ntp.refreshNtp.label = Yoklama h\u0131z\u0131
|
||||
thing-type.config.ntp.ntp.refreshNtp.description = Zaman sunucusuna bir istek yapmadan önce güncelleme say\u0131s\u0131.
|
||||
thing-type.config.ntp.ntp.serverPort.label = Ba\u011flant\u0131 noktas\u0131
|
||||
thing-type.config.ntp.ntp.serverPort.description = Zaman sunucunun ba\u011flant\u0131 noktas\u0131
|
||||
thing-type.config.ntp.ntp.timeZone.label = Saat dilimi
|
||||
thing-type.config.ntp.ntp.timeZone.description = Sistemin saat dilimi
|
||||
|
||||
# channel types
|
||||
channel-type.ntp.dateTime-channel.label = Tarih ve saat
|
||||
channel-type.ntp.dateTime-channel.description = Zaman sunucusunun tarihini ve saatini görünteler.
|
||||
channel-type.ntp.string-channel.label = Tarih ve saat
|
||||
channel-type.ntp.string-channel.description = Zaman sunucusunun biçimlendirilmi\u015f tarihini ve saatini görüntüler.
|
||||
|
||||
# channel type configuration
|
||||
channel-type.config.ntp.string-channel.DateTimeFormat.label = Tarih biçimi
|
||||
channel-type.config.ntp.string-channel.DateTimeFormat.description = Tarih ve saat görünteleme biçimi.
|
||||
|
||||
# Thing status descriptions
|
||||
offline.comm-error-unknown-host = Zaman sunucusu {0} tan\u0131nm\u0131yor. Sistem tarihi ve saati sunulacakt\u0131r.
|
||||
offline.comm-error-connection = {0} zaman sunucusuna ba\u011flan\u0131lam\u0131yor. Sistem zaman\u0131 sunulacakt\u0131r.
|
||||
|
||||
# Discovery result
|
||||
discovery.ntp.ntp.local.label = Yerel zaman
|
||||
@@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<thing:thing-descriptions bindingId="ntp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:thing="https://openhab.org/schemas/thing-description/v1.0.0"
|
||||
xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd">
|
||||
|
||||
<thing-type id="ntp">
|
||||
<label>NTP Server</label>
|
||||
<description>An NTP server that provides current date and time</description>
|
||||
<channels>
|
||||
<channel id="dateTime" typeId="dateTime-channel"/>
|
||||
<channel id="string" typeId="string-channel"/>
|
||||
</channels>
|
||||
<config-description>
|
||||
<parameter name="hostname" type="text" required="true">
|
||||
<context>network-address</context>
|
||||
<label>Hostname</label>
|
||||
<description>The NTP server hostname.</description>
|
||||
<default>0.pool.ntp.org</default>
|
||||
</parameter>
|
||||
<parameter name="refreshInterval" type="integer" unit="s">
|
||||
<label>Refresh Interval</label>
|
||||
<description>
|
||||
Interval that new time updates are posted to the event bus in seconds.
|
||||
</description>
|
||||
<default>60</default>
|
||||
</parameter>
|
||||
<parameter name="refreshNtp" type="integer">
|
||||
<label>NTP Refresh Frequency</label>
|
||||
<description>
|
||||
Number of updates before querying the NTP server.
|
||||
</description>
|
||||
<default>30</default>
|
||||
</parameter>
|
||||
<parameter name="serverPort" type="integer">
|
||||
<label>Server Port</label>
|
||||
<description>The port that the NTP server could use.</description>
|
||||
<default>123</default>
|
||||
</parameter>
|
||||
<parameter name="timeZone" type="text">
|
||||
<label>Timezone</label>
|
||||
<description>The configured timezone.</description>
|
||||
</parameter>
|
||||
</config-description>
|
||||
</thing-type>
|
||||
<channel-type id="dateTime-channel">
|
||||
<item-type>DateTime</item-type>
|
||||
<label>Date</label>
|
||||
<description>NTP refreshed date & time</description>
|
||||
<category>Date</category>
|
||||
<state readOnly="true"/>
|
||||
</channel-type>
|
||||
<channel-type id="string-channel">
|
||||
<item-type>String</item-type>
|
||||
<label>Date</label>
|
||||
<description>NTP refreshed date & time</description>
|
||||
<category>Date</category>
|
||||
<state readOnly="true"/>
|
||||
<config-description>
|
||||
<parameter name="DateTimeFormat" type="text" required="false">
|
||||
<label>Date Time Format</label>
|
||||
<description>Formattting of the date & time.</description>
|
||||
<default>yyyy-MM-dd HH:mm:ss z</default>
|
||||
</parameter>
|
||||
</config-description>
|
||||
</channel-type>
|
||||
</thing:thing-descriptions>
|
||||
Reference in New Issue
Block a user