62 lines
2.4 KiB
C#
62 lines
2.4 KiB
C#
|
// 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.Domain.Telemetry;
|
||
|
|
||
|
namespace NucuCar.TestClient
|
||
|
{
|
||
|
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<string, object> _dummyTelemeterData;
|
||
|
|
||
|
public DummyTelemeter(Dictionary<string, object> dummyData)
|
||
|
{
|
||
|
_dummyTelemeterData = dummyData;
|
||
|
}
|
||
|
|
||
|
public string GetIdentifier()
|
||
|
{
|
||
|
return "DummyTelemeter";
|
||
|
}
|
||
|
|
||
|
public Dictionary<string, object> GetTelemetryData()
|
||
|
{
|
||
|
return _dummyTelemeterData;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static async Task RunAzurePublisherTelemetryTest(AzureTelemetryPublishOptions opts)
|
||
|
{
|
||
|
var telemetryPublisher = new TelemetryPublisherAzure {TelemetrySource = "TestClient"};
|
||
|
var anonymousTelemeter =
|
||
|
new DummyTelemeter(
|
||
|
JsonConvert.DeserializeObject<Dictionary<string, object>>(opts.PublisherJsonMessage));
|
||
|
var logger = LoggerFactory.Create(builder => { builder.AddConsole(); })
|
||
|
.CreateLogger<AzureTelemetryPublishCmd>();
|
||
|
|
||
|
logger.LogInformation($"Publishing message: {opts.PublisherJsonMessage}");
|
||
|
telemetryPublisher.ConnectionString = opts.PublisherConnectionString;
|
||
|
telemetryPublisher.RegisterTelemeter(anonymousTelemeter);
|
||
|
telemetryPublisher.SetLogger(logger);
|
||
|
telemetryPublisher.Start();
|
||
|
await telemetryPublisher.PublishAsync(CancellationToken.None);
|
||
|
}
|
||
|
}
|
||
|
}
|