From 4db467f22146361b835bcca40dacab07fed9b029 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Sun, 24 Nov 2019 18:40:42 +0200 Subject: [PATCH] Refactor NucuCar.Sensors configuration handling --- .../EnvironmentSensor/Bme680Config.cs | 18 ++++++++++++++ .../EnvironmentSensor/Bme680Sensor.cs | 5 ++-- .../EnvironmentSensor/Bme680Worker.cs | 11 ++++----- NucuCar.Sensors/Program.cs | 24 ++++++++----------- NucuCar.Sensors/Telemetry/SensorTelemetry.cs | 7 +++--- NucuCar.Sensors/Telemetry/TelemetryConfig.cs | 19 +++++++++++++++ NucuCar.Sensors/Telemetry/TelemetryWorker.cs | 5 ++-- NucuCar.Sensors/appsettings.json | 4 ++-- 8 files changed, 61 insertions(+), 32 deletions(-) create mode 100644 NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs create mode 100644 NucuCar.Sensors/Telemetry/TelemetryConfig.cs diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs new file mode 100644 index 0000000..e95f645 --- /dev/null +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Configuration; + +namespace NucuCar.Sensors.EnvironmentSensor +{ + public class Bme680Config + { + public bool SensorEnabled { get; } + public bool TelemetryEnabled { get; } + public int MeasurementInterval { get; } + + public Bme680Config(IConfiguration configuration) + { + SensorEnabled = configuration.GetValue("EnvironmentSensor:Enabled"); + TelemetryEnabled = configuration.GetValue("EnvironmentSensor:TelemetryEnabled"); + MeasurementInterval = configuration.GetValue("EnvironmentSensor:MeasurementInterval"); + } + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs index a5d7991..e5f7084 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs @@ -4,7 +4,6 @@ using System.Device.I2c; using System.Threading.Tasks; using Iot.Device.Bmxx80; using Iot.Device.Bmxx80.PowerMode; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using NucuCar.Domain.Telemetry; using NucuCarSensorsProto; @@ -24,11 +23,11 @@ namespace NucuCar.Sensors.EnvironmentSensor private EnvironmentSensorMeasurement _lastMeasurement; private SensorStateEnum _sensorStateEnum; - public Bme680Sensor(ILogger logger, IConfiguration configuration) + public Bme680Sensor(ILogger logger, Bme680Config configuration) { _sensorStateEnum = SensorStateEnum.Uninitialized; _logger = logger; - if (configuration.GetValue("EnvironmentSensor:Enabled")) + if (configuration.SensorEnabled) { InitializeSensor(); } diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs index f55fc7b..0a6fcf9 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs @@ -1,6 +1,5 @@ using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NucuCar.Domain.Telemetry; @@ -16,18 +15,18 @@ namespace NucuCar.Sensors.EnvironmentSensor public class Bme680Worker : BackgroundService { private readonly bool _telemetryEnabled; - private readonly int _measurementDelay; + private readonly int _measurementInterval; private readonly ILogger _logger; private readonly TelemetryPublisher _telemetryPublisher; private readonly Bme680Sensor _bme680Sensor; - public Bme680Worker(ILogger logger, IConfiguration config, + public Bme680Worker(ILogger logger, Bme680Config config, SensorTelemetry sensorTelemetry, Bme680Sensor bme680Sensor) { _logger = logger; - _telemetryEnabled = config.GetValue("EnvironmentSensor:Telemetry"); - _measurementDelay = config.GetValue("EnvironmentSensor:MeasurementInterval"); + _telemetryEnabled = config.TelemetryEnabled; + _measurementInterval = config.MeasurementInterval; _telemetryPublisher = sensorTelemetry.Publisher; _bme680Sensor = bme680Sensor; } @@ -54,7 +53,7 @@ namespace NucuCar.Sensors.EnvironmentSensor _bme680Sensor.InitializeSensor(); } - await Task.Delay(_measurementDelay, stoppingToken); + await Task.Delay(_measurementInterval, stoppingToken); } _telemetryPublisher?.UnRegisterTelemeter(_bme680Sensor); diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index f0df053..b274581 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -2,6 +2,8 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using NucuCar.Sensors.EnvironmentSensor; +using NucuCar.Sensors.Telemetry; namespace NucuCar.Sensors { @@ -16,23 +18,17 @@ namespace NucuCar.Sensors Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { - var config = hostContext.Configuration; - + // Transient + services.AddTransient(); + services.AddTransient(); + // Singletons - services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); // Workers - if (config.GetValue("Telemetry:Enabled")) - { - services.AddHostedService(); - } - - if (config.GetValue("EnvironmentSensor:Enabled")) - { - services.AddHostedService(); - } - + services.AddHostedService(); + services.AddHostedService(); }) .ConfigureWebHostDefaults(webBuilder => { diff --git a/NucuCar.Sensors/Telemetry/SensorTelemetry.cs b/NucuCar.Sensors/Telemetry/SensorTelemetry.cs index decff8e..679043e 100644 --- a/NucuCar.Sensors/Telemetry/SensorTelemetry.cs +++ b/NucuCar.Sensors/Telemetry/SensorTelemetry.cs @@ -1,4 +1,3 @@ -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using NucuCar.Domain.Telemetry; @@ -8,13 +7,13 @@ namespace NucuCar.Sensors.Telemetry { public TelemetryPublisher Publisher { get; } - public SensorTelemetry(ILogger logger, IConfiguration configuration) + public SensorTelemetry(ILogger logger, TelemetryConfig configuration) { - if (configuration.GetValue("Telemetry:Enabled")) + if (configuration.ServiceEnabled) { Publisher = new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions() { - ConnectionString = configuration.GetValue("Telemetry:ConnectionString"), + ConnectionString = configuration.ConnectionString, TelemetrySource = "NucuCar.Sensors", Logger = logger }); diff --git a/NucuCar.Sensors/Telemetry/TelemetryConfig.cs b/NucuCar.Sensors/Telemetry/TelemetryConfig.cs new file mode 100644 index 0000000..64223cb --- /dev/null +++ b/NucuCar.Sensors/Telemetry/TelemetryConfig.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.Configuration; + +namespace NucuCar.Sensors.Telemetry +{ + public class TelemetryConfig + { + public bool ServiceEnabled { get; } + public int PublishInterval { get; } + public string ConnectionString { get; } + + public TelemetryConfig(IConfiguration configuration) + { + ServiceEnabled = configuration.GetValue("Telemetry:Enabled"); + PublishInterval = configuration.GetValue("Telemetry:PublishInterval"); + ConnectionString = configuration.GetValue("Telemetry:ConnectionString"); + } + + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Telemetry/TelemetryWorker.cs b/NucuCar.Sensors/Telemetry/TelemetryWorker.cs index 002a443..ff4c9cf 100644 --- a/NucuCar.Sensors/Telemetry/TelemetryWorker.cs +++ b/NucuCar.Sensors/Telemetry/TelemetryWorker.cs @@ -1,6 +1,5 @@ using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NucuCar.Domain.Telemetry; @@ -16,11 +15,11 @@ namespace NucuCar.Sensors.Telemetry private readonly ILogger _logger; private readonly TelemetryPublisher _telemetryPublisher; - public TelemetryWorker(ILogger logger, IConfiguration configuration, + public TelemetryWorker(ILogger logger, TelemetryConfig config, SensorTelemetry sensorTelemetry) { _logger = logger; - _interval = configuration.GetValue("Telemetry:Interval"); + _interval = config.PublishInterval; _telemetryPublisher = sensorTelemetry.Publisher; if (_telemetryPublisher == null) { diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index 300030d..94b9869 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -1,12 +1,12 @@ { "Telemetry": { "Enabled": true, - "Interval": 3000, + "PublishInterval": 3000, "ConnectionString": "YOUR_CONNECTION_STRING" }, "EnvironmentSensor": { "Enabled": true, - "Telemetry": true, + "TelemetryEnabled": true, "MeasurementInterval": 1000 }, "Logging": {