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