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..9ac176c 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 (MqttCommunicationException) { - throw new AstarteTransportException - ($"Error publishing on MQTT. Code: {result.ReasonCode}"); + _astarteTransportEventListener?.OnTransportDisconnected(); } + } public override async Task SendIntrospection()