2 * Copyright (c) 2010-2021 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.library.types.PointType;
31 import org.openhab.core.thing.ThingUID;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link WeatherCompanyDiscoveryService} creates things based on the location
37 * configured in openHAB
39 * @author Mark Hilbush - Initial Contribution
42 public class WeatherCompanyDiscoveryService extends AbstractDiscoveryService {
43 // Thing for local weather created during discovery
44 private static final String LOCAL = "local";
46 private static final int DISCOVER_TIMEOUT_SECONDS = 4;
47 private static final int DISCOVERY_INTERVAL_SECONDS = 1200;
49 private final Logger logger = LoggerFactory.getLogger(WeatherCompanyDiscoveryService.class);
51 private final LocationProvider locationProvider;
52 private final LocaleProvider localeProvider;
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) {
62 super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS, true);
63 this.bridgeHandler = bridgeHandler;
64 this.locationProvider = locationProvider;
65 this.localeProvider = localeProvider;
69 public void activate(@Nullable Map<String, Object> configProperties) {
70 super.activate(configProperties);
71 logger.debug("Discovery: Activating discovery service for {}", bridgeHandler.getThing().getUID());
75 public void deactivate() {
77 logger.debug("Discovery: Deactivating discovery service for {}", bridgeHandler.getThing().getUID());
81 protected void startScan() {
82 logger.debug("Discovery: Starting Weather Company discovery scan");
83 createDiscoveryResult();
88 protected void startBackgroundDiscovery() {
89 ScheduledFuture<?> job = discoveryJob;
90 if (job == null || job.isCancelled()) {
91 job = scheduler.scheduleWithFixedDelay(() -> {
92 createDiscoveryResult();
93 }, 15, DISCOVERY_INTERVAL_SECONDS, TimeUnit.SECONDS);
94 logger.debug("Discovery: Scheduled Weather Company discovery job to run every {} seconds",
95 DISCOVERY_INTERVAL_SECONDS);
100 protected void stopBackgroundDiscovery() {
101 ScheduledFuture<?> job = discoveryJob;
102 if (job != null && !job.isCancelled()) {
105 logger.debug("Discovery: Stopped Weather Company device background discovery");
109 private void createDiscoveryResult() {
110 PointType location = locationProvider.getLocation();
111 if (location == null) {
112 logger.debug("Discovery: Can't create discovery result because location is not set in openHAB");
115 Map<String, Object> properties = new HashMap<>(3);
116 properties.put(CONFIG_LOCATION_TYPE, CONFIG_LOCATION_TYPE_GEOCODE);
117 properties.put(CONFIG_GEOCODE, String.format("%s,%s", location.getLatitude(), location.getLongitude()));
118 properties.put(CONFIG_LANGUAGE, WeatherCompanyAbstractHandler.lookupLanguage(localeProvider.getLocale()));
119 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
120 ThingUID localWeatherThing = new ThingUID(THING_TYPE_WEATHER_FORECAST, bridgeUID, LOCAL);
121 thingDiscovered(DiscoveryResultBuilder.create(localWeatherThing).withBridge(bridgeUID)
122 .withLabel("@text/discovery.weather-forecast.local.label").withProperties(properties).build());