diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs b/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs index 8b16b99..78efcaf 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs +++ b/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs @@ -27,50 +27,6 @@ namespace NucuCar.Domain.Telemetry Logger?.LogDebug("Initialized the AzureTelemetryPublisher!"); } - /// - /// Creates an instance of that is used to publish data to Microsoft Azure. - /// - /// The device connection string for Microsoft Azure IoT hub device. - /// A instance. - public static TelemetryPublisher CreateFromConnectionString(string connectionString) - { - Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); - return new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions() - {ConnectionString = connectionString, TelemetrySource = "TelemetryPublisherAzure"}); - } - - /// - /// Creates an instance of that is used to publish data to Microsoft Azure. - /// - /// Device connection string for Microsoft Azure IoT hub device. - /// String that is used to identify the source of the telemetry data. - /// A instance. - public static TelemetryPublisher CreateFromConnectionString(string connectionString, - string telemetrySource) - { - Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); - Guard.ArgumentNotNullOrWhiteSpace(nameof(telemetrySource), telemetrySource); - return new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions() - {ConnectionString = connectionString, TelemetrySource = telemetrySource}); - } - - /// - /// Creates an instance of that is used to publish data to Microsoft Azure. - /// - /// Device connection string for Microsoft Azure IoT hub device. - /// String that is used to identify the source of the telemetry data. - /// An logger instance. - /// A instance. - public static TelemetryPublisher CreateFromConnectionString(string connectionString, - string telemetrySource, ILogger logger) - { - Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); - Guard.ArgumentNotNullOrWhiteSpace(nameof(telemetrySource), telemetrySource); - Guard.ArgumentNotNull(nameof(logger), logger); - return new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions() - {ConnectionString = connectionString, TelemetrySource = telemetrySource, Logger = logger}); - } - public override async Task PublishAsync(CancellationToken cancellationToken) { var data = GetTelemetry(); diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherFactory.cs b/NucuCar.Domain/Telemetry/TelemetryPublisherFactory.cs new file mode 100644 index 0000000..f6c28bb --- /dev/null +++ b/NucuCar.Domain/Telemetry/TelemetryPublisherFactory.cs @@ -0,0 +1,54 @@ +using System; +using Microsoft.Extensions.Logging; + +namespace NucuCar.Domain.Telemetry +{ + /// + /// The TelemetryPublisherFactory is used instantiate TelemetryPublishers. + /// + public static class TelemetryPublisherFactory + { + /// + /// Creates an instance of . See + /// + /// The type of the publisher. + /// Device connection string for Microsoft Azure IoT hub device. + /// String that is used to identify the source of the telemetry data. + /// An logger instance. + /// A instance. + public static TelemetryPublisher Create(string type, string connectionString, + string telemetrySource, ILogger logger) + { + Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); + Guard.ArgumentNotNullOrWhiteSpace(nameof(telemetrySource), telemetrySource); + Guard.ArgumentNotNull(nameof(logger), logger); + var opts = new TelemetryPublisherBuilderOptions() + {ConnectionString = connectionString, TelemetrySource = telemetrySource, Logger = logger}; + return SpawnPublisher(type, opts); + } + + /// + /// Creates an instance of . + /// + /// The type of the publisher. See + /// The device connection string for the selected publisher. + /// A instance. + public static TelemetryPublisher CreateFromConnectionString(string type, string connectionString) + { + Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); + var opts = new TelemetryPublisherBuilderOptions() + {ConnectionString = connectionString, TelemetrySource = "TelemetryPublisherAzure"}; + return SpawnPublisher(type, opts); + } + + private static TelemetryPublisher SpawnPublisher(string type, TelemetryPublisherBuilderOptions opts) + { + return type switch + { + TelemetryPublisherType.Azure => (TelemetryPublisher) new TelemetryPublisherAzure(opts), + TelemetryPublisherType.Disk => new TelemetryPublisherDisk(opts), + _ => throw new ArgumentException($"Invalid TelemetryPublisher type: {type}.") + }; + } + } +} \ No newline at end of file diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherType.cs b/NucuCar.Domain/Telemetry/TelemetryPublisherType.cs new file mode 100644 index 0000000..f4f691f --- /dev/null +++ b/NucuCar.Domain/Telemetry/TelemetryPublisherType.cs @@ -0,0 +1,12 @@ +namespace NucuCar.Domain.Telemetry +{ + /// + /// TelemetryPublisherType holds constants for instantiating , + /// see . + /// + public static class TelemetryPublisherType + { + public const string Azure = "Azure"; + public const string Disk = "Disk"; + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Telemetry/SensorTelemetry.cs b/NucuCar.Sensors/Telemetry/SensorTelemetry.cs index 48a39be..951e07c 100644 --- a/NucuCar.Sensors/Telemetry/SensorTelemetry.cs +++ b/NucuCar.Sensors/Telemetry/SensorTelemetry.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NucuCar.Domain.Telemetry; + // ReSharper disable ClassWithVirtualMembersNeverInherited.Global namespace NucuCar.Sensors.Telemetry @@ -11,14 +12,13 @@ namespace NucuCar.Sensors.Telemetry public SensorTelemetry() { - } - + public SensorTelemetry(ILogger logger, IOptions options) { if (options.Value.ServiceEnabled) { - Publisher = TelemetryPublisherAzure.CreateFromConnectionString(options.Value.ConnectionString, + Publisher = TelemetryPublisherFactory.Create(options.Value.Publisher, options.Value.ConnectionString, "NucuCar.Sensors", logger); } else diff --git a/NucuCar.Sensors/Telemetry/TelemetryConfig.cs b/NucuCar.Sensors/Telemetry/TelemetryConfig.cs index 338faf0..e1d0590 100644 --- a/NucuCar.Sensors/Telemetry/TelemetryConfig.cs +++ b/NucuCar.Sensors/Telemetry/TelemetryConfig.cs @@ -1,9 +1,17 @@ // ReSharper disable UnusedAutoPropertyAccessor.Global +using NucuCar.Domain.Telemetry; + namespace NucuCar.Sensors.Telemetry { public class TelemetryConfig { + /// + /// The Publisher is used by to instantiate + /// the correct . For available types see + /// + public string Publisher { get; set; } + public bool ServiceEnabled { get; set; } public int PublishInterval { get; set; } public string ConnectionString { get; set; } diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index 8aa155f..1ae3585 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -1,5 +1,6 @@ { "Telemetry": { + "Publisher": "Azure", "ServiceEnabled": true, "PublishInterval": 3000, "ConnectionString": "YOUR_CONNECTION_STRING" diff --git a/NucuCar.TestClient/Telemetry/AzureTelemetryPublishCmd.cs b/NucuCar.TestClient/Telemetry/AzureTelemetryPublishCmd.cs index 35a020c..f08584d 100644 --- a/NucuCar.TestClient/Telemetry/AzureTelemetryPublishCmd.cs +++ b/NucuCar.TestClient/Telemetry/AzureTelemetryPublishCmd.cs @@ -48,10 +48,9 @@ namespace NucuCar.TestClient.Telemetry var logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger(); - var telemetryPublisher = - TelemetryPublisherAzure.CreateFromConnectionString(opts.PublisherConnectionString, - "NucuCar.TestClient", logger); - + var telemetryPublisher = TelemetryPublisherFactory.Create(TelemetryPublisherType.Azure, + opts.PublisherConnectionString, "NucuCar.TestClient", logger); + var anonymousTelemeter = new DummyTelemeter( JsonConvert.DeserializeObject>(opts.PublisherJsonMessage)); diff --git a/NucuCar.UnitTests/NucuCar.Domain.Tests/Telemetry/TelemetryPublisherFactoryTest.cs b/NucuCar.UnitTests/NucuCar.Domain.Tests/Telemetry/TelemetryPublisherFactoryTest.cs new file mode 100644 index 0000000..e917086 --- /dev/null +++ b/NucuCar.UnitTests/NucuCar.Domain.Tests/Telemetry/TelemetryPublisherFactoryTest.cs @@ -0,0 +1,38 @@ +using System; +using NucuCar.Domain.Telemetry; +using Xunit; + +namespace NucuCar.UnitTests.NucuCar.Domain.Tests.Telemetry +{ + public class TelemetryPublisherFactoryTest + { + [Fact] + private void Test_Build_TelemetryPublisherAzure() + { + const string connectionString = + "HostName=something.azure-devices.net;DeviceId=something;SharedAccessKey=test"; + var telemetryPublisher = + TelemetryPublisherFactory.CreateFromConnectionString(TelemetryPublisherType.Azure, connectionString); + Assert.IsType(telemetryPublisher); + } + + [Fact] + private void Test_Build_TelemetryPublisherDisk() + { + const string connectionString = + "Filename=test;BufferSize=4096"; + var telemetryPublisher = + TelemetryPublisherFactory.CreateFromConnectionString(TelemetryPublisherType.Disk, connectionString); + Assert.IsType(telemetryPublisher); + } + + [Fact] + private void Test_Build_ThrowsOnInvalidType() + { + Assert.Throws(() => + { + TelemetryPublisherFactory.CreateFromConnectionString("_1", "a=b"); + }); + } + } +} \ No newline at end of file