From 62064f33b74558d90fb73548586394e59e3db961 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sun, 25 Apr 2021 18:40:56 +0300 Subject: [PATCH] NUC-32: Improve Pms5003 sensor implementation & register --- NucuCar.Domain/Protos/NucuCarSensors.proto | 6 +++- NucuCar.Sensors/Grpc/GrpcStartup.cs | 2 ++ NucuCar.Sensors/Grpc/Responses.cs | 2 +- .../Modules/Heartbeat/HeartbeatConfig.cs | 2 +- .../Modules/PMS5003/Pms5003GrpcService.cs | 35 +++++++++++++++++-- .../Modules/PMS5003/Pms5003Sensor.cs | 15 +++++--- NucuCar.Sensors/Program.cs | 4 +++ NucuCar.Sensors/appsettings.json | 5 +++ .../Bme680GrpcServiceTest.cs | 2 +- 9 files changed, 62 insertions(+), 11 deletions(-) diff --git a/NucuCar.Domain/Protos/NucuCarSensors.proto b/NucuCar.Domain/Protos/NucuCarSensors.proto index 55ec222..f1c6123 100644 --- a/NucuCar.Domain/Protos/NucuCarSensors.proto +++ b/NucuCar.Domain/Protos/NucuCarSensors.proto @@ -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; diff --git a/NucuCar.Sensors/Grpc/GrpcStartup.cs b/NucuCar.Sensors/Grpc/GrpcStartup.cs index fe40ae3..d8bfc7c 100644 --- a/NucuCar.Sensors/Grpc/GrpcStartup.cs +++ b/NucuCar.Sensors/Grpc/GrpcStartup.cs @@ -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(); endpoints.MapGrpcService(); + endpoints.MapGrpcService(); endpoints.MapGet("/", async context => diff --git a/NucuCar.Sensors/Grpc/Responses.cs b/NucuCar.Sensors/Grpc/Responses.cs index a85d5f7..a28a88a 100644 --- a/NucuCar.Sensors/Grpc/Responses.cs +++ b/NucuCar.Sensors/Grpc/Responses.cs @@ -6,7 +6,7 @@ namespace NucuCar.Sensors.Grpc { public static NucuCarSensorResponse GrpcIsDisabledResponse = new NucuCarSensorResponse() { - State = SensorStateEnum.GrpcDisabled, + State = SensorStateEnum.Disabled, JsonData = "{}" }; } diff --git a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs index 74ccd1a..976efbe 100644 --- a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs +++ b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs @@ -2,6 +2,6 @@ { public class HeartbeatConfig : BaseSensorConfig { - public bool Grpc { get; } = false; + public new bool Grpc { get; } = false; } } \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs b/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs index 808736e..5eda822 100644 --- a/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs +++ b/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs @@ -1,7 +1,36 @@ -namespace NucuCar.Sensors.Modules.PMS5003 +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 + public class Pms5003GrpcService : Pms5003SensorGrpcService.Pms5003SensorGrpcServiceBase { - + private readonly ILogger _logger; + private readonly IOptions _options; + private readonly ISensor _pms5003Sensor; + + public Pms5003GrpcService(ILogger logger, ISensor pms5003Sensor, IOptions options) + { + _pms5003Sensor = pms5003Sensor; + _logger = logger; + _options = options; + } + + public override async Task 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); + } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs b/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs index b7e7c6a..5e5cb83 100644 --- a/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs +++ b/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs @@ -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(), diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index 8ec70c7..ce283c3 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -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(hostContext.Configuration.GetSection("EnvironmentSensor")); services.Configure(hostContext.Configuration.GetSection("HealthSensor")); services.Configure(hostContext.Configuration.GetSection("HeartbeatSensor")); + services.Configure(hostContext.Configuration.GetSection("Pms5003Sensor")); // Singletons services.AddSingleton(); services.AddSingleton, Bme680Sensor>(); services.AddSingleton, CpuTempSensor>(); services.AddSingleton, HeartbeatSensor>(); + services.AddSingleton, Pms5003Sensor>(); // Workers // Telemetry @@ -39,6 +42,7 @@ namespace NucuCar.Sensors services.AddHostedService(); services.AddHostedService(); services.AddHostedService(); + services.AddHostedService(); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); } diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index 09fb4b7..94e7019 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -20,6 +20,11 @@ "Telemetry": true, "Grpc": false }, + "Pms5003Sensor": { + "Enabled": false, + "Telemetry": true, + "Grpc": true + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/Bme680GrpcServiceTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/Bme680GrpcServiceTest.cs index 145b3cf..42a0b63 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/Bme680GrpcServiceTest.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/Bme680GrpcServiceTest.cs @@ -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); } } } \ No newline at end of file