2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth.radoneye.internal;
15 import java.util.HashMap;
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;
32 * This discovery participant is able to recognize RadonEye devices and create discovery results for them.
34 * @author Peter Obel - Initial contribution
39 public class RadoneyeDiscoveryParticipant implements BluetoothDiscoveryParticipant {
41 private static final String RADONEYE_BLUETOOTH_COMPANY_ID = "f24be3";
43 private static final String RD200 = "R20"; // RadonEye First Generation BLE
46 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47 return RadoneyeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
51 public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
52 if (isRadoneyeDevice(device)) {
53 if (RD200.equals(getModel(device))) {
54 return new ThingUID(RadoneyeBindingConstants.THING_TYPE_RADONEYE, device.getAdapter().getUID(),
55 device.getAddress().toString().toLowerCase().replace(":", ""));
62 public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
63 if (!isRadoneyeDevice(device)) {
66 ThingUID thingUID = getThingUID(device);
67 if (thingUID == null) {
70 if (RD200.equals(getModel(device))) {
71 return createResult(device, thingUID, "RadonEye (BLE)");
77 public boolean requiresConnection(BluetoothDiscoveryDevice device) {
78 return isRadoneyeDevice(device);
81 private boolean isRadoneyeDevice(BluetoothDiscoveryDevice device) {
82 String manufacturerMacId = device.getAddress().toString().toLowerCase().replace(":", "").substring(0, 6);
83 if (manufacturerMacId.equals(RADONEYE_BLUETOOTH_COMPANY_ID.toLowerCase())) {
89 private String getSerial(BluetoothDiscoveryDevice device) {
90 String name = device.getName();
91 String[] parts = name.split(":");
92 if (parts.length == 3) {
99 private String getManufacturer(BluetoothDiscoveryDevice device) {
100 String name = device.getName();
101 String[] parts = name.split(":");
102 if (parts.length == 3) {
109 private String getModel(BluetoothDiscoveryDevice device) {
110 String name = device.getName();
111 String[] parts = name.split(":");
112 if (parts.length == 3) {
119 private DiscoveryResult createResult(BluetoothDiscoveryDevice device, ThingUID thingUID, String label) {
120 Map<String, Object> properties = new HashMap<>();
121 properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
122 properties.put(Thing.PROPERTY_VENDOR, "RadonEye");
123 String name = device.getName();
124 String serialNumber = device.getSerialNumber();
125 String firmwareRevision = device.getFirmwareRevision();
126 String model = device.getModel();
127 String hardwareRevision = device.getHardwareRevision();
128 Integer txPower = device.getTxPower();
129 if (serialNumber != null) {
130 properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
132 properties.put(Thing.PROPERTY_MODEL_ID, getSerial(device));
134 if (firmwareRevision != null) {
135 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
138 properties.put(Thing.PROPERTY_MODEL_ID, model);
140 properties.put(Thing.PROPERTY_MODEL_ID, getModel(device));
142 if (hardwareRevision != null) {
143 properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
145 if (txPower != null) {
146 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
148 properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
150 // Create the discovery result and add to the inbox
151 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
152 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
153 .withBridge(device.getAdapter().getUID()).withLabel(label).build();