Skip to content

Commit

Permalink
[AstarteDeviceSDKCSharp] Fix: automatic reconnect when device lost co…
Browse files Browse the repository at this point in the history
…nnection to Astarte

Signed-off-by: Osman Hadzic <osman.hadzic@secomind.com>
  • Loading branch information
osmanhadzic committed Dec 13, 2023
1 parent 3af1296 commit 1eb6d95
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
10 changes: 5 additions & 5 deletions AstarteDeviceSDKCSharp/AstartePairingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AstarteTransport> GetTransports()
Expand Down
6 changes: 2 additions & 4 deletions AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
14 changes: 10 additions & 4 deletions AstarteDeviceSDKCSharp/Device/AstarteDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 14 additions & 4 deletions AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1eb6d95

Please sign in to comment.