diff --git a/NucuCar.Sensors/EnvironmentSensor/GrpcService.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs similarity index 100% rename from NucuCar.Sensors/EnvironmentSensor/GrpcService.cs rename to NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs diff --git a/NucuCar.Sensors/EnvironmentSensor/BackgroundWorker.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs similarity index 81% rename from NucuCar.Sensors/EnvironmentSensor/BackgroundWorker.cs rename to NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs index 202f718..c2d3aba 100644 --- a/NucuCar.Sensors/EnvironmentSensor/BackgroundWorker.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using NucuCar.Domain.Telemetry; using NucuCar.Sensors.Telemetry; using NucuCarSensorsProto; @@ -18,14 +19,17 @@ namespace NucuCar.Sensors.EnvironmentSensor private readonly bool _telemetryEnabled; private readonly int _measurementDelay; private readonly ILogger _logger; + private readonly TelemetryPublisher _telemetryPublisher; - public BackgroundWorker(ILogger logger, IConfiguration config) + public BackgroundWorker(ILogger logger, IConfiguration config, + SensorTelemetry sensorTelemetry) { _logger = logger; _serviceEnabled = config.GetValue("EnvironmentSensor:Enabled"); _telemetryEnabled = config.GetValue("EnvironmentSensor:Telemetry"); _measurementDelay = config.GetValue("EnvironmentSensor:MeasurementInterval"); + _telemetryPublisher = sensorTelemetry.Publisher; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) @@ -40,26 +44,27 @@ namespace NucuCar.Sensors.EnvironmentSensor sensor.InitializeSensor(); if (_telemetryEnabled) { - SensorTelemetryPublisher.Instance.RegisterTelemeter(sensor); + _telemetryPublisher?.RegisterTelemeter(sensor); } while (!stoppingToken.IsCancellationRequested) { + /* If sensor is ok attempt to read. */ if (sensor.GetState() == SensorStateEnum.Initialized) { await sensor.TakeMeasurement(); } + /* Else attempt to re-initialize. */ else { await Task.Delay(10000, stoppingToken); - /* Attempt to reinitialize the sensor. */ sensor.InitializeSensor(); } await Task.Delay(_measurementDelay, stoppingToken); } - SensorTelemetryPublisher.Instance.UnRegisterTelemeter(sensor); + _telemetryPublisher?.UnRegisterTelemeter(sensor); } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index 864fe78..9a269a3 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -15,7 +16,17 @@ namespace NucuCar.Sensors Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { - services.AddHostedService(); + var config = hostContext.Configuration; + + // Singletons + services.AddSingleton(); + + // Workers + if (config.GetValue("Telemetry:Enabled")) + { + services.AddHostedService(); + } + services.AddHostedService(); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); diff --git a/NucuCar.Sensors/Telemetry/BackgroundWorker.cs b/NucuCar.Sensors/Telemetry/BackgroundWorker.cs deleted file mode 100644 index a5cd7d7..0000000 --- a/NucuCar.Sensors/Telemetry/BackgroundWorker.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; - -namespace NucuCar.Sensors.Telemetry -{ - /// - /// Telemetry service, which pools the telemetry sources and pushes telemetry data to the cloud. - /// - public class BackgroundWorker : BackgroundService - { - private readonly bool _serviceEnabled; - private readonly int _interval; - private readonly ILogger _logger; - - public BackgroundWorker(ILogger logger, IConfiguration configuration) - { - _logger = logger; - _serviceEnabled = configuration.GetValue("Telemetry:Enabled"); - _interval = configuration.GetValue("Telemetry:Interval"); - var connectionString = configuration.GetValue("Telemetry:ConnectionString"); - if (_serviceEnabled) - { - SensorTelemetryPublisher.CreateSingleton(connectionString, "NucuCar.Sensors", logger); - } - } - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - if (!_serviceEnabled) - { - return; - } - - await Task.Delay(_interval, stoppingToken); - - var telemetryService = SensorTelemetryPublisher.Instance; - - while (!stoppingToken.IsCancellationRequested) - { - _logger.LogInformation("Publishing telemetry data!"); - await telemetryService.PublishAsync(stoppingToken); - await Task.Delay(_interval, stoppingToken); - } - } - - public override void Dispose() - { - base.Dispose(); - SensorTelemetryPublisher.Instance?.Dispose(); - } - } -} \ No newline at end of file diff --git a/NucuCar.Sensors/Telemetry/SensorTelemetry.cs b/NucuCar.Sensors/Telemetry/SensorTelemetry.cs new file mode 100644 index 0000000..decff8e --- /dev/null +++ b/NucuCar.Sensors/Telemetry/SensorTelemetry.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using NucuCar.Domain.Telemetry; + +namespace NucuCar.Sensors.Telemetry +{ + public class SensorTelemetry + { + public TelemetryPublisher Publisher { get; } + + public SensorTelemetry(ILogger logger, IConfiguration configuration) + { + if (configuration.GetValue("Telemetry:Enabled")) + { + Publisher = new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions() + { + ConnectionString = configuration.GetValue("Telemetry:ConnectionString"), + TelemetrySource = "NucuCar.Sensors", + Logger = logger + }); + } + else + { + Publisher = null; + } + } + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Telemetry/SensorTelemetryPublisher.cs b/NucuCar.Sensors/Telemetry/SensorTelemetryPublisher.cs deleted file mode 100644 index 8f3e553..0000000 --- a/NucuCar.Sensors/Telemetry/SensorTelemetryPublisher.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using Microsoft.Extensions.Logging; -using NucuCar.Domain.Telemetry; - -namespace NucuCar.Sensors.Telemetry -{ - public class SensorTelemetryPublisher : IDisposable - { - private static object _palock = new object(); - public static TelemetryPublisher Instance { get; private set; } - - /// - /// Creates a telemetry publisher instance see . - /// - public static TelemetryPublisher CreateSingleton(string connectionString, string telemetrySource, - ILogger logger) - { - lock (_palock) - { - if (Instance != null) return Instance; - var telemetryPublisher = - TelemetryPublisherAzure.CreateFromConnectionString(connectionString, telemetrySource, logger); - Instance = telemetryPublisher; - return Instance; - } - } - - private static void ReleaseUnmanagedResources() - { - Instance?.Dispose(); - } - - public void Dispose() - { - ReleaseUnmanagedResources(); - GC.SuppressFinalize(this); - } - - ~SensorTelemetryPublisher() - { - ReleaseUnmanagedResources(); - } - } -} \ No newline at end of file diff --git a/NucuCar.Sensors/Telemetry/TelemetryWorker.cs b/NucuCar.Sensors/Telemetry/TelemetryWorker.cs new file mode 100644 index 0000000..27de392 --- /dev/null +++ b/NucuCar.Sensors/Telemetry/TelemetryWorker.cs @@ -0,0 +1,38 @@ +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using NucuCar.Domain.Telemetry; + +namespace NucuCar.Sensors.Telemetry +{ + /// + /// Telemetry service, which pools the telemetry sources and pushes telemetry data to the cloud. + /// + public class TelemetryBackgroundWorker : BackgroundService + { + private readonly int _interval; + private readonly ILogger _logger; + private readonly SensorTelemetry _sensorTelemetry; + + public TelemetryBackgroundWorker(ILogger logger, IConfiguration configuration, + SensorTelemetry sensorTelemetry) + { + _logger = logger; + _interval = configuration.GetValue("Telemetry:Interval"); + _sensorTelemetry = sensorTelemetry; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + await Task.Delay(_interval, stoppingToken); + while (!stoppingToken.IsCancellationRequested) + { + _logger.LogInformation("Is publishing telemetry data!"); + await _sensorTelemetry.Publisher.PublishAsync(stoppingToken); + await Task.Delay(_interval, stoppingToken); + } + } + } +} \ No newline at end of file