diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.Telemetry/Destinations/AzureHubTelemetryDestination.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Telemetry/Destinations/AzureHubTelemetryDestination.cs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Telemetry/Destinations/AzureHubTelemetryDestination.cs b/Software/Visual_Studio/Tango.Telemetry/Destinations/AzureHubTelemetryDestination.cs new file mode 100644 index 000000000..713805471 --- /dev/null +++ b/Software/Visual_Studio/Tango.Telemetry/Destinations/AzureHubTelemetryDestination.cs @@ -0,0 +1,56 @@ +using Microsoft.Azure.Devices.Client; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; + +namespace Tango.Telemetry.Destinations +{ + public class AzureHubTelemetryDestination : ExtendedObject, ITelemetryDestination + { + private DeviceClient _hubClient; + + public bool Enable { get; set; } = true; + public String ConnectionString { get; private set; } + + public IReadOnlyList<TelemetrySource> SupportedSources { get; private set; } + + public AzureHubTelemetryDestination(String connectionString) + { + ConnectionString = connectionString; + SupportedSources = new List<TelemetrySource>() { TelemetrySource.PendingStorage, TelemetrySource.Streaming, TelemetrySource.ExternalStorage }; + } + + public async Task Publish(TelemetryPublishPackage package, List<KeyValuePair<String, String>> properties) + { + if (_hubClient == null) + { + _hubClient = DeviceClient.CreateFromConnectionString(ConnectionString, TransportType.Mqtt); + _hubClient.SetConnectionStatusChangesHandler((status, reason) => + { + LogManager.Log($"IoT hub status changed to: {status}, Reason: {reason}."); + }); + } + + var message = new Message(Encoding.UTF8.GetBytes(package.ToPayload())) + { + ContentType = "application/json", + ContentEncoding = "utf-8" + }; + + foreach (var prop in properties) + { + message.Properties.Add(prop.Key, prop.Value); + } + + await _hubClient.SendEventAsync(message); + } + + public void Dispose() + { + _hubClient?.Dispose(); + } + } +} |
