Refactor NucuCar.Domain GenericSensor.GetMeasurements to return a generic response

This commit is contained in:
Denis-Cosmin Nutiu 2019-12-19 22:59:17 +02:00
parent c68dfa9890
commit 53cc15ed5d
5 changed files with 13 additions and 16 deletions

View file

@ -15,7 +15,7 @@ namespace NucuCar.Domain.Sensors
public abstract void Initialize(); public abstract void Initialize();
public abstract Task TakeMeasurementAsync(); public abstract Task TakeMeasurementAsync();
public abstract Bme680MeasurementData GetMeasurement(); public abstract NucuCarSensorResponse GetMeasurement();
public abstract SensorStateEnum GetState(); public abstract SensorStateEnum GetState();
} }
} }

View file

@ -2,7 +2,6 @@ using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes; using Google.Protobuf.WellKnownTypes;
using Grpc.Core; using Grpc.Core;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NucuCarSensorsProto; using NucuCarSensorsProto;
namespace NucuCar.Sensors.EnvironmentSensor namespace NucuCar.Sensors.EnvironmentSensor
@ -35,13 +34,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
ServerCallContext context) ServerCallContext context)
{ {
_logger?.LogDebug($"Calling {nameof(GetMeasurement)}."); _logger?.LogDebug($"Calling {nameof(GetMeasurement)}.");
var sensorMeasurement = _bme680Sensor.Object.GetMeasurement(); return Task.FromResult(_bme680Sensor.Object.GetMeasurement());
var jsonResponse = JsonConvert.SerializeObject(sensorMeasurement);
return Task.FromResult(new NucuCarSensorResponse()
{
State = _bme680Sensor.Object.GetState(),
JsonData = jsonResponse
});
} }
} }
} }

View file

@ -6,6 +6,7 @@ using Iot.Device.Bmxx80;
using Iot.Device.Bmxx80.PowerMode; using Iot.Device.Bmxx80.PowerMode;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using NucuCar.Domain.Sensors; using NucuCar.Domain.Sensors;
using NucuCarSensorsProto; using NucuCarSensorsProto;
@ -41,9 +42,14 @@ namespace NucuCar.Sensors.EnvironmentSensor
Object = this; Object = this;
} }
public override Bme680MeasurementData GetMeasurement() public override NucuCarSensorResponse GetMeasurement()
{ {
return _lastMeasurement; var jsonResponse = JsonConvert.SerializeObject(_lastMeasurement);
return new NucuCarSensorResponse
{
State = GetState(),
JsonData = jsonResponse
};
} }
public override SensorStateEnum GetState() public override SensorStateEnum GetState()

View file

@ -43,7 +43,7 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
[Fact] [Fact]
public void Test_GetSensorMeasurement() public void Test_GetSensorMeasurement()
{ {
_mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new Bme680MeasurementData()); _mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new NucuCarSensorResponse());
var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object); var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object);
service.GetMeasurement(null, null); service.GetMeasurement(null, null);

View file

@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using NucuCar.Domain.Sensors;
using NucuCar.Sensors.EnvironmentSensor; using NucuCar.Sensors.EnvironmentSensor;
using NucuCarSensorsProto; using NucuCarSensorsProto;
@ -18,9 +16,9 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
} }
public override Bme680MeasurementData GetMeasurement() public override NucuCarSensorResponse GetMeasurement()
{ {
return new Bme680MeasurementData(); return new NucuCarSensorResponse();
} }
public override SensorStateEnum GetState() public override SensorStateEnum GetState()