From 241829dd184def5f4f10411f980ad6299176c149 Mon Sep 17 00:00:00 2001 From: joerg1985 <16140691+joerg1985@users.noreply.github.com> Date: Sat, 18 Jan 2025 17:37:08 +0100 Subject: [PATCH] [bluetooth.bluez] update to dbus version 0.3.0 (#18124) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörg Sautter --- .../pom.xml | 2 +- .../src/main/feature/feature.xml | 2 +- .../bluez/internal/BlueZBluetoothDevice.java | 27 +++--- .../bluez/internal/BlueZBridgeHandler.java | 11 ++- .../BlueZPropertiesChangedHandler.java | 91 ++++++++----------- .../src/main/resources/footer.xml | 2 +- 6 files changed, 58 insertions(+), 77 deletions(-) diff --git a/bundles/org.openhab.binding.bluetooth.bluez/pom.xml b/bundles/org.openhab.binding.bluetooth.bluez/pom.xml index 5bee636635b..a4ab4d934a8 100644 --- a/bundles/org.openhab.binding.bluetooth.bluez/pom.xml +++ b/bundles/org.openhab.binding.bluetooth.bluez/pom.xml @@ -26,7 +26,7 @@ com.github.hypfvieh bluez-dbus-osgi - 0.1.4 + 0.3.0 provided diff --git a/bundles/org.openhab.binding.bluetooth.bluez/src/main/feature/feature.xml b/bundles/org.openhab.binding.bluetooth.bluez/src/main/feature/feature.xml index 6893d5b93e7..0db38e40680 100644 --- a/bundles/org.openhab.binding.bluetooth.bluez/src/main/feature/feature.xml +++ b/bundles/org.openhab.binding.bluetooth.bluez/src/main/feature/feature.xml @@ -4,7 +4,7 @@ openhab-runtime-base - mvn:com.github.hypfvieh/bluez-dbus-osgi/0.2.0 + mvn:com.github.hypfvieh/bluez-dbus-osgi/0.3.0 mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version} mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.bluez/${project.version} diff --git a/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBluetoothDevice.java b/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBluetoothDevice.java index b9ba9aff49f..fba118984af 100644 --- a/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBluetoothDevice.java +++ b/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBluetoothDevice.java @@ -104,10 +104,9 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv this.name = blueZDevice.getName(); Map manData = blueZDevice.getManufacturerData(); if (manData != null) { - manData.entrySet().stream().map(Map.Entry::getKey).filter(Objects::nonNull).findFirst() - .ifPresent((UInt16 manufacturerId) -> - // Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers - this.manufacturer = manufacturerId.intValue() & 0xFFFF); + manData.keySet().stream().filter(Objects::nonNull).findFirst().ifPresent((UInt16 manufacturerId) -> + // Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers + this.manufacturer = manufacturerId.intValue() & 0xFFFF); } if (Boolean.TRUE.equals(blueZDevice.isConnected())) { @@ -208,7 +207,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv private void ensureConnected() { BluetoothDevice dev = device; - if (dev == null || !dev.isConnected()) { + if (dev == null || Boolean.FALSE.equals(dev.isConnected())) { throw new IllegalStateException("DBusBlueZ device is not set or not connected"); } } @@ -265,7 +264,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv @Override public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) { BluetoothDevice dev = device; - if (dev == null || !dev.isConnected()) { + if (dev == null || Boolean.FALSE.equals(dev.isConnected())) { return CompletableFuture .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected")); } @@ -301,7 +300,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv logger.debug("writeCharacteristic()"); BluetoothDevice dev = device; - if (dev == null || !dev.isConnected()) { + if (dev == null || Boolean.FALSE.equals(dev.isConnected())) { return CompletableFuture .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected")); } @@ -346,13 +345,13 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv @Override public void onManufacturerDataUpdate(ManufacturerDataEvent event) { - for (Map.Entry entry : event.getData().entrySet()) { + event.getData().forEach((key, value) -> { BluetoothScanNotification notification = new BluetoothScanNotification(); - byte[] data = new byte[entry.getValue().length + 2]; - data[0] = (byte) (entry.getKey() & 0xFF); - data[1] = (byte) (entry.getKey() >>> 8); + byte[] data = new byte[value.length + 2]; + data[0] = (byte) (key & 0xFF); + data[1] = (byte) (key >>> 8); - System.arraycopy(entry.getValue(), 0, data, 2, entry.getValue().length); + System.arraycopy(value, 0, data, 2, value.length); if (logger.isDebugEnabled()) { logger.debug("Received manufacturer data for '{}': {}", address, HexUtils.bytesToHex(data, " ")); @@ -360,7 +359,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv notification.setManufacturerData(data); notifyListeners(BluetoothEventType.SCAN_RECORD, notification); - } + }); } @Override @@ -513,7 +512,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv @Override public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) { BluetoothDevice dev = device; - if (dev == null || !dev.isConnected()) { + if (dev == null || Boolean.FALSE.equals(dev.isConnected())) { return CompletableFuture .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected")); } diff --git a/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBridgeHandler.java b/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBridgeHandler.java index 5475f63d34a..1fda28ba99f 100644 --- a/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBridgeHandler.java +++ b/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBridgeHandler.java @@ -25,6 +25,7 @@ import org.bluez.exceptions.BluezNotReadyException; import org.bluez.exceptions.BluezNotSupportedException; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.types.Variant; import org.openhab.binding.bluetooth.AbstractBluetoothBridgeHandler; import org.openhab.binding.bluetooth.BluetoothAddress; @@ -149,10 +150,10 @@ public class BlueZBridgeHandler extends AbstractBluetoothBridgeHandler> filter = new HashMap<>(); filter.put("DuplicateData", new Variant<>(true)); try { - adapter.setDiscoveryFilter(filter); + localAdapter.setDiscoveryFilter(filter); } catch (BluezInvalidArgumentsException | BluezFailedException | BluezNotSupportedException | BluezNotReadyException e) { - throw new RuntimeException(e); + throw new DBusExecutionException("failed to set the discovery filter", e); } // now lets make sure that discovery is turned on @@ -175,14 +176,14 @@ public class BlueZBridgeHandler extends AbstractBluetoothBridgeHandler bluezDevices = deviceManager.getDevices(adapter); + List bluezDevices = deviceManager.getDevices(localAdapter); logger.debug("Found {} Bluetooth devices.", bluezDevices.size()); for (BluetoothDevice bluezDevice : bluezDevices) { if (bluezDevice.getAddress() == null) { diff --git a/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZPropertiesChangedHandler.java b/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZPropertiesChangedHandler.java index 976c708c7d4..bef8cf6bbfb 100644 --- a/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZPropertiesChangedHandler.java +++ b/bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZPropertiesChangedHandler.java @@ -20,7 +20,6 @@ import java.util.concurrent.ScheduledExecutorService; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.freedesktop.dbus.DBusMap; import org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler; import org.freedesktop.dbus.interfaces.Properties.PropertiesChanged; import org.freedesktop.dbus.types.UInt16; @@ -138,102 +137,84 @@ public class BlueZPropertiesChangedHandler extends AbstractPropertiesChangedHand } private void onDiscoveringUpdate(String dbusPath, Variant variant) { - Object discovered = variant.getValue(); - if (discovered instanceof Boolean) { - notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, (boolean) discovered)); + if (variant.getValue() instanceof Boolean discovered) { + notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, discovered)); } } private void onPoweredUpdate(String dbusPath, Variant variant) { - Object powered = variant.getValue(); - if (powered instanceof Boolean) { - notifyListeners(new AdapterPoweredChangedEvent(dbusPath, (boolean) powered)); + if (variant.getValue() instanceof Boolean powered) { + notifyListeners(new AdapterPoweredChangedEvent(dbusPath, powered)); } } private void onServicesResolved(String dbusPath, Variant variant) { - Object resolved = variant.getValue(); - if (resolved instanceof Boolean) { - notifyListeners(new ServicesResolvedEvent(dbusPath, (boolean) resolved)); + if (variant.getValue() instanceof Boolean resolved) { + notifyListeners(new ServicesResolvedEvent(dbusPath, resolved)); } } private void onNameUpdate(String dbusPath, Variant variant) { - Object name = variant.getValue(); - if (name instanceof String) { - notifyListeners(new NameEvent(dbusPath, (String) name)); + if (variant.getValue() instanceof String name) { + notifyListeners(new NameEvent(dbusPath, name)); } } private void onTXPowerUpdate(String dbusPath, Variant variant) { - Object txPower = variant.getValue(); - if (txPower instanceof Short) { - notifyListeners(new TXPowerEvent(dbusPath, (short) txPower)); + if (variant.getValue() instanceof Short txPower) { + notifyListeners(new TXPowerEvent(dbusPath, txPower)); } } private void onConnectedUpdate(String dbusPath, Variant variant) { - Object connected = variant.getValue(); - if (connected instanceof Boolean) { - notifyListeners(new ConnectedEvent(dbusPath, (boolean) connected)); + if (variant.getValue() instanceof Boolean connected) { + notifyListeners(new ConnectedEvent(dbusPath, connected)); } } private void onManufacturerDataUpdate(String dbusPath, Variant variant) { - Map eventData = new HashMap<>(); + if (variant.getValue() instanceof Map map) { + Map eventData = new HashMap<>(); - Object map = variant.getValue(); - if (map instanceof DBusMap) { - DBusMap dbm = (DBusMap) map; - for (Map.Entry entry : dbm.entrySet()) { - Object key = entry.getKey(); - Object value = entry.getValue(); - if (key instanceof UInt16 && value instanceof Variant) { - value = ((Variant) value).getValue(); - if (value instanceof byte[]) { - eventData.put(((UInt16) key).shortValue(), ((byte[]) value)); - } + map.forEach((key, value) -> { + if (key instanceof UInt16 iKey && value instanceof Variant vValue + && vValue.getValue() instanceof byte[] bValue) { + eventData.put(iKey.shortValue(), bValue); } + }); + + if (!eventData.isEmpty()) { + notifyListeners(new ManufacturerDataEvent(dbusPath, eventData)); } } - if (!eventData.isEmpty()) { - notifyListeners(new ManufacturerDataEvent(dbusPath, eventData)); - } } private void onServiceDataUpdate(String dbusPath, Variant variant) { - Map serviceData = new HashMap<>(); + if (variant.getValue() instanceof Map map) { + Map serviceData = new HashMap<>(); - Object map = variant.getValue(); - if (map instanceof DBusMap) { - DBusMap dbm = (DBusMap) map; - for (Map.Entry entry : dbm.entrySet()) { - Object key = entry.getKey(); - Object value = entry.getValue(); - if (key instanceof String && value instanceof Variant) { - value = ((Variant) value).getValue(); - if (value instanceof byte[]) { - serviceData.put(((String) key), ((byte[]) value)); - } + map.forEach((key, value) -> { + if (key instanceof String sKey && value instanceof Variant vValue + && vValue.getValue() instanceof byte[] bValue) { + serviceData.put(sKey, bValue); } + }); + + if (!serviceData.isEmpty()) { + notifyListeners(new ServiceDataEvent(dbusPath, serviceData)); } } - if (!serviceData.isEmpty()) { - notifyListeners(new ServiceDataEvent(dbusPath, serviceData)); - } } private void onValueUpdate(String dbusPath, Variant variant) { - Object value = variant.getValue(); - if (value instanceof byte[]) { - notifyListeners(new CharacteristicUpdateEvent(dbusPath, (byte[]) value)); + if (variant.getValue() instanceof byte[] bytes) { + notifyListeners(new CharacteristicUpdateEvent(dbusPath, bytes)); } } private void onRSSIUpdate(String dbusPath, Variant variant) { - Object rssi = variant.getValue(); - if (rssi instanceof Short) { - notifyListeners(new RssiEvent(dbusPath, (short) rssi)); + if (variant.getValue() instanceof Short rssi) { + notifyListeners(new RssiEvent(dbusPath, rssi)); } } } diff --git a/features/openhab-addons/src/main/resources/footer.xml b/features/openhab-addons/src/main/resources/footer.xml index 8867b5f5423..59818c6178d 100644 --- a/features/openhab-addons/src/main/resources/footer.xml +++ b/features/openhab-addons/src/main/resources/footer.xml @@ -2,7 +2,7 @@ openhab-runtime-base openhab-transport-serial - mvn:com.github.hypfvieh/bluez-dbus-osgi/0.2.0 + mvn:com.github.hypfvieh/bluez-dbus-osgi/0.3.0 mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version} mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.airthings/${project.version} mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.am43/${project.version}