2019-12-28 15:09:17 +00:00
|
|
|
using System;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-08-01 13:03:06 +00:00
|
|
|
using NucuCar.Domain.Utilities;
|
2020-08-01 15:07:13 +00:00
|
|
|
using NucuCar.Telemetry.Abstractions;
|
|
|
|
using NucuCar.Telemetry.Publishers;
|
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>
|
|
|
|
/// The TelemetryPublisherFactory is used instantiate TelemetryPublishers.
|
|
|
|
/// </summary>
|
|
|
|
public static class TelemetryPublisherFactory
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Creates an instance of <see cref="TelemetryPublisher"/>. See <see cref="TelemetryPublisherType"/>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The type of the publisher. <see cref="TelemetryPublisherType"/> </param>
|
|
|
|
/// <param name="connectionString">Device connection string for Microsoft Azure IoT hub device.</param>
|
|
|
|
/// <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>
|
|
|
|
/// <returns>A <see cref="TelemetryPublisher"/> instance.</returns>
|
|
|
|
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);
|
2020-08-01 15:07:13 +00:00
|
|
|
var opts = new TelemetryPublisherOptions()
|
2019-12-28 15:09:17 +00:00
|
|
|
{ConnectionString = connectionString, TelemetrySource = telemetrySource, Logger = logger};
|
|
|
|
return SpawnPublisher(type, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates an instance of <see cref="TelemetryPublisher"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The type of the publisher. See <see cref="TelemetryPublisherType"/> </param>
|
|
|
|
/// <param name="connectionString">The device connection string for the selected publisher.</param>
|
|
|
|
/// <returns>A <see cref="TelemetryPublisher"/> instance.</returns>
|
|
|
|
public static TelemetryPublisher CreateFromConnectionString(string type, string connectionString)
|
|
|
|
{
|
|
|
|
Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString);
|
2020-08-01 15:07:13 +00:00
|
|
|
var opts = new TelemetryPublisherOptions()
|
2019-12-28 15:09:17 +00:00
|
|
|
{ConnectionString = connectionString, TelemetrySource = "TelemetryPublisherAzure"};
|
|
|
|
return SpawnPublisher(type, opts);
|
|
|
|
}
|
|
|
|
|
2020-08-01 15:07:13 +00:00
|
|
|
private static TelemetryPublisher SpawnPublisher(string type, TelemetryPublisherOptions opts)
|
2019-12-28 15:09:17 +00:00
|
|
|
{
|
|
|
|
return type switch
|
|
|
|
{
|
2020-08-01 14:49:20 +00:00
|
|
|
TelemetryPublisherType.Azure => new TelemetryPublisherAzure(opts),
|
2019-12-28 15:09:17 +00:00
|
|
|
TelemetryPublisherType.Disk => new TelemetryPublisherDisk(opts),
|
2020-02-08 17:47:44 +00:00
|
|
|
TelemetryPublisherType.Firestore => new TelemetryPublisherFirestore(opts),
|
2020-08-01 14:49:20 +00:00
|
|
|
TelemetryPublisherType.Console => new TelemetryPublisherConsole(opts),
|
2019-12-28 15:09:17 +00:00
|
|
|
_ => throw new ArgumentException($"Invalid TelemetryPublisher type: {type}.")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|