2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ntp.internal.discovery;
15 import static org.openhab.binding.ntp.internal.NtpBindingConstants.*;
17 import java.util.HashMap;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.i18n.LocaleProvider;
28 import org.openhab.core.i18n.TimeZoneProvider;
29 import org.openhab.core.i18n.TranslationProvider;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
37 * The {@link NtpDiscovery} is used to add a ntp Thing for the local time in the discovery inbox
40 * @author Marcel Verpaalen - Initial contribution
43 @Component(service = DiscoveryService.class, configurationPid = "discovery.ntp")
44 public class NtpDiscovery extends AbstractDiscoveryService {
46 private final TimeZoneProvider timeZoneProvider;
49 public NtpDiscovery(final @Reference LocaleProvider localeProvider,
50 final @Reference TranslationProvider i18nProvider, final @Reference TimeZoneProvider timeZoneProvider,
51 @Nullable Map<String, Object> configProperties) throws IllegalArgumentException {
52 super(SUPPORTED_THING_TYPES_UIDS, 2);
53 this.localeProvider = localeProvider;
54 this.i18nProvider = i18nProvider;
55 this.timeZoneProvider = timeZoneProvider;
56 activate(configProperties);
60 protected void startBackgroundDiscovery() {
61 scheduler.schedule(() -> {
63 }, 1, TimeUnit.SECONDS);
67 protected void startScan() {
72 * Add a ntp Thing for the local time in the discovery inbox
74 private void discoverNtp() {
75 Map<String, Object> properties = new HashMap<>(4);
76 properties.put(PROPERTY_TIMEZONE, timeZoneProvider.getTimeZone().getId());
77 ThingUID uid = new ThingUID(THING_TYPE_NTP, "local");
78 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("Local Time")
80 thingDiscovered(result);