]> git.basschouten.com Git - openhab-addons.git/blob
e2e9fb4d5c60d2933894f864c8132e61d8db462a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.weathercompany.internal.discovery;
14
15 import static org.openhab.binding.weathercompany.internal.WeatherCompanyBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21
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;
34
35 /**
36  * The {@link WeatherCompanyDiscoveryService} creates things based on the location
37  * configured in openHAB
38  *
39  * @author Mark Hilbush - Initial Contribution
40  */
41 @NonNullByDefault
42 public class WeatherCompanyDiscoveryService extends AbstractDiscoveryService {
43     // Thing for local weather created during discovery
44     private static final String LOCAL = "local";
45
46     private static final int DISCOVER_TIMEOUT_SECONDS = 4;
47     private static final int DISCOVERY_INTERVAL_SECONDS = 1200;
48
49     private final Logger logger = LoggerFactory.getLogger(WeatherCompanyDiscoveryService.class);
50
51     private final LocationProvider locationProvider;
52     private final LocaleProvider localeProvider;
53     private final WeatherCompanyBridgeHandler bridgeHandler;
54
55     private @Nullable ScheduledFuture<?> discoveryJob;
56
57     /**
58      * Creates a WeatherCompanyDiscoveryService with discovery enabled
59      */
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;
66     }
67
68     @Override
69     public void activate(@Nullable Map<String, Object> configProperties) {
70         super.activate(configProperties);
71         logger.debug("Discovery: Activating discovery service for {}", bridgeHandler.getThing().getUID());
72     }
73
74     @Override
75     public void deactivate() {
76         super.deactivate();
77         logger.debug("Discovery: Deactivating discovery service for {}", bridgeHandler.getThing().getUID());
78     }
79
80     @Override
81     protected void startScan() {
82         logger.debug("Discovery: Starting Weather Company discovery scan");
83         createDiscoveryResult();
84         stopScan();
85     }
86
87     @Override
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);
96         }
97     }
98
99     @Override
100     protected void stopBackgroundDiscovery() {
101         ScheduledFuture<?> job = discoveryJob;
102         if (job != null && !job.isCancelled()) {
103             job.cancel(true);
104             discoveryJob = null;
105             logger.debug("Discovery: Stopped Weather Company device background discovery");
106         }
107     }
108
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");
113             return;
114         }
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());
123     }
124 }