NucuCar/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs

48 lines
1.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using NucuCarSensorsProto;
namespace NucuCar.Sensors.EnvironmentSensor
{
2019-11-24 13:12:12 +00:00
/// <summary>
/// EnvironmentSensor's gRPC service.
/// It allows reading the sensor's data using remote procedure calls.
/// </summary>
public class Bme680GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase
{
private readonly ILogger<Bme680GrpcService> _logger;
2019-11-30 15:44:54 +00:00
private readonly ISensor<Bme680Sensor> _bme680Sensor;
2019-11-30 15:44:54 +00:00
public Bme680GrpcService(ILogger<Bme680GrpcService> logger, ISensor<Bme680Sensor> bme680Sensor)
{
_bme680Sensor = bme680Sensor;
_logger = logger;
}
public override Task<NucuCarSensorState> GetSensorState(Empty request, ServerCallContext context)
{
_logger?.LogDebug($"Calling {nameof(GetSensorState)}.");
return Task.FromResult(new NucuCarSensorState()
{
2019-11-30 15:44:54 +00:00
State = _bme680Sensor.Object.GetState()
});
}
2019-11-24 13:12:12 +00:00
public override Task<EnvironmentSensorMeasurement> GetSensorMeasurement(Empty request,
ServerCallContext context)
{
_logger?.LogDebug($"Calling {nameof(GetSensorMeasurement)}.");
2019-11-30 15:44:54 +00:00
var sensorMeasurement = _bme680Sensor.Object.GetMeasurement();
return Task.FromResult(new EnvironmentSensorMeasurement()
{
Temperature = sensorMeasurement.GetValueOrDefault("temperature", -1.0),
Humidity = sensorMeasurement.GetValueOrDefault("pressure", -1.0),
Pressure = sensorMeasurement.GetValueOrDefault("humidity",-1.0),
VolatileOrganicCompound = sensorMeasurement.GetValueOrDefault("voc", -1.0),
});
}
}
}