]> git.basschouten.com Git - openhab-addons.git/blob
35c1c2a68fe950ed957d3024226b8da82016881f
[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.bluetooth.blukii.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
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.bluetooth.BluetoothBindingConstants;
23 import org.openhab.binding.bluetooth.blukii.BlukiiBindingConstants;
24 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
25 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32
33 /**
34  * This discovery participant is able to recognize blukii devices and create discovery results for them.
35  *
36  * @author Kai Kreuzer - Initial contribution and API
37  *
38  */
39 @NonNullByDefault
40 @Component
41 public class BlukiiDiscoveryParticipant implements BluetoothDiscoveryParticipant {
42
43     @Override
44     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
45         return Collections.singleton(BlukiiBindingConstants.THING_TYPE_BEACON);
46     }
47
48     @Override
49     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
50         String name = device.getName();
51         if (name != null && name.startsWith(BlukiiBindingConstants.BLUKII_PREFIX)) {
52             if (name.charAt(BlukiiBindingConstants.BLUKII_PREFIX.length()) == 'B') {
53                 return new ThingUID(BlukiiBindingConstants.THING_TYPE_BEACON, device.getAdapter().getUID(),
54                         device.getAddress().toString().toLowerCase().replace(":", ""));
55             }
56         }
57         return null;
58     }
59
60     @Override
61     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
62         ThingUID thingUID = getThingUID(device);
63
64         if (thingUID != null) {
65             String label = "Blukii SmartBeacon";
66
67             Map<String, Object> properties = new HashMap<>();
68             properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
69             properties.put(Thing.PROPERTY_VENDOR, "Schneider Schreibgeräte GmbH");
70             Integer txPower = device.getTxPower();
71             if (txPower != null) {
72                 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
73             }
74
75             // Create the discovery result and add to the inbox
76             return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
77                     .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
78                     .withBridge(device.getAdapter().getUID()).withLabel(label).build();
79         } else {
80             return null;
81         }
82     }
83 }