]> git.basschouten.com Git - openhab-addons.git/blob
8ad3363106769273e72df756fffba9c9fa669b4a
[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.sagercaster.internal.discovery;
14
15 import static org.openhab.binding.sagercaster.internal.SagerCasterBindingConstants.*;
16
17 import java.util.Arrays;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.i18n.LocationProvider;
30 import org.openhab.core.library.types.PointType;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link SagerCasterDiscoveryService} creates things based on the configured location.
40  *
41  * @author GaĆ«l L'hopital - Initial Contribution
42  */
43 @NonNullByDefault
44 @Component(service = DiscoveryService.class, configurationPid = "discovery.sagercaster")
45 public class SagerCasterDiscoveryService extends AbstractDiscoveryService {
46     private final Logger logger = LoggerFactory.getLogger(SagerCasterDiscoveryService.class);
47     private static final int DISCOVER_TIMEOUT_SECONDS = 30;
48     private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60;
49     private @NonNullByDefault({}) LocationProvider locationProvider;
50     private @NonNullByDefault({}) ScheduledFuture<?> sagerCasterDiscoveryJob;
51     private @Nullable PointType previousLocation;
52
53     private static final ThingUID sagerCasterThing = new ThingUID(THING_TYPE_SAGERCASTER, LOCAL);
54
55     /**
56      * Creates a SagerCasterDiscoveryService with enabled autostart.
57      */
58     public SagerCasterDiscoveryService() {
59         super(new HashSet<>(Arrays.asList(new ThingTypeUID(BINDING_ID, "-"))), DISCOVER_TIMEOUT_SECONDS, true);
60     }
61
62     @Override
63     protected void activate(@Nullable Map<String, Object> configProperties) {
64         super.activate(configProperties);
65     }
66
67     @Override
68     protected void modified(@Nullable Map<String, Object> configProperties) {
69         super.modified(configProperties);
70     }
71
72     @Override
73     protected void startScan() {
74         logger.debug("Starting Sager Weathercaster discovery scan");
75         PointType location = locationProvider.getLocation();
76         if (location == null) {
77             logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
78             return;
79         }
80         createResults(location);
81     }
82
83     @Override
84     protected void startBackgroundDiscovery() {
85         if (sagerCasterDiscoveryJob == null) {
86             sagerCasterDiscoveryJob = scheduler.scheduleWithFixedDelay(() -> {
87                 PointType currentLocation = locationProvider.getLocation();
88                 if (currentLocation != null && !Objects.equals(currentLocation, previousLocation)) {
89                     logger.debug("Location has been changed from {} to {}: Creating new discovery results",
90                             previousLocation, currentLocation);
91                     createResults(currentLocation);
92                     previousLocation = currentLocation;
93                 }
94             }, 0, LOCATION_CHANGED_CHECK_INTERVAL, TimeUnit.SECONDS);
95             logger.debug("Scheduled SagerCaster location-changed job every {} seconds",
96                     LOCATION_CHANGED_CHECK_INTERVAL);
97         }
98     }
99
100     @Override
101     protected void stopBackgroundDiscovery() {
102         logger.debug("Stopping Sager Weathercaster background discovery");
103         if (sagerCasterDiscoveryJob != null && !sagerCasterDiscoveryJob.isCancelled()) {
104             if (sagerCasterDiscoveryJob.cancel(true)) {
105                 sagerCasterDiscoveryJob = null;
106                 logger.debug("Stopped SagerCaster device background discovery");
107             }
108         }
109     }
110
111     public void createResults(PointType location) {
112         String propGeolocation;
113         propGeolocation = String.format("%s,%s", location.getLatitude(), location.getLongitude());
114         thingDiscovered(DiscoveryResultBuilder.create(sagerCasterThing).withLabel("Local Sager Weathercaster")
115                 .withRepresentationProperty(CONFIG_LOCATION).withProperty(CONFIG_LOCATION, propGeolocation).build());
116     }
117
118     @Reference
119     protected void setLocationProvider(LocationProvider locationProvider) {
120         this.locationProvider = locationProvider;
121     }
122
123     protected void unsetLocationProvider(LocationProvider locationProvider) {
124         this.locationProvider = null;
125     }
126 }