From 8a6094b8a3c659c160b03e8a2c1f13ea0934524c Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Mon, 11 Nov 2019 17:58:46 +0200 Subject: [PATCH] Add EnvironmentSensor settings --- .../EnvironmentSensor/BackgroundWorker.cs | 15 +++++++++++++-- NucuCar.Sensors/appsettings.json | 5 +++++ Readme.md | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/NucuCar.Sensors/EnvironmentSensor/BackgroundWorker.cs b/NucuCar.Sensors/EnvironmentSensor/BackgroundWorker.cs index a0b420e..b827596 100644 --- a/NucuCar.Sensors/EnvironmentSensor/BackgroundWorker.cs +++ b/NucuCar.Sensors/EnvironmentSensor/BackgroundWorker.cs @@ -9,20 +9,31 @@ namespace NucuCar.Sensors.EnvironmentSensor { public class BackgroundWorker : BackgroundService { + private readonly bool _serviceEnabled; + private readonly bool _telemetryEnabled; + private readonly int _measurementDelay; private readonly ILogger _logger; public BackgroundWorker(ILogger logger, IConfiguration config) { _logger = logger; + var configSection = config.GetSection("EnvironmentSensor"); + _serviceEnabled = configSection.GetValue("Enabled"); + _telemetryEnabled = configSection.GetValue("Telemetry"); + _measurementDelay = configSection.GetValue("MeasurementDelay"); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { + if (!_serviceEnabled) + { + return; + } + using var sensor = Sensor.Instance; sensor.SetLogger(_logger); sensor.InitializeSensor(); - while (!stoppingToken.IsCancellationRequested) { if (sensor.GetState() == SensorStateEnum.Initialized) @@ -36,7 +47,7 @@ namespace NucuCar.Sensors.EnvironmentSensor sensor.InitializeSensor(); } - await Task.Delay(1000, stoppingToken); + await Task.Delay(_measurementDelay, stoppingToken); } } } diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index a05d2bd..f61fd23 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -1,4 +1,9 @@ { + "EnvironmentSensor": { + "Enabled": true, + "Telemetry": false, + "MeasurementDelay": 1000 + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/Readme.md b/Readme.md index ce9bc2c..4e59ce5 100644 --- a/Readme.md +++ b/Readme.md @@ -5,14 +5,14 @@ You can build the project then run them as you would run an executable file. ```$xslt -dotnet build --runtime debian-arm +dotnet build --runtime linux-arm ``` Milestones: - - [X] Make a working BME680 module. ~~(Unit tests?)~~ - - [ ] Add settings: gRPC enabled, Telemetry Enabled, Sensor Enabled. + - [X] Add settings: ~~gRPC enabled~~, Telemetry Enabled, Sensor Enabled, Measurement Delay - [X] Make a gRPC test project to test the modules. - [ ] Add systemd config file. - [ ] Add makefile for easy building & installing.