Refactor NucuCar.Sensors.EnvironmentSensor classes
This commit is contained in:
parent
bf9332b81b
commit
0f4a9089b8
3 changed files with 19 additions and 22 deletions
|
@ -31,8 +31,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
using var sensor = Sensor.Instance;
|
using var sensor = Bme680Sensor.Instance;
|
||||||
sensor.SetLogger(_logger);
|
sensor.Logger = _logger;
|
||||||
sensor.InitializeSensor();
|
sensor.InitializeSensor();
|
||||||
if (_telemetryEnabled)
|
if (_telemetryEnabled)
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,9 +10,9 @@ using NucuCarSensorsProto;
|
||||||
|
|
||||||
namespace NucuCar.Sensors.EnvironmentSensor
|
namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
{
|
{
|
||||||
public class Sensor : IDisposable, ITelemeter
|
public class Bme680Sensor : IDisposable, ITelemeter
|
||||||
{
|
{
|
||||||
private ILogger _logger;
|
public ILogger Logger;
|
||||||
private I2cConnectionSettings _i2CSettings;
|
private I2cConnectionSettings _i2CSettings;
|
||||||
private I2cDevice _i2CDevice;
|
private I2cDevice _i2CDevice;
|
||||||
private Bme680 _bme680;
|
private Bme680 _bme680;
|
||||||
|
@ -20,13 +20,13 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
private SensorStateEnum _sensorStateEnum;
|
private SensorStateEnum _sensorStateEnum;
|
||||||
|
|
||||||
/* Singleton Instance */
|
/* 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;
|
_sensorStateEnum = SensorStateEnum.Uninitialized;
|
||||||
}
|
}
|
||||||
|
@ -46,11 +46,6 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
_bme680?.Dispose();
|
_bme680?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetLogger(ILogger logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void InitializeSensor()
|
internal void InitializeSensor()
|
||||||
{
|
{
|
||||||
if (_sensorStateEnum == SensorStateEnum.Initialized)
|
if (_sensorStateEnum == SensorStateEnum.Initialized)
|
||||||
|
@ -73,12 +68,12 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
_bme680.SetPressureSampling(Sampling.UltraLowPower);
|
_bme680.SetPressureSampling(Sampling.UltraLowPower);
|
||||||
_sensorStateEnum = SensorStateEnum.Initialized;
|
_sensorStateEnum = SensorStateEnum.Initialized;
|
||||||
|
|
||||||
_logger.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK.");
|
Logger?.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK.");
|
||||||
}
|
}
|
||||||
catch (System.IO.IOException e)
|
catch (System.IO.IOException e)
|
||||||
{
|
{
|
||||||
_logger.LogError($"{DateTimeOffset.Now}:BME680 Sensor initialization FAIL.");
|
Logger?.LogError($"{DateTimeOffset.Now}:BME680 Sensor initialization FAIL.");
|
||||||
_logger.LogTrace(e.Message);
|
Logger?.LogTrace(e.Message);
|
||||||
_sensorStateEnum = SensorStateEnum.Error;
|
_sensorStateEnum = SensorStateEnum.Error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,7 +82,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
{
|
{
|
||||||
if (_sensorStateEnum != SensorStateEnum.Initialized)
|
if (_sensorStateEnum != SensorStateEnum.Initialized)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(
|
Logger?.LogWarning(
|
||||||
$"{DateTimeOffset.Now}:BME680: Attempting to take measurement while sensor is not initialized!");
|
$"{DateTimeOffset.Now}:BME680: Attempting to take measurement while sensor is not initialized!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -99,8 +94,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
_lastMeasurement.Pressure = await _bme680.ReadPressureAsync();
|
_lastMeasurement.Pressure = await _bme680.ReadPressureAsync();
|
||||||
_lastMeasurement.Humidity = await _bme680.ReadHumidityAsync();
|
_lastMeasurement.Humidity = await _bme680.ReadHumidityAsync();
|
||||||
|
|
||||||
_logger.LogInformation($"{DateTimeOffset.Now}:BME680: reading");
|
Logger?.LogInformation($"{DateTimeOffset.Now}:BME680: reading");
|
||||||
_logger.LogInformation(
|
Logger?.LogInformation(
|
||||||
$"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH");
|
$"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH");
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,25 +9,27 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
public class GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase
|
public class GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase
|
||||||
{
|
{
|
||||||
private readonly ILogger<GrpcService> _logger;
|
private readonly ILogger<GrpcService> _logger;
|
||||||
private readonly Sensor _sensor;
|
private readonly Bme680Sensor _bme680Sensor;
|
||||||
|
|
||||||
public GrpcService(ILogger<GrpcService> logger)
|
public GrpcService(ILogger<GrpcService> logger)
|
||||||
{
|
{
|
||||||
_sensor = Sensor.Instance;
|
_bme680Sensor = Bme680Sensor.Instance;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<NucuCarSensorState> GetSensorState(Empty request, ServerCallContext context)
|
public override Task<NucuCarSensorState> GetSensorState(Empty request, ServerCallContext context)
|
||||||
{
|
{
|
||||||
|
_logger?.LogDebug($"Calling {nameof(GetSensorState)}.");
|
||||||
return Task.FromResult(new NucuCarSensorState()
|
return Task.FromResult(new NucuCarSensorState()
|
||||||
{
|
{
|
||||||
State = _sensor.GetState()
|
State = _bme680Sensor.GetState()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<EnvironmentSensorMeasurement> GetSensorMeasurement(Empty request, ServerCallContext context)
|
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);
|
return Task.FromResult(sensorMeasurement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue