NUC-32: Refactor Pms5003Sensor code.

This commit is contained in:
Denis-Cosmin Nutiu 2021-04-25 23:33:12 +03:00
parent 62064f33b7
commit e99e67bd58
2 changed files with 19 additions and 4 deletions

View file

@ -15,7 +15,6 @@ namespace NucuCar.Sensors.Modules.PMS5003
{ {
private Pms5003 _pms5003; private Pms5003 _pms5003;
private Pms5003Data _pms5003Data; private Pms5003Data _pms5003Data;
public Pms5003Sensor() public Pms5003Sensor()
{ {
} }
@ -34,17 +33,33 @@ namespace NucuCar.Sensors.Modules.PMS5003
} }
public override void Initialize() public override void Initialize()
{
if (_pms5003 != null) return;
try
{ {
_pms5003 = new Pms5003(23, 24); _pms5003 = new Pms5003(23, 24);
_pms5003.Reset();
CurrentState = SensorStateEnum.Initialized; CurrentState = SensorStateEnum.Initialized;
Logger.LogInformation("Pms5003Sensor has initialized");
}
catch (ArgumentException e)
{
Logger.LogError("{Message}", e.Message);
CurrentState = SensorStateEnum.Error;
}
} }
public override Task TakeMeasurementAsync() public override Task TakeMeasurementAsync()
{ {
if (CurrentState == SensorStateEnum.Disabled)
{
throw new InvalidOperationException("Can't take measurement on disabled sensor!");
}
try try
{ {
_pms5003.WakeUp(); _pms5003.WakeUp();
_pms5003Data = _pms5003.ReadData(); _pms5003Data = _pms5003.ReadData();
Logger?.LogDebug("{Message}",_pms5003Data.ToString());
CurrentState = SensorStateEnum.Initialized; CurrentState = SensorStateEnum.Initialized;
} }
catch (ReadFailedException e) catch (ReadFailedException e)

View file

@ -21,7 +21,7 @@
"Grpc": false "Grpc": false
}, },
"Pms5003Sensor": { "Pms5003Sensor": {
"Enabled": false, "Enabled": true,
"Telemetry": true, "Telemetry": true,
"Grpc": true "Grpc": true
}, },