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.sagercaster.internal.discovery;
15 import static org.openhab.binding.sagercaster.internal.SagerCasterBindingConstants.*;
18 import java.util.Objects;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.i18n.LocaleProvider;
29 import org.openhab.core.i18n.LocationProvider;
30 import org.openhab.core.i18n.TranslationProvider;
31 import org.openhab.core.library.types.PointType;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link SagerCasterDiscoveryService} creates things based on the configured location.
42 * @author Gaƫl L'hopital - Initial Contribution
45 @Component(service = DiscoveryService.class, configurationPid = "discovery.sagercaster")
46 public class SagerCasterDiscoveryService extends AbstractDiscoveryService {
47 private static final int DISCOVER_TIMEOUT_SECONDS = 30;
48 private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60;
49 private static final ThingUID SAGER_CASTER_THING = new ThingUID(THING_TYPE_SAGERCASTER, LOCAL);
51 private final Logger logger = LoggerFactory.getLogger(SagerCasterDiscoveryService.class);
52 private final LocationProvider locationProvider;
54 private @Nullable ScheduledFuture<?> discoveryJob;
55 private @Nullable PointType previousLocation;
58 * Creates a SagerCasterDiscoveryService with enabled autostart.
61 public SagerCasterDiscoveryService(final @Reference LocaleProvider localeProvider,
62 final @Reference TranslationProvider i18nProvider, final @Reference LocationProvider locationProvider) {
63 super(Set.of(THING_TYPE_SAGERCASTER), DISCOVER_TIMEOUT_SECONDS, true);
64 this.locationProvider = locationProvider;
65 this.localeProvider = localeProvider;
66 this.i18nProvider = i18nProvider;
70 protected void activate(@Nullable Map<String, Object> configProperties) {
71 super.activate(configProperties);
75 protected void modified(@Nullable Map<String, Object> configProperties) {
76 super.modified(configProperties);
80 protected void startScan() {
81 logger.debug("Starting Sager Weathercaster discovery scan");
82 PointType location = locationProvider.getLocation();
83 if (location == null) {
84 logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
87 createResults(location);
91 protected void startBackgroundDiscovery() {
92 if (discoveryJob == null) {
93 discoveryJob = scheduler.scheduleWithFixedDelay(() -> {
94 PointType currentLocation = locationProvider.getLocation();
95 if (currentLocation != null && !Objects.equals(currentLocation, previousLocation)) {
96 logger.debug("Location has been changed from {} to {}: Creating new discovery results",
97 previousLocation, currentLocation);
98 createResults(currentLocation);
99 previousLocation = currentLocation;
101 }, 0, LOCATION_CHANGED_CHECK_INTERVAL, TimeUnit.SECONDS);
102 logger.debug("Scheduled SagerCaster location-changed job every {} seconds",
103 LOCATION_CHANGED_CHECK_INTERVAL);
108 protected void stopBackgroundDiscovery() {
109 ScheduledFuture<?> localJob = discoveryJob;
110 logger.debug("Stopping Sager Weathercaster background discovery");
111 if (localJob != null && !localJob.isCancelled()) {
112 if (localJob.cancel(true)) {
114 logger.debug("Stopped SagerCaster device background discovery");
119 public void createResults(PointType location) {
120 String propGeolocation = String.format("%s,%s", location.getLatitude(), location.getLongitude());
121 thingDiscovered(DiscoveryResultBuilder.create(SAGER_CASTER_THING).withLabel("Local Weather Forecast")
122 .withRepresentationProperty(CONFIG_LOCATION).withProperty(CONFIG_LOCATION, propGeolocation).build());