2 * Copyright (c) 2010-2023 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.*;
16 import static org.openhab.binding.openuv.internal.config.ReportConfiguration.LOCATION;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
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;
31 * The {@link OpenUVDiscoveryService} creates things based on the configured location.
33 * @author Gaƫl L'hopital - Initial Contribution
36 public class OpenUVDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
37 private static final int DISCOVER_TIMEOUT_SECONDS = 2;
39 private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class);
41 private @Nullable OpenUVBridgeHandler bridgeHandler;
43 public OpenUVDiscoveryService() {
44 super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS);
48 public void setThingHandler(ThingHandler handler) {
49 if (handler instanceof OpenUVBridgeHandler) {
50 OpenUVBridgeHandler localHandler = (OpenUVBridgeHandler) handler;
51 bridgeHandler = localHandler;
52 i18nProvider = localHandler.getI18nProvider();
53 localeProvider = localHandler.getLocaleProvider();
58 public @Nullable ThingHandler getThingHandler() {
63 public void deactivate() {
68 protected void startScan() {
69 logger.debug("Starting OpenUV discovery scan");
70 OpenUVBridgeHandler bridge = bridgeHandler;
72 PointType location = bridge.getLocation();
73 if (location != null) {
74 ThingUID bridgeUID = bridge.getThing().getUID();
76 DiscoveryResultBuilder.create(new ThingUID(LOCATION_REPORT_THING_TYPE, bridgeUID, LOCAL))
77 .withLabel("@text/discovery.openuv.uvreport.local.label")
78 .withProperty(LOCATION, location.toString()).withRepresentationProperty(LOCATION)
79 .withBridge(bridgeUID).build());
81 logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
84 logger.debug("OpenUV Bridge Handler is not set -> Will not provide any discovery results");