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