using System;
using Microsoft.Extensions.Logging;
using NucuCar.Common.Utilities;
using NucuCar.Domain.Telemetry;
namespace NucuCar.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),
TelemetryPublisherType.Firestore => new TelemetryPublisherFirestore(opts),
_ => throw new ArgumentException($"Invalid TelemetryPublisher type: {type}.")
};
}
}
}