Refactor NucuCar.Sensors configuration handling

This commit is contained in:
Denis-Cosmin Nutiu 2019-11-24 18:40:42 +02:00
parent 52ac34761c
commit 4db467f221
8 changed files with 61 additions and 32 deletions

View file

@ -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<bool>("EnvironmentSensor:Enabled");
TelemetryEnabled = configuration.GetValue<bool>("EnvironmentSensor:TelemetryEnabled");
MeasurementInterval = configuration.GetValue<int>("EnvironmentSensor:MeasurementInterval");
}
}
}

View file

@ -4,7 +4,6 @@ using System.Device.I2c;
using System.Threading.Tasks; 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.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Domain.Telemetry; using NucuCar.Domain.Telemetry;
using NucuCarSensorsProto; using NucuCarSensorsProto;
@ -24,11 +23,11 @@ namespace NucuCar.Sensors.EnvironmentSensor
private EnvironmentSensorMeasurement _lastMeasurement; private EnvironmentSensorMeasurement _lastMeasurement;
private SensorStateEnum _sensorStateEnum; private SensorStateEnum _sensorStateEnum;
public Bme680Sensor(ILogger<Bme680Sensor> logger, IConfiguration configuration) public Bme680Sensor(ILogger<Bme680Sensor> logger, Bme680Config configuration)
{ {
_sensorStateEnum = SensorStateEnum.Uninitialized; _sensorStateEnum = SensorStateEnum.Uninitialized;
_logger = logger; _logger = logger;
if (configuration.GetValue<bool>("EnvironmentSensor:Enabled")) if (configuration.SensorEnabled)
{ {
InitializeSensor(); InitializeSensor();
} }

View file

@ -1,6 +1,5 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Domain.Telemetry; using NucuCar.Domain.Telemetry;
@ -16,18 +15,18 @@ namespace NucuCar.Sensors.EnvironmentSensor
public class Bme680Worker : BackgroundService public class Bme680Worker : BackgroundService
{ {
private readonly bool _telemetryEnabled; private readonly bool _telemetryEnabled;
private readonly int _measurementDelay; private readonly int _measurementInterval;
private readonly ILogger<Bme680Worker> _logger; private readonly ILogger<Bme680Worker> _logger;
private readonly TelemetryPublisher _telemetryPublisher; private readonly TelemetryPublisher _telemetryPublisher;
private readonly Bme680Sensor _bme680Sensor; private readonly Bme680Sensor _bme680Sensor;
public Bme680Worker(ILogger<Bme680Worker> logger, IConfiguration config, public Bme680Worker(ILogger<Bme680Worker> logger, Bme680Config config,
SensorTelemetry sensorTelemetry, Bme680Sensor bme680Sensor) SensorTelemetry sensorTelemetry, Bme680Sensor bme680Sensor)
{ {
_logger = logger; _logger = logger;
_telemetryEnabled = config.GetValue<bool>("EnvironmentSensor:Telemetry"); _telemetryEnabled = config.TelemetryEnabled;
_measurementDelay = config.GetValue<int>("EnvironmentSensor:MeasurementInterval"); _measurementInterval = config.MeasurementInterval;
_telemetryPublisher = sensorTelemetry.Publisher; _telemetryPublisher = sensorTelemetry.Publisher;
_bme680Sensor = bme680Sensor; _bme680Sensor = bme680Sensor;
} }
@ -54,7 +53,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
_bme680Sensor.InitializeSensor(); _bme680Sensor.InitializeSensor();
} }
await Task.Delay(_measurementDelay, stoppingToken); await Task.Delay(_measurementInterval, stoppingToken);
} }
_telemetryPublisher?.UnRegisterTelemeter(_bme680Sensor); _telemetryPublisher?.UnRegisterTelemeter(_bme680Sensor);

View file

@ -2,6 +2,8 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NucuCar.Sensors.EnvironmentSensor;
using NucuCar.Sensors.Telemetry;
namespace NucuCar.Sensors namespace NucuCar.Sensors
{ {
@ -16,23 +18,17 @@ namespace NucuCar.Sensors
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) => .ConfigureServices((hostContext, services) =>
{ {
var config = hostContext.Configuration; // Transient
services.AddTransient<TelemetryConfig>();
services.AddTransient<Bme680Config>();
// Singletons // Singletons
services.AddSingleton<Telemetry.SensorTelemetry>(); services.AddSingleton<SensorTelemetry>();
services.AddSingleton<EnvironmentSensor.Bme680Sensor>(); services.AddSingleton<Bme680Sensor>();
// Workers // Workers
if (config.GetValue<bool>("Telemetry:Enabled")) services.AddHostedService<TelemetryWorker>();
{ services.AddHostedService<Bme680Worker>();
services.AddHostedService<Telemetry.TelemetryWorker>();
}
if (config.GetValue<bool>("EnvironmentSensor:Enabled"))
{
services.AddHostedService<EnvironmentSensor.Bme680Worker>();
}
}) })
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {

View file

@ -1,4 +1,3 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Domain.Telemetry; using NucuCar.Domain.Telemetry;
@ -8,13 +7,13 @@ namespace NucuCar.Sensors.Telemetry
{ {
public TelemetryPublisher Publisher { get; } public TelemetryPublisher Publisher { get; }
public SensorTelemetry(ILogger<SensorTelemetry> logger, IConfiguration configuration) public SensorTelemetry(ILogger<SensorTelemetry> logger, TelemetryConfig configuration)
{ {
if (configuration.GetValue<bool>("Telemetry:Enabled")) if (configuration.ServiceEnabled)
{ {
Publisher = new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions() Publisher = new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions()
{ {
ConnectionString = configuration.GetValue<string>("Telemetry:ConnectionString"), ConnectionString = configuration.ConnectionString,
TelemetrySource = "NucuCar.Sensors", TelemetrySource = "NucuCar.Sensors",
Logger = logger Logger = logger
}); });

View file

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

View file

@ -1,6 +1,5 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Domain.Telemetry; using NucuCar.Domain.Telemetry;
@ -16,11 +15,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, IConfiguration configuration, public TelemetryWorker(ILogger<TelemetryWorker> logger, TelemetryConfig config,
SensorTelemetry sensorTelemetry) SensorTelemetry sensorTelemetry)
{ {
_logger = logger; _logger = logger;
_interval = configuration.GetValue<int>("Telemetry:Interval"); _interval = config.PublishInterval;
_telemetryPublisher = sensorTelemetry.Publisher; _telemetryPublisher = sensorTelemetry.Publisher;
if (_telemetryPublisher == null) if (_telemetryPublisher == null)
{ {

View file

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