From e1ae7ecdd0eafd07b7c6f22d4cf8b7c642737352 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Mon, 2 Aug 2021 21:28:02 +0300 Subject: [PATCH] Refactor NucuCar.Telemetry to use interfaces. --- .../Modules/BME680/Bme680Worker.cs | 5 ++-- .../Modules/CpuTemperature/CpuTempWorker.cs | 5 ++-- .../Modules/Heartbeat/HeartbeatWorker.cs | 5 ++-- .../Modules/PMS5003/Pms5003Worker.cs | 5 ++-- NucuCar.Sensors/Program.cs | 3 +- NucuCar.Sensors/SensorWorker.cs | 3 +- NucuCar.Telemetry/Abstractions/ITelemeter.cs | 1 + .../Abstractions/ITelemetryPublisher.cs | 30 +++++++++++++++++++ .../Abstractions/TelemetryPublisher.cs | 30 +++++++++---------- .../TelemetryPublisherFactory.cs | 6 ++-- ...elemetry.cs => TelemetryPublisherProxy.cs} | 26 ++++++++++++---- NucuCar.Telemetry/TelemetryWorker.cs | 6 ++-- .../NucuCar.Sensors.Tests/Bme680WorkerTest.cs | 4 +-- 13 files changed, 91 insertions(+), 38 deletions(-) create mode 100644 NucuCar.Telemetry/Abstractions/ITelemetryPublisher.cs rename NucuCar.Telemetry/{Telemetry.cs => TelemetryPublisherProxy.cs} (50%) diff --git a/NucuCar.Sensors/Modules/BME680/Bme680Worker.cs b/NucuCar.Sensors/Modules/BME680/Bme680Worker.cs index 712511c..3255a2d 100644 --- a/NucuCar.Sensors/Modules/BME680/Bme680Worker.cs +++ b/NucuCar.Sensors/Modules/BME680/Bme680Worker.cs @@ -1,17 +1,18 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NucuCar.Sensors.Abstractions; +using NucuCar.Telemetry.Abstractions; namespace NucuCar.Sensors.Modules.BME680 { public class Bme680Worker : SensorWorker { - public Bme680Worker(ILogger logger, Telemetry.Telemetry telemetry, ISensor sensor, + public Bme680Worker(ILogger logger, ITelemetryPublisher telemetryPublisherProxy, ISensor sensor, IOptions options) { Logger = logger; MeasurementInterval = options.Value.MeasurementInterval; - TelemetryPublisher = telemetry.Publisher; + TelemetryPublisher = telemetryPublisherProxy; Sensor = sensor.Object; } } diff --git a/NucuCar.Sensors/Modules/CpuTemperature/CpuTempWorker.cs b/NucuCar.Sensors/Modules/CpuTemperature/CpuTempWorker.cs index 4ccedc4..e32bbcc 100644 --- a/NucuCar.Sensors/Modules/CpuTemperature/CpuTempWorker.cs +++ b/NucuCar.Sensors/Modules/CpuTemperature/CpuTempWorker.cs @@ -1,17 +1,18 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NucuCar.Sensors.Abstractions; +using NucuCar.Telemetry.Abstractions; namespace NucuCar.Sensors.Modules.CpuTemperature { public class CpuTempWorker : SensorWorker { - public CpuTempWorker(ILogger logger, Telemetry.Telemetry telemetry, + public CpuTempWorker(ILogger logger, ITelemetryPublisher telemetryPublisherProxy, ISensor sensor, IOptions options) { Logger = logger; MeasurementInterval = options.Value.MeasurementInterval; - TelemetryPublisher = telemetry.Publisher; + TelemetryPublisher = telemetryPublisherProxy; Sensor = sensor.Object; } } diff --git a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatWorker.cs b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatWorker.cs index 2f4d5ae..585a1d5 100644 --- a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatWorker.cs +++ b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatWorker.cs @@ -1,17 +1,18 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NucuCar.Sensors.Abstractions; +using NucuCar.Telemetry.Abstractions; namespace NucuCar.Sensors.Modules.Heartbeat { public class HeartbeatWorker : SensorWorker { - public HeartbeatWorker(ILogger logger, Telemetry.Telemetry telemetry, + public HeartbeatWorker(ILogger logger, ITelemetryPublisher telemetryPublisherProxy, ISensor sensor, IOptions options) { Logger = logger; MeasurementInterval = options.Value.MeasurementInterval; - TelemetryPublisher = telemetry.Publisher; + TelemetryPublisher = telemetryPublisherProxy; Sensor = sensor.Object; } } diff --git a/NucuCar.Sensors/Modules/PMS5003/Pms5003Worker.cs b/NucuCar.Sensors/Modules/PMS5003/Pms5003Worker.cs index fa12550..e496515 100644 --- a/NucuCar.Sensors/Modules/PMS5003/Pms5003Worker.cs +++ b/NucuCar.Sensors/Modules/PMS5003/Pms5003Worker.cs @@ -1,17 +1,18 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NucuCar.Sensors.Abstractions; +using NucuCar.Telemetry.Abstractions; namespace NucuCar.Sensors.Modules.PMS5003 { public class Pms5003Worker : SensorWorker { - public Pms5003Worker(ILogger logger, Telemetry.Telemetry telemetry, + public Pms5003Worker(ILogger logger, ITelemetryPublisher telemetryPublisherProxy, ISensor sensor, IOptions options) { Logger = logger; MeasurementInterval = options.Value.MeasurementInterval; - TelemetryPublisher = telemetry.Publisher; + TelemetryPublisher = telemetryPublisherProxy; Sensor = sensor.Object; } } diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index 9dcc242..2ba0c21 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -6,6 +6,7 @@ using NucuCar.Sensors.Modules.CpuTemperature; using NucuCar.Sensors.Modules.Heartbeat; using NucuCar.Sensors.Modules.PMS5003; using NucuCar.Telemetry; +using NucuCar.Telemetry.Abstractions; namespace NucuCar.Sensors { @@ -27,7 +28,7 @@ namespace NucuCar.Sensors services.Configure(hostContext.Configuration.GetSection("Pms5003Sensor")); // Singletons - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton, Bme680Sensor>(); services.AddSingleton, CpuTempSensor>(); services.AddSingleton, HeartbeatSensor>(); diff --git a/NucuCar.Sensors/SensorWorker.cs b/NucuCar.Sensors/SensorWorker.cs index 34b951e..d08a046 100644 --- a/NucuCar.Sensors/SensorWorker.cs +++ b/NucuCar.Sensors/SensorWorker.cs @@ -17,7 +17,7 @@ namespace NucuCar.Sensors private int _intializationDelay = 10000; protected int MeasurementInterval; protected ILogger Logger; - protected TelemetryPublisher TelemetryPublisher; + protected ITelemetryPublisher TelemetryPublisher; protected GenericTelemeterSensor Sensor; @@ -25,6 +25,7 @@ namespace NucuCar.Sensors { if (Sensor == null) { + Logger?.LogDebug("{Message}","Sensor is null, abandoning execution."); return; } diff --git a/NucuCar.Telemetry/Abstractions/ITelemeter.cs b/NucuCar.Telemetry/Abstractions/ITelemeter.cs index a33917e..7f4749e 100644 --- a/NucuCar.Telemetry/Abstractions/ITelemeter.cs +++ b/NucuCar.Telemetry/Abstractions/ITelemeter.cs @@ -14,6 +14,7 @@ namespace NucuCar.Telemetry.Abstractions /// An identifier for the telemetry source. string GetIdentifier(); + // TODO: Perhaps here it's better if we return a string. /// /// This function should return a dictionary containing the telemetry data. /// When implementing this function you should return null if the telemetry is disabled. diff --git a/NucuCar.Telemetry/Abstractions/ITelemetryPublisher.cs b/NucuCar.Telemetry/Abstractions/ITelemetryPublisher.cs new file mode 100644 index 0000000..79c95bb --- /dev/null +++ b/NucuCar.Telemetry/Abstractions/ITelemetryPublisher.cs @@ -0,0 +1,30 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace NucuCar.Telemetry.Abstractions +{ + public interface ITelemetryPublisher + { + /// + /// Publishes telemetry data. + /// + /// A cancellation token. + /// A task + public abstract Task PublishAsync(CancellationToken cancellationToken); + + /// + /// 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); + + /// + /// 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); + } +} \ No newline at end of file diff --git a/NucuCar.Telemetry/Abstractions/TelemetryPublisher.cs b/NucuCar.Telemetry/Abstractions/TelemetryPublisher.cs index a662715..e281650 100644 --- a/NucuCar.Telemetry/Abstractions/TelemetryPublisher.cs +++ b/NucuCar.Telemetry/Abstractions/TelemetryPublisher.cs @@ -7,10 +7,9 @@ using Microsoft.Extensions.Logging; namespace NucuCar.Telemetry.Abstractions { /// - /// TODO: make an interface. /// The TelemetryPublisher is an abstract class, which provides a base for implementing telemetry publishers. /// - public abstract class TelemetryPublisher : IDisposable + public abstract class TelemetryPublisher : IDisposable, ITelemetryPublisher { /// /// Raw connection string that is used to connect to the cloud service. Should be parsed if required. @@ -40,6 +39,19 @@ namespace NucuCar.Telemetry.Abstractions { 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. @@ -80,19 +92,7 @@ namespace NucuCar.Telemetry.Abstractions RegisteredTelemeters.Remove(t); return true; } - - /// - /// Constructor for . - /// - /// TelemetryPublisher options, see: - protected TelemetryPublisher(TelemetryPublisherOptions opts) - { - ConnectionString = opts.ConnectionString; - TelemetrySource = opts.TelemetrySource; - Logger = opts.Logger; - RegisteredTelemeters = new List(10); - } - + /// /// Iterates through the registered telemeters and returns the telemetry data as dictionary. /// It also adds metadata information such as: source and timestamp. diff --git a/NucuCar.Telemetry/TelemetryPublisherFactory.cs b/NucuCar.Telemetry/TelemetryPublisherFactory.cs index cd09594..85fab04 100644 --- a/NucuCar.Telemetry/TelemetryPublisherFactory.cs +++ b/NucuCar.Telemetry/TelemetryPublisherFactory.cs @@ -19,7 +19,7 @@ namespace NucuCar.Telemetry /// 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, + public static ITelemetryPublisher Create(string type, string connectionString, string telemetrySource, ILogger logger) { Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); @@ -36,7 +36,7 @@ namespace NucuCar.Telemetry /// The type of the publisher. See /// The device connection string for the selected publisher. /// A instance. - public static TelemetryPublisher CreateFromConnectionString(string type, string connectionString) + public static ITelemetryPublisher CreateFromConnectionString(string type, string connectionString) { Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); var opts = new TelemetryPublisherOptions() @@ -44,7 +44,7 @@ namespace NucuCar.Telemetry return SpawnPublisher(type, opts); } - private static TelemetryPublisher SpawnPublisher(string type, TelemetryPublisherOptions opts) + private static ITelemetryPublisher SpawnPublisher(string type, TelemetryPublisherOptions opts) { return type switch { diff --git a/NucuCar.Telemetry/Telemetry.cs b/NucuCar.Telemetry/TelemetryPublisherProxy.cs similarity index 50% rename from NucuCar.Telemetry/Telemetry.cs rename to NucuCar.Telemetry/TelemetryPublisherProxy.cs index 567768e..61b953e 100644 --- a/NucuCar.Telemetry/Telemetry.cs +++ b/NucuCar.Telemetry/TelemetryPublisherProxy.cs @@ -1,24 +1,25 @@ +using System.Threading; +using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NucuCar.Telemetry.Abstractions; // ReSharper disable ClassWithVirtualMembersNeverInherited.Global - namespace NucuCar.Telemetry { - public class Telemetry + public class TelemetryPublisherProxy : ITelemetryPublisher { - public TelemetryPublisher Publisher { get; set; } + private ITelemetryPublisher Publisher { get; set; } /// /// Class used together with the DI, holds a Publisher instance that's being create by options from /// TelemetryConfig. /// - public Telemetry() + public TelemetryPublisherProxy() { } - public Telemetry(ILogger logger, IOptions options) + public TelemetryPublisherProxy(ILogger logger, IOptions options) { if (options.Value.ServiceEnabled) { @@ -30,5 +31,20 @@ namespace NucuCar.Telemetry Publisher = null; } } + + public Task PublishAsync(CancellationToken cancellationToken) + { + return Publisher.PublishAsync(cancellationToken); + } + + public bool RegisterTelemeter(ITelemeter t) + { + return Publisher.RegisterTelemeter(t); + } + + public bool UnRegisterTelemeter(ITelemeter t) + { + return Publisher.UnRegisterTelemeter(t); + } } } \ No newline at end of file diff --git a/NucuCar.Telemetry/TelemetryWorker.cs b/NucuCar.Telemetry/TelemetryWorker.cs index 01bfebc..9201fe1 100644 --- a/NucuCar.Telemetry/TelemetryWorker.cs +++ b/NucuCar.Telemetry/TelemetryWorker.cs @@ -16,15 +16,15 @@ namespace NucuCar.Telemetry private readonly int _interval; private readonly bool _serviceEnabled; private readonly ILogger _logger; - private readonly TelemetryPublisher _telemetryPublisher; + private readonly ITelemetryPublisher _telemetryPublisher; public TelemetryWorker(ILogger logger, IOptions options, - Telemetry telemetry) + ITelemetryPublisher telemetryPublisherProxy) { _logger = logger; _interval = options.Value.PublishInterval; _serviceEnabled = options.Value.ServiceEnabled; - _telemetryPublisher = telemetry.Publisher; + _telemetryPublisher = telemetryPublisherProxy; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs index 37c0f19..605a4a2 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs @@ -18,7 +18,7 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests { private readonly Mock> _mockLogger; private readonly Mock> _mockOptions; - private readonly Mock _mockSensorTelemetry; + private readonly Mock _mockSensorTelemetry; private readonly Mock _mockTestBme680Sensor; private readonly Mock> _mockBme680ISensor; private readonly CancellationTokenSource _cts; @@ -28,7 +28,7 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests _cts = new CancellationTokenSource(); _mockLogger = new Mock>(); _mockOptions = new Mock>(); - _mockSensorTelemetry = new Mock(); + _mockSensorTelemetry = new Mock(); _mockTestBme680Sensor = new Mock(); _mockBme680ISensor = new Mock>();