diff --git a/NucuCar.Domain/Sensors/GenericSensor.cs b/NucuCar.Domain/Sensors/GenericSensor.cs index c12cc91..b1bdb1f 100644 --- a/NucuCar.Domain/Sensors/GenericSensor.cs +++ b/NucuCar.Domain/Sensors/GenericSensor.cs @@ -11,7 +11,7 @@ namespace NucuCar.Domain.Sensors { protected bool TelemetryEnabled; protected ILogger Logger; - protected SensorStateEnum SensorStateEnum; + protected SensorStateEnum CurrentState; public abstract void Initialize(); public abstract Task TakeMeasurementAsync(); diff --git a/NucuCar.Sensors/Environment/Bme680Sensor.cs b/NucuCar.Sensors/Environment/Bme680Sensor.cs index c5a68a0..2fad57f 100644 --- a/NucuCar.Sensors/Environment/Bme680Sensor.cs +++ b/NucuCar.Sensors/Environment/Bme680Sensor.cs @@ -37,12 +37,12 @@ namespace NucuCar.Sensors.Environment public Bme680Sensor(ILogger logger, IOptions options) { - SensorStateEnum = SensorStateEnum.Uninitialized; + CurrentState = SensorStateEnum.Uninitialized; Logger = logger; if (!options.Value.Enabled) { Logger?.LogInformation("BME680 Sensor is disabled!"); - SensorStateEnum = SensorStateEnum.Disabled; + CurrentState = SensorStateEnum.Disabled; } TelemetryEnabled = options.Value.Telemetry; @@ -62,7 +62,7 @@ namespace NucuCar.Sensors.Environment public override SensorStateEnum GetState() { - return SensorStateEnum; + return CurrentState; } public void Dispose() @@ -72,7 +72,7 @@ namespace NucuCar.Sensors.Environment public override void Initialize() { - if (SensorStateEnum == SensorStateEnum.Initialized || SensorStateEnum == SensorStateEnum.Disabled) + if (CurrentState == SensorStateEnum.Initialized || CurrentState == SensorStateEnum.Disabled) { return; } @@ -91,7 +91,7 @@ namespace NucuCar.Sensors.Environment _bme680.SetHumiditySampling(Sampling.UltraLowPower); _bme680.SetTemperatureSampling(Sampling.UltraHighResolution); _bme680.SetPressureSampling(Sampling.UltraLowPower); - SensorStateEnum = SensorStateEnum.Initialized; + CurrentState = SensorStateEnum.Initialized; 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?.LogTrace(e.Message); - SensorStateEnum = SensorStateEnum.Error; + CurrentState = SensorStateEnum.Error; } } public override async Task TakeMeasurementAsync() { - if (SensorStateEnum != SensorStateEnum.Initialized) + if (CurrentState != SensorStateEnum.Initialized) { throw new InvalidOperationException("Can't take measurement on uninitialized sensor!"); } @@ -138,7 +138,7 @@ namespace NucuCar.Sensors.Environment { returnValue = new Dictionary { - ["sensor_state"] = SensorStateEnum, + ["sensor_state"] = CurrentState, ["temperature"] = _lastMeasurement.Temperature, ["humidity"] = _lastMeasurement.Humidity, ["pressure"] = _lastMeasurement.Pressure, diff --git a/NucuCar.Sensors/Health/CpuTemp.cs b/NucuCar.Sensors/Health/CpuTemp.cs new file mode 100644 index 0000000..1d1f493 --- /dev/null +++ b/NucuCar.Sensors/Health/CpuTemp.cs @@ -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 + { + 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 + { + ["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 GetTelemetryData() + { + Dictionary returnValue = null; + if (!double.IsNaN(_lastTemperatureCelsius) && TelemetryEnabled) + { + returnValue = new Dictionary + { + ["sensor_state"] = CurrentState, + ["cpu_temperature"] = _lastTemperatureCelsius, + }; + } + + return returnValue; + } + + public CpuTemp Object { get; } + } +} \ No newline at end of file