]> git.basschouten.com Git - openhab-addons.git/blob
29092f49047444eccbc139888d48c5b250803b38
[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.openuv.internal.discovery;
14
15 import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.openuv.internal.config.ReportConfiguration;
20 import org.openhab.binding.openuv.internal.handler.OpenUVBridgeHandler;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.library.types.PointType;
24 import org.openhab.core.thing.ThingUID;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.openhab.core.thing.binding.ThingHandlerService;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link OpenUVDiscoveryService} creates things based on the configured location.
32  *
33  * @author GaĆ«l L'hopital - Initial Contribution
34  */
35 @NonNullByDefault
36 public class OpenUVDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
37     private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class);
38
39     private static final int DISCOVER_TIMEOUT_SECONDS = 2;
40
41     private @Nullable OpenUVBridgeHandler bridgeHandler;
42
43     /**
44      * Creates a OpenUVDiscoveryService with enabled autostart.
45      */
46     public OpenUVDiscoveryService() {
47         super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS);
48     }
49
50     @Override
51     public void setThingHandler(ThingHandler handler) {
52         if (handler instanceof OpenUVBridgeHandler) {
53             OpenUVBridgeHandler localHandler = (OpenUVBridgeHandler) handler;
54             this.bridgeHandler = localHandler;
55             this.i18nProvider = localHandler.getI18nProvider();
56             this.localeProvider = localHandler.getLocaleProvider();
57         }
58     }
59
60     @Override
61     public @Nullable ThingHandler getThingHandler() {
62         return bridgeHandler;
63     }
64
65     @Override
66     public void deactivate() {
67         super.deactivate();
68     }
69
70     @Override
71     protected void startScan() {
72         logger.debug("Starting OpenUV discovery scan");
73         OpenUVBridgeHandler bridge = bridgeHandler;
74         if (bridge != null) {
75             PointType location = bridge.getLocation();
76             if (location != null) {
77                 ThingUID bridgeUID = bridge.getThing().getUID();
78                 thingDiscovered(DiscoveryResultBuilder
79                         .create(new ThingUID(LOCATION_REPORT_THING_TYPE, bridgeUID, LOCAL))
80                         .withLabel("@text/discovery.openuv.uvreport.local.label")
81                         .withProperty(ReportConfiguration.LOCATION, location.toString())
82                         .withRepresentationProperty(ReportConfiguration.LOCATION).withBridge(bridgeUID).build());
83             } else {
84                 logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
85             }
86         } else {
87             logger.debug("OpenUV Bridge Handler is not set -> Will not provide any discovery results");
88         }
89     }
90 }