Scaffold for telemetry service
This commit is contained in:
parent
8a6094b8a3
commit
fb4034773c
7 changed files with 134 additions and 3 deletions
|
@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using NucuCar.Sensors.Telemetry;
|
||||||
using NucuCarSensorsProto;
|
using NucuCarSensorsProto;
|
||||||
|
|
||||||
namespace NucuCar.Sensors.EnvironmentSensor
|
namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
|
@ -21,7 +22,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
var configSection = config.GetSection("EnvironmentSensor");
|
var configSection = config.GetSection("EnvironmentSensor");
|
||||||
_serviceEnabled = configSection.GetValue<bool>("Enabled");
|
_serviceEnabled = configSection.GetValue<bool>("Enabled");
|
||||||
_telemetryEnabled = configSection.GetValue<bool>("Telemetry");
|
_telemetryEnabled = configSection.GetValue<bool>("Telemetry");
|
||||||
_measurementDelay = configSection.GetValue<int>("MeasurementDelay");
|
_measurementDelay = configSection.GetValue<int>("MeasurementInterval");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
@ -34,6 +35,11 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
using var sensor = Sensor.Instance;
|
using var sensor = Sensor.Instance;
|
||||||
sensor.SetLogger(_logger);
|
sensor.SetLogger(_logger);
|
||||||
sensor.InitializeSensor();
|
sensor.InitializeSensor();
|
||||||
|
if (_telemetryEnabled)
|
||||||
|
{
|
||||||
|
TelemetryService.Instance.RegisterSensor(sensor);
|
||||||
|
}
|
||||||
|
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
if (sensor.GetState() == SensorStateEnum.Initialized)
|
if (sensor.GetState() == SensorStateEnum.Initialized)
|
||||||
|
@ -49,6 +55,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
|
|
||||||
await Task.Delay(_measurementDelay, stoppingToken);
|
await Task.Delay(_measurementDelay, stoppingToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TelemetryService.Instance.UnregisterSensor(sensor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,14 +1,16 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Device.I2c;
|
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.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using NucuCar.Sensors.Telemetry;
|
||||||
using NucuCarSensorsProto;
|
using NucuCarSensorsProto;
|
||||||
|
|
||||||
namespace NucuCar.Sensors.EnvironmentSensor
|
namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
{
|
{
|
||||||
public class Sensor : IDisposable
|
public class Sensor : IDisposable, ITelemetrySensor
|
||||||
{
|
{
|
||||||
private ILogger _logger;
|
private ILogger _logger;
|
||||||
private I2cConnectionSettings _i2CSettings;
|
private I2cConnectionSettings _i2CSettings;
|
||||||
|
@ -101,5 +103,16 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
$"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH");
|
$"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Dictionary<string, double> GetTelemetryData()
|
||||||
|
{
|
||||||
|
return new Dictionary<string, double>
|
||||||
|
{
|
||||||
|
["temperature"] = _lastMeasurement.Temperature,
|
||||||
|
["humidity"] = _lastMeasurement.Humidity,
|
||||||
|
["pressure"] = _lastMeasurement.Pressure,
|
||||||
|
["voc"] = _lastMeasurement.VolatileOrganicCompound
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -16,6 +16,7 @@ namespace NucuCar.Sensors
|
||||||
Host.CreateDefaultBuilder(args)
|
Host.CreateDefaultBuilder(args)
|
||||||
.ConfigureServices((hostContext, services) =>
|
.ConfigureServices((hostContext, services) =>
|
||||||
{
|
{
|
||||||
|
services.AddHostedService<Telemetry.BackgroundWorker>();
|
||||||
services.AddHostedService<EnvironmentSensor.BackgroundWorker>();
|
services.AddHostedService<EnvironmentSensor.BackgroundWorker>();
|
||||||
})
|
})
|
||||||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<GrpcStartup>(); });
|
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<GrpcStartup>(); });
|
||||||
|
|
41
NucuCar.Sensors/Telemetry/BackgroundWorker.cs
Normal file
41
NucuCar.Sensors/Telemetry/BackgroundWorker.cs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace NucuCar.Sensors.Telemetry
|
||||||
|
{
|
||||||
|
public class BackgroundWorker : BackgroundService
|
||||||
|
{
|
||||||
|
private readonly bool _serviceEnabled;
|
||||||
|
private readonly int _interval;
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
|
public BackgroundWorker(ILogger<BackgroundWorker> logger, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
var configSection = configuration.GetSection("Telemetry");
|
||||||
|
_serviceEnabled = configSection.GetValue<bool>("Enabled");
|
||||||
|
_interval = configSection.GetValue<int>("Interval");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
if (!_serviceEnabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await Task.Delay(_interval, stoppingToken);
|
||||||
|
|
||||||
|
using var telemetryService = TelemetryService.Instance;
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Publishing telemetry data!");
|
||||||
|
await telemetryService.PublishData();
|
||||||
|
await Task.Delay(_interval, stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
NucuCar.Sensors/Telemetry/ITelemetrySensor.cs
Normal file
10
NucuCar.Sensors/Telemetry/ITelemetrySensor.cs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace NucuCar.Sensors.Telemetry
|
||||||
|
{
|
||||||
|
public interface ITelemetrySensor
|
||||||
|
{
|
||||||
|
/* Dictionary containing the topic and the value */
|
||||||
|
Dictionary<string, double> GetTelemetryData();
|
||||||
|
}
|
||||||
|
}
|
54
NucuCar.Sensors/Telemetry/TelemetryService.cs
Normal file
54
NucuCar.Sensors/Telemetry/TelemetryService.cs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace NucuCar.Sensors.Telemetry
|
||||||
|
{
|
||||||
|
public class TelemetryService : IDisposable
|
||||||
|
{
|
||||||
|
private readonly List<ITelemetrySensor> _registeredSensors;
|
||||||
|
|
||||||
|
/* Singleton Instance */
|
||||||
|
public static TelemetryService Instance { get; } = new TelemetryService();
|
||||||
|
|
||||||
|
static TelemetryService()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private TelemetryService()
|
||||||
|
{
|
||||||
|
_registeredSensors = new List<ITelemetrySensor>(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task PublishData()
|
||||||
|
{
|
||||||
|
foreach (var sensor in _registeredSensors)
|
||||||
|
{
|
||||||
|
var data = sensor.GetTelemetryData();
|
||||||
|
await UploadData(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task UploadData(Dictionary<string, double> data)
|
||||||
|
{
|
||||||
|
foreach (var entry in data)
|
||||||
|
{
|
||||||
|
// TODO: Publish data to IoTCore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterSensor(ITelemetrySensor sensor)
|
||||||
|
{
|
||||||
|
_registeredSensors.Add(sensor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnregisterSensor(ITelemetrySensor sensor)
|
||||||
|
{
|
||||||
|
_registeredSensors.Remove(sensor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,12 @@
|
||||||
{
|
{
|
||||||
|
"Telemetry": {
|
||||||
|
"Enabled": true,
|
||||||
|
"Interval": 5000
|
||||||
|
},
|
||||||
"EnvironmentSensor": {
|
"EnvironmentSensor": {
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"Telemetry": false,
|
"Telemetry": false,
|
||||||
"MeasurementDelay": 1000
|
"MeasurementInterval": 1000
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|
Loading…
Reference in a new issue