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
{
public class Bme680Config
{
public bool SensorEnabled { get; }
public bool TelemetryEnabled { get; }
public int MeasurementInterval { get; }
public Bme680Config(IConfiguration configuration)
{
SensorEnabled = configuration.GetValue<bool>("EnvironmentSensor:Enabled");
TelemetryEnabled = configuration.GetValue<bool>("EnvironmentSensor:TelemetryEnabled");
MeasurementInterval = configuration.GetValue<int>("EnvironmentSensor:MeasurementInterval");
}
public bool ServiceEnabled { get; set; }
public bool TelemetryEnabled { get; set; }
public int MeasurementInterval { get; set; }
}
}

View file

@ -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<Bme680Sensor> logger, Bme680Config configuration)
public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options)
{
_sensorStateEnum = SensorStateEnum.Uninitialized;
_logger = logger;
if (configuration.SensorEnabled)
if (options.Value.ServiceEnabled)
{
InitializeSensor();
}

View file

@ -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<Bme680Worker> logger, Bme680Config config,
public Bme680Worker(ILogger<Bme680Worker> logger, IOptions<Bme680Config> 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;
}

View file

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

View file

@ -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<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);
}
else

View file

@ -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<bool>("Telemetry:Enabled");
PublishInterval = configuration.GetValue<int>("Telemetry:PublishInterval");
ConnectionString = configuration.GetValue<string>("Telemetry:ConnectionString");
}
public bool ServiceEnabled { get; set; }
public int PublishInterval { get; set; }
public string ConnectionString { get; set; }
}
}

View file

@ -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<TelemetryWorker> logger, TelemetryConfig config,
public TelemetryWorker(ILogger<TelemetryWorker> logger, IOptions<TelemetryConfig> options,
SensorTelemetry sensorTelemetry)
{
_logger = logger;
_interval = config.PublishInterval;
_interval = options.Value.PublishInterval;
_telemetryPublisher = sensorTelemetry.Publisher;
if (_telemetryPublisher == null)
{

View file

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