Refactor Telemetry project structure

This commit is contained in:
Denis-Cosmin Nutiu 2020-08-01 18:07:13 +03:00
parent 350fc6bc77
commit 937ea25a2c
29 changed files with 81 additions and 68 deletions

View file

@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace NucuCar.Domain.Telemetry namespace NucuCar.Telemetry.Abstractions
{ {
/// <summary> /// <summary>
/// Interface that specifies that the component implementing it is willing to provide telemetry data and can be /// Interface that specifies that the component implementing it is willing to provide telemetry data and can be

View file

@ -3,8 +3,9 @@ using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Telemetry.Publishers;
namespace NucuCar.Domain.Telemetry namespace NucuCar.Telemetry.Abstractions
{ {
/// <summary> /// <summary>
/// The TelemetryPublisher is an abstract class, which provides a base for implementing telemetry publishers. /// The TelemetryPublisher is an abstract class, which provides a base for implementing telemetry publishers.
@ -83,8 +84,8 @@ namespace NucuCar.Domain.Telemetry
/// <summary> /// <summary>
/// Constructor for <see cref="TelemetryPublisher"/>. /// Constructor for <see cref="TelemetryPublisher"/>.
/// </summary> /// </summary>
/// <param name="opts">TelemetryPublisher options, see: <see cref="TelemetryPublisherBuilderOptions"/></param> /// <param name="opts">TelemetryPublisher options, see: <see cref="TelemetryPublisherOptions"/></param>
protected TelemetryPublisher(TelemetryPublisherBuilderOptions opts) protected TelemetryPublisher(TelemetryPublisherOptions opts)
{ {
ConnectionString = opts.ConnectionString; ConnectionString = opts.ConnectionString;
TelemetrySource = opts.TelemetrySource; TelemetrySource = opts.TelemetrySource;

View file

@ -5,9 +5,9 @@ using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client; using Microsoft.Azure.Devices.Client;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using NucuCar.Domain.Telemetry; using NucuCar.Telemetry.Abstractions;
namespace NucuCar.Telemetry namespace NucuCar.Telemetry.Publishers
{ {
/// <summary> /// <summary>
/// Constructs an instance of <see cref="TelemetryPublisherAzure"/>. It is used to publish telemetry to Microsoft /// Constructs an instance of <see cref="TelemetryPublisherAzure"/>. It is used to publish telemetry to Microsoft
@ -20,7 +20,7 @@ namespace NucuCar.Telemetry
{ {
protected readonly DeviceClient DeviceClient; protected readonly DeviceClient DeviceClient;
public TelemetryPublisherAzure(TelemetryPublisherBuilderOptions opts) : base(opts) public TelemetryPublisherAzure(TelemetryPublisherOptions opts) : base(opts)
{ {
try try
{ {

View file

@ -2,14 +2,14 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using NucuCar.Domain.Telemetry; using NucuCar.Telemetry.Abstractions;
namespace NucuCar.Telemetry namespace NucuCar.Telemetry.Publishers
{ {
public class TelemetryPublisherConsole : TelemetryPublisher public class TelemetryPublisherConsole : TelemetryPublisher
{ {
public TelemetryPublisherConsole(TelemetryPublisherBuilderOptions opts) : base(opts) public TelemetryPublisherConsole(TelemetryPublisherOptions opts) : base(opts)
{ {
} }

View file

@ -6,10 +6,10 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using NucuCar.Domain.Telemetry;
using NucuCar.Domain.Utilities; using NucuCar.Domain.Utilities;
using NucuCar.Telemetry.Abstractions;
namespace NucuCar.Telemetry namespace NucuCar.Telemetry.Publishers
{ {
/// <summary> /// <summary>
/// The TelemetryPublisherDisk is used to publish telemetry data to a file on the disk. /// The TelemetryPublisherDisk is used to publish telemetry data to a file on the disk.
@ -30,7 +30,7 @@ namespace NucuCar.Telemetry
/// </remarks> /// </remarks>
/// </summary> /// </summary>
/// <param name="opts"></param> /// <param name="opts"></param>
public TelemetryPublisherDisk(TelemetryPublisherBuilderOptions opts) : base(opts) public TelemetryPublisherDisk(TelemetryPublisherOptions opts) : base(opts)
{ {
var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString); var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString);
var fileName = connectionStringParams.GetValueOrDefault("FileName", "telemetry"); var fileName = connectionStringParams.GetValueOrDefault("FileName", "telemetry");

View file

@ -4,12 +4,11 @@ using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Domain;
using NucuCar.Domain.Http; using NucuCar.Domain.Http;
using NucuCar.Domain.Telemetry;
using NucuCar.Domain.Utilities; using NucuCar.Domain.Utilities;
using NucuCar.Telemetry.Abstractions;
namespace NucuCar.Telemetry namespace NucuCar.Telemetry.Publishers
{ {
/// <summary> /// <summary>
/// This class is used to publish the telemetry data to Google's Cloud Firestore. /// This class is used to publish the telemetry data to Google's Cloud Firestore.
@ -37,7 +36,7 @@ namespace NucuCar.Telemetry
private readonly string _webPassword; private readonly string _webPassword;
private readonly string _webApiKey; private readonly string _webApiKey;
public TelemetryPublisherFirestore(TelemetryPublisherBuilderOptions opts) : base(opts) public TelemetryPublisherFirestore(TelemetryPublisherOptions opts) : base(opts)
{ {
// Parse Options // Parse Options
var options = ConnectionStringParser.Parse(opts.ConnectionString); var options = ConnectionStringParser.Parse(opts.ConnectionString);

View file

@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using NucuCar.Domain.Telemetry; using NucuCar.Telemetry.Abstractions;
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global // ReSharper disable ClassWithVirtualMembersNeverInherited.Global

View file

@ -1,6 +1,6 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global // ReSharper disable UnusedAutoPropertyAccessor.Global
using NucuCar.Domain.Telemetry; using NucuCar.Telemetry.Abstractions;
namespace NucuCar.Telemetry namespace NucuCar.Telemetry
{ {

View file

@ -1,7 +1,8 @@
using System; using System;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Domain.Telemetry;
using NucuCar.Domain.Utilities; using NucuCar.Domain.Utilities;
using NucuCar.Telemetry.Abstractions;
using NucuCar.Telemetry.Publishers;
namespace NucuCar.Telemetry namespace NucuCar.Telemetry
{ {
@ -24,7 +25,7 @@ namespace NucuCar.Telemetry
Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString);
Guard.ArgumentNotNullOrWhiteSpace(nameof(telemetrySource), telemetrySource); Guard.ArgumentNotNullOrWhiteSpace(nameof(telemetrySource), telemetrySource);
Guard.ArgumentNotNull(nameof(logger), logger); Guard.ArgumentNotNull(nameof(logger), logger);
var opts = new TelemetryPublisherBuilderOptions() var opts = new TelemetryPublisherOptions()
{ConnectionString = connectionString, TelemetrySource = telemetrySource, Logger = logger}; {ConnectionString = connectionString, TelemetrySource = telemetrySource, Logger = logger};
return SpawnPublisher(type, opts); return SpawnPublisher(type, opts);
} }
@ -38,12 +39,12 @@ namespace NucuCar.Telemetry
public static TelemetryPublisher CreateFromConnectionString(string type, string connectionString) public static TelemetryPublisher CreateFromConnectionString(string type, string connectionString)
{ {
Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString);
var opts = new TelemetryPublisherBuilderOptions() var opts = new TelemetryPublisherOptions()
{ConnectionString = connectionString, TelemetrySource = "TelemetryPublisherAzure"}; {ConnectionString = connectionString, TelemetrySource = "TelemetryPublisherAzure"};
return SpawnPublisher(type, opts); return SpawnPublisher(type, opts);
} }
private static TelemetryPublisher SpawnPublisher(string type, TelemetryPublisherBuilderOptions opts) private static TelemetryPublisher SpawnPublisher(string type, TelemetryPublisherOptions opts)
{ {
return type switch return type switch
{ {

View file

@ -1,11 +1,12 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Telemetry.Abstractions;
namespace NucuCar.Domain.Telemetry namespace NucuCar.Telemetry
{ {
/// <summary> /// <summary>
/// This class contains options for the <see cref="TelemetryPublisher"/>. /// This class contains options for the <see cref="TelemetryPublisher"/>.
/// </summary> /// </summary>
public class TelemetryPublisherBuilderOptions public class TelemetryPublisherOptions
{ {
/// <summary> /// <summary>
/// The ConnectionString used by the publisher to connect to the cloud service. /// The ConnectionString used by the publisher to connect to the cloud service.

View file

@ -1,4 +1,4 @@
using NucuCar.Domain.Telemetry; using NucuCar.Telemetry.Abstractions;
namespace NucuCar.Telemetry namespace NucuCar.Telemetry
{ {

View file

@ -4,7 +4,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using NucuCar.Domain.Telemetry; using NucuCar.Telemetry.Abstractions;
namespace NucuCar.Telemetry namespace NucuCar.Telemetry
{ {

View file

@ -2,7 +2,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCarSensorsProto; using NucuCarSensorsProto;
namespace NucuCar.Domain.Sensors namespace NucuCar.Sensors.Abstractions
{ {
/// <summary> /// <summary>
/// The GenericSensor is an abstract class, which provides a base for abstracting hardware sensors. /// The GenericSensor is an abstract class, which provides a base for abstracting hardware sensors.

View file

@ -1,13 +1,13 @@
using System.Collections.Generic; using System.Collections.Generic;
using NucuCar.Domain.Telemetry; using NucuCar.Telemetry.Abstractions;
namespace NucuCar.Domain.Sensors namespace NucuCar.Sensors.Abstractions
{ {
/// <summary> /// <summary>
/// The GenericSensor is an abstract class, which provides a base for abstracting hardware sensors /// The GenericSensor is an abstract class, which provides a base for abstracting hardware sensors
/// with telemetry support. /// with telemetry support.
/// See: <see cref="ITelemeter"/> /// See: <see cref="ITelemeter"/>
/// See: <see cref="GenericSensor"/> /// See: <see cref="NucuCar.Sensors.GenericSensor"/>
/// </summary> /// </summary>
public abstract class GenericTelemeterSensor : GenericSensor, ITelemeter public abstract class GenericTelemeterSensor : GenericSensor, ITelemeter
{ {

View file

@ -1,6 +1,4 @@
using NucuCar.Domain.Sensors; namespace NucuCar.Sensors.Abstractions
namespace NucuCar.Sensors
{ {
public interface ISensor<out TSensor> where TSensor : GenericTelemeterSensor public interface ISensor<out TSensor> where TSensor : GenericTelemeterSensor
{ {

View file

@ -3,6 +3,7 @@ using Google.Protobuf.WellKnownTypes;
using Grpc.Core; using Grpc.Core;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using NucuCar.Sensors.Abstractions;
using NucuCar.Sensors.Grpc; using NucuCar.Sensors.Grpc;
using NucuCarSensorsProto; using NucuCarSensorsProto;

View file

@ -5,7 +5,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using NucuCar.Domain.Sensors; using NucuCar.Sensors.Abstractions;
using NucuCar.Sensors.Environment.Bmxx80; using NucuCar.Sensors.Environment.Bmxx80;
using NucuCarSensorsProto; using NucuCarSensorsProto;
using Bme680 = NucuCar.Sensors.Environment.Bmxx80.Bme680; using Bme680 = NucuCar.Sensors.Environment.Bmxx80.Bme680;

View file

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Sensors.Abstractions;
using NucuCar.Telemetry; using NucuCar.Telemetry;
namespace NucuCar.Sensors.Environment namespace NucuCar.Sensors.Environment

View file

@ -3,6 +3,7 @@ using Google.Protobuf.WellKnownTypes;
using Grpc.Core; using Grpc.Core;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using NucuCar.Sensors.Abstractions;
using NucuCar.Sensors.Grpc; using NucuCar.Sensors.Grpc;
using NucuCarSensorsProto; using NucuCarSensorsProto;

View file

@ -5,7 +5,7 @@ using Iot.Device.CpuTemperature;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using NucuCar.Domain.Sensors; using NucuCar.Sensors.Abstractions;
using NucuCarSensorsProto; using NucuCarSensorsProto;
namespace NucuCar.Sensors.Health namespace NucuCar.Sensors.Health

View file

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Sensors.Abstractions;
using NucuCar.Telemetry; using NucuCar.Telemetry;
namespace NucuCar.Sensors.Health namespace NucuCar.Sensors.Health

View file

@ -81,4 +81,8 @@
<ProjectReference Include="..\NucuCar.Domain.Telemetry\NucuCar.Domain.Telemetry.csproj" /> <ProjectReference Include="..\NucuCar.Domain.Telemetry\NucuCar.Domain.Telemetry.csproj" />
<ProjectReference Include="..\NucuCar.Domain\NucuCar.Domain.csproj" /> <ProjectReference Include="..\NucuCar.Domain\NucuCar.Domain.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Modules" />
</ItemGroup>
</Project> </Project>

View file

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NucuCar.Sensors.Abstractions;
using NucuCar.Sensors.Environment; using NucuCar.Sensors.Environment;
using NucuCar.Sensors.Grpc; using NucuCar.Sensors.Grpc;
using NucuCar.Sensors.Health; using NucuCar.Sensors.Health;

View file

@ -3,8 +3,8 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Domain.Sensors; using NucuCar.Sensors.Abstractions;
using NucuCar.Domain.Telemetry; using NucuCar.Telemetry.Abstractions;
using NucuCarSensorsProto; using NucuCarSensorsProto;
namespace NucuCar.Sensors namespace NucuCar.Sensors

View file

@ -6,8 +6,8 @@ using System.Threading.Tasks;
using CommandLine; using CommandLine;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using NucuCar.Domain.Telemetry;
using NucuCar.Telemetry; using NucuCar.Telemetry;
using NucuCar.Telemetry.Abstractions;
namespace NucuCar.TestClient.Telemetry namespace NucuCar.TestClient.Telemetry
{ {

View file

@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Moq; using Moq;
using NucuCar.Sensors; using NucuCar.Sensors;
using NucuCar.Sensors.Abstractions;
using NucuCar.Sensors.Environment; using NucuCar.Sensors.Environment;
using NucuCarSensorsProto; using NucuCarSensorsProto;
using Xunit; using Xunit;

View file

@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Moq; using Moq;
using NucuCar.Sensors; using NucuCar.Sensors;
using NucuCar.Sensors.Abstractions;
using NucuCar.Sensors.Environment; using NucuCar.Sensors.Environment;
using NucuCarSensorsProto; using NucuCarSensorsProto;
using Xunit; using Xunit;

View file

@ -1,5 +1,6 @@
using System; using System;
using NucuCar.Telemetry; using NucuCar.Telemetry;
using NucuCar.Telemetry.Publishers;
using Xunit; using Xunit;
namespace NucuCar.UnitTests.NucuCar.Telemetry.Tests namespace NucuCar.UnitTests.NucuCar.Telemetry.Tests

View file

@ -6,8 +6,9 @@ using System.Net.Http.Headers;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NucuCar.Domain.Http; using NucuCar.Domain.Http;
using NucuCar.Domain.Telemetry;
using NucuCar.Telemetry; using NucuCar.Telemetry;
using NucuCar.Telemetry.Abstractions;
using NucuCar.Telemetry.Publishers;
using Xunit; using Xunit;
using HttpClient = NucuCar.Domain.Http.HttpClient; using HttpClient = NucuCar.Domain.Http.HttpClient;
@ -20,7 +21,7 @@ namespace NucuCar.UnitTests.NucuCar.Telemetry.Tests
{ {
private Dictionary<string, object> _mockData; private Dictionary<string, object> _mockData;
public MockTelemetryPublisherFirestore(TelemetryPublisherBuilderOptions opts) : base(opts) public MockTelemetryPublisherFirestore(TelemetryPublisherOptions opts) : base(opts)
{ {
_mockData = new Dictionary<string, object>(); _mockData = new Dictionary<string, object>();
} }
@ -47,7 +48,7 @@ namespace NucuCar.UnitTests.NucuCar.Telemetry.Tests
private void Test_Construct_BadProjectId() private void Test_Construct_BadProjectId()
{ {
// Setup // Setup
var opts = new TelemetryPublisherBuilderOptions() var opts = new TelemetryPublisherOptions()
{ {
ConnectionString = "ProjectIdBAD=test;CollectionName=test" ConnectionString = "ProjectIdBAD=test;CollectionName=test"
}; };
@ -60,7 +61,7 @@ namespace NucuCar.UnitTests.NucuCar.Telemetry.Tests
private void Test_Construct_BadCollectiontName() private void Test_Construct_BadCollectiontName()
{ {
// Setup // Setup
var opts = new TelemetryPublisherBuilderOptions() var opts = new TelemetryPublisherOptions()
{ {
ConnectionString = "ProjectId=test;CollectionNameBAD=test" ConnectionString = "ProjectId=test;CollectionNameBAD=test"
}; };
@ -73,7 +74,7 @@ namespace NucuCar.UnitTests.NucuCar.Telemetry.Tests
private async Task Test_PublishAsync_OK() private async Task Test_PublishAsync_OK()
{ {
// Setup // Setup
var opts = new TelemetryPublisherBuilderOptions() var opts = new TelemetryPublisherOptions()
{ {
ConnectionString = "ProjectId=test;CollectionName=test" ConnectionString = "ProjectId=test;CollectionName=test"
}; };
@ -98,7 +99,7 @@ namespace NucuCar.UnitTests.NucuCar.Telemetry.Tests
private async Task Test_PublishAsync_Cancel() private async Task Test_PublishAsync_Cancel()
{ {
// Setup // Setup
var opts = new TelemetryPublisherBuilderOptions() var opts = new TelemetryPublisherOptions()
{ {
ConnectionString = "ProjectId=test;CollectionName=test" ConnectionString = "ProjectId=test;CollectionName=test"
}; };
@ -122,7 +123,7 @@ namespace NucuCar.UnitTests.NucuCar.Telemetry.Tests
private async Task Test_PublishAsync_Authorization_OK() private async Task Test_PublishAsync_Authorization_OK()
{ {
// Setup // Setup
var opts = new TelemetryPublisherBuilderOptions() var opts = new TelemetryPublisherOptions()
{ {
ConnectionString = ConnectionString =
"ProjectId=test;CollectionName=test;WebApiKey=TAPIKEY;WebApiEmail=t@emai.com;WebApiPassword=tpass" "ProjectId=test;CollectionName=test;WebApiKey=TAPIKEY;WebApiEmail=t@emai.com;WebApiPassword=tpass"