Refactor NucuCar.Sensors.EnvironmentSensor classes

This commit is contained in:
Denis-Cosmin Nutiu 2019-11-23 23:06:01 +02:00
parent bf9332b81b
commit 0f4a9089b8
3 changed files with 19 additions and 22 deletions

View file

@ -31,8 +31,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
return;
}
using var sensor = Sensor.Instance;
sensor.SetLogger(_logger);
using var sensor = Bme680Sensor.Instance;
sensor.Logger = _logger;
sensor.InitializeSensor();
if (_telemetryEnabled)
{

View file

@ -10,9 +10,9 @@ using NucuCarSensorsProto;
namespace NucuCar.Sensors.EnvironmentSensor
{
public class Sensor : IDisposable, ITelemeter
public class Bme680Sensor : IDisposable, ITelemeter
{
private ILogger _logger;
public ILogger Logger;
private I2cConnectionSettings _i2CSettings;
private I2cDevice _i2CDevice;
private Bme680 _bme680;
@ -20,13 +20,13 @@ namespace NucuCar.Sensors.EnvironmentSensor
private SensorStateEnum _sensorStateEnum;
/* Singleton Instance */
public static Sensor Instance { get; } = new Sensor();
public static Bme680Sensor Instance { get; } = new Bme680Sensor();
static Sensor()
static Bme680Sensor()
{
}
private Sensor()
private Bme680Sensor()
{
_sensorStateEnum = SensorStateEnum.Uninitialized;
}
@ -46,11 +46,6 @@ namespace NucuCar.Sensors.EnvironmentSensor
_bme680?.Dispose();
}
internal void SetLogger(ILogger logger)
{
_logger = logger;
}
internal void InitializeSensor()
{
if (_sensorStateEnum == SensorStateEnum.Initialized)
@ -73,12 +68,12 @@ namespace NucuCar.Sensors.EnvironmentSensor
_bme680.SetPressureSampling(Sampling.UltraLowPower);
_sensorStateEnum = SensorStateEnum.Initialized;
_logger.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK.");
Logger?.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK.");
}
catch (System.IO.IOException e)
{
_logger.LogError($"{DateTimeOffset.Now}:BME680 Sensor initialization FAIL.");
_logger.LogTrace(e.Message);
Logger?.LogError($"{DateTimeOffset.Now}:BME680 Sensor initialization FAIL.");
Logger?.LogTrace(e.Message);
_sensorStateEnum = SensorStateEnum.Error;
}
}
@ -87,7 +82,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
{
if (_sensorStateEnum != SensorStateEnum.Initialized)
{
_logger.LogWarning(
Logger?.LogWarning(
$"{DateTimeOffset.Now}:BME680: Attempting to take measurement while sensor is not initialized!");
return;
}
@ -99,8 +94,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
_lastMeasurement.Pressure = await _bme680.ReadPressureAsync();
_lastMeasurement.Humidity = await _bme680.ReadHumidityAsync();
_logger.LogInformation($"{DateTimeOffset.Now}:BME680: reading");
_logger.LogInformation(
Logger?.LogInformation($"{DateTimeOffset.Now}:BME680: reading");
Logger?.LogInformation(
$"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH");
}

View file

@ -9,25 +9,27 @@ namespace NucuCar.Sensors.EnvironmentSensor
public class GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase
{
private readonly ILogger<GrpcService> _logger;
private readonly Sensor _sensor;
private readonly Bme680Sensor _bme680Sensor;
public GrpcService(ILogger<GrpcService> logger)
{
_sensor = Sensor.Instance;
_bme680Sensor = Bme680Sensor.Instance;
_logger = logger;
}
public override Task<NucuCarSensorState> GetSensorState(Empty request, ServerCallContext context)
{
_logger?.LogDebug($"Calling {nameof(GetSensorState)}.");
return Task.FromResult(new NucuCarSensorState()
{
State = _sensor.GetState()
State = _bme680Sensor.GetState()
});
}
public override Task<EnvironmentSensorMeasurement> GetSensorMeasurement(Empty request, ServerCallContext context)
{
var sensorMeasurement = _sensor.GetMeasurement();
_logger?.LogDebug($"Calling {nameof(GetSensorMeasurement)}.");
var sensorMeasurement = _bme680Sensor.GetMeasurement();
return Task.FromResult(sensorMeasurement);
}
}