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 return manufacturerMacId.equals(RADONEYE_BLUETOOTH_COMPANY_ID.toLowerCase());
86 private String getSerial(BluetoothDiscoveryDevice device) {
87 String name = device.getName();
88 String[] parts = name.split(":");
89 if (parts.length == 3) {
96 private String getManufacturer(BluetoothDiscoveryDevice device) {
97 String name = device.getName();
98 String[] parts = name.split(":");
99 if (parts.length == 3) {
106 private String getModel(BluetoothDiscoveryDevice device) {
107 String name = device.getName();
108 String[] parts = name.split(":");
109 if (parts.length == 3) {
116 private DiscoveryResult createResult(BluetoothDiscoveryDevice device, ThingUID thingUID, String label) {
117 Map<String, Object> properties = new HashMap<>();
118 properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
119 properties.put(Thing.PROPERTY_VENDOR, "RadonEye");
120 String serialNumber = device.getSerialNumber();
121 String firmwareRevision = device.getFirmwareRevision();
122 String model = device.getModel();
123 String hardwareRevision = device.getHardwareRevision();
124 Integer txPower = device.getTxPower();
125 if (serialNumber != null) {
126 properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
128 properties.put(Thing.PROPERTY_MODEL_ID, getSerial(device));
130 if (firmwareRevision != null) {
131 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
134 properties.put(Thing.PROPERTY_MODEL_ID, model);
136 properties.put(Thing.PROPERTY_MODEL_ID, getModel(device));
138 if (hardwareRevision != null) {
139 properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
141 if (txPower != null) {
142 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
144 properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
146 // Create the discovery result and add to the inbox
147 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
148 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
149 .withBridge(device.getAdapter().getUID()).withLabel(label).build();