]> git.basschouten.com Git - openhab-addons.git/blob
f1c911f859a15f86010147e8cbbf9a0651ff90b8
[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.radoneye.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 RadonEye devices and create discovery results for them.
33  *
34  * @author Peter Obel - Initial contribution
35  *
36  */
37 @NonNullByDefault
38 @Component
39 public class RadoneyeDiscoveryParticipant implements BluetoothDiscoveryParticipant {
40
41     private static final String RADONEYE_BLUETOOTH_COMPANY_ID = "f24be3";
42
43     private static final String RD200 = "R20"; // RadonEye First Generation BLE
44
45     @Override
46     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47         return RadoneyeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
48     }
49
50     @Override
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(":", ""));
56             }
57         }
58         return null;
59     }
60
61     @Override
62     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
63         if (!isRadoneyeDevice(device)) {
64             return null;
65         }
66         ThingUID thingUID = getThingUID(device);
67         if (thingUID == null) {
68             return null;
69         }
70         if (RD200.equals(getModel(device))) {
71             return createResult(device, thingUID, "RadonEye (BLE)");
72         }
73         return null;
74     }
75
76     @Override
77     public boolean requiresConnection(BluetoothDiscoveryDevice device) {
78         return isRadoneyeDevice(device);
79     }
80
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());
84     }
85
86     private String getSerial(BluetoothDiscoveryDevice device) {
87         String name = device.getName();
88         String[] parts = name.split(":");
89         if (parts.length == 3) {
90             return parts[2];
91         } else {
92             return "";
93         }
94     }
95
96     private String getManufacturer(BluetoothDiscoveryDevice device) {
97         String name = device.getName();
98         String[] parts = name.split(":");
99         if (parts.length == 3) {
100             return parts[0];
101         } else {
102             return "";
103         }
104     }
105
106     private String getModel(BluetoothDiscoveryDevice device) {
107         String name = device.getName();
108         String[] parts = name.split(":");
109         if (parts.length == 3) {
110             return parts[1];
111         } else {
112             return "";
113         }
114     }
115
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);
127         } else {
128             properties.put(Thing.PROPERTY_MODEL_ID, getSerial(device));
129         }
130         if (firmwareRevision != null) {
131             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
132         }
133         if (model != null) {
134             properties.put(Thing.PROPERTY_MODEL_ID, model);
135         } else {
136             properties.put(Thing.PROPERTY_MODEL_ID, getModel(device));
137         }
138         if (hardwareRevision != null) {
139             properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
140         }
141         if (txPower != null) {
142             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
143         }
144         properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
145
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();
150     }
151 }