]> git.basschouten.com Git - openhab-addons.git/blob
7d2e7794a9bf28c4abacf92bd3a52e364b429194
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.shelly.internal.discovery;
14
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_MAC_ADDRESS;
17
18 import java.util.Hashtable;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.shelly.internal.handler.ShellyThingTable;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.ServiceRegistration;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Device discovery creates a thing in the inbox for each vehicle
37  * found in the data received from {@link ShellyBluDiscoveryService}.
38  *
39  * @author Markus Michels - Initial Contribution
40  *
41  */
42 @NonNullByDefault
43 public class ShellyBluDiscoveryService extends AbstractDiscoveryService {
44     private final Logger logger = LoggerFactory.getLogger(ShellyBluDiscoveryService.class);
45
46     private final BundleContext bundleContext;
47     private final ShellyThingTable thingTable;
48     private static final int TIMEOUT = 10;
49     private @Nullable ServiceRegistration<?> discoveryService;
50
51     public ShellyBluDiscoveryService(BundleContext bundleContext, ShellyThingTable thingTable) {
52         super(SUPPORTED_THING_TYPES_UIDS, TIMEOUT);
53         this.bundleContext = bundleContext;
54         this.thingTable = thingTable;
55     }
56
57     @SuppressWarnings("null")
58     public void registerDeviceDiscoveryService() {
59         if (discoveryService == null) {
60             discoveryService = bundleContext.registerService(DiscoveryService.class.getName(), this, new Hashtable<>());
61         }
62     }
63
64     @Override
65     protected void startScan() {
66         logger.debug("Starting BLU Discovery");
67         thingTable.startScan();
68     }
69
70     public void discoveredResult(ThingTypeUID tuid, String model, String serviceName, String address,
71             Map<String, Object> properties) {
72         ThingUID uid = ShellyThingCreator.getThingUID(serviceName, model, "", true);
73         logger.debug("Adding discovered thing with id {}", uid.toString());
74         properties.put(PROPERTY_MAC_ADDRESS, address);
75         String thingLabel = "Shelly BLU " + model + " (" + serviceName + ")";
76         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
77                 .withRepresentationProperty(PROPERTY_DEV_NAME).withLabel(thingLabel).build();
78         thingDiscovered(result);
79     }
80
81     public void unregisterDeviceDiscoveryService() {
82         if (discoveryService != null) {
83             discoveryService.unregister();
84         }
85     }
86
87     @Override
88     public void deactivate() {
89         super.deactivate();
90         unregisterDeviceDiscoveryService();
91     }
92 }