using System; using Microsoft.Extensions.Logging; using NucuCar.Domain.Utilities; using NucuCar.Telemetry.Abstractions; using NucuCar.Telemetry.Publishers; 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 the telemetry publisher. /// 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 TelemetryPublisherOptions {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 TelemetryPublisherOptions() {ConnectionString = connectionString, TelemetrySource = "NucuCar.Sensors"}; return SpawnPublisher(type, opts); } private static TelemetryPublisher SpawnPublisher(string type, TelemetryPublisherOptions opts) { return type switch { TelemetryPublisherType.Azure => new TelemetryPublisherAzure(opts), TelemetryPublisherType.Disk => new TelemetryPublisherDisk(opts), TelemetryPublisherType.Firestore => new TelemetryPublisherFirestore(opts), TelemetryPublisherType.Console => new TelemetryPublisherConsole(opts), _ => throw new ArgumentException($"Invalid TelemetryPublisher type: {type}.") }; } } }