Skip to content

Commit

Permalink
[bluetooth] Make BluetoothDevice more abstract (openhab#7380)
Browse files Browse the repository at this point in the history
Signed-off-by: Connor Petty <[email protected]>
  • Loading branch information
cpmeister authored and LoungeFlyZ committed Jun 8, 2020
1 parent f231c59 commit b9de136
Show file tree
Hide file tree
Showing 17 changed files with 883 additions and 477 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.openhab.binding.bluetooth.BluetoothBindingConstants;
import org.openhab.binding.bluetooth.BluetoothDevice;
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
import org.osgi.service.component.annotations.Component;

Expand All @@ -49,8 +49,7 @@ public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
}

@Override
@Nullable
public ThingUID getThingUID(BluetoothDevice device) {
public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
if (isAirthingsDevice(device)) {
if (WAVE_PLUS_MODEL.equals(device.getModel())) {
return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_PLUS,
Expand All @@ -61,8 +60,7 @@ public ThingUID getThingUID(BluetoothDevice device) {
}

@Override
@Nullable
public DiscoveryResult createResult(BluetoothDevice device) {
public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
if (!isAirthingsDevice(device)) {
return null;
}
Expand All @@ -77,19 +75,19 @@ public DiscoveryResult createResult(BluetoothDevice device) {
}

@Override
public boolean requiresConnection(BluetoothDevice device) {
public boolean requiresConnection(BluetoothDiscoveryDevice device) {
return isAirthingsDevice(device);
}

private boolean isAirthingsDevice(BluetoothDevice device) {
private boolean isAirthingsDevice(BluetoothDiscoveryDevice device) {
Integer manufacturerId = device.getManufacturerId();
if (manufacturerId != null && manufacturerId == AIRTHINGS_COMPANY_ID) {
return true;
}
return false;
}

private DiscoveryResult createWavePlus(BluetoothDevice device, ThingUID thingUID) {
private DiscoveryResult createWavePlus(BluetoothDiscoveryDevice device, ThingUID thingUID) {
Map<String, Object> properties = new HashMap<>();
properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
properties.put(Thing.PROPERTY_VENDOR, "Airthings AS");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.openhab.binding.bluetooth.BluetoothBindingConstants;
import org.openhab.binding.bluetooth.BluetoothDevice;
import org.openhab.binding.bluetooth.BluetoothDevice.ConnectionState;
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
import org.osgi.service.component.annotations.Component;

Expand All @@ -46,7 +46,7 @@ public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
}

@Override
public @Nullable DiscoveryResult createResult(BluetoothDevice device) {
public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
ThingUID thingUID = getThingUID(device);
if (thingUID == null) {
return null;
Expand All @@ -68,7 +68,7 @@ public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
}

@Override
public @Nullable ThingUID getThingUID(BluetoothDevice device) {
public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
if (device.getConnectionState() == ConnectionState.CONNECTED
&& device.supportsService(AM43BindingConstants.SERVICE_UUID)) {
return new ThingUID(AM43BindingConstants.THING_TYPE_AM43, device.getAdapter().getUID(),
Expand All @@ -78,7 +78,7 @@ public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
}

@Override
public boolean requiresConnection(BluetoothDevice device) {
public boolean requiresConnection(BluetoothDiscoveryDevice device) {
return device.getManufacturerId() == null && device.getName() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.common.ThreadPoolManager;
import org.openhab.binding.bluetooth.BaseBluetoothDevice;
import org.openhab.binding.bluetooth.BluetoothAddress;
import org.openhab.binding.bluetooth.BluetoothCharacteristic;
import org.openhab.binding.bluetooth.BluetoothCompletionStatus;
import org.openhab.binding.bluetooth.BluetoothDescriptor;
import org.openhab.binding.bluetooth.BluetoothDevice;
import org.openhab.binding.bluetooth.BluetoothService;
import org.openhab.binding.bluetooth.bluegiga.handler.BlueGigaBridgeHandler;
Expand Down Expand Up @@ -52,7 +54,7 @@
* @author Chris Jackson - Initial contribution
*/
@NonNullByDefault
public class BlueGigaBluetoothDevice extends BluetoothDevice implements BlueGigaEventListener {
public class BlueGigaBluetoothDevice extends BaseBluetoothDevice implements BlueGigaEventListener {
private final long TIMEOUT_SEC = 60;

private final Logger logger = LoggerFactory.getLogger(BlueGigaBluetoothDevice.class);
Expand Down Expand Up @@ -169,6 +171,30 @@ public boolean discoverServices() {
return true;
}

@Override
public boolean enableNotifications(BluetoothCharacteristic characteristic) {
// TODO will be implemented in a followup PR
return false;
}

@Override
public boolean disableNotifications(BluetoothCharacteristic characteristic) {
// TODO will be implemented in a followup PR
return false;
}

@Override
public boolean enableNotifications(BluetoothDescriptor descriptor) {
// TODO will be implemented in a followup PR
return false;
}

@Override
public boolean disableNotifications(BluetoothDescriptor descriptor) {
// TODO will be implemented in a followup PR
return false;
}

@Override
public boolean readCharacteristic(@Nullable BluetoothCharacteristic characteristic) {
if (characteristic == null || characteristic.getHandle() == 0) {
Expand Down Expand Up @@ -278,7 +304,7 @@ private void handleScanEvent(BlueGigaScanResponseEvent event) {
Map<Short, int[]> eirRecord = (Map<Short, int[]>) obj;
Map.Entry<Short, int[]> eirEntry = eirRecord.entrySet().iterator().next();

manufacturer = (int) eirEntry.getKey();
manufacturer = eirEntry.getKey().intValue();

int[] manufacturerInt = eirEntry.getValue();
manufacturerData = new byte[manufacturerInt.length + 2];
Expand Down Expand Up @@ -539,4 +565,5 @@ private void cancelTimer(@Nullable ScheduledFuture<?> task) {
private ScheduledFuture<?> startTimer(Runnable command, long timeout) {
return scheduler.schedule(command, timeout, TimeUnit.SECONDS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import org.eclipse.smarthome.core.common.ThreadPoolManager;
import org.eclipse.smarthome.core.util.HexUtils;
import org.openhab.binding.bluetooth.BaseBluetoothDevice;
import org.openhab.binding.bluetooth.BluetoothAddress;
import org.openhab.binding.bluetooth.BluetoothCharacteristic;
import org.openhab.binding.bluetooth.BluetoothCompletionStatus;
import org.openhab.binding.bluetooth.BluetoothDescriptor;
import org.openhab.binding.bluetooth.BluetoothDevice;
import org.openhab.binding.bluetooth.BluetoothService;
import org.openhab.binding.bluetooth.bluez.handler.BlueZBridgeHandler;
import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
Expand All @@ -43,7 +43,7 @@
* @author Kai Kreuzer - Initial contribution and API
*
*/
public class BlueZBluetoothDevice extends BluetoothDevice {
public class BlueZBluetoothDevice extends BaseBluetoothDevice {

private tinyb.BluetoothDevice device;

Expand Down Expand Up @@ -113,12 +113,14 @@ public synchronized void updateTinybDevice(tinyb.BluetoothDevice tinybDevice) {
private void enableNotifications() {
logger.debug("Enabling notifications for device '{}'", device.getAddress());
device.enableRSSINotifications(n -> {
updateLastSeenTime();
rssi = (int) n;
BluetoothScanNotification notification = new BluetoothScanNotification();
notification.setRssi(n);
notifyListeners(BluetoothEventType.SCAN_RECORD, notification);
});
device.enableManufacturerDataNotifications(n -> {
updateLastSeenTime();
for (Map.Entry<Short, byte[]> entry : n.entrySet()) {
BluetoothScanNotification notification = new BluetoothScanNotification();
byte[] data = new byte[entry.getValue().length + 2];
Expand All @@ -133,19 +135,22 @@ private void enableNotifications() {
}
});
device.enableConnectedNotifications(connected -> {
updateLastSeenTime();
connectionState = connected ? ConnectionState.CONNECTED : ConnectionState.DISCONNECTED;
logger.debug("Connection state of '{}' changed to {}", address, connectionState);
notifyListeners(BluetoothEventType.CONNECTION_STATE,
new BluetoothConnectionStatusNotification(connectionState));
});
device.enableServicesResolvedNotifications(resolved -> {
updateLastSeenTime();
logger.debug("Received services resolved event for '{}': {}", address, resolved);
if (resolved) {
refreshServices();
notifyListeners(BluetoothEventType.SERVICES_DISCOVERED);
}
});
device.enableServiceDataNotifications(data -> {
updateLastSeenTime();
if (logger.isDebugEnabled()) {
logger.debug("Received service data for '{}':", address);
for (Map.Entry<String, byte[]> entry : data.entrySet()) {
Expand Down Expand Up @@ -221,6 +226,11 @@ public boolean disconnect() {
return false;
}

@Override
public boolean discoverServices() {
return false;
}

private void ensureConnected() {
if (device == null || !device.getConnected()) {
throw new IllegalStateException("TinyB device is not set or not connected");
Expand Down Expand Up @@ -396,4 +406,5 @@ public void dispose() {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
import java.util.Map;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.openhab.binding.bluetooth.BluetoothBindingConstants;
import org.openhab.binding.bluetooth.BluetoothDevice;
import org.openhab.binding.bluetooth.blukii.BlukiiBindingConstants;
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
import org.osgi.service.component.annotations.Component;

Expand All @@ -36,16 +36,17 @@
* @author Kai Kreuzer - Initial contribution and API
*
*/
@NonNullByDefault
@Component(immediate = true)
public class BlukiiDiscoveryParticipant implements BluetoothDiscoveryParticipant {

@Override
public @NonNull Set<@NonNull ThingTypeUID> getSupportedThingTypeUIDs() {
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
return Collections.singleton(BlukiiBindingConstants.THING_TYPE_BEACON);
}

@Override
public @Nullable ThingUID getThingUID(@NonNull BluetoothDevice device) {
public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
String name = device.getName();
if (name != null && name.startsWith(BlukiiBindingConstants.BLUKII_PREFIX)) {
if (name.charAt(BlukiiBindingConstants.BLUKII_PREFIX.length()) == 'B') {
Expand All @@ -57,7 +58,7 @@ public class BlukiiDiscoveryParticipant implements BluetoothDiscoveryParticipant
}

@Override
public DiscoveryResult createResult(@NonNull BluetoothDevice device) {
public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
ThingUID thingUID = getThingUID(device);

if (thingUID != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.openhab.binding.bluetooth.BluetoothBindingConstants;
import org.openhab.binding.bluetooth.BluetoothDevice;
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
import org.osgi.service.component.annotations.Component;

Expand All @@ -47,8 +47,7 @@ public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
}

@Override
@Nullable
public ThingUID getThingUID(BluetoothDevice device) {
public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
Integer manufacturerId = device.getManufacturerId();
if (manufacturerId != null && manufacturerId == RUUVITAG_COMPANY_ID) {
return new ThingUID(RuuviTagBindingConstants.THING_TYPE_BEACON, device.getAdapter().getUID(),
Expand All @@ -58,8 +57,7 @@ public ThingUID getThingUID(BluetoothDevice device) {
}

@Override
@Nullable
public DiscoveryResult createResult(BluetoothDevice device) {
public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
ThingUID thingUID = getThingUID(device);
if (thingUID == null) {
return null;
Expand Down
Loading

0 comments on commit b9de136

Please sign in to comment.