]> git.basschouten.com Git - openhab-addons.git/blob
80ff2ac7713cd07447a7c79ee36d4b5cc739cc12
[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.generic.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.BluetoothCompanyIdentifiers;
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 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class implements the BluetoothDiscoveryParticipant for generic bluetooth devices.
36  *
37  * @author Connor Petty - Initial contribution
38  *
39  */
40 @NonNullByDefault
41 @Component(service = BluetoothDiscoveryParticipant.class)
42 public class GenericDiscoveryParticipant implements BluetoothDiscoveryParticipant {
43
44     private final Logger logger = LoggerFactory.getLogger(GenericDiscoveryParticipant.class);
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return Set.of(GenericBindingConstants.THING_TYPE_GENERIC);
49     }
50
51     @Override
52     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
53         ThingUID thingUID = getThingUID(device);
54         if (thingUID == null) {
55             // the thingUID will never be null in practice but this makes the null checker happy
56             return null;
57         }
58         String label = "Generic Connectable Bluetooth Device";
59         Map<String, Object> properties = new HashMap<>();
60         properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
61         Integer txPower = device.getTxPower();
62         if (txPower != null && txPower > 0) {
63             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
64         }
65         String manufacturer = BluetoothCompanyIdentifiers.get(device.getManufacturerId());
66         if (manufacturer == null) {
67             logger.debug("Unknown manufacturer Id ({}) found on bluetooth device.", device.getManufacturerId());
68         } else {
69             properties.put(Thing.PROPERTY_VENDOR, manufacturer);
70             label += " (" + manufacturer + ")";
71         }
72
73         addPropertyIfPresent(properties, Thing.PROPERTY_MODEL_ID, device.getModel());
74         addPropertyIfPresent(properties, Thing.PROPERTY_SERIAL_NUMBER, device.getSerialNumber());
75         addPropertyIfPresent(properties, Thing.PROPERTY_HARDWARE_VERSION, device.getHardwareRevision());
76         addPropertyIfPresent(properties, Thing.PROPERTY_FIRMWARE_VERSION, device.getFirmwareRevision());
77         addPropertyIfPresent(properties, BluetoothBindingConstants.PROPERTY_SOFTWARE_VERSION,
78                 device.getSoftwareRevision());
79
80         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
81                 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
82                 .withBridge(device.getAdapter().getUID()).withLabel(label).build();
83     }
84
85     private static void addPropertyIfPresent(Map<String, Object> properties, String key, @Nullable Object value) {
86         if (value != null) {
87             properties.put(key, value);
88         }
89     }
90
91     @Override
92     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
93         return new ThingUID(GenericBindingConstants.THING_TYPE_GENERIC, device.getAdapter().getUID(),
94                 device.getAddress().toString().toLowerCase().replace(":", ""));
95     }
96
97     @Override
98     public boolean requiresConnection(BluetoothDiscoveryDevice device) {
99         return true;
100     }
101
102     @Override
103     public int order() {
104         // we want to go last
105         return Integer.MAX_VALUE;
106     }
107 }