Remove GRPC from NucuCar.Sensors
This commit is contained in:
parent
57e0c2d891
commit
8ed64e9a46
24 changed files with 27 additions and 313 deletions
|
@ -4,22 +4,8 @@
|
|||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="Protos\NucuCarSensors.proto">
|
||||
<GrpcServices>Both</GrpcServices>
|
||||
<Access>Public</Access>
|
||||
<ProtoCompile>True</ProtoCompile>
|
||||
<CompileOutputs>True</CompileOutputs>
|
||||
<OutputDir>obj/Debug/netcoreapp3.0/</OutputDir>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Protobuf>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FirebaseRestTranslator" Version="0.1.1" />
|
||||
<PackageReference Include="Google.Protobuf" Version="3.10.1" />
|
||||
<PackageReference Include="Grpc" Version="2.25.0" />
|
||||
<PackageReference Include="Grpc.Tools" Version="2.25.0" />
|
||||
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.29.0-preview-002" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NucuCarSensorsProto;
|
||||
|
||||
namespace NucuCar.Sensors.Abstractions
|
||||
{
|
||||
|
|
|
@ -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 cref="ITelemeter"/>
|
||||
/// See: <see cref="NucuCar.Sensors.GenericSensor"/>
|
||||
/// See: <see cref="NucuCar.Sensors.Abstractions.GenericSensor"/>
|
||||
/// </summary>
|
||||
public abstract class GenericTelemeterSensor : GenericSensor, ITelemeter
|
||||
{
|
||||
|
|
8
NucuCar.Sensors/Abstractions/SensorResponse.cs
Normal file
8
NucuCar.Sensors/Abstractions/SensorResponse.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
namespace NucuCar.Sensors.Abstractions
|
||||
{
|
||||
public class NucuCarSensorResponse
|
||||
{
|
||||
public SensorStateEnum State;
|
||||
public string JsonData;
|
||||
}
|
||||
}
|
12
NucuCar.Sensors/Abstractions/SensorStatecs.cs
Normal file
12
NucuCar.Sensors/Abstractions/SensorStatecs.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
|
||||
namespace NucuCar.Sensors.Abstractions
|
||||
{
|
||||
public enum SensorStateEnum : ushort
|
||||
{
|
||||
Error = 0,
|
||||
Uninitialized = 1,
|
||||
Initialized = 2,
|
||||
Disabled = 3,
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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<Bme680GrpcService>();
|
||||
endpoints.MapGrpcService<CpuTempGrpcService>();
|
||||
endpoints.MapGrpcService<Pms5003GrpcService>();
|
||||
|
||||
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");
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 = "{}"
|
||||
};
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// EnvironmentSensor's gRPC service.
|
||||
/// It allows reading the sensor's data using remote procedure calls.
|
||||
/// </summary>
|
||||
public class Bme680GrpcService : EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceBase
|
||||
{
|
||||
private readonly ILogger<Bme680GrpcService> _logger;
|
||||
private readonly IOptions<Bme680Config> _options;
|
||||
private readonly ISensor<Bme680Sensor> _bme680Sensor;
|
||||
|
||||
public Bme680GrpcService(ILogger<Bme680GrpcService> logger, ISensor<Bme680Sensor> bme680Sensor, IOptions<Bme680Config> options)
|
||||
{
|
||||
_bme680Sensor = bme680Sensor;
|
||||
_logger = logger;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public override async Task<NucuCarSensorResponse> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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<CpuTempGrpcService> _logger;
|
||||
private readonly ISensor<CpuTempSensor> _sensor;
|
||||
private readonly IOptions<CpuTempConfig> _options;
|
||||
|
||||
public CpuTempGrpcService(ILogger<CpuTempGrpcService> logger, ISensor<CpuTempSensor> sensor,
|
||||
IOptions<CpuTempConfig> options)
|
||||
{
|
||||
_logger = logger;
|
||||
_sensor = sensor;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public override async Task<NucuCarSensorResponse> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -2,6 +2,5 @@
|
|||
{
|
||||
public class HeartbeatConfig : BaseSensorConfig
|
||||
{
|
||||
public new bool Grpc { get; } = false;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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<Pms5003GrpcService> _logger;
|
||||
private readonly IOptions<Pms5003Config> _options;
|
||||
private readonly ISensor<Pms5003Sensor> _pms5003Sensor;
|
||||
|
||||
public Pms5003GrpcService(ILogger<Pms5003GrpcService> logger, ISensor<Pms5003Sensor> pms5003Sensor, IOptions<Pms5003Config> options)
|
||||
{
|
||||
_pms5003Sensor = pms5003Sensor;
|
||||
_logger = logger;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public override async Task<NucuCarSensorResponse> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<CpuTempWorker>();
|
||||
services.AddHostedService<HeartbeatWorker>();
|
||||
services.AddHostedService<Pms5003Worker>();
|
||||
})
|
||||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<GrpcStartup>(); });
|
||||
});
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Debug",
|
||||
"Grpc": "Debug",
|
||||
"Microsoft": "Debug"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<ILogger<Bme680GrpcService>> _mockLogger;
|
||||
private readonly Mock<ISensor<Bme680Sensor>> _mockSensor;
|
||||
private readonly Mock<IOptions<Bme680Config>> _mockOptions;
|
||||
private readonly Mock<TestBme680Sensor> _mockTestSensor;
|
||||
|
||||
public Bme680GrpcServiceTest()
|
||||
{
|
||||
_mockLogger = new Mock<ILogger<Bme680GrpcService>>();
|
||||
_mockSensor = new Mock<ISensor<Bme680Sensor>>();
|
||||
_mockOptions = new Mock<IOptions<Bme680Config>>();
|
||||
_mockTestSensor = new Mock<TestBme680Sensor>();
|
||||
|
||||
_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<IOptions<Bme680Config>>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue