]> git.basschouten.com Git - openhab-addons.git/blob
15d1ab5e444ee75aa379389b1afc04969e6df833
[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.ruuvitag.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.discovery.BluetoothDiscoveryDevice;
23 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30
31 /**
32  * This discovery participant is able to recognize ruuvitag devices and create discovery results for them.
33  *
34  * @author Sami Salonen - Initial contribution
35  *
36  */
37 @NonNullByDefault
38 @Component
39 public class RuuviTagDiscoveryParticipant implements BluetoothDiscoveryParticipant {
40
41     private static final int RUUVITAG_COMPANY_ID = 1177;
42
43     @Override
44     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
45         return Set.of(RuuviTagBindingConstants.THING_TYPE_BEACON);
46     }
47
48     @Override
49     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
50         Integer manufacturerId = device.getManufacturerId();
51         if (manufacturerId != null && manufacturerId == RUUVITAG_COMPANY_ID) {
52             return new ThingUID(RuuviTagBindingConstants.THING_TYPE_BEACON, device.getAdapter().getUID(),
53                     device.getAddress().toString().toLowerCase().replace(":", ""));
54         }
55         return null;
56     }
57
58     @Override
59     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
60         ThingUID thingUID = getThingUID(device);
61         if (thingUID == null) {
62             return null;
63         }
64         String label = "Ruuvi Tag";
65         Map<String, Object> properties = new HashMap<>();
66         properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
67         properties.put(Thing.PROPERTY_VENDOR, "Ruuvi Innovations Ltd (Oy)");
68         Integer txPower = device.getTxPower();
69         if (txPower != null) {
70             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
71         }
72
73         // Create the discovery result and add to the inbox
74         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
75                 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
76                 .withBridge(device.getAdapter().getUID()).withLabel(label).build();
77     }
78 }