2 * Copyright (c) 2010-2020 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.sagercaster.internal.discovery;
15 import static org.openhab.binding.sagercaster.internal.SagerCasterBindingConstants.*;
17 import java.util.Arrays;
18 import java.util.HashSet;
20 import java.util.Objects;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
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;
39 * The {@link SagerCasterDiscoveryService} creates things based on the configured location.
41 * @author Gaƫl L'hopital - Initial Contribution
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;
53 private static final ThingUID sagerCasterThing = new ThingUID(THING_TYPE_SAGERCASTER, LOCAL);
56 * Creates a SagerCasterDiscoveryService with enabled autostart.
58 public SagerCasterDiscoveryService() {
59 super(new HashSet<>(Arrays.asList(new ThingTypeUID(BINDING_ID, "-"))), DISCOVER_TIMEOUT_SECONDS, true);
63 protected void activate(@Nullable Map<String, @Nullable Object> configProperties) {
64 super.activate(configProperties);
68 protected void modified(@Nullable Map<String, @Nullable Object> configProperties) {
69 super.modified(configProperties);
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");
80 createResults(location);
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;
94 }, 0, LOCATION_CHANGED_CHECK_INTERVAL, TimeUnit.SECONDS);
95 logger.debug("Scheduled SagerCaster location-changed job every {} seconds",
96 LOCATION_CHANGED_CHECK_INTERVAL);
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");
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());
119 protected void setLocationProvider(LocationProvider locationProvider) {
120 this.locationProvider = locationProvider;
123 protected void unsetLocationProvider(LocationProvider locationProvider) {
124 this.locationProvider = null;