Implement CPU temperature sensor

This commit is contained in:
Denis-Cosmin Nutiu 2019-12-29 13:58:32 +02:00
parent 80db516f2c
commit bcc0588cc5
3 changed files with 90 additions and 9 deletions

View file

@ -11,7 +11,7 @@ namespace NucuCar.Domain.Sensors
{ {
protected bool TelemetryEnabled; protected bool TelemetryEnabled;
protected ILogger Logger; protected ILogger Logger;
protected SensorStateEnum SensorStateEnum; protected SensorStateEnum CurrentState;
public abstract void Initialize(); public abstract void Initialize();
public abstract Task TakeMeasurementAsync(); public abstract Task TakeMeasurementAsync();

View file

@ -37,12 +37,12 @@ namespace NucuCar.Sensors.Environment
public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options) public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options)
{ {
SensorStateEnum = SensorStateEnum.Uninitialized; CurrentState = SensorStateEnum.Uninitialized;
Logger = logger; Logger = logger;
if (!options.Value.Enabled) if (!options.Value.Enabled)
{ {
Logger?.LogInformation("BME680 Sensor is disabled!"); Logger?.LogInformation("BME680 Sensor is disabled!");
SensorStateEnum = SensorStateEnum.Disabled; CurrentState = SensorStateEnum.Disabled;
} }
TelemetryEnabled = options.Value.Telemetry; TelemetryEnabled = options.Value.Telemetry;
@ -62,7 +62,7 @@ namespace NucuCar.Sensors.Environment
public override SensorStateEnum GetState() public override SensorStateEnum GetState()
{ {
return SensorStateEnum; return CurrentState;
} }
public void Dispose() public void Dispose()
@ -72,7 +72,7 @@ namespace NucuCar.Sensors.Environment
public override void Initialize() public override void Initialize()
{ {
if (SensorStateEnum == SensorStateEnum.Initialized || SensorStateEnum == SensorStateEnum.Disabled) if (CurrentState == SensorStateEnum.Initialized || CurrentState == SensorStateEnum.Disabled)
{ {
return; return;
} }
@ -91,7 +91,7 @@ namespace NucuCar.Sensors.Environment
_bme680.SetHumiditySampling(Sampling.UltraLowPower); _bme680.SetHumiditySampling(Sampling.UltraLowPower);
_bme680.SetTemperatureSampling(Sampling.UltraHighResolution); _bme680.SetTemperatureSampling(Sampling.UltraHighResolution);
_bme680.SetPressureSampling(Sampling.UltraLowPower); _bme680.SetPressureSampling(Sampling.UltraLowPower);
SensorStateEnum = SensorStateEnum.Initialized; CurrentState = SensorStateEnum.Initialized;
Logger?.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK."); Logger?.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK.");
} }
@ -99,13 +99,13 @@ namespace NucuCar.Sensors.Environment
{ {
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; CurrentState = SensorStateEnum.Error;
} }
} }
public override async Task TakeMeasurementAsync() public override async Task TakeMeasurementAsync()
{ {
if (SensorStateEnum != SensorStateEnum.Initialized) if (CurrentState != SensorStateEnum.Initialized)
{ {
throw new InvalidOperationException("Can't take measurement on uninitialized sensor!"); throw new InvalidOperationException("Can't take measurement on uninitialized sensor!");
} }
@ -138,7 +138,7 @@ namespace NucuCar.Sensors.Environment
{ {
returnValue = new Dictionary<string, object> returnValue = new Dictionary<string, object>
{ {
["sensor_state"] = SensorStateEnum, ["sensor_state"] = CurrentState,
["temperature"] = _lastMeasurement.Temperature, ["temperature"] = _lastMeasurement.Temperature,
["humidity"] = _lastMeasurement.Humidity, ["humidity"] = _lastMeasurement.Humidity,
["pressure"] = _lastMeasurement.Pressure, ["pressure"] = _lastMeasurement.Pressure,

View file

@ -0,0 +1,81 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Iot.Device.CpuTemperature;
using Newtonsoft.Json;
using NucuCar.Domain.Sensors;
using NucuCarSensorsProto;
namespace NucuCar.Sensors.Health
{
public class CpuTemp : GenericTelemeterSensor, ISensor<CpuTemp>
{
private readonly CpuTemperature _cpuTemperature;
private double _lastTemperatureCelsius;
public CpuTemp()
{
_cpuTemperature = new CpuTemperature();
Object = this;
}
public override void Initialize()
{
CurrentState = SensorStateEnum.Initialized;
}
public override Task TakeMeasurementAsync()
{
if (CurrentState == SensorStateEnum.Initialized)
{
_lastTemperatureCelsius = _cpuTemperature.Temperature.Celsius;
if (double.IsNaN(_lastTemperatureCelsius))
{
CurrentState = SensorStateEnum.Error;
_lastTemperatureCelsius = double.NaN;
}
}
return Task.FromResult(_lastTemperatureCelsius);
}
public override NucuCarSensorResponse GetMeasurement()
{
var jsonResponse = JsonConvert.SerializeObject(new Dictionary<string, object>
{
["cpu_temperature"] = _lastTemperatureCelsius,
});
return new NucuCarSensorResponse()
{
State = CurrentState,
JsonData = jsonResponse
};
}
public override SensorStateEnum GetState()
{
return CurrentState;
}
public override string GetIdentifier()
{
return "CpuTemperature";
}
public override Dictionary<string, object> GetTelemetryData()
{
Dictionary<string, object> returnValue = null;
if (!double.IsNaN(_lastTemperatureCelsius) && TelemetryEnabled)
{
returnValue = new Dictionary<string, object>
{
["sensor_state"] = CurrentState,
["cpu_temperature"] = _lastTemperatureCelsius,
};
}
return returnValue;
}
public CpuTemp Object { get; }
}
}