]> git.basschouten.com Git - openhab-addons.git/blob
9b41422a1a6b745866175c63a0e14dbb5a8ff7eb
[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.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,
61                     new Hashtable<String, Object>());
62         }
63     }
64
65     @Override
66     protected void startScan() {
67         logger.debug("Starting BLU Discovery");
68         thingTable.startScan();
69     }
70
71     public void discoveredResult(ThingTypeUID tuid, String model, String serviceName, String address,
72             Map<String, Object> properties) {
73         ThingUID uid = ShellyThingCreator.getThingUID(serviceName, model, "", true);
74         logger.debug("Adding discovered thing with id {}", uid.toString());
75         properties.put(PROPERTY_MAC_ADDRESS, address);
76         String thingLabel = "Shelly BLU " + model + " (" + serviceName + ")";
77         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
78                 .withRepresentationProperty(PROPERTY_DEV_NAME).withLabel(thingLabel).build();
79         thingDiscovered(result);
80     }
81
82     public void unregisterDeviceDiscoveryService() {
83         if (discoveryService != null) {
84             discoveryService.unregister();
85         }
86     }
87
88     @Override
89     public void deactivate() {
90         super.deactivate();
91         unregisterDeviceDiscoveryService();
92     }
93 }