2019-12-28 15:09:17 +00:00
|
|
|
using System;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-08-02 18:57:52 +00:00
|
|
|
using NucuCar.Core.Utilities;
|
2020-08-01 15:07:13 +00:00
|
|
|
using NucuCar.Telemetry.Abstractions;
|
|
|
|
using NucuCar.Telemetry.Publishers;
|
2021-10-03 20:37:53 +00:00
|
|
|
using Console = NucuCar.Telemetry.Publishers.Console;
|
2019-12-28 15:09:17 +00:00
|
|
|
|
2020-04-17 15:11:07 +00:00
|
|
|
namespace NucuCar.Telemetry
|
2019-12-28 15:09:17 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// The PublisherFactory is used instantiate TelemetryPublishers.
|
2019-12-28 15:09:17 +00:00
|
|
|
/// </summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
public static class PublisherFactory
|
2019-12-28 15:09:17 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// Creates an instance of <see cref="BasePublisher"/>. See <see cref="PublisherType"/>
|
2019-12-28 15:09:17 +00:00
|
|
|
/// </summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// <param name="type">The type of the publisher. <see cref="PublisherType"/> </param>
|
2020-11-25 18:49:15 +00:00
|
|
|
/// <param name="connectionString">Device connection string for the telemetry publisher.</param>
|
2019-12-28 15:09:17 +00:00
|
|
|
/// <param name="telemetrySource">String that is used to identify the source of the telemetry data.</param>
|
|
|
|
/// <param name="logger">An <see cref="ILogger"/> logger instance. </param>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// <returns>A <see cref="BasePublisher"/> instance.</returns>
|
2021-08-02 18:28:02 +00:00
|
|
|
public static ITelemetryPublisher Create(string type, string connectionString,
|
2019-12-28 15:09:17 +00:00
|
|
|
string telemetrySource, ILogger logger)
|
|
|
|
{
|
|
|
|
Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString);
|
|
|
|
Guard.ArgumentNotNullOrWhiteSpace(nameof(telemetrySource), telemetrySource);
|
|
|
|
Guard.ArgumentNotNull(nameof(logger), logger);
|
2021-10-03 20:37:53 +00:00
|
|
|
var opts = new PublisherOptions
|
2019-12-28 15:09:17 +00:00
|
|
|
{ConnectionString = connectionString, TelemetrySource = telemetrySource, Logger = logger};
|
|
|
|
return SpawnPublisher(type, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// Creates an instance of <see cref="BasePublisher"/>.
|
2019-12-28 15:09:17 +00:00
|
|
|
/// </summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// <param name="type">The type of the publisher. See <see cref="PublisherType"/> </param>
|
2019-12-28 15:09:17 +00:00
|
|
|
/// <param name="connectionString">The device connection string for the selected publisher.</param>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// <returns>A <see cref="BasePublisher"/> instance.</returns>
|
2021-08-02 18:28:02 +00:00
|
|
|
public static ITelemetryPublisher CreateFromConnectionString(string type, string connectionString)
|
2019-12-28 15:09:17 +00:00
|
|
|
{
|
|
|
|
Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString);
|
2021-10-03 20:37:53 +00:00
|
|
|
var opts = new PublisherOptions()
|
2020-11-25 18:49:15 +00:00
|
|
|
{ConnectionString = connectionString, TelemetrySource = "NucuCar.Sensors"};
|
2019-12-28 15:09:17 +00:00
|
|
|
return SpawnPublisher(type, opts);
|
|
|
|
}
|
|
|
|
|
2021-10-03 20:37:53 +00:00
|
|
|
private static ITelemetryPublisher SpawnPublisher(string type, PublisherOptions opts)
|
2019-12-28 15:09:17 +00:00
|
|
|
{
|
|
|
|
return type switch
|
|
|
|
{
|
2021-10-03 20:37:53 +00:00
|
|
|
PublisherType.Azure => new Azure(opts),
|
|
|
|
PublisherType.Disk => new Disk(opts),
|
|
|
|
PublisherType.Console => new Console(opts),
|
2019-12-28 15:09:17 +00:00
|
|
|
_ => throw new ArgumentException($"Invalid TelemetryPublisher type: {type}.")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|