From f90d226797eaeb28a9cf8fce18980ddb66a81fb6 Mon Sep 17 00:00:00 2001 From: Osman Hadzic Date: Wed, 13 Dec 2023 12:14:12 +0100 Subject: [PATCH] [AstarteDeviceSDKCSharp] Fix: automatic reconnect when device lost connection to Astarte Signed-off-by: Osman Hadzic --- .../AstartePairingHandler.cs | 10 +++++----- .../Crypto/AstarteCryptoStore.cs | 6 ++---- AstarteDeviceSDKCSharp/Device/AstarteDevice.cs | 14 ++++++++++---- .../Transport/MQTT/AstarteMqttV1Transport.cs | 18 ++++++++++++++---- CHANGELOG.md | 3 +++ 5 files changed, 34 insertions(+), 17 deletions(-) diff --git a/AstarteDeviceSDKCSharp/AstartePairingHandler.cs b/AstarteDeviceSDKCSharp/AstartePairingHandler.cs index d857693..3e973c1 100644 --- a/AstarteDeviceSDKCSharp/AstartePairingHandler.cs +++ b/AstarteDeviceSDKCSharp/AstartePairingHandler.cs @@ -52,15 +52,15 @@ public AstartePairingHandler(string pairingUrl, string astarteRealm, string devi } - public void Init() + public async Task Init() { - ReloadTransports(); + await ReloadTransports(); } - private void ReloadTransports() + private async Task ReloadTransports() { - _transports = _AstartePairingService.ReloadTransports(_credentialSecret, - _cryptoStore, _deviceId).Result; + _transports = await _AstartePairingService.ReloadTransports(_credentialSecret, + _cryptoStore, _deviceId); } public List GetTransports() diff --git a/AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs b/AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs index 61b7405..51af932 100644 --- a/AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs +++ b/AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs @@ -175,10 +175,8 @@ public string GenerateCSR(string commonName) public MqttClientOptionsBuilderTlsParameters GetMqttClientOptionsBuilderTlsParameters() { - if (_parametersFactory == null) - { - _parametersFactory = new AstarteMutualTLSParametersFactory(this); - } + _parametersFactory = new AstarteMutualTLSParametersFactory(this); + return _parametersFactory.Get(); } diff --git a/AstarteDeviceSDKCSharp/Device/AstarteDevice.cs b/AstarteDeviceSDKCSharp/Device/AstarteDevice.cs index 4d4ae15..524fa94 100644 --- a/AstarteDeviceSDKCSharp/Device/AstarteDevice.cs +++ b/AstarteDeviceSDKCSharp/Device/AstarteDevice.cs @@ -37,7 +37,7 @@ public class AstarteDevice : IAstarteTransportEventListener private IAstarteMessageListener? _astarteMessagelistener; private IAstartePropertyStorage astartePropertyStorage; private AstarteFailedMessageStorage _astarteFailedMessageStorage; - private bool _initialized; + private bool _initialized = false; private const string _cryptoSubDir = "crypto"; private bool _alwaysReconnect = false; private bool _explicitDisconnectionRequest; @@ -117,9 +117,9 @@ public AstarteDevice( _astarteFailedMessageStorage = new(fullCryptoDirPath); } - private void Init() + private async Task Init() { - _pairingHandler.Init(); + await _pairingHandler.Init(); // Get and configure the first available transport SetFirstTransportFromPairingHandler(); @@ -221,10 +221,16 @@ public void SetAlwaysReconnect(bool alwaysReconnect) public async Task Connect() { + if (!_pairingHandler.IsCertificateAvailable()) + { + await _pairingHandler.RequestNewCertificate(); + _initialized = false; + } + if (!_initialized) { - Init(); + await Init(); _initialized = true; } diff --git a/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs b/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs index 4a3b6e9..05f5d83 100644 --- a/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs +++ b/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs @@ -79,13 +79,23 @@ private async Task DoSendMqttMessage(string topic, byte[] payload, int qos) .WithRetainFlag(false) .Build(); - MqttClientPublishResult result = await _client.PublishAsync(applicationMessage); + try + { + + MqttClientPublishResult result = await _client.PublishAsync(applicationMessage); + + if (result.ReasonCode != MqttClientPublishReasonCode.Success) + { + throw new AstarteTransportException + ($"Error publishing on MQTT. Code: {result.ReasonCode}"); + } - if (result.ReasonCode != MqttClientPublishReasonCode.Success) + } + catch (Exception) { - throw new AstarteTransportException - ($"Error publishing on MQTT. Code: {result.ReasonCode}"); + _astarteTransportEventListener?.OnTransportDisconnected(); } + } public override async Task SendIntrospection() diff --git a/CHANGELOG.md b/CHANGELOG.md index 917fb88..7b7ccaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add option to ignore SSL errors in the AstarteDevice constructor. - Add a method to remove interface from device. +### Fixed +- Fix Connect method to handle certificate expiration. + ## [0.5.4] - 2023-06-23 ### Added - Expose an interface path from Astarte aggregate datastream event.