Refactor transient config services to use IOptions

This commit is contained in:
Denis-Cosmin Nutiu 2019-11-26 20:24:27 +02:00
parent 651c1d6a56
commit c4c55f798e
8 changed files with 28 additions and 43 deletions

View file

@ -1,18 +1,11 @@
using Microsoft.Extensions.Configuration; // ReSharper disable UnusedAutoPropertyAccessor.Global
namespace NucuCar.Sensors.EnvironmentSensor namespace NucuCar.Sensors.EnvironmentSensor
{ {
public class Bme680Config public class Bme680Config
{ {
public bool SensorEnabled { get; } public bool ServiceEnabled { get; set; }
public bool TelemetryEnabled { get; } public bool TelemetryEnabled { get; set; }
public int MeasurementInterval { get; } public int MeasurementInterval { get; set; }
public Bme680Config(IConfiguration configuration)
{
SensorEnabled = configuration.GetValue<bool>("EnvironmentSensor:Enabled");
TelemetryEnabled = configuration.GetValue<bool>("EnvironmentSensor:TelemetryEnabled");
MeasurementInterval = configuration.GetValue<int>("EnvironmentSensor:MeasurementInterval");
}
} }
} }

View file

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using Iot.Device.Bmxx80; using Iot.Device.Bmxx80;
using Iot.Device.Bmxx80.PowerMode; using Iot.Device.Bmxx80.PowerMode;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NucuCar.Domain.Telemetry; using NucuCar.Domain.Telemetry;
using NucuCarSensorsProto; using NucuCarSensorsProto;
@ -23,11 +24,11 @@ namespace NucuCar.Sensors.EnvironmentSensor
private EnvironmentSensorMeasurement _lastMeasurement; private EnvironmentSensorMeasurement _lastMeasurement;
private SensorStateEnum _sensorStateEnum; private SensorStateEnum _sensorStateEnum;
public Bme680Sensor(ILogger<Bme680Sensor> logger, Bme680Config configuration) public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options)
{ {
_sensorStateEnum = SensorStateEnum.Uninitialized; _sensorStateEnum = SensorStateEnum.Uninitialized;
_logger = logger; _logger = logger;
if (configuration.SensorEnabled) if (options.Value.ServiceEnabled)
{ {
InitializeSensor(); InitializeSensor();
} }

View file

@ -2,6 +2,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NucuCar.Domain.Telemetry; using NucuCar.Domain.Telemetry;
using NucuCar.Sensors.Telemetry; using NucuCar.Sensors.Telemetry;
using NucuCarSensorsProto; using NucuCarSensorsProto;
@ -21,12 +22,12 @@ namespace NucuCar.Sensors.EnvironmentSensor
private readonly Bme680Sensor _bme680Sensor; private readonly Bme680Sensor _bme680Sensor;
public Bme680Worker(ILogger<Bme680Worker> logger, Bme680Config config, public Bme680Worker(ILogger<Bme680Worker> logger, IOptions<Bme680Config> options,
SensorTelemetry sensorTelemetry, Bme680Sensor bme680Sensor) SensorTelemetry sensorTelemetry, Bme680Sensor bme680Sensor)
{ {
_logger = logger; _logger = logger;
_telemetryEnabled = config.TelemetryEnabled; _telemetryEnabled = options.Value.TelemetryEnabled;
_measurementInterval = config.MeasurementInterval; _measurementInterval = options.Value.MeasurementInterval;
_telemetryPublisher = sensorTelemetry.Publisher; _telemetryPublisher = sensorTelemetry.Publisher;
_bme680Sensor = bme680Sensor; _bme680Sensor = bme680Sensor;
} }

View file

@ -18,9 +18,8 @@ namespace NucuCar.Sensors
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) => .ConfigureServices((hostContext, services) =>
{ {
// Transient services.Configure<TelemetryConfig>(hostContext.Configuration.GetSection("Telemetry"));
services.AddTransient<TelemetryConfig>(); services.Configure<Bme680Config>(hostContext.Configuration.GetSection("EnvironmentSensor"));
services.AddTransient<Bme680Config>();
// Singletons // Singletons
services.AddSingleton<SensorTelemetry>(); services.AddSingleton<SensorTelemetry>();
@ -30,9 +29,6 @@ namespace NucuCar.Sensors
services.AddHostedService<TelemetryWorker>(); services.AddHostedService<TelemetryWorker>();
services.AddHostedService<Bme680Worker>(); services.AddHostedService<Bme680Worker>();
}) })
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<GrpcStartup>(); });
{
webBuilder.UseStartup<GrpcStartup>();
});
} }
} }

View file

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NucuCar.Domain.Telemetry; using NucuCar.Domain.Telemetry;
namespace NucuCar.Sensors.Telemetry namespace NucuCar.Sensors.Telemetry
@ -7,11 +8,11 @@ namespace NucuCar.Sensors.Telemetry
{ {
public TelemetryPublisher Publisher { get; } public TelemetryPublisher Publisher { get; }
public SensorTelemetry(ILogger<SensorTelemetry> logger, TelemetryConfig configuration) public SensorTelemetry(ILogger<SensorTelemetry> logger, IOptions<TelemetryConfig> options)
{ {
if (configuration.ServiceEnabled) if (options.Value.ServiceEnabled)
{ {
Publisher = TelemetryPublisherAzure.CreateFromConnectionString(configuration.ConnectionString, Publisher = TelemetryPublisherAzure.CreateFromConnectionString(options.Value.ConnectionString,
"NucuCar.Sensors", logger); "NucuCar.Sensors", logger);
} }
else else

View file

@ -1,19 +1,11 @@
using Microsoft.Extensions.Configuration; // ReSharper disable UnusedAutoPropertyAccessor.Global
namespace NucuCar.Sensors.Telemetry namespace NucuCar.Sensors.Telemetry
{ {
public class TelemetryConfig public class TelemetryConfig
{ {
public bool ServiceEnabled { get; } public bool ServiceEnabled { get; set; }
public int PublishInterval { get; } public int PublishInterval { get; set; }
public string ConnectionString { get; } public string ConnectionString { get; set; }
public TelemetryConfig(IConfiguration configuration)
{
ServiceEnabled = configuration.GetValue<bool>("Telemetry:Enabled");
PublishInterval = configuration.GetValue<int>("Telemetry:PublishInterval");
ConnectionString = configuration.GetValue<string>("Telemetry:ConnectionString");
}
} }
} }

View file

@ -2,6 +2,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NucuCar.Domain.Telemetry; using NucuCar.Domain.Telemetry;
namespace NucuCar.Sensors.Telemetry namespace NucuCar.Sensors.Telemetry
@ -15,11 +16,11 @@ namespace NucuCar.Sensors.Telemetry
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly TelemetryPublisher _telemetryPublisher; private readonly TelemetryPublisher _telemetryPublisher;
public TelemetryWorker(ILogger<TelemetryWorker> logger, TelemetryConfig config, public TelemetryWorker(ILogger<TelemetryWorker> logger, IOptions<TelemetryConfig> options,
SensorTelemetry sensorTelemetry) SensorTelemetry sensorTelemetry)
{ {
_logger = logger; _logger = logger;
_interval = config.PublishInterval; _interval = options.Value.PublishInterval;
_telemetryPublisher = sensorTelemetry.Publisher; _telemetryPublisher = sensorTelemetry.Publisher;
if (_telemetryPublisher == null) if (_telemetryPublisher == null)
{ {

View file

@ -1,11 +1,11 @@
{ {
"Telemetry": { "Telemetry": {
"Enabled": true, "ServiceEnabled": true,
"PublishInterval": 3000, "PublishInterval": 3000,
"ConnectionString": "YOUR_CONNECTION_STRING" "ConnectionString": "YOUR_CONNECTION_STRING"
}, },
"EnvironmentSensor": { "EnvironmentSensor": {
"Enabled": true, "ServiceEnabled": true,
"TelemetryEnabled": true, "TelemetryEnabled": true,
"MeasurementInterval": 1000 "MeasurementInterval": 1000
}, },