2020-01-24 12:28:48 +00:00
|
|
|
using System;
|
2019-12-29 11:58:32 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Iot.Device.CpuTemperature;
|
2020-01-24 12:28:48 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-12-29 12:08:18 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2019-12-29 11:58:32 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using NucuCar.Domain.Sensors;
|
|
|
|
using NucuCarSensorsProto;
|
|
|
|
|
|
|
|
namespace NucuCar.Sensors.Health
|
|
|
|
{
|
2019-12-29 12:08:18 +00:00
|
|
|
public class CpuTempSensor : GenericTelemeterSensor, ISensor<CpuTempSensor>
|
2019-12-29 11:58:32 +00:00
|
|
|
{
|
|
|
|
private readonly CpuTemperature _cpuTemperature;
|
|
|
|
private double _lastTemperatureCelsius;
|
|
|
|
|
2019-12-29 12:08:18 +00:00
|
|
|
public CpuTempSensor()
|
2019-12-29 11:58:32 +00:00
|
|
|
{
|
2019-12-29 12:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public CpuTempSensor(IOptions<CpuTempConfig> options)
|
|
|
|
{
|
|
|
|
if (options.Value.Enabled)
|
|
|
|
{
|
|
|
|
CurrentState = SensorStateEnum.Uninitialized;
|
|
|
|
_cpuTemperature = new CpuTemperature();
|
|
|
|
Object = this;
|
|
|
|
TelemetryEnabled = options.Value.Telemetry;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CurrentState = SensorStateEnum.Disabled;
|
|
|
|
}
|
2019-12-29 11:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Initialize()
|
2019-12-29 12:08:18 +00:00
|
|
|
{
|
|
|
|
if (CurrentState == SensorStateEnum.Initialized || CurrentState == SensorStateEnum.Disabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-12-29 13:56:38 +00:00
|
|
|
CurrentState = _cpuTemperature.IsAvailable ? SensorStateEnum.Initialized : SensorStateEnum.Error;
|
2019-12-29 11:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override Task TakeMeasurementAsync()
|
|
|
|
{
|
|
|
|
if (CurrentState == SensorStateEnum.Initialized)
|
|
|
|
{
|
|
|
|
_lastTemperatureCelsius = _cpuTemperature.Temperature.Celsius;
|
|
|
|
if (double.IsNaN(_lastTemperatureCelsius))
|
|
|
|
{
|
|
|
|
CurrentState = SensorStateEnum.Error;
|
|
|
|
_lastTemperatureCelsius = double.NaN;
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 12:28:48 +00:00
|
|
|
Logger?.LogDebug($"{DateTimeOffset.Now}:HealthSensor: reading");
|
|
|
|
Logger.LogInformation($"CPU Temperature ${_lastTemperatureCelsius}");
|
2019-12-29 11:58:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:08:18 +00:00
|
|
|
public CpuTempSensor Object { get; }
|
2019-12-29 11:58:32 +00:00
|
|
|
}
|
|
|
|
}
|