]> git.basschouten.com Git - openhab-addons.git/blob
868ef7d4e9c702db4ebba71a1d6ec269f784eadd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.airquality.internal.discovery;
14
15 import static org.openhab.binding.airquality.internal.AirQualityBindingConstants.*;
16 import static org.openhab.binding.airquality.internal.config.AirQualityConfiguration.LOCATION;
17
18 import java.util.Collections;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.airquality.internal.handler.AirQualityBridgeHandler;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.i18n.LocationProvider;
28 import org.openhab.core.library.types.PointType;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.osgi.service.component.annotations.Component;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link AirQualityDiscoveryService} creates things based on the configured location.
39  *
40  * @author GaĆ«l L'hopital - Initial Contribution
41  */
42 @Component(service = DiscoveryService.class, configurationPid = "discovery.airquality")
43 @NonNullByDefault
44 public class AirQualityDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
45     private static final int DISCOVER_TIMEOUT_SECONDS = 2;
46     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_STATION);
47
48     private final Logger logger = LoggerFactory.getLogger(AirQualityDiscoveryService.class);
49
50     private @Nullable LocationProvider locationProvider;
51     private @Nullable AirQualityBridgeHandler bridgeHandler;
52
53     /**
54      * Creates an AirQualityDiscoveryService with enabled autostart.
55      */
56     public AirQualityDiscoveryService() {
57         super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS, false);
58     }
59
60     @Override
61     public void setThingHandler(@Nullable ThingHandler handler) {
62         if (handler instanceof AirQualityBridgeHandler) {
63             final AirQualityBridgeHandler bridgeHandler = (AirQualityBridgeHandler) handler;
64             this.bridgeHandler = bridgeHandler;
65             this.locationProvider = bridgeHandler.getLocationProvider();
66         }
67     }
68
69     @Override
70     public @Nullable ThingHandler getThingHandler() {
71         return bridgeHandler;
72     }
73
74     @Override
75     public void deactivate() {
76         super.deactivate();
77     }
78
79     @Override
80     protected void startScan() {
81         logger.debug("Starting Air Quality discovery scan");
82         LocationProvider provider = locationProvider;
83         if (provider != null) {
84             PointType location = provider.getLocation();
85             AirQualityBridgeHandler bridge = this.bridgeHandler;
86             if (location == null || bridge == null) {
87                 logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
88                 return;
89             }
90             createResults(location, bridge.getThing().getUID());
91         }
92     }
93
94     public void createResults(PointType location, ThingUID bridgeUID) {
95         ThingUID localAirQualityThing = new ThingUID(THING_TYPE_STATION, bridgeUID, LOCAL);
96         thingDiscovered(DiscoveryResultBuilder.create(localAirQualityThing).withLabel("Local Air Quality")
97                 .withProperty(LOCATION, String.format("%s,%s", location.getLatitude(), location.getLongitude()))
98                 .withBridge(bridgeUID).build());
99     }
100 }