From 53cc15ed5ddd4c6661dac7321ceb3c6ebcd5d081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Thu, 19 Dec 2019 22:59:17 +0200 Subject: [PATCH] Refactor NucuCar.Domain GenericSensor.GetMeasurements to return a generic response --- NucuCar.Domain/Sensors/GenericSensor.cs | 2 +- NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs | 9 +-------- NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs | 10 ++++++++-- .../EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs | 2 +- .../EnvironmentSensor.Tests/TestBme680Sensor.cs | 6 ++---- 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/NucuCar.Domain/Sensors/GenericSensor.cs b/NucuCar.Domain/Sensors/GenericSensor.cs index e70a975..c12cc91 100644 --- a/NucuCar.Domain/Sensors/GenericSensor.cs +++ b/NucuCar.Domain/Sensors/GenericSensor.cs @@ -15,7 +15,7 @@ namespace NucuCar.Domain.Sensors public abstract void Initialize(); public abstract Task TakeMeasurementAsync(); - public abstract Bme680MeasurementData GetMeasurement(); + public abstract NucuCarSensorResponse GetMeasurement(); public abstract SensorStateEnum GetState(); } } \ No newline at end of file diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs index 4a5907c..429dfd0 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680GrpcService.cs @@ -2,7 +2,6 @@ using System.Threading.Tasks; using Google.Protobuf.WellKnownTypes; using Grpc.Core; using Microsoft.Extensions.Logging; -using Newtonsoft.Json; using NucuCarSensorsProto; namespace NucuCar.Sensors.EnvironmentSensor @@ -35,13 +34,7 @@ namespace NucuCar.Sensors.EnvironmentSensor ServerCallContext context) { _logger?.LogDebug($"Calling {nameof(GetMeasurement)}."); - var sensorMeasurement = _bme680Sensor.Object.GetMeasurement(); - var jsonResponse = JsonConvert.SerializeObject(sensorMeasurement); - return Task.FromResult(new NucuCarSensorResponse() - { - State = _bme680Sensor.Object.GetState(), - JsonData = jsonResponse - }); + return Task.FromResult(_bme680Sensor.Object.GetMeasurement()); } } } \ No newline at end of file diff --git a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs index 45567f5..4b216ae 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs @@ -6,6 +6,7 @@ using Iot.Device.Bmxx80; using Iot.Device.Bmxx80.PowerMode; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Newtonsoft.Json; using NucuCar.Domain.Sensors; using NucuCarSensorsProto; @@ -41,9 +42,14 @@ namespace NucuCar.Sensors.EnvironmentSensor 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() diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs index c363ff8..cb37010 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680GrpcServiceTest.cs @@ -43,7 +43,7 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests [Fact] 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); service.GetMeasurement(null, null); diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs index 519a8a3..f00e40c 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/TestBme680Sensor.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; using System.Threading.Tasks; -using NucuCar.Domain.Sensors; using NucuCar.Sensors.EnvironmentSensor; 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()