aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Telemetry/Destinations/TelemetryAzureHubDestination.cs
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2025-08-18 21:23:02 +0300
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2025-08-18 21:23:02 +0300
commitbda71b704d17773316b4b08e7dae7e5e536d0d0c (patch)
treed46f18a2bcfc5d5d089368c3e0c40cc714c4e29f /Software/Visual_Studio/Tango.Telemetry/Destinations/TelemetryAzureHubDestination.cs
parent94fb36e2eb00dfb575a5f5cc18bd377224b126ce (diff)
downloadTango-bda71b704d17773316b4b08e7dae7e5e536d0d0c.tar.gz
Tango-bda71b704d17773316b4b08e7dae7e5e536d0d0c.zip
Improved Telemetry IoT Destination.
Diffstat (limited to 'Software/Visual_Studio/Tango.Telemetry/Destinations/TelemetryAzureHubDestination.cs')
-rw-r--r--Software/Visual_Studio/Tango.Telemetry/Destinations/TelemetryAzureHubDestination.cs47
1 files changed, 33 insertions, 14 deletions
diff --git a/Software/Visual_Studio/Tango.Telemetry/Destinations/TelemetryAzureHubDestination.cs b/Software/Visual_Studio/Tango.Telemetry/Destinations/TelemetryAzureHubDestination.cs
index 94e949ba3..61a4bd880 100644
--- a/Software/Visual_Studio/Tango.Telemetry/Destinations/TelemetryAzureHubDestination.cs
+++ b/Software/Visual_Studio/Tango.Telemetry/Destinations/TelemetryAzureHubDestination.cs
@@ -7,6 +7,7 @@ using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Logging;
+using Tango.Telemetry.Helpers;
namespace Tango.Telemetry.Destinations
{
@@ -96,13 +97,21 @@ namespace Tango.Telemetry.Destinations
/// <returns>True if the destination is available; otherwise, false.</returns>
public Task<bool> IsAvailable()
{
- if (_hubClient == null)
+ //if (NetworkListManager.GetNetworks(NetworkConnectivityLevels.Connected).Any(x => x.IsConnectedToInternet))
+ if (InternetConnectivity.IsInternetAvailable())
{
- return Task.FromResult(true);
+ if (_hubClient == null)
+ {
+ return Task.FromResult(true);
+ }
+ else
+ {
+ return Task.FromResult(HubConnectionStatus == ConnectionStatus.Connected);
+ }
}
else
{
- return Task.FromResult(HubConnectionStatus == ConnectionStatus.Connected);
+ return Task.FromResult(false);
}
}
@@ -117,6 +126,7 @@ namespace Tango.Telemetry.Destinations
if (_hubClient == null)
{
_hubClient = DeviceClient.CreateFromConnectionString(ConnectionString, TransportType.Mqtt);
+ _hubClient.OperationTimeoutInMilliseconds = 2000;
_hubClient.SetConnectionStatusChangesHandler((status, reason) =>
{
HubConnectionStatus = status;
@@ -147,25 +157,34 @@ namespace Tango.Telemetry.Destinations
message.Properties.Add(prop.Key, prop.Value);
}
- if (BatchSize > 1)
+ try
{
- _batch.Add(message);
-
- if (_batch.Count >= BatchSize)
+ if (BatchSize > 1)
{
- LogManager.Log($"Sending telemetry batch of {_batch.Count} messages to Azure IoT Hub.", LogCategory.Debug);
- await _hubClient.SendEventBatchAsync(_batch.ToList());
- _batch.Clear();
+ _batch.Add(message);
+
+ if (_batch.Count >= BatchSize)
+ {
+ LogManager.Log($"Sending telemetry batch of {_batch.Count} messages to Azure IoT Hub.", LogCategory.Debug);
+ await _hubClient.SendEventBatchAsync(_batch.ToList());
+ _batch.Clear();
+ }
+ else
+ {
+ LogManager.Log($"Queued telemetry message for batching. {_batch.Count}/{BatchSize} currently queued.", LogCategory.Debug);
+ }
}
else
{
- LogManager.Log($"Queued telemetry message for batching. {_batch.Count}/{BatchSize} currently queued.", LogCategory.Debug);
+ LogManager.Log("Sending single telemetry message to Azure IoT Hub.", LogCategory.Debug);
+ await _hubClient.SendEventAsync(message);
}
}
- else
+ catch (Exception)
{
- LogManager.Log("Sending single telemetry message to Azure IoT Hub.", LogCategory.Debug);
- await _hubClient.SendEventAsync(message);
+ _hubClient?.Dispose();
+ _hubClient = null;
+ throw;
}
}