// ReSharper disable UnusedAutoPropertyAccessor.Global using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using CommandLine; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using NucuCar.Telemetry; using NucuCar.Telemetry.Abstractions; namespace NucuCar.TestClient.Telemetry { public class AzureTelemetryPublishCmd { [Verb("azure-telemetry-publish", HelpText = "Test the publishing telemetry using Microsoft Azure IoT Hub.")] public class AzureTelemetryPublishOptions { [Option('c', "connectionString", Required = true, HelpText = "The publisher's connection string. Get it from the Device.")] public string PublisherConnectionString { get; set; } [Option('m', "message", Required = true, HelpText = "The message to publish")] public string PublisherJsonMessage { get; set; } } private class DummyTelemeter : ITelemeter { private readonly Dictionary _dummyTelemeterData; public DummyTelemeter(Dictionary dummyData) { _dummyTelemeterData = dummyData; } public string GetIdentifier() { return "DummyTelemeter"; } public Dictionary GetTelemetryData() { return _dummyTelemeterData; } public bool IsTelemetryEnabled() { return true; } } public static async Task RunAzurePublisherTelemetryTest(AzureTelemetryPublishOptions opts) { var logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger(); var telemetryPublisher = TelemetryPublisherFactory.Create(TelemetryPublisherType.Azure, opts.PublisherConnectionString, "NucuCar.TestClient", logger); var anonymousTelemeter = new DummyTelemeter( JsonConvert.DeserializeObject>(opts.PublisherJsonMessage)); logger.LogInformation($"Publishing message: {opts.PublisherJsonMessage}"); telemetryPublisher.RegisterTelemeter(anonymousTelemeter); await telemetryPublisher.PublishAsync(CancellationToken.None); } } }