2019-11-09 14:54:06 +00:00
|
|
|
using System;
|
2019-11-14 14:45:32 +00:00
|
|
|
using System.Collections.Generic;
|
2019-11-09 14:54:06 +00:00
|
|
|
using System.Device.I2c;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Iot.Device.Bmxx80;
|
|
|
|
using Iot.Device.Bmxx80.PowerMode;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2019-11-26 18:24:27 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2019-11-17 16:27:58 +00:00
|
|
|
using NucuCar.Domain.Telemetry;
|
2019-11-11 11:28:52 +00:00
|
|
|
using NucuCarSensorsProto;
|
2019-11-09 14:54:06 +00:00
|
|
|
|
2019-11-09 17:23:07 +00:00
|
|
|
namespace NucuCar.Sensors.EnvironmentSensor
|
2019-11-09 14:54:06 +00:00
|
|
|
{
|
2019-11-24 13:12:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Abstraction for the BME680 sensor.
|
|
|
|
/// See: https://www.bosch-sensortec.com/bst/products/all_products/bme680
|
|
|
|
/// </summary>
|
2019-11-23 21:06:01 +00:00
|
|
|
public class Bme680Sensor : IDisposable, ITelemeter
|
2019-11-09 14:54:06 +00:00
|
|
|
{
|
2019-11-24 16:03:46 +00:00
|
|
|
private readonly ILogger _logger;
|
2019-11-09 14:54:06 +00:00
|
|
|
private I2cConnectionSettings _i2CSettings;
|
|
|
|
private I2cDevice _i2CDevice;
|
|
|
|
private Bme680 _bme680;
|
2019-11-11 10:59:07 +00:00
|
|
|
private EnvironmentSensorMeasurement _lastMeasurement;
|
2019-11-10 14:02:50 +00:00
|
|
|
private SensorStateEnum _sensorStateEnum;
|
2019-11-09 14:54:06 +00:00
|
|
|
|
2019-11-30 15:19:56 +00:00
|
|
|
public Bme680Sensor()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-26 18:24:27 +00:00
|
|
|
public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options)
|
2019-11-10 12:38:40 +00:00
|
|
|
{
|
2019-11-10 14:02:50 +00:00
|
|
|
_sensorStateEnum = SensorStateEnum.Uninitialized;
|
2019-11-24 16:03:46 +00:00
|
|
|
_logger = logger;
|
2019-11-26 18:24:27 +00:00
|
|
|
if (options.Value.ServiceEnabled)
|
2019-11-24 16:03:46 +00:00
|
|
|
{
|
|
|
|
InitializeSensor();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger?.LogInformation("BME680 Sensor is disabled!");
|
|
|
|
_sensorStateEnum = SensorStateEnum.Disabled;
|
|
|
|
}
|
2019-11-10 12:38:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 15:19:56 +00:00
|
|
|
// TODO Make more generic, Add interface and remove virtual
|
|
|
|
public virtual EnvironmentSensorMeasurement GetMeasurement()
|
2019-11-10 12:38:40 +00:00
|
|
|
{
|
2019-11-11 10:59:07 +00:00
|
|
|
return _lastMeasurement;
|
2019-11-10 12:38:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 15:19:56 +00:00
|
|
|
// TODO: Add interface and remove virtual
|
|
|
|
public virtual SensorStateEnum GetState()
|
2019-11-10 12:38:40 +00:00
|
|
|
{
|
2019-11-10 14:02:50 +00:00
|
|
|
return _sensorStateEnum;
|
2019-11-10 12:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
_bme680?.Dispose();
|
|
|
|
}
|
|
|
|
|
2019-11-30 15:19:56 +00:00
|
|
|
public void InitializeSensor()
|
2019-11-09 14:54:06 +00:00
|
|
|
{
|
2019-11-10 14:02:50 +00:00
|
|
|
if (_sensorStateEnum == SensorStateEnum.Initialized)
|
2019-11-10 12:38:40 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-09 14:54:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
/* Connect to default i2c address 0x76 */
|
|
|
|
_i2CSettings = new I2cConnectionSettings(1, Bme680.DefaultI2cAddress);
|
|
|
|
_i2CDevice = I2cDevice.Create(_i2CSettings);
|
|
|
|
_bme680 = new Bme680(_i2CDevice);
|
2019-11-09 17:37:28 +00:00
|
|
|
|
2019-11-09 14:54:06 +00:00
|
|
|
/* Initialize measurement */
|
2019-11-11 10:59:07 +00:00
|
|
|
_lastMeasurement = new EnvironmentSensorMeasurement();
|
2019-11-09 17:37:28 +00:00
|
|
|
_bme680.Reset();
|
|
|
|
_bme680.SetHumiditySampling(Sampling.UltraLowPower);
|
|
|
|
_bme680.SetTemperatureSampling(Sampling.UltraHighResolution);
|
|
|
|
_bme680.SetPressureSampling(Sampling.UltraLowPower);
|
2019-11-10 14:02:50 +00:00
|
|
|
_sensorStateEnum = SensorStateEnum.Initialized;
|
2019-11-09 17:37:28 +00:00
|
|
|
|
2019-11-24 16:03:46 +00:00
|
|
|
_logger?.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK.");
|
2019-11-09 14:54:06 +00:00
|
|
|
}
|
|
|
|
catch (System.IO.IOException e)
|
|
|
|
{
|
2019-11-24 16:03:46 +00:00
|
|
|
_logger?.LogError($"{DateTimeOffset.Now}:BME680 Sensor initialization FAIL.");
|
|
|
|
_logger?.LogTrace(e.Message);
|
2019-11-10 14:02:50 +00:00
|
|
|
_sensorStateEnum = SensorStateEnum.Error;
|
2019-11-09 14:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-30 15:19:56 +00:00
|
|
|
|
|
|
|
public async Task TakeMeasurement()
|
2019-11-09 14:54:06 +00:00
|
|
|
{
|
2019-11-10 14:02:50 +00:00
|
|
|
if (_sensorStateEnum != SensorStateEnum.Initialized)
|
2019-11-09 14:54:06 +00:00
|
|
|
{
|
2019-11-24 16:03:46 +00:00
|
|
|
_logger?.LogWarning(
|
2019-11-09 17:37:28 +00:00
|
|
|
$"{DateTimeOffset.Now}:BME680: Attempting to take measurement while sensor is not initialized!");
|
2019-11-09 14:54:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-09 17:37:28 +00:00
|
|
|
|
2019-11-09 14:54:06 +00:00
|
|
|
/* Force the sensor to take a measurement. */
|
|
|
|
_bme680.SetPowerMode(Bme680PowerMode.Forced);
|
2019-11-09 17:37:28 +00:00
|
|
|
|
2019-11-11 10:59:07 +00:00
|
|
|
_lastMeasurement.Temperature = (await _bme680.ReadTemperatureAsync()).Celsius;
|
|
|
|
_lastMeasurement.Pressure = await _bme680.ReadPressureAsync();
|
|
|
|
_lastMeasurement.Humidity = await _bme680.ReadHumidityAsync();
|
2019-11-09 17:37:28 +00:00
|
|
|
|
2019-11-24 16:03:46 +00:00
|
|
|
_logger?.LogInformation($"{DateTimeOffset.Now}:BME680: reading");
|
|
|
|
_logger?.LogInformation(
|
2019-11-11 10:59:07 +00:00
|
|
|
$"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH");
|
2019-11-09 14:54:06 +00:00
|
|
|
}
|
2019-11-14 14:45:32 +00:00
|
|
|
|
2019-11-15 15:53:20 +00:00
|
|
|
public string GetIdentifier()
|
|
|
|
{
|
|
|
|
return nameof(EnvironmentSensor);
|
|
|
|
}
|
|
|
|
|
2019-11-17 13:29:33 +00:00
|
|
|
public Dictionary<string, object> GetTelemetryData()
|
2019-11-14 14:45:32 +00:00
|
|
|
{
|
2019-11-17 13:29:33 +00:00
|
|
|
Dictionary<string, object> returnValue = null;
|
2019-11-15 15:53:20 +00:00
|
|
|
if (_lastMeasurement != null)
|
2019-11-14 14:45:32 +00:00
|
|
|
{
|
2019-11-17 13:29:33 +00:00
|
|
|
returnValue = new Dictionary<string, object>
|
2019-11-15 15:53:20 +00:00
|
|
|
{
|
|
|
|
["temperature"] = _lastMeasurement.Temperature,
|
|
|
|
["humidity"] = _lastMeasurement.Humidity,
|
|
|
|
["pressure"] = _lastMeasurement.Pressure,
|
|
|
|
["voc"] = _lastMeasurement.VolatileOrganicCompound
|
|
|
|
};
|
|
|
|
}
|
2019-11-24 13:12:12 +00:00
|
|
|
|
2019-11-15 15:53:20 +00:00
|
|
|
return returnValue;
|
2019-11-14 14:45:32 +00:00
|
|
|
}
|
2019-11-09 14:54:06 +00:00
|
|
|
}
|
|
|
|
}
|