diff --git a/NucuCar.Domain/NucuCar.Domain.csproj b/NucuCar.Domain/NucuCar.Domain.csproj index e1cf505..ae26a9f 100644 --- a/NucuCar.Domain/NucuCar.Domain.csproj +++ b/NucuCar.Domain/NucuCar.Domain.csproj @@ -4,22 +4,8 @@ netcoreapp3.1 - - - Both - Public - True - True - obj/Debug/netcoreapp3.0/ - MSBuild:Compile - - - - - - diff --git a/NucuCar.Domain/Protos/NucuCarSensors.proto b/NucuCar.Domain/Protos/NucuCarSensors.proto deleted file mode 100644 index 27688e7..0000000 --- a/NucuCar.Domain/Protos/NucuCarSensors.proto +++ /dev/null @@ -1,34 +0,0 @@ -syntax = "proto3"; -import "google/protobuf/empty.proto"; - -package NucuCarSensorsProto; - -// General -enum SensorStateEnum { - Error = 0; - Uninitialized = 1; - Initialized = 2; - Disabled = 3; -} - -// Environment Sensor -service EnvironmentSensorGrpcService { - rpc GetMeasurement(google.protobuf.Empty) returns (NucuCarSensorResponse) {} -} - -// Health Sensor -service HealthSensorGrpcService { - rpc GetCpuTemperature(google.protobuf.Empty) returns (NucuCarSensorResponse) {} -} - -// Pms5003 Sensor -service Pms5003SensorGrpcService { - rpc GetMeasurement(google.protobuf.Empty) returns (NucuCarSensorResponse) {} -} - -// Responses -// TODO: Create a normal map and clean this. -message NucuCarSensorResponse { - SensorStateEnum State = 1; - string JsonData = 2; -} \ No newline at end of file diff --git a/NucuCar.Sensors/Abstractions/GenericSensor.cs b/NucuCar.Sensors/Abstractions/GenericSensor.cs index f90407b..3bde341 100644 --- a/NucuCar.Sensors/Abstractions/GenericSensor.cs +++ b/NucuCar.Sensors/Abstractions/GenericSensor.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; -using NucuCarSensorsProto; namespace NucuCar.Sensors.Abstractions { diff --git a/NucuCar.Sensors/Abstractions/GenericTelemeterSensor.cs b/NucuCar.Sensors/Abstractions/GenericTelemeterSensor.cs index 08d1510..7a13a67 100644 --- a/NucuCar.Sensors/Abstractions/GenericTelemeterSensor.cs +++ b/NucuCar.Sensors/Abstractions/GenericTelemeterSensor.cs @@ -7,7 +7,7 @@ namespace NucuCar.Sensors.Abstractions /// The GenericSensor is an abstract class, which provides a base for abstracting hardware sensors /// with telemetry support. /// See: - /// See: + /// See: /// public abstract class GenericTelemeterSensor : GenericSensor, ITelemeter { diff --git a/NucuCar.Sensors/Abstractions/SensorResponse.cs b/NucuCar.Sensors/Abstractions/SensorResponse.cs new file mode 100644 index 0000000..50c2033 --- /dev/null +++ b/NucuCar.Sensors/Abstractions/SensorResponse.cs @@ -0,0 +1,8 @@ +namespace NucuCar.Sensors.Abstractions +{ + public class NucuCarSensorResponse + { + public SensorStateEnum State; + public string JsonData; + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Abstractions/SensorStatecs.cs b/NucuCar.Sensors/Abstractions/SensorStatecs.cs new file mode 100644 index 0000000..7f14757 --- /dev/null +++ b/NucuCar.Sensors/Abstractions/SensorStatecs.cs @@ -0,0 +1,12 @@ +using System; + +namespace NucuCar.Sensors.Abstractions +{ + public enum SensorStateEnum : ushort + { + Error = 0, + Uninitialized = 1, + Initialized = 2, + Disabled = 3, + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/BaseSensorConfig.cs b/NucuCar.Sensors/BaseSensorConfig.cs index 5884c81..b9bd647 100644 --- a/NucuCar.Sensors/BaseSensorConfig.cs +++ b/NucuCar.Sensors/BaseSensorConfig.cs @@ -4,7 +4,6 @@ { public bool Enabled { get; set; } = false; public bool Telemetry { get; set; } = false; - public bool Grpc { get; set; } = false; public int MeasurementInterval { get; set; } = 3000; } diff --git a/NucuCar.Sensors/Grpc/GrpcStartup.cs b/NucuCar.Sensors/Grpc/GrpcStartup.cs deleted file mode 100644 index d8bfc7c..0000000 --- a/NucuCar.Sensors/Grpc/GrpcStartup.cs +++ /dev/null @@ -1,48 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using NucuCar.Sensors.Modules.Environment; -using NucuCar.Sensors.Modules.Health; -using NucuCar.Sensors.Modules.PMS5003; - -namespace NucuCar.Sensors.Grpc -{ - public class GrpcStartup - { - // This method gets called by the runtime. Use this method to add services to the container. - // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 - public void ConfigureServices(IServiceCollection services) - { - services.AddGrpc(); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - app.UseRouting(); - - app.UseEndpoints(endpoints => - { - // Add the gRPC services here. - endpoints.MapGrpcService(); - endpoints.MapGrpcService(); - endpoints.MapGrpcService(); - - endpoints.MapGet("/", - async context => - { - await context.Response.WriteAsync( - "Communication with gRPC endpoints must be made through a gRPC client. " + - "To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); - }); - }); - } - } -} \ No newline at end of file diff --git a/NucuCar.Sensors/Grpc/Responses.cs b/NucuCar.Sensors/Grpc/Responses.cs deleted file mode 100644 index 34a3606..0000000 --- a/NucuCar.Sensors/Grpc/Responses.cs +++ /dev/null @@ -1,13 +0,0 @@ -using NucuCarSensorsProto; - -namespace NucuCar.Sensors.Grpc -{ - public static class Responses - { - public static readonly NucuCarSensorResponse GrpcIsDisabledResponse = new NucuCarSensorResponse() - { - State = SensorStateEnum.Disabled, - JsonData = "{}" - }; - } -} \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/Environment/Bme680GrpcService.cs b/NucuCar.Sensors/Modules/Environment/Bme680GrpcService.cs deleted file mode 100644 index c3cbf71..0000000 --- a/NucuCar.Sensors/Modules/Environment/Bme680GrpcService.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Threading.Tasks; -using Google.Protobuf.WellKnownTypes; -using Grpc.Core; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using NucuCar.Sensors.Abstractions; -using NucuCar.Sensors.Grpc; -using NucuCarSensorsProto; - -namespace NucuCar.Sensors.Modules.Environment -{ - /// - /// EnvironmentSensor's gRPC service. - /// It allows reading the sensor's data using remote procedure calls. - /// - public class Bme680GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase - { - private readonly ILogger _logger; - private readonly IOptions _options; - private readonly ISensor _bme680Sensor; - - public Bme680GrpcService(ILogger logger, ISensor bme680Sensor, IOptions options) - { - _bme680Sensor = bme680Sensor; - _logger = logger; - _options = options; - } - - public override async Task GetMeasurement(Empty request, - ServerCallContext context) - { - _logger?.LogDebug($"Calling {nameof(GetMeasurement)}."); - if (_options.Value.Grpc) - { - return await Task.FromResult(_bme680Sensor.Object.GetMeasurement()); - } - - return await Task.FromResult(Responses.GrpcIsDisabledResponse); - } - } -} \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/Environment/Bme680Sensor.cs b/NucuCar.Sensors/Modules/Environment/Bme680Sensor.cs index 02b42b7..8b1d02c 100644 --- a/NucuCar.Sensors/Modules/Environment/Bme680Sensor.cs +++ b/NucuCar.Sensors/Modules/Environment/Bme680Sensor.cs @@ -6,7 +6,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; using NucuCar.Sensors.Abstractions; -using NucuCarSensorsProto; using Iot.Device.Bmxx80; using UnitsNet; using Bme680 = Iot.Device.Bmxx80.Bme680; diff --git a/NucuCar.Sensors/Modules/Health/CpuTempGrpcService.cs b/NucuCar.Sensors/Modules/Health/CpuTempGrpcService.cs deleted file mode 100644 index 4251cdc..0000000 --- a/NucuCar.Sensors/Modules/Health/CpuTempGrpcService.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.Threading.Tasks; -using Google.Protobuf.WellKnownTypes; -using Grpc.Core; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using NucuCar.Sensors.Abstractions; -using NucuCar.Sensors.Grpc; -using NucuCarSensorsProto; - -namespace NucuCar.Sensors.Modules.Health -{ - public class CpuTempGrpcService : HealthSensorGrpcService.HealthSensorGrpcServiceBase - { - private readonly ILogger _logger; - private readonly ISensor _sensor; - private readonly IOptions _options; - - public CpuTempGrpcService(ILogger logger, ISensor sensor, - IOptions options) - { - _logger = logger; - _sensor = sensor; - _options = options; - } - - public override async Task GetCpuTemperature(Empty request, ServerCallContext context) - { - _logger?.LogDebug($"Calling {nameof(GetCpuTemperature)}."); - if (_options.Value.Grpc) - { - return await Task.FromResult(_sensor.Object.GetMeasurement()); - } - - return await Task.FromResult(Responses.GrpcIsDisabledResponse); - } - } -} \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/Health/CpuTempSensor.cs b/NucuCar.Sensors/Modules/Health/CpuTempSensor.cs index 6dfd155..7ef8703 100644 --- a/NucuCar.Sensors/Modules/Health/CpuTempSensor.cs +++ b/NucuCar.Sensors/Modules/Health/CpuTempSensor.cs @@ -6,7 +6,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; using NucuCar.Sensors.Abstractions; -using NucuCarSensorsProto; namespace NucuCar.Sensors.Modules.Health { diff --git a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs index 976efbe..6d0acad 100644 --- a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs +++ b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs @@ -2,6 +2,5 @@ { public class HeartbeatConfig : BaseSensorConfig { - public new bool Grpc { get; } = false; } } \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatSensor.cs b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatSensor.cs index 6f9d16c..95f4dc3 100644 --- a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatSensor.cs +++ b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatSensor.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NucuCar.Sensors.Abstractions; -using NucuCarSensorsProto; namespace NucuCar.Sensors.Modules.Heartbeat { diff --git a/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs b/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs deleted file mode 100644 index 5eda822..0000000 --- a/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Threading.Tasks; -using Google.Protobuf.WellKnownTypes; -using Grpc.Core; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using NucuCar.Sensors.Abstractions; -using NucuCar.Sensors.Grpc; -using NucuCarSensorsProto; - -namespace NucuCar.Sensors.Modules.PMS5003 -{ - public class Pms5003GrpcService : Pms5003SensorGrpcService.Pms5003SensorGrpcServiceBase - { - private readonly ILogger _logger; - private readonly IOptions _options; - private readonly ISensor _pms5003Sensor; - - public Pms5003GrpcService(ILogger logger, ISensor pms5003Sensor, IOptions options) - { - _pms5003Sensor = pms5003Sensor; - _logger = logger; - _options = options; - } - - public override async Task GetMeasurement(Empty request, ServerCallContext context) - { - _logger?.LogDebug($"Calling {nameof(GetMeasurement)}."); - if (_options.Value.Grpc) - { - return await Task.FromResult(_pms5003Sensor.Object.GetMeasurement()); - } - - return await Task.FromResult(Responses.GrpcIsDisabledResponse); - } - } -} \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs b/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs index 63fd23a..5b4d63b 100644 --- a/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs +++ b/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; using NucuCar.Sensors.Abstractions; -using NucuCarSensorsProto; using PMS5003; using PMS5003.Exceptions; diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index ce283c3..71c2394 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -1,8 +1,6 @@ -using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using NucuCar.Sensors.Abstractions; -using NucuCar.Sensors.Grpc; using NucuCar.Sensors.Modules.Environment; using NucuCar.Sensors.Modules.Health; using NucuCar.Sensors.Modules.Heartbeat; @@ -43,7 +41,6 @@ namespace NucuCar.Sensors services.AddHostedService(); services.AddHostedService(); services.AddHostedService(); - }) - .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); + }); } } \ No newline at end of file diff --git a/NucuCar.Sensors/SensorWorker.cs b/NucuCar.Sensors/SensorWorker.cs index 9700f58..57bac78 100644 --- a/NucuCar.Sensors/SensorWorker.cs +++ b/NucuCar.Sensors/SensorWorker.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NucuCar.Sensors.Abstractions; using NucuCar.Telemetry.Abstractions; -using NucuCarSensorsProto; namespace NucuCar.Sensors { diff --git a/NucuCar.Sensors/appsettings.Development.json b/NucuCar.Sensors/appsettings.Development.json index 70a9ba3..331805c 100644 --- a/NucuCar.Sensors/appsettings.Development.json +++ b/NucuCar.Sensors/appsettings.Development.json @@ -3,7 +3,6 @@ "LogLevel": { "Default": "Debug", "System": "Debug", - "Grpc": "Debug", "Microsoft": "Debug" } } diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index 11b0b0f..35b9c42 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -7,23 +7,19 @@ }, "EnvironmentSensor": { "Enabled": true, - "Telemetry": true, - "Grpc": true + "Telemetry": true }, "HealthSensor": { "Enabled": true, - "Telemetry": true, - "Grpc": true + "Telemetry": true }, "HeartbeatSensor": { "Enabled": false, - "Telemetry": true, - "Grpc": false + "Telemetry": true }, "Pms5003Sensor": { "Enabled": true, - "Telemetry": true, - "Grpc": true + "Telemetry": true }, "Logging": { "LogLevel": { diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs index 9c20f4d..e50a477 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs @@ -6,7 +6,6 @@ using Moq; using NucuCar.Sensors.Abstractions; using NucuCar.Sensors.Modules.Environment; using NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor; -using NucuCarSensorsProto; using Xunit; namespace NucuCar.UnitTests.NucuCar.Sensors.Tests diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/Bme680GrpcServiceTest.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/Bme680GrpcServiceTest.cs deleted file mode 100644 index 42a0b63..0000000 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/Bme680GrpcServiceTest.cs +++ /dev/null @@ -1,66 +0,0 @@ -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using Moq; -using NucuCar.Sensors.Abstractions; -using NucuCar.Sensors.Modules.Environment; -using NucuCarSensorsProto; -using Xunit; - -namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor -{ - public class Bme680GrpcServiceTest - { - private readonly Mock> _mockLogger; - private readonly Mock> _mockSensor; - private readonly Mock> _mockOptions; - private readonly Mock _mockTestSensor; - - public Bme680GrpcServiceTest() - { - _mockLogger = new Mock>(); - _mockSensor = new Mock>(); - _mockOptions = new Mock>(); - _mockTestSensor = new Mock(); - - _mockOptions.Setup(mo => mo.Value).Returns(new Bme680Config() - { - Grpc = true, - Telemetry = true, - Enabled = true - }); - _mockSensor.Setup(ms => ms.Object).Returns(_mockTestSensor.Object); - } - - - [Fact] - public void Test_GetSensorMeasurement() - { - _mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new NucuCarSensorResponse()); - var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object, _mockOptions.Object); - service.GetMeasurement(null, null); - - // Verify that the sensor get measurement method is called. - _mockSensor.Verify(s => s.Object.GetMeasurement(), Times.AtLeastOnce()); - } - - [Fact] - public void Test_GetSensorMeasurement_Disabled() - { - _mockTestSensor.Setup(s => s.GetMeasurement()).Returns(new NucuCarSensorResponse()); - var options = new Mock>(); - options.Setup(o => o.Value).Returns(new Bme680Config - { - Enabled = true, - Telemetry = true, - Grpc = false - }); - - var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object, options.Object); - var result = service.GetMeasurement(null, null); - - // Verify that the sensor get measurement method is not called. - _mockSensor.Verify(s => s.Object.GetMeasurement(), Times.Never()); - Assert.Equal(SensorStateEnum.Disabled, result.Result.State); - } - } -} \ No newline at end of file diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/TestBme680Sensor.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/TestBme680Sensor.cs index bfe065f..762d809 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/TestBme680Sensor.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor/TestBme680Sensor.cs @@ -1,6 +1,6 @@ using System.Threading.Tasks; +using NucuCar.Sensors.Abstractions; using NucuCar.Sensors.Modules.Environment; -using NucuCarSensorsProto; namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor {