From c4c55f798e0caf79828ecd4e6e0bcb2815d97dc3 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Tue, 26 Nov 2019 20:24:27 +0200 Subject: [PATCH] Refactor transient config services to use IOptions --- .../EnvironmentSensor/Bme680Config.cs | 15 ++++----------- .../EnvironmentSensor/Bme680Sensor.cs | 5 +++-- .../EnvironmentSensor/Bme680Worker.cs | 7 ++++--- NucuCar.Sensors/Program.cs | 12 ++++-------- NucuCar.Sensors/Telemetry/SensorTelemetry.cs | 7 ++++--- NucuCar.Sensors/Telemetry/TelemetryConfig.cs | 16 ++++------------ NucuCar.Sensors/Telemetry/TelemetryWorker.cs | 5 +++-- NucuCar.Sensors/appsettings.json | 4 ++-- 8 files changed, 28 insertions(+), 43 deletions(-) diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs index e95f645..26cf5cc 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Config.cs @@ -1,18 +1,11 @@ -using Microsoft.Extensions.Configuration; +// ReSharper disable UnusedAutoPropertyAccessor.Global 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"); - } + public bool ServiceEnabled { get; set; } + public bool TelemetryEnabled { get; set; } + public int MeasurementInterval { get; set; } } } \ No newline at end of file diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs index e5f7084..2c5c7f4 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Iot.Device.Bmxx80; using Iot.Device.Bmxx80.PowerMode; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NucuCar.Domain.Telemetry; using NucuCarSensorsProto; @@ -23,11 +24,11 @@ namespace NucuCar.Sensors.EnvironmentSensor private EnvironmentSensorMeasurement _lastMeasurement; private SensorStateEnum _sensorStateEnum; - public Bme680Sensor(ILogger logger, Bme680Config configuration) + public Bme680Sensor(ILogger logger, IOptions options) { _sensorStateEnum = SensorStateEnum.Uninitialized; _logger = logger; - if (configuration.SensorEnabled) + if (options.Value.ServiceEnabled) { InitializeSensor(); } diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs index 0a6fcf9..85f510c 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Worker.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NucuCar.Domain.Telemetry; using NucuCar.Sensors.Telemetry; using NucuCarSensorsProto; @@ -21,12 +22,12 @@ namespace NucuCar.Sensors.EnvironmentSensor private readonly Bme680Sensor _bme680Sensor; - public Bme680Worker(ILogger logger, Bme680Config config, + public Bme680Worker(ILogger logger, IOptions options, SensorTelemetry sensorTelemetry, Bme680Sensor bme680Sensor) { _logger = logger; - _telemetryEnabled = config.TelemetryEnabled; - _measurementInterval = config.MeasurementInterval; + _telemetryEnabled = options.Value.TelemetryEnabled; + _measurementInterval = options.Value.MeasurementInterval; _telemetryPublisher = sensorTelemetry.Publisher; _bme680Sensor = bme680Sensor; } diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index b274581..0eb1245 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -18,10 +18,9 @@ namespace NucuCar.Sensors Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { - // Transient - services.AddTransient(); - services.AddTransient(); - + services.Configure(hostContext.Configuration.GetSection("Telemetry")); + services.Configure(hostContext.Configuration.GetSection("EnvironmentSensor")); + // Singletons services.AddSingleton(); services.AddSingleton(); @@ -30,9 +29,6 @@ namespace NucuCar.Sensors services.AddHostedService(); services.AddHostedService(); }) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); + .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); } } \ No newline at end of file diff --git a/NucuCar.Sensors/Telemetry/SensorTelemetry.cs b/NucuCar.Sensors/Telemetry/SensorTelemetry.cs index f440cdd..b896745 100644 --- a/NucuCar.Sensors/Telemetry/SensorTelemetry.cs +++ b/NucuCar.Sensors/Telemetry/SensorTelemetry.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NucuCar.Domain.Telemetry; namespace NucuCar.Sensors.Telemetry @@ -7,11 +8,11 @@ namespace NucuCar.Sensors.Telemetry { public TelemetryPublisher Publisher { get; } - public SensorTelemetry(ILogger logger, TelemetryConfig configuration) + public SensorTelemetry(ILogger logger, IOptions options) { - if (configuration.ServiceEnabled) + if (options.Value.ServiceEnabled) { - Publisher = TelemetryPublisherAzure.CreateFromConnectionString(configuration.ConnectionString, + Publisher = TelemetryPublisherAzure.CreateFromConnectionString(options.Value.ConnectionString, "NucuCar.Sensors", logger); } else diff --git a/NucuCar.Sensors/Telemetry/TelemetryConfig.cs b/NucuCar.Sensors/Telemetry/TelemetryConfig.cs index 64223cb..338faf0 100644 --- a/NucuCar.Sensors/Telemetry/TelemetryConfig.cs +++ b/NucuCar.Sensors/Telemetry/TelemetryConfig.cs @@ -1,19 +1,11 @@ -using Microsoft.Extensions.Configuration; +// ReSharper disable UnusedAutoPropertyAccessor.Global 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"); - } - + public bool ServiceEnabled { get; set; } + public int PublishInterval { get; set; } + public string ConnectionString { get; set; } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Telemetry/TelemetryWorker.cs b/NucuCar.Sensors/Telemetry/TelemetryWorker.cs index ff4c9cf..790abe1 100644 --- a/NucuCar.Sensors/Telemetry/TelemetryWorker.cs +++ b/NucuCar.Sensors/Telemetry/TelemetryWorker.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NucuCar.Domain.Telemetry; namespace NucuCar.Sensors.Telemetry @@ -15,11 +16,11 @@ namespace NucuCar.Sensors.Telemetry private readonly ILogger _logger; private readonly TelemetryPublisher _telemetryPublisher; - public TelemetryWorker(ILogger logger, TelemetryConfig config, + public TelemetryWorker(ILogger logger, IOptions options, SensorTelemetry sensorTelemetry) { _logger = logger; - _interval = config.PublishInterval; + _interval = options.Value.PublishInterval; _telemetryPublisher = sensorTelemetry.Publisher; if (_telemetryPublisher == null) { diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index 94b9869..e7d450e 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -1,11 +1,11 @@ { "Telemetry": { - "Enabled": true, + "ServiceEnabled": true, "PublishInterval": 3000, "ConnectionString": "YOUR_CONNECTION_STRING" }, "EnvironmentSensor": { - "Enabled": true, + "ServiceEnabled": true, "TelemetryEnabled": true, "MeasurementInterval": 1000 },