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.weathercompany.internal.discovery;
15 import static org.openhab.binding.weathercompany.internal.WeatherCompanyBindingConstants.*;
17 import java.util.HashMap;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.weathercompany.internal.handler.WeatherCompanyAbstractHandler;
25 import org.openhab.binding.weathercompany.internal.handler.WeatherCompanyBridgeHandler;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
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.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link WeatherCompanyDiscoveryService} creates things based on the location
38 * configured in openHAB
40 * @author Mark Hilbush - Initial Contribution
43 public class WeatherCompanyDiscoveryService extends AbstractDiscoveryService {
44 // Thing for local weather created during discovery
45 private static final String LOCAL = "local";
47 private static final int DISCOVER_TIMEOUT_SECONDS = 4;
48 private static final int DISCOVERY_INTERVAL_SECONDS = 1200;
50 private final Logger logger = LoggerFactory.getLogger(WeatherCompanyDiscoveryService.class);
52 private final LocationProvider locationProvider;
53 private final WeatherCompanyBridgeHandler bridgeHandler;
55 private @Nullable ScheduledFuture<?> discoveryJob;
58 * Creates a WeatherCompanyDiscoveryService with discovery enabled
60 public WeatherCompanyDiscoveryService(WeatherCompanyBridgeHandler bridgeHandler, LocationProvider locationProvider,
61 LocaleProvider localeProvider, TranslationProvider i18nProvider) {
62 super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS, true);
63 this.bridgeHandler = bridgeHandler;
64 this.locationProvider = locationProvider;
65 this.localeProvider = localeProvider;
66 this.i18nProvider = i18nProvider;
70 public void activate(@Nullable Map<String, Object> configProperties) {
71 super.activate(configProperties);
72 logger.debug("Discovery: Activating discovery service for {}", bridgeHandler.getThing().getUID());
76 public void deactivate() {
78 logger.debug("Discovery: Deactivating discovery service for {}", bridgeHandler.getThing().getUID());
82 protected void startScan() {
83 logger.debug("Discovery: Starting Weather Company discovery scan");
84 createDiscoveryResult();
89 protected void startBackgroundDiscovery() {
90 ScheduledFuture<?> job = discoveryJob;
91 if (job == null || job.isCancelled()) {
92 job = scheduler.scheduleWithFixedDelay(() -> {
93 createDiscoveryResult();
94 }, 15, DISCOVERY_INTERVAL_SECONDS, TimeUnit.SECONDS);
95 logger.debug("Discovery: Scheduled Weather Company discovery job to run every {} seconds",
96 DISCOVERY_INTERVAL_SECONDS);
101 protected void stopBackgroundDiscovery() {
102 ScheduledFuture<?> job = discoveryJob;
103 if (job != null && !job.isCancelled()) {
106 logger.debug("Discovery: Stopped Weather Company device background discovery");
110 private void createDiscoveryResult() {
111 PointType location = locationProvider.getLocation();
112 if (location == null) {
113 logger.debug("Discovery: Can't create discovery result because location is not set in openHAB");
116 Map<String, Object> properties = new HashMap<>(3);
117 properties.put(CONFIG_LOCATION_TYPE, CONFIG_LOCATION_TYPE_GEOCODE);
118 properties.put(CONFIG_GEOCODE, String.format("%s,%s", location.getLatitude(), location.getLongitude()));
119 properties.put(CONFIG_LANGUAGE, WeatherCompanyAbstractHandler.lookupLanguage(localeProvider.getLocale()));
120 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
121 ThingUID localWeatherThing = new ThingUID(THING_TYPE_WEATHER_FORECAST, bridgeUID, LOCAL);
122 thingDiscovered(DiscoveryResultBuilder.create(localWeatherThing).withBridge(bridgeUID)
123 .withLabel("@text/discovery.weather-forecast.local.label").withProperties(properties).build());