using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; namespace NucuCar.Telemetry.Abstractions { /// /// The TelemetryPublisher is an abstract class, which provides a base for implementing telemetry publishers. /// public abstract class TelemetryPublisher : IDisposable, ITelemetryPublisher { /// /// Raw connection string that is used to connect to the cloud service. Should be parsed if required. /// protected string ConnectionString { get; set; } /// /// Telemetry source that indicates the source of the telemetry data. /// protected string TelemetrySource { get; set; } /// /// A list containing entries to the telemeters that want to publish data to the cloud. /// protected readonly List RegisteredTelemeters; /// /// The logging instance, if it's null then the module won't log anything. /// // ReSharper disable once UnassignedField.Global protected readonly ILogger Logger; /// /// Parameter less constructor, mainly used for testing. /// public TelemetryPublisher() { RegisteredTelemeters = new List(10); } /// /// Constructor for . /// /// TelemetryPublisher options, see: protected TelemetryPublisher(TelemetryPublisherOptions opts) { ConnectionString = opts.ConnectionString; TelemetrySource = opts.TelemetrySource; Logger = opts.Logger; RegisteredTelemeters = new List(10); } /// /// Method that sends all data from the () to the cloud. /// /// A cancellation token. /// A task public abstract Task PublishAsync(CancellationToken cancellationToken); /// /// Method that releases all managed resources. /// public abstract void Dispose(); /// /// Method that adds a telemeter to the collection. /// The telemeter can register only once. /// /// The /// Returns true if the telemeter has registered successfully and false otherwise. public bool RegisterTelemeter(ITelemeter t) { if (RegisteredTelemeters.Contains(t) || !t.IsTelemetryEnabled()) return false; Logger?.LogDebug("Registering telemeter {Identifier}", t.GetIdentifier()); RegisteredTelemeters.Add(t); return true; } /// /// Method that deletes a telemeter from the collection. /// /// The /// Returns true if the telemeter has unregistered successfully and false otherwise. public bool UnRegisterTelemeter(ITelemeter t) { if (!RegisteredTelemeters.Contains(t)) return false; Logger?.LogDebug("UnRegistering telemeter {Identifier}", t.GetIdentifier()); RegisteredTelemeters.Remove(t); return true; } /// /// Iterates through the registered telemeters and returns the telemetry data as dictionary. /// It also adds metadata information such as: source and timestamp. /// /// A dictionary containing all telemetry data. protected virtual Dictionary GetTelemetry() { var data = new List>(); foreach (var telemeter in RegisteredTelemeters) { var telemetryData = telemeter.GetTelemetryData(); if (telemetryData == null) { Logger?.LogWarning("Warning! Data for {Identifier} is null!", telemeter.GetIdentifier()); continue; } telemetryData["_id"] = telemeter.GetIdentifier(); data.Add(telemetryData); } var metadata = new Dictionary { ["source"] = TelemetrySource ?? nameof(TelemetryPublisher), ["timestamp"] = DateTime.UtcNow, ["data"] = data.ToArray() }; return metadata; } } }