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
Improved network reconnection
Rainer Hitz78c21800ee6b
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 eb85b7151..5f91b4a33 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@@ -60,7 +60,7 @@ public class BluetoothMeshNetwork extends BluetoothCentralManagerCallback implemprivate volatile BluetoothMeshProxyScanner proxyScanner;private volatile BluetoothMeshProxy bluetoothMeshProxy;private volatile MeshManagerApi meshManagerApi;- private volatile boolean isConnected = false;+ private volatile boolean isStarted = false;private final ScheduledExecutorService executorService;private final BluetoothCentralManager bluetoothCentral;@@ -278,55 +278,16 @@ public class BluetoothMeshNetwork extends BluetoothCentralManagerCallback implem// Public Instance Methods --------------------------------------------------------------------- public synchronized void connect() {- if (isConnected) {+ public synchronized void start() {+ if (isStarted) {return;}- isConnected = true;- bluetoothMeshProxy = null;- proxyScanner = new BluetoothMeshProxyScanner(mainThreadManager, bluetoothCentral, executorService);- proxyScanner.start(networkKey, SCAN_DURATION, new BluetoothMeshProxyScannerCallback() {- @Override- public void onMeshProxiesScanned(List<BluetoothMeshProxy> meshProxies, Integer errorCode) {- LOG.info("Finished scanning Bluetooth mesh proxies.");- if (errorCode == null) {- for (BluetoothMeshProxy curMeshProxy : meshProxies) {- LOG.info("Scan found Bluetooth mesh proxy: [Name=" + curMeshProxy.getPeripheral().getName() + ", Address=" + curMeshProxy.getPeripheral().getAddress() + ", Rssi=" + curMeshProxy.getRssi() + "]");- }- if (meshProxies.size() > 0) {- synchronized (BluetoothMeshNetwork.this) {- // TODO: consider rssi and choose proxy in close proximity- BluetoothMeshNetwork.this.bluetoothMeshProxy = meshProxies.get(0);- BluetoothMeshNetwork.this.bluetoothMeshProxy.setRxDataCallback(BluetoothMeshNetwork.this);- BluetoothMeshNetwork.this.bluetoothMeshProxy.connect(statusConsumer, new BluetoothMeshProxyConnectCallback() {- @Override- public void onMeshProxyConnected(BluetoothPeripheral peripheral, boolean isSuccess) {- synchronized (BluetoothMeshNetwork.this) {- if (isSuccess) {- LOG.info("Successfully connected to Bluetooth mesh proxy: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");- } else {- LOG.warning("Failed to connect to Bluetooth mesh proxy: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");- BluetoothMeshNetwork.this.bluetoothMeshProxy.disconnect();- BluetoothMeshNetwork.this.bluetoothMeshProxy = null;- }- }- }- });- }- } else {- LOG.info("No Bluetooth mesh proxy found!");- executorService.execute(() -> statusConsumer.accept(ConnectionStatus.ERROR));- }- } else {- LOG.info("Failed to scan Bluetooth mesh proxies: [error code=" + errorCode + "]");- executorService.execute(() -> statusConsumer.accept(ConnectionStatus.ERROR));- }- }- });+ isStarted = true;+ connect();}- public synchronized void disconnect() {- isConnected = false;+ public synchronized void stop() {+ isStarted = false;if (proxyScanner != null) {proxyScanner.stop();proxyScanner = null;@@ -488,6 +449,55 @@ public class BluetoothMeshNetwork extends BluetoothCentralManagerCallback implem// Private Instance Methods -------------------------------------------------------------------+ private synchronized void connect() {+ bluetoothMeshProxy = null;+ if (proxyScanner != null) {+ proxyScanner.stop();+ }+ executorService.execute(() -> statusConsumer.accept(ConnectionStatus.CONNECTING));+ proxyScanner = new BluetoothMeshProxyScanner(mainThreadManager, bluetoothCentral, executorService);+ proxyScanner.start(networkKey, SCAN_DURATION, new BluetoothMeshProxyScannerCallback() {+ @Override+ public void onMeshProxiesScanned(List<BluetoothMeshProxy> meshProxies, Integer errorCode) {+ LOG.info("Finished scanning Bluetooth mesh proxies.");+ if (errorCode == null) {+ for (BluetoothMeshProxy curMeshProxy : meshProxies) {+ LOG.info("Scan found Bluetooth mesh proxy: [Name=" + curMeshProxy.getPeripheral().getName() + ", Address=" + curMeshProxy.getPeripheral().getAddress() + ", Rssi=" + curMeshProxy.getRssi() + "]");+ }+ if (meshProxies.size() > 0) {+ synchronized (BluetoothMeshNetwork.this) {+ // TODO: consider rssi and choose proxy in close proximity+ BluetoothMeshNetwork.this.bluetoothMeshProxy = meshProxies.get(0);+ BluetoothMeshNetwork.this.bluetoothMeshProxy.setRxDataCallback(BluetoothMeshNetwork.this);+ BluetoothMeshNetwork.this.bluetoothMeshProxy.connect(statusConsumer, new BluetoothMeshProxyConnectCallback() {+ @Override+ public void onMeshProxyConnected(BluetoothPeripheral peripheral, boolean isSuccess, boolean isConnectionLoss) {+ synchronized (BluetoothMeshNetwork.this) {+ if (isSuccess) {+ LOG.info("Successfully connected to Bluetooth mesh proxy: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");+ } else {+ LOG.warning("Failed to connect to Bluetooth mesh proxy: [Name=" + peripheral.getName() + ", Address=" + peripheral.getAddress() + "]");+ BluetoothMeshNetwork.this.bluetoothMeshProxy = null;+ // Try it again+ executorService.execute(() -> connect());+ }+ }+ }+ });+ }+ } else {+ LOG.info("No Bluetooth mesh proxy found!");+ // Try it again+ executorService.execute(() -> connect());+ }+ } else {+ LOG.info("Failed to scan Bluetooth mesh proxies: [error code=" + errorCode + "]");+ executorService.execute(() -> statusConsumer.accept(ConnectionStatus.ERROR));+ }+ }+ });+ }+private ShadowMeshModel searchShadowModel(int address, int modelId) {ShadowMeshModel model = null;ShadowMeshElement element = elementMap.get(address);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 433386901..89df8a15e 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@@ -246,17 +246,14 @@ public class BluetoothMeshProtocol extends AbstractProtocol<BluetoothMeshAgent,applicationKeyMap, mtuParam, oldSequenceNumber, executorService, statusConsumer);BluetoothMeshProtocol.addNetwork(meshNetwork);- Runnable runnable = () -> {- meshNetwork.connect();- };- BluetoothMeshProtocol.mainThread.enqueue(runnable);+ BluetoothMeshProtocol.mainThread.enqueue(() -> meshNetwork.start());}@Overrideprotected synchronized void doStop(Container container) throws Exception {LOG.info("Stopping Bluetooth Mesh protocol.");if (meshNetwork != null) {- meshNetwork.disconnect();+ meshNetwork.stop();meshNetwork = null;}}diff --git a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyConnectCallback.java b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyConnectCallback.javaindex c127db4ee..a75b96a34 100644--- a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyConnectCallback.java+++ b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyConnectCallback.java@@ -22,5 +22,5 @@ package org.openremote.agent.protocol.bluetooth.mesh;import com.welie.blessed.BluetoothPeripheral;public interface BluetoothMeshProxyConnectCallback {- void onMeshProxyConnected(BluetoothPeripheral peripheral, boolean isSuccess);+ void onMeshProxyConnected(BluetoothPeripheral peripheral, boolean isSuccess, boolean isConnectionLoss);}diff --git a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyStateMachine.java b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyStateMachine.javaindex 44620988b..e41307ef0 100644--- a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyStateMachine.java+++ b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProxyStateMachine.java@@ -60,6 +60,7 @@ public class BluetoothMeshProxyStateMachine {private volatile BluetoothGattCharacteristic dataInCharacteristic;private volatile BluetoothGattCharacteristic dataOutCharacteristic;private volatile int retryCount = 0;+ private volatile boolean isInitialConnectionSuccessful = false;private final BlockingQueue<SendDataCommand> sendDataQueue = new LinkedBlockingQueue<>();private final QueueWorker queueWorker = new QueueWorker(sendDataQueue);@@ -293,7 +294,8 @@ public class BluetoothMeshProxyStateMachine {}void notifyListeners(boolean isSuccess, ConnectionStatus status) {- executorService.execute(() -> connectCallback.onMeshProxyConnected(peripheral, isSuccess));+ boolean isConnectionLoss = !isSuccess ? isInitialConnectionSuccessful : false;+ executorService.execute(() -> connectCallback.onMeshProxyConnected(peripheral, isSuccess, isConnectionLoss));executorService.execute(() -> statusConsumer.accept(status));}@@ -912,6 +914,7 @@ public class BluetoothMeshProxyStateMachine {public void onNotificationStateUpdate(BluetoothPeripheral peripheral, BluetoothGattCharacteristic characteristic, BluetoothCommandStatus status) {if (isExpectedPeripheral(peripheral) && getDataOutCharacteristic().getUuid().equals(characteristic.getUuid())) {if (status == BluetoothCommandStatus.COMMAND_SUCCESS) {+ isInitialConnectionSuccessful = true;setRetryCount(0);notifyListeners(true, ConnectionStatus.CONNECTED);setState(getEndState());@@ -1013,6 +1016,7 @@ public class BluetoothMeshProxyStateMachine {startTimeout(SCAN_TIMEOUT);setState(getScanState());scanOn();+ executorService.execute(() -> statusConsumer.accept(ConnectionStatus.DISCONNECTED));}}@@ -1068,6 +1072,7 @@ public class BluetoothMeshProxyStateMachine {@Overridepublic void disconnect() {stopTimeout();+ scanOff();setState(getDisconnectedState());notifyStatusConsumer(ConnectionStatus.DISCONNECTED);stopQueueWorker();@@ -1125,6 +1130,7 @@ public class BluetoothMeshProxyStateMachine {stopTimeout();scanOff();setState(getRetryToConnectState());+ executorService.execute(() -> statusConsumer.accept(ConnectionStatus.CONNECTING));}}