using System;
using Microsoft.Extensions.Logging;
using NucuCar.Core.Utilities;
using NucuCar.Telemetry.Abstractions;
using NucuCar.Telemetry.Publishers;
using Console = NucuCar.Telemetry.Publishers.Console;
namespace NucuCar.Telemetry
{
///
/// The PublisherFactory is used instantiate TelemetryPublishers.
///
public static class PublisherFactory
{
///
/// Creates an instance of . See
///
/// The type of the publisher.
/// Device connection string for the telemetry publisher.
/// String that is used to identify the source of the telemetry data.
/// An logger instance.
/// A instance.
public static ITelemetryPublisher 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 PublisherOptions
{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 ITelemetryPublisher CreateFromConnectionString(string type, string connectionString)
{
Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString);
var opts = new PublisherOptions()
{ConnectionString = connectionString, TelemetrySource = "NucuCar.Sensors"};
return SpawnPublisher(type, opts);
}
private static ITelemetryPublisher SpawnPublisher(string type, PublisherOptions opts)
{
return type switch
{
PublisherType.Azure => new Azure(opts),
PublisherType.Disk => new Disk(opts),
PublisherType.Console => new Console(opts),
_ => throw new ArgumentException($"Invalid TelemetryPublisher type: {type}.")
};
}
}
}