NUC-32: Improve Pms5003 sensor implementation & register

This commit is contained in:
Denis-Cosmin Nutiu 2021-04-25 18:40:56 +03:00
parent 7bf36ed53a
commit 62064f33b7
9 changed files with 62 additions and 11 deletions

View file

@ -9,7 +9,6 @@ enum SensorStateEnum {
Uninitialized = 1;
Initialized = 2;
Disabled = 3;
GrpcDisabled = 4;
}
// Environment Sensor
@ -22,6 +21,11 @@ service HealthSensorGrpcService {
rpc GetCpuTemperature(google.protobuf.Empty) returns (NucuCarSensorResponse) {}
}
// Pms5003 Sensor
service Pms5003SensorGrpcService {
rpc GetMeasurement(google.protobuf.Empty) returns (NucuCarSensorResponse) {}
}
// Responses
message NucuCarSensorResponse {
SensorStateEnum State = 1;

View file

@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NucuCar.Sensors.Modules.Environment;
using NucuCar.Sensors.Modules.Health;
using NucuCar.Sensors.Modules.PMS5003;
namespace NucuCar.Sensors.Grpc
{
@ -32,6 +33,7 @@ namespace NucuCar.Sensors.Grpc
// Add the gRPC services here.
endpoints.MapGrpcService<Bme680GrpcService>();
endpoints.MapGrpcService<CpuTempGrpcService>();
endpoints.MapGrpcService<Pms5003GrpcService>();
endpoints.MapGet("/",
async context =>

View file

@ -6,7 +6,7 @@ namespace NucuCar.Sensors.Grpc
{
public static NucuCarSensorResponse GrpcIsDisabledResponse = new NucuCarSensorResponse()
{
State = SensorStateEnum.GrpcDisabled,
State = SensorStateEnum.Disabled,
JsonData = "{}"
};
}

View file

@ -2,6 +2,6 @@
{
public class HeartbeatConfig : BaseSensorConfig
{
public bool Grpc { get; } = false;
public new bool Grpc { get; } = false;
}
}

View file

@ -1,7 +1,36 @@
namespace NucuCar.Sensors.Modules.PMS5003
{
public class Pms5003GrpcService
{
using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NucuCar.Sensors.Abstractions;
using NucuCar.Sensors.Grpc;
using NucuCarSensorsProto;
namespace NucuCar.Sensors.Modules.PMS5003
{
public class Pms5003GrpcService : Pms5003SensorGrpcService.Pms5003SensorGrpcServiceBase
{
private readonly ILogger<Pms5003GrpcService> _logger;
private readonly IOptions<Pms5003Config> _options;
private readonly ISensor<Pms5003Sensor> _pms5003Sensor;
public Pms5003GrpcService(ILogger<Pms5003GrpcService> logger, ISensor<Pms5003Sensor> pms5003Sensor, IOptions<Pms5003Config> options)
{
_pms5003Sensor = pms5003Sensor;
_logger = logger;
_options = options;
}
public override async Task<NucuCarSensorResponse> GetMeasurement(Empty request, ServerCallContext context)
{
_logger?.LogDebug($"Calling {nameof(GetMeasurement)}.");
if (_options.Value.Grpc)
{
return await Task.FromResult(_pms5003Sensor.Object.GetMeasurement());
}
return await Task.FromResult(Responses.GrpcIsDisabledResponse);
}
}
}

View file

@ -36,17 +36,26 @@ namespace NucuCar.Sensors.Modules.PMS5003
public override void Initialize()
{
_pms5003 = new Pms5003(23, 24);
CurrentState = SensorStateEnum.Initialized;
}
public override Task TakeMeasurementAsync()
{
try
{
_pms5003.WakeUp();
_pms5003Data = _pms5003.ReadData();
CurrentState = SensorStateEnum.Initialized;
}
catch (ReadFailedException e)
{
Logger?.LogError("{message}", e.Message);
Logger?.LogError("{Message}", e.Message);
CurrentState = SensorStateEnum.Error;
_pms5003.Reset();
}
finally
{
_pms5003.Sleep();
}
return Task.CompletedTask;
@ -54,9 +63,7 @@ namespace NucuCar.Sensors.Modules.PMS5003
public override NucuCarSensorResponse GetMeasurement()
{
string jsonResponse = null;
jsonResponse = _pms5003Data != null ? JsonConvert.SerializeObject(_pms5003Data) : "{}";
var jsonResponse = _pms5003Data != null ? JsonConvert.SerializeObject(_pms5003Data) : "{}";
return new NucuCarSensorResponse()
{
State = GetState(),

View file

@ -6,6 +6,7 @@ using NucuCar.Sensors.Grpc;
using NucuCar.Sensors.Modules.Environment;
using NucuCar.Sensors.Modules.Health;
using NucuCar.Sensors.Modules.Heartbeat;
using NucuCar.Sensors.Modules.PMS5003;
using NucuCar.Telemetry;
namespace NucuCar.Sensors
@ -25,12 +26,14 @@ namespace NucuCar.Sensors
services.Configure<Bme680Config>(hostContext.Configuration.GetSection("EnvironmentSensor"));
services.Configure<CpuTempConfig>(hostContext.Configuration.GetSection("HealthSensor"));
services.Configure<HeartbeatConfig>(hostContext.Configuration.GetSection("HeartbeatSensor"));
services.Configure<Pms5003Config>(hostContext.Configuration.GetSection("Pms5003Sensor"));
// Singletons
services.AddSingleton<Telemetry.Telemetry>();
services.AddSingleton<ISensor<Bme680Sensor>, Bme680Sensor>();
services.AddSingleton<ISensor<CpuTempSensor>, CpuTempSensor>();
services.AddSingleton<ISensor<HeartbeatSensor>, HeartbeatSensor>();
services.AddSingleton<ISensor<Pms5003Sensor>, Pms5003Sensor>();
// Workers
// Telemetry
@ -39,6 +42,7 @@ namespace NucuCar.Sensors
services.AddHostedService<Bme680Worker>();
services.AddHostedService<CpuTempWorker>();
services.AddHostedService<HeartbeatWorker>();
services.AddHostedService<Pms5003Worker>();
})
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<GrpcStartup>(); });
}

View file

@ -20,6 +20,11 @@
"Telemetry": true,
"Grpc": false
},
"Pms5003Sensor": {
"Enabled": false,
"Telemetry": true,
"Grpc": true
},
"Logging": {
"LogLevel": {
"Default": "Information",

View file

@ -60,7 +60,7 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor
// Verify that the sensor get measurement method is not called.
_mockSensor.Verify(s => s.Object.GetMeasurement(), Times.Never());
Assert.Equal(SensorStateEnum.GrpcDisabled, result.Result.State);
Assert.Equal(SensorStateEnum.Disabled, result.Result.State);
}
}
}