]> git.basschouten.com Git - openhab-addons.git/blob
decf69417ae3b1b080d5deb35b8b766166203557
[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         if (manufacturerMacId.equals(RADONEYE_BLUETOOTH_COMPANY_ID.toLowerCase())) {
84             return true;
85         }
86         return false;
87     }
88
89     private String getSerial(BluetoothDiscoveryDevice device) {
90         String name = device.getName();
91         String[] parts = name.split(":");
92         if (parts.length == 3) {
93             return parts[2];
94         } else {
95             return "";
96         }
97     }
98
99     private String getManufacturer(BluetoothDiscoveryDevice device) {
100         String name = device.getName();
101         String[] parts = name.split(":");
102         if (parts.length == 3) {
103             return parts[0];
104         } else {
105             return "";
106         }
107     }
108
109     private String getModel(BluetoothDiscoveryDevice device) {
110         String name = device.getName();
111         String[] parts = name.split(":");
112         if (parts.length == 3) {
113             return parts[1];
114         } else {
115             return "";
116         }
117     }
118
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);
131         } else {
132             properties.put(Thing.PROPERTY_MODEL_ID, getSerial(device));
133         }
134         if (firmwareRevision != null) {
135             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
136         }
137         if (model != null) {
138             properties.put(Thing.PROPERTY_MODEL_ID, model);
139         } else {
140             properties.put(Thing.PROPERTY_MODEL_ID, getModel(device));
141         }
142         if (hardwareRevision != null) {
143             properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
144         }
145         if (txPower != null) {
146             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
147         }
148         properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
149
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();
154     }
155 }