]> git.basschouten.com Git - openhab-addons.git/blob
d7a172fc32d8a36130fbab5ebbd36cebede3a2c8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.ntp.internal.discovery;
14
15 import static org.openhab.binding.ntp.internal.NtpBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.concurrent.TimeUnit;
20
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;
34
35 /**
36  *
37  * The {@link NtpDiscovery} is used to add a ntp Thing for the local time in the discovery inbox
38  * *
39  *
40  * @author Marcel Verpaalen - Initial contribution
41  */
42 @NonNullByDefault
43 @Component(service = DiscoveryService.class, configurationPid = "discovery.ntp")
44 public class NtpDiscovery extends AbstractDiscoveryService {
45
46     private final TimeZoneProvider timeZoneProvider;
47
48     @Activate
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);
57     }
58
59     @Override
60     protected void startBackgroundDiscovery() {
61         scheduler.schedule(() -> {
62             discoverNtp();
63         }, 1, TimeUnit.SECONDS);
64     }
65
66     @Override
67     protected void startScan() {
68         discoverNtp();
69     }
70
71     /**
72      * Add a ntp Thing for the local time in the discovery inbox
73      */
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")
79                 .build();
80         thingDiscovered(result);
81     }
82 }