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.openuv.internal.discovery;
15 import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.*;
17 import java.util.HashMap;
19 import java.util.Objects;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.openuv.internal.handler.OpenUVBridgeHandler;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.i18n.LocationProvider;
29 import org.openhab.core.library.types.PointType;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Modified;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link OpenUVDiscoveryService} creates things based on the configured location.
38 * @author Gaƫl L'hopital - Initial Contribution
41 public class OpenUVDiscoveryService extends AbstractDiscoveryService {
42 private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class);
44 private static final int DISCOVER_TIMEOUT_SECONDS = 10;
45 private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60;
47 private final LocationProvider locationProvider;
48 private final OpenUVBridgeHandler bridgeHandler;
49 private @Nullable ScheduledFuture<?> discoveryJob;
50 private @Nullable PointType previousLocation;
53 * Creates a OpenUVDiscoveryService with enabled autostart.
55 public OpenUVDiscoveryService(OpenUVBridgeHandler bridgeHandler, LocationProvider locationProvider) {
56 super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS, true);
57 this.locationProvider = locationProvider;
58 this.bridgeHandler = bridgeHandler;
62 protected void activate(@Nullable Map<String, @Nullable Object> configProperties) {
63 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 OpenUV 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 (discoveryJob == null) {
86 discoveryJob = 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 OpenUV-changed job every {} seconds", LOCATION_CHANGED_CHECK_INTERVAL);
99 public void createResults(PointType location) {
100 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
101 ThingUID localOpenUVThing = new ThingUID(LOCATION_REPORT_THING_TYPE, bridgeUID, LOCAL);
102 Map<String, Object> properties = new HashMap<>();
103 properties.put(LOCATION, location.toString());
104 thingDiscovered(DiscoveryResultBuilder.create(localOpenUVThing).withLabel("Local UV Information")
105 .withProperties(properties).withRepresentationProperty(location.toString()).withBridge(bridgeUID)
109 @SuppressWarnings("null")
111 protected void stopBackgroundDiscovery() {
112 logger.debug("Stopping OpenUV background discovery");
113 if (discoveryJob != null && !discoveryJob.isCancelled()) {
114 if (discoveryJob.cancel(true)) {
116 logger.debug("Stopped OpenUV background discovery");