]> git.basschouten.com Git - openhab-addons.git/blob
8ab5bb69dd655445ba708ca59d7922ead696ee55
[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.sagercaster.internal.discovery;
14
15 import static org.openhab.binding.sagercaster.internal.SagerCasterBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.Set;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22
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;
38
39 /**
40  * The {@link SagerCasterDiscoveryService} creates things based on the configured location.
41  *
42  * @author GaĆ«l L'hopital - Initial Contribution
43  */
44 @NonNullByDefault
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);
50
51     private final Logger logger = LoggerFactory.getLogger(SagerCasterDiscoveryService.class);
52     private final LocationProvider locationProvider;
53
54     private @Nullable ScheduledFuture<?> discoveryJob;
55     private @Nullable PointType previousLocation;
56
57     /**
58      * Creates a SagerCasterDiscoveryService with enabled autostart.
59      */
60     @Activate
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;
67     }
68
69     @Override
70     protected void activate(@Nullable Map<String, Object> configProperties) {
71         super.activate(configProperties);
72     }
73
74     @Override
75     protected void modified(@Nullable Map<String, Object> configProperties) {
76         super.modified(configProperties);
77     }
78
79     @Override
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");
85             return;
86         }
87         createResults(location);
88     }
89
90     @Override
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;
100                 }
101             }, 0, LOCATION_CHANGED_CHECK_INTERVAL, TimeUnit.SECONDS);
102             logger.debug("Scheduled SagerCaster location-changed job every {} seconds",
103                     LOCATION_CHANGED_CHECK_INTERVAL);
104         }
105     }
106
107     @Override
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)) {
113                 discoveryJob = null;
114                 logger.debug("Stopped SagerCaster device background discovery");
115             }
116         }
117     }
118
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());
123     }
124 }