100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added bluetooth mesh proxy address to agent configuration
Rainer Hitzfc68054e31b9
diff --git a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshAgent.java b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshAgent.javaindex 7612f810c..fc8125f68 100644--- a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshAgent.java+++ b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshAgent.java@@ -32,6 +32,7 @@ public class BluetoothMeshAgent extends Agent<BluetoothMeshAgent, BluetoothMeshPpublic static final AttributeDescriptor<String> NETWORK_KEY = new AttributeDescriptor<String>("networkKey", ValueType.TEXT);public static final AttributeDescriptor<String> APPLICATION_KEY = new AttributeDescriptor<String>("applicationKey", ValueType.TEXT);+ public static final AttributeDescriptor<String> PROXY_ADDRESS = new AttributeDescriptor<String>("proxyAddress", ValueType.TEXT);public static final AttributeDescriptor<String> SOURCE_ADDRESS = new AttributeDescriptor<String>("sourceAddress", ValueType.TEXT);public static final AttributeDescriptor<Integer> SEQUENCE_NUMBER = new AttributeDescriptor<Integer>("sequenceNumber", ValueType.POSITIVE_INTEGER);public static final AttributeDescriptor<Integer> MTU = new AttributeDescriptor<Integer>("mtu", ValueType.POSITIVE_INTEGER);@@ -75,6 +76,15 @@ public class BluetoothMeshAgent extends Agent<BluetoothMeshAgent, BluetoothMeshPreturn this;}+ public Optional<String> getProxyAddress() {+ return getAttributes().getValue(PROXY_ADDRESS);+ }++ public BluetoothMeshAgent setProxyAddress(String value) {+ getAttributes().getOrCreate(PROXY_ADDRESS).setValue(value);+ return this;+ }+public Optional<String> getSourceAddress() {return getAttributes().getValue(SOURCE_ADDRESS);}diff --git a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshNetwork.java b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshNetwork.javaindex e8029bbef..4664dfc23 100644--- a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshNetwork.java+++ b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshNetwork.java@@ -68,6 +68,7 @@ public class BluetoothMeshNetwork extends BluetoothCentralManagerCallback implemprivate final MainThreadManager mainThreadManager;private final Consumer<ConnectionStatus> statusConsumer;private final NetworkKey networkKey;+ private final String proxyAddress;private final int sourceUnicastAddress;private final Map<Integer, ApplicationKey> applicationKeyMap;private final Integer mtu;@@ -77,12 +78,13 @@ public class BluetoothMeshNetwork extends BluetoothCentralManagerCallback implem// Constructors -------------------------------------------------------------------------------public BluetoothMeshNetwork(BluetoothCentralManager bluetoothCentral, SequenceNumberPersistencyManager sequenceNumberManager, MainThreadManager mainThread,- int unicastAddress, NetworkKey networkKey, Map<Integer, ApplicationKey> applicationKeyMap,+ String proxyAddress, int unicastAddress, NetworkKey networkKey, Map<Integer, ApplicationKey> applicationKeyMap,int mtu, int sequenceNumber, ScheduledExecutorService executorService, Consumer<ConnectionStatus> statusConsumer) {this.bluetoothCentral = bluetoothCentral;this.sequenceNumberManager = sequenceNumberManager;this.mainThreadManager = mainThread;this.networkKey = networkKey;+ this.proxyAddress = proxyAddress;this.sourceUnicastAddress = unicastAddress;this.applicationKeyMap = applicationKeyMap;this.mtu = mtu;@@ -456,7 +458,7 @@ public class BluetoothMeshNetwork extends BluetoothCentralManagerCallback implem}executorService.execute(() -> statusConsumer.accept(ConnectionStatus.CONNECTING));proxyScanner = new BluetoothMeshProxyScanner(mainThreadManager, bluetoothCentral, executorService);- proxyScanner.start(networkKey, SCAN_DURATION, new BluetoothMeshProxyScannerCallback() {+ proxyScanner.start(networkKey, proxyAddress, SCAN_DURATION, new BluetoothMeshProxyScannerCallback() {@Overridepublic void onMeshProxiesScanned(List<BluetoothMeshProxy> meshProxies, Integer errorCode) {LOG.info("Finished scanning Bluetooth mesh proxies.");diff --git a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProtocol.javaindex 5604ec66d..9d894e279 100644--- a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProtocol.java@@ -67,6 +67,7 @@ public class BluetoothMeshProtocol extends AbstractProtocol<BluetoothMeshAgent,public static final int DEFAULT_NETWORK_KEY_INDEX = 0;public static final int DEFAULT_APPLICATION_KEY_INDEX = 0;public static final String REGEXP_INDEX_AND_KEY = "^(\\s*(0|([1-9]+[0-9]*))\\s*:)?(\\s*[0-9A-Fa-f]{32}\\s*)";+ public static final String REGEXP_PROXY_ADDRESS = "^(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$";// Class Members --------------------------------------------------------------------------------@@ -208,6 +209,14 @@ public class BluetoothMeshProtocol extends AbstractProtocol<BluetoothMeshAgent,}ApplicationKey applicationKey = new ApplicationKey(appKeyIndex, MeshParserUtils.toByteArray(appKeyAsString));+ String proxyAddress = agent.getProxyAddress().orElse(null);+ proxyAddress = proxyAddress != null ? proxyAddress.trim() : null;+ if (proxyAddress != null && !proxyAddress.matches(REGEXP_PROXY_ADDRESS)) {+ String msg = "Format of proxy address '" + proxyAddress + "' is invalid for protocol: " + this;+ LOG.warning(msg);+ throw new IllegalArgumentException(msg);+ }+String sourceAddressParam = agent.getSourceAddress().orElseThrow(() -> {String msg = "No Bluetooth Mesh unicast source address provided for protocol: " + this;LOG.warning(msg);@@ -242,8 +251,8 @@ public class BluetoothMeshProtocol extends AbstractProtocol<BluetoothMeshAgent,}BluetoothMeshProtocol.initMainThread(executorService);meshNetwork = new BluetoothMeshNetwork(- BluetoothMeshProtocol.bluetoothCentral, BluetoothMeshProtocol.sequenceNumberManager, BluetoothMeshProtocol.mainThread, sourceAddress, networkKey,- applicationKeyMap, mtuParam, oldSequenceNumber, executorService, statusConsumer+ BluetoothMeshProtocol.bluetoothCentral, BluetoothMeshProtocol.sequenceNumberManager, BluetoothMeshProtocol.mainThread,+ proxyAddress, sourceAddress, networkKey, applicationKeyMap, mtuParam, oldSequenceNumber, executorService, statusConsumer);BluetoothMeshProtocol.addNetwork(meshNetwork);BluetoothMeshProtocol.mainThread.enqueue(() -> meshNetwork.start());diff --git a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyScanner.java b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyScanner.javaindex 95c72c115..3043e368a 100644--- a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyScanner.java+++ b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyScanner.java@@ -62,6 +62,7 @@ public class BluetoothMeshProxyScanner extends BluetoothCentralManagerCallback {private final ScheduledExecutorService executorService;private volatile BluetoothMeshProxyScannerCallback callback;private volatile NetworkKey networkKey;+ private volatile String address;private volatile boolean isStarted = false;private ScheduledFuture<?> timeoutFuture;@@ -77,13 +78,14 @@ public class BluetoothMeshProxyScanner extends BluetoothCentralManagerCallback {this.executorService = executorService;}- public synchronized void start(final NetworkKey networkKey, int duration, final BluetoothMeshProxyScannerCallback callback) {+ public synchronized void start(final NetworkKey networkKey, String address, int duration, final BluetoothMeshProxyScannerCallback callback) {LOG.info("Starting mesh proxy scanner");if (isStarted) {stop();}this.callback = callback;this.networkKey = networkKey;+ this.address = address;meshProxyMap.clear();isStarted = true;Runnable runnable = () -> {@@ -91,22 +93,13 @@ public class BluetoothMeshProxyScanner extends BluetoothCentralManagerCallback {bluetoothCentral.scanForPeripheralsWithServices(new UUID[] {MESH_PROXY_UUID});};bluetoothCommandSerializer.enqueue(runnable);- timeoutFuture = executorService.schedule(new Runnable() {- @Override- public void run() {+ timeoutFuture = executorService.schedule(() -> {synchronized (BluetoothMeshProxyScanner.this) {- if (isStarted && callback != null) {- final List<BluetoothMeshProxy> proxies = meshProxyMap.values()- .stream()- .sorted(Comparator.comparingInt(BluetoothMeshProxy::getRssi).reversed())- .collect(Collectors.toList());- executorService.execute(() -> callback.onMeshProxiesScanned(proxies, null));- }timeoutFuture = null;- stop();+ executeCallbackAndStopScanner();}- }- }, duration, TimeUnit.MILLISECONDS);+ }, duration, TimeUnit.MILLISECONDS+ );}@@ -160,7 +153,7 @@ public class BluetoothMeshProxyScanner extends BluetoothCentralManagerCallback {LOG.info("Checking network identity of scanned Bluetooth mesh proxy: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");if (networkIdMatches(serviceData, networkKey)) {LOG.info("Network identity of scanned Bluetooth mesh proxy matches: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");- meshProxyMap.put(peripheral.getAddress(), new BluetoothMeshProxy(bluetoothCommandSerializer, executorService, bluetoothCentral, peripheral, scanResult));+ handleScannedPeripheral(peripheral, scanResult);} else {LOG.info("Network identity of scanned Bluetooth mesh proxy does NOT match: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");}@@ -168,7 +161,7 @@ public class BluetoothMeshProxyScanner extends BluetoothCentralManagerCallback {LOG.info("Checking node identity of scanned Bluetooth mesh proxy: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");if (checkIfNodeIdentityMatches(serviceData)) {LOG.info("Node identity of scanned Bluetooth mesh proxy matches: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");- meshProxyMap.put((peripheral.getAddress()), new BluetoothMeshProxy(bluetoothCommandSerializer, executorService, bluetoothCentral, peripheral, scanResult));+ handleScannedPeripheral(peripheral, scanResult);} else {LOG.info("Node identity of scanned Bluetooth mesh proxy does NOT match: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");}@@ -195,6 +188,29 @@ public class BluetoothMeshProxyScanner extends BluetoothCentralManagerCallback {// Private Instance Methods -------------------------------------------------------------------+ private void handleScannedPeripheral(BluetoothPeripheral peripheral, ScanResult scanResult) {+ if (address == null || (address != null && address.equalsIgnoreCase(peripheral.getAddress()))) {+ meshProxyMap.put(+ peripheral.getAddress(),+ new BluetoothMeshProxy(bluetoothCommandSerializer, executorService, bluetoothCentral, peripheral, scanResult)+ );+ }+ if (address != null && meshProxyMap.size() == 1) {+ executeCallbackAndStopScanner();+ }+ }++ private void executeCallbackAndStopScanner() {+ if (isStarted && callback != null) {+ final List<BluetoothMeshProxy> proxies = meshProxyMap.values()+ .stream()+ .sorted(Comparator.comparingInt(BluetoothMeshProxy::getRssi).reversed())+ .collect(Collectors.toList());+ executorService.execute(() -> callback.onMeshProxiesScanned(proxies, null));+ }+ stop();+ }+private boolean isAdvertisingWithNetworkIdentity(final byte[] serviceData) {return serviceData != null &&serviceData.length == 9 &&