Refactor SensorMeasurement.cs

This commit is contained in:
Denis-Cosmin Nutiu 2021-08-02 21:08:41 +03:00
parent f543aa1a18
commit 315526ba7d
6 changed files with 66 additions and 16 deletions

View file

@ -0,0 +1,20 @@
using System.Diagnostics.CodeAnalysis;
namespace NucuCar.Sensors.Abstractions
{
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global")]
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")]
public class SensorMeasurement
{
public string Name { get; set; }
public string Unit { get; set; }
public double Value { get; set; }
public SensorMeasurement(string name, string unit, double value)
{
Name = name;
Unit = unit;
Value = value;
}
}
}

View file

@ -1,10 +1,12 @@
namespace NucuCar.Sensors.Abstractions
using System.Collections.Generic;
namespace NucuCar.Sensors.Abstractions
{
public class NucuCarSensorResponse
{
// TODO: Fix this class, it is horrible at this moment.
// TODO: Fix names in NucuCar.Sensors.Modules.
public SensorStateEnum State;
public string JsonData;
public string SensorId;
public List<SensorMeasurement> Data;
}
}

View file

@ -4,7 +4,6 @@ using System.Device.I2c;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using NucuCar.Sensors.Abstractions;
using Iot.Device.Bmxx80;
using UnitsNet;
@ -54,11 +53,17 @@ namespace NucuCar.Sensors.Modules.Environment
public override NucuCarSensorResponse GetMeasurement()
{
var jsonResponse = JsonConvert.SerializeObject(_lastMeasurement);
return new NucuCarSensorResponse
{
SensorId = GetIdentifier(),
State = GetState(),
JsonData = jsonResponse
Data = new List<SensorMeasurement>
{
new SensorMeasurement("temperature", "celsius", _lastMeasurement.Temperature),
new SensorMeasurement("humidity", "rh", _lastMeasurement.Humidity),
new SensorMeasurement("pressure", "hPa", _lastMeasurement.Pressure),
new SensorMeasurement("volatileOrganicCoumpounds", "", _lastMeasurement.VolatileOrganicCompounds),
}
};
}

View file

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using NucuCar.Sensors.Abstractions;
using Iot.Device.CpuTemperature;
@ -65,14 +64,14 @@ namespace NucuCar.Sensors.Modules.Health
public override NucuCarSensorResponse GetMeasurement()
{
var jsonResponse = JsonConvert.SerializeObject(new Dictionary<string, object>
{
["cpu_temperature"] = _lastTemperatureCelsius,
});
return new NucuCarSensorResponse()
{
SensorId = GetIdentifier(),
State = CurrentState,
JsonData = jsonResponse
Data = new List<SensorMeasurement>
{
new SensorMeasurement("temperature", "celsius", _lastTemperatureCelsius)
}
};
}

View file

@ -41,7 +41,12 @@ namespace NucuCar.Sensors.Modules.Heartbeat
public override NucuCarSensorResponse GetMeasurement()
{
return null;
return new NucuCarSensorResponse()
{
SensorId = GetIdentifier(),
State = CurrentState,
Data = new List<SensorMeasurement>()
};
}
public override SensorStateEnum GetState()

View file

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using NucuCar.Sensors.Abstractions;
using PMS5003;
using PMS5003.Exceptions;
@ -85,11 +84,31 @@ namespace NucuCar.Sensors.Modules.PMS5003
public override NucuCarSensorResponse GetMeasurement()
{
var jsonResponse = _pms5003Data != null ? JsonConvert.SerializeObject(_pms5003Data) : "{}";
return new NucuCarSensorResponse()
{
SensorId = GetIdentifier(),
State = GetState(),
JsonData = jsonResponse
Data = new List<SensorMeasurement>
{
new SensorMeasurement("Pm1Atmospheric", "?", _pms5003Data.Pm1Atmospheric),
new SensorMeasurement("Pm1Standard", "?", _pms5003Data.Pm1Standard),
new SensorMeasurement("Pm10Atmospheric", "?", _pms5003Data.Pm10Atmospheric),
new SensorMeasurement("Pm10Standard", "?", _pms5003Data.Pm10Standard),
new SensorMeasurement("Pm2Dot5Atmospheric", "?", _pms5003Data.Pm2Dot5Atmospheric),
new SensorMeasurement("Pm2Dot5Standard", "?", _pms5003Data.Pm2Dot5Standard),
new SensorMeasurement("ParticlesDiameterBeyond0Dot3", "?",
_pms5003Data.ParticlesDiameterBeyond0Dot3),
new SensorMeasurement("ParticlesDiameterBeyond0Dot5", "?",
_pms5003Data.ParticlesDiameterBeyond0Dot5),
new SensorMeasurement("ParticlesDiameterBeyond1Dot0", "?",
_pms5003Data.ParticlesDiameterBeyond1Dot0),
new SensorMeasurement("ParticlesDiameterBeyond2Dot5", "?",
_pms5003Data.ParticlesDiameterBeyond2Dot5),
new SensorMeasurement("ParticlesDiameterBeyond5Dot0", "?",
_pms5003Data.ParticlesDiameterBeyond5Dot0),
new SensorMeasurement("ParticlesDiameterBeyond10Dot0", "?",
_pms5003Data.ParticlesDiameterBeyond10Dot0),
}
};
}