Generate SensorMeasurement and State using gRPC proto

This commit is contained in:
Denis-Cosmin Nutiu 2019-11-11 12:59:07 +02:00
parent 908c0d763d
commit a32e59f80c
9 changed files with 36 additions and 62 deletions

View file

@ -1,16 +1,18 @@
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 NucuCarGrpcSensors;
namespace NucuCar.Sensors.EnvironmentSensor namespace NucuCar.Sensors.EnvironmentSensor
{ {
public class Worker : BackgroundService public class BackgroundWorker : BackgroundService
{ {
private readonly ILogger<Worker> _logger; private readonly ILogger<BackgroundWorker> _logger;
public Worker(ILogger<Worker> logger) public BackgroundWorker(ILogger<BackgroundWorker> logger, IConfiguration config)
{ {
_logger = logger; _logger = logger;
} }

View file

@ -6,12 +6,12 @@ using NucuCarGrpcSensors;
namespace NucuCar.Sensors.EnvironmentSensor namespace NucuCar.Sensors.EnvironmentSensor
{ {
public class Service : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase public class GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase
{ {
private readonly ILogger<Service> _logger; private readonly ILogger<GrpcService> _logger;
private readonly Sensor _sensor; private readonly Sensor _sensor;
public Service(ILogger<Service> logger) public GrpcService(ILogger<GrpcService> logger)
{ {
_sensor = Sensor.Instance; _sensor = Sensor.Instance;
_logger = logger; _logger = logger;
@ -21,19 +21,14 @@ namespace NucuCar.Sensors.EnvironmentSensor
{ {
return Task.FromResult(new NucuCarSensorState() return Task.FromResult(new NucuCarSensorState()
{ {
State = (int) _sensor.GetState() State = _sensor.GetState()
}); });
} }
public override Task<EnvironmentSensorMeasurement> GetSensorMeasurement(Empty request, ServerCallContext context) public override Task<EnvironmentSensorMeasurement> GetSensorMeasurement(Empty request, ServerCallContext context)
{ {
var sensorMeasurement = _sensor.GetMeasurement(); var sensorMeasurement = _sensor.GetMeasurement();
return Task.FromResult(new EnvironmentSensorMeasurement() return Task.FromResult(sensorMeasurement);
{
Temperature = sensorMeasurement.Temperature.Celsius,
Humidity = sensorMeasurement.Humidity,
Pressure = sensorMeasurement.Pressure
});
} }
} }
} }

View file

@ -1,23 +0,0 @@
using Iot.Units;
namespace NucuCar.Sensors.EnvironmentSensor
{
public struct Measurement
{
public Temperature Temperature { get; private set; }
public double Pressure { get; private set; }
public double Humidity { get; private set; }
public Measurement(Temperature temperature, double pressure, double humidity) : this()
{
SetMeasurement(temperature, pressure, humidity);
}
public void SetMeasurement(Temperature temperature, double pressure, double humidity)
{
Temperature = temperature;
Pressure = pressure;
Humidity = humidity;
}
}
}

View file

@ -4,6 +4,7 @@ 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 NucuCarGrpcSensors;
namespace NucuCar.Sensors.EnvironmentSensor namespace NucuCar.Sensors.EnvironmentSensor
{ {
@ -13,7 +14,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
private I2cConnectionSettings _i2CSettings; private I2cConnectionSettings _i2CSettings;
private I2cDevice _i2CDevice; private I2cDevice _i2CDevice;
private Bme680 _bme680; private Bme680 _bme680;
private Measurement _measurement; private EnvironmentSensorMeasurement _lastMeasurement;
private SensorStateEnum _sensorStateEnum; private SensorStateEnum _sensorStateEnum;
/* Singleton Instance */ /* Singleton Instance */
@ -28,9 +29,9 @@ namespace NucuCar.Sensors.EnvironmentSensor
_sensorStateEnum = SensorStateEnum.Uninitialized; _sensorStateEnum = SensorStateEnum.Uninitialized;
} }
public Measurement GetMeasurement() public EnvironmentSensorMeasurement GetMeasurement()
{ {
return _measurement; return _lastMeasurement;
} }
public SensorStateEnum GetState() public SensorStateEnum GetState()
@ -63,7 +64,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
_bme680 = new Bme680(_i2CDevice); _bme680 = new Bme680(_i2CDevice);
/* Initialize measurement */ /* Initialize measurement */
_measurement = new Measurement(); _lastMeasurement = new EnvironmentSensorMeasurement();
_bme680.Reset(); _bme680.Reset();
_bme680.SetHumiditySampling(Sampling.UltraLowPower); _bme680.SetHumiditySampling(Sampling.UltraLowPower);
_bme680.SetTemperatureSampling(Sampling.UltraHighResolution); _bme680.SetTemperatureSampling(Sampling.UltraHighResolution);
@ -92,14 +93,13 @@ namespace NucuCar.Sensors.EnvironmentSensor
/* Force the sensor to take a measurement. */ /* Force the sensor to take a measurement. */
_bme680.SetPowerMode(Bme680PowerMode.Forced); _bme680.SetPowerMode(Bme680PowerMode.Forced);
var temperature = await _bme680.ReadTemperatureAsync(); _lastMeasurement.Temperature = (await _bme680.ReadTemperatureAsync()).Celsius;
var pressure = await _bme680.ReadPressureAsync(); _lastMeasurement.Pressure = await _bme680.ReadPressureAsync();
var humidity = await _bme680.ReadHumidityAsync(); _lastMeasurement.Humidity = await _bme680.ReadHumidityAsync();
_measurement.SetMeasurement(temperature, pressure, humidity);
_logger.LogInformation($"{DateTimeOffset.Now}:BME680: reading"); _logger.LogInformation($"{DateTimeOffset.Now}:BME680: reading");
_logger.LogInformation( _logger.LogInformation(
$"{temperature.Celsius:N2} \u00B0C | {pressure} hPa | {humidity:N2} %rH"); $"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH");
} }
} }
} }

View file

@ -28,7 +28,7 @@ namespace NucuCar.Sensors
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapGrpcService<EnvironmentSensor.Service>(); endpoints.MapGrpcService<EnvironmentSensor.GrpcService>();
endpoints.MapGet("/", endpoints.MapGet("/",
async context => async context =>

View file

@ -14,7 +14,7 @@ namespace NucuCar.Sensors
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); }) .ConfigureServices((hostContext, services) => { services.AddHostedService<BackgroundWorker>(); })
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<GrpcStartup>(); }); .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<GrpcStartup>(); });
} }
} }

View file

@ -3,18 +3,27 @@ import "google/protobuf/empty.proto";
package NucuCarGrpcSensors; package NucuCarGrpcSensors;
// General
enum SensorStateEnum {
Error = 0;
Uninitialized = 1;
Initialized = 2;
}
// Environment Sensor
service EnvironmentSensorGrpcService { service EnvironmentSensorGrpcService {
rpc GetSensorState(google.protobuf.Empty) returns (NucuCarSensorState) {} rpc GetSensorState(google.protobuf.Empty) returns (NucuCarSensorState) {}
rpc GetSensorMeasurement(google.protobuf.Empty) returns (EnvironmentSensorMeasurement) {} rpc GetSensorMeasurement(google.protobuf.Empty) returns (EnvironmentSensorMeasurement) {}
} }
message EnvironmentSensorMeasurement { message EnvironmentSensorMeasurement {
double temperature = 1; double Temperature = 1;
double pressure = 2; double Pressure = 2;
double humidity = 3; double Humidity = 3;
double voc = 4; double VolatileOrganicCompound = 4;
} }
message NucuCarSensorState { message NucuCarSensorState {
int32 state = 1; SensorStateEnum state = 1;
} }

View file

@ -1,9 +0,0 @@
namespace NucuCar.Sensors
{
public enum SensorStateEnum
{
Error,
Uninitialized,
Initialized
}
}

View file

@ -37,7 +37,7 @@ namespace NucuCar.TestClient
new GrpcChannelOptions {HttpClient = _httpClient}); new GrpcChannelOptions {HttpClient = _httpClient});
var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel); var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
var reply = await client.GetSensorStateAsync(new Empty()); var reply = await client.GetSensorStateAsync(new Empty());
var state = (SensorStateEnum) reply.State; var state = reply.State;
Console.WriteLine("EnviromentSensorState: " + state); Console.WriteLine("EnviromentSensorState: " + state);
if (state == SensorStateEnum.Initialized) if (state == SensorStateEnum.Initialized)
{ {