From d85447aee8f1ce8eae24d236e830a187fa30e951 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Fri, 17 Apr 2020 18:11:07 +0300 Subject: [PATCH] NUC-41: Extract concrete Telemetry implementations in a project. --- NucuCar.Domain/Guard.cs | 74 +++++----- NucuCar.Sensors/Environment/Bme680Worker.cs | 2 +- NucuCar.Sensors/Health/CpuTempWorker.cs | 2 +- NucuCar.Sensors/NucuCar.Sensors.csproj | 7 +- NucuCar.Sensors/Program.cs | 2 +- NucuCar.Telemetry/NucuCar.Telemetry.csproj | 15 ++ .../SensorTelemetry.cs | 2 +- .../TelemetryConfig.cs | 2 +- .../TelemetryPublisherAzure.cs | 3 +- .../TelemetryPublisherDisk.cs | 3 +- .../TelemetryPublisherFactory.cs | 4 +- .../TelemetryPublisherFirestore.cs | 4 +- .../TelemetryWorker.cs | 2 +- NucuCar.TestClient/NucuCar.TestClient.csproj | 1 + .../Telemetry/AzureTelemetryPublishCmd.cs | 137 +++++++++--------- .../TelemetryPublisherFactoryTest.cs | 1 + .../Bme680WorkerTests.cs | 2 +- NucuCar.UnitTests/NucuCar.UnitTests.csproj | 1 + NucuCar.sln | 6 + 19 files changed, 153 insertions(+), 117 deletions(-) create mode 100644 NucuCar.Telemetry/NucuCar.Telemetry.csproj rename {NucuCar.Sensors/Telemetry => NucuCar.Telemetry}/SensorTelemetry.cs (95%) rename {NucuCar.Sensors/Telemetry => NucuCar.Telemetry}/TelemetryConfig.cs (94%) rename {NucuCar.Domain/Telemetry => NucuCar.Telemetry}/TelemetryPublisherAzure.cs (97%) rename {NucuCar.Domain/Telemetry => NucuCar.Telemetry}/TelemetryPublisherDisk.cs (98%) rename {NucuCar.Domain/Telemetry => NucuCar.Telemetry}/TelemetryPublisherFactory.cs (97%) rename {NucuCar.Domain/Telemetry => NucuCar.Telemetry}/TelemetryPublisherFirestore.cs (97%) rename {NucuCar.Sensors/Telemetry => NucuCar.Telemetry}/TelemetryWorker.cs (97%) diff --git a/NucuCar.Domain/Guard.cs b/NucuCar.Domain/Guard.cs index 009ddad..b12ff78 100644 --- a/NucuCar.Domain/Guard.cs +++ b/NucuCar.Domain/Guard.cs @@ -1,38 +1,38 @@ -using System; - -namespace NucuCar.Domain -{ - /// - /// Helper class used for checking arguments and raise exception if the checks don't pass. - /// - public static class Guard - { - /// - /// Checks if the argument string is null or whitespace and raises exception on check fail. - /// - /// The argument name that will be logged in the exception message. - /// The argument to check if it's null or whitespace. - /// Raised if the argument is null or whitespace. - internal static void ArgumentNotNullOrWhiteSpace(string argumentName, string argument) - { - if (string.IsNullOrWhiteSpace(argument)) - { - throw new ArgumentNullException($"The argument {argumentName} is null or whitespace!"); - } - } - - /// - /// Checks if the argument is null and raises exception on check fail. - /// - /// The argument name that will be logged in the exception message. - /// The argument to check if it's null. - /// Raised if the argument is null. - public static void ArgumentNotNull(string argumentName, object argument) - { - if (argument == null) - { - throw new ArgumentNullException($"The argument {argumentName} is null or whitespace!"); - } - } - } +using System; + +namespace NucuCar.Domain +{ + /// + /// Helper class used for checking arguments and raise exception if the checks don't pass. + /// + public static class Guard + { + /// + /// Checks if the argument string is null or whitespace and raises exception on check fail. + /// + /// The argument name that will be logged in the exception message. + /// The argument to check if it's null or whitespace. + /// Raised if the argument is null or whitespace. + public static void ArgumentNotNullOrWhiteSpace(string argumentName, string argument) + { + if (string.IsNullOrWhiteSpace(argument)) + { + throw new ArgumentNullException($"The argument {argumentName} is null or whitespace!"); + } + } + + /// + /// Checks if the argument is null and raises exception on check fail. + /// + /// The argument name that will be logged in the exception message. + /// The argument to check if it's null. + /// Raised if the argument is null. + public static void ArgumentNotNull(string argumentName, object argument) + { + if (argument == null) + { + throw new ArgumentNullException($"The argument {argumentName} is null or whitespace!"); + } + } + } } \ No newline at end of file diff --git a/NucuCar.Sensors/Environment/Bme680Worker.cs b/NucuCar.Sensors/Environment/Bme680Worker.cs index 1e643e6..47db676 100644 --- a/NucuCar.Sensors/Environment/Bme680Worker.cs +++ b/NucuCar.Sensors/Environment/Bme680Worker.cs @@ -1,5 +1,5 @@ using Microsoft.Extensions.Logging; -using NucuCar.Sensors.Telemetry; +using NucuCar.Telemetry; namespace NucuCar.Sensors.Environment { diff --git a/NucuCar.Sensors/Health/CpuTempWorker.cs b/NucuCar.Sensors/Health/CpuTempWorker.cs index 9416936..89d18a5 100644 --- a/NucuCar.Sensors/Health/CpuTempWorker.cs +++ b/NucuCar.Sensors/Health/CpuTempWorker.cs @@ -1,5 +1,5 @@ using Microsoft.Extensions.Logging; -using NucuCar.Sensors.Telemetry; +using NucuCar.Telemetry; namespace NucuCar.Sensors.Health { diff --git a/NucuCar.Sensors/NucuCar.Sensors.csproj b/NucuCar.Sensors/NucuCar.Sensors.csproj index f864812..c257cd8 100644 --- a/NucuCar.Sensors/NucuCar.Sensors.csproj +++ b/NucuCar.Sensors/NucuCar.Sensors.csproj @@ -10,7 +10,7 @@ - + @@ -79,5 +79,10 @@ + + + + + diff --git a/NucuCar.Sensors/Program.cs b/NucuCar.Sensors/Program.cs index 4be4a44..4268762 100644 --- a/NucuCar.Sensors/Program.cs +++ b/NucuCar.Sensors/Program.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.Hosting; using NucuCar.Sensors.Environment; using NucuCar.Sensors.Grpc; using NucuCar.Sensors.Health; -using NucuCar.Sensors.Telemetry; +using NucuCar.Telemetry; namespace NucuCar.Sensors { diff --git a/NucuCar.Telemetry/NucuCar.Telemetry.csproj b/NucuCar.Telemetry/NucuCar.Telemetry.csproj new file mode 100644 index 0000000..d731bc7 --- /dev/null +++ b/NucuCar.Telemetry/NucuCar.Telemetry.csproj @@ -0,0 +1,15 @@ + + + + netcoreapp3.0;netcoreapp3.1 + + + + + + + + + + + diff --git a/NucuCar.Sensors/Telemetry/SensorTelemetry.cs b/NucuCar.Telemetry/SensorTelemetry.cs similarity index 95% rename from NucuCar.Sensors/Telemetry/SensorTelemetry.cs rename to NucuCar.Telemetry/SensorTelemetry.cs index 951e07c..4549da7 100644 --- a/NucuCar.Sensors/Telemetry/SensorTelemetry.cs +++ b/NucuCar.Telemetry/SensorTelemetry.cs @@ -4,7 +4,7 @@ using NucuCar.Domain.Telemetry; // ReSharper disable ClassWithVirtualMembersNeverInherited.Global -namespace NucuCar.Sensors.Telemetry +namespace NucuCar.Telemetry { public class SensorTelemetry { diff --git a/NucuCar.Sensors/Telemetry/TelemetryConfig.cs b/NucuCar.Telemetry/TelemetryConfig.cs similarity index 94% rename from NucuCar.Sensors/Telemetry/TelemetryConfig.cs rename to NucuCar.Telemetry/TelemetryConfig.cs index e1d0590..53d39b0 100644 --- a/NucuCar.Sensors/Telemetry/TelemetryConfig.cs +++ b/NucuCar.Telemetry/TelemetryConfig.cs @@ -2,7 +2,7 @@ using NucuCar.Domain.Telemetry; -namespace NucuCar.Sensors.Telemetry +namespace NucuCar.Telemetry { public class TelemetryConfig { diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs b/NucuCar.Telemetry/TelemetryPublisherAzure.cs similarity index 97% rename from NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs rename to NucuCar.Telemetry/TelemetryPublisherAzure.cs index bfbc376..5c08e87 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs +++ b/NucuCar.Telemetry/TelemetryPublisherAzure.cs @@ -5,8 +5,9 @@ using System.Threading.Tasks; using Microsoft.Azure.Devices.Client; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using NucuCar.Domain.Telemetry; -namespace NucuCar.Domain.Telemetry +namespace NucuCar.Telemetry { /// /// Constructs an instance of . It is used to publish telemetry to Microsoft diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs b/NucuCar.Telemetry/TelemetryPublisherDisk.cs similarity index 98% rename from NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs rename to NucuCar.Telemetry/TelemetryPublisherDisk.cs index 7c01e13..380d401 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs +++ b/NucuCar.Telemetry/TelemetryPublisherDisk.cs @@ -6,9 +6,10 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using NucuCar.Domain.Telemetry; using NucuCar.Domain.Utilities; -namespace NucuCar.Domain.Telemetry +namespace NucuCar.Telemetry { /// /// The TelemetryPublisherDisk is used to publish telemetry data to a file on the disk. diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherFactory.cs b/NucuCar.Telemetry/TelemetryPublisherFactory.cs similarity index 97% rename from NucuCar.Domain/Telemetry/TelemetryPublisherFactory.cs rename to NucuCar.Telemetry/TelemetryPublisherFactory.cs index 4782df5..868e2d3 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherFactory.cs +++ b/NucuCar.Telemetry/TelemetryPublisherFactory.cs @@ -1,7 +1,9 @@ using System; using Microsoft.Extensions.Logging; +using NucuCar.Domain; +using NucuCar.Domain.Telemetry; -namespace NucuCar.Domain.Telemetry +namespace NucuCar.Telemetry { /// /// The TelemetryPublisherFactory is used instantiate TelemetryPublishers. diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherFirestore.cs b/NucuCar.Telemetry/TelemetryPublisherFirestore.cs similarity index 97% rename from NucuCar.Domain/Telemetry/TelemetryPublisherFirestore.cs rename to NucuCar.Telemetry/TelemetryPublisherFirestore.cs index 6b09f27..a1b0246 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherFirestore.cs +++ b/NucuCar.Telemetry/TelemetryPublisherFirestore.cs @@ -8,9 +8,10 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using NucuCar.Domain.Telemetry; using NucuCar.Domain.Utilities; -namespace NucuCar.Domain.Telemetry +namespace NucuCar.Telemetry { /// /// This class is used to publish the telemetry data to Google's Cloud Firestore. @@ -66,6 +67,7 @@ namespace NucuCar.Domain.Telemetry _httpClient = new HttpClient(); _httpClient.BaseAddress = new Uri(requestUrl); Logger?.LogInformation($"Initialized {nameof(TelemetryPublisherFirestore)}"); + Logger?.LogInformation($"ProjectId: {firestoreProjectId}; CollectionName: {firestoreCollection}."); } private async Task SetupAuthenticationHeaders() diff --git a/NucuCar.Sensors/Telemetry/TelemetryWorker.cs b/NucuCar.Telemetry/TelemetryWorker.cs similarity index 97% rename from NucuCar.Sensors/Telemetry/TelemetryWorker.cs rename to NucuCar.Telemetry/TelemetryWorker.cs index ebe8a8e..642d7c9 100644 --- a/NucuCar.Sensors/Telemetry/TelemetryWorker.cs +++ b/NucuCar.Telemetry/TelemetryWorker.cs @@ -5,7 +5,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NucuCar.Domain.Telemetry; -namespace NucuCar.Sensors.Telemetry +namespace NucuCar.Telemetry { /// /// Telemetry service, which pools the telemetry sources and pushes telemetry data to the cloud. diff --git a/NucuCar.TestClient/NucuCar.TestClient.csproj b/NucuCar.TestClient/NucuCar.TestClient.csproj index 34a11dc..6d344b9 100644 --- a/NucuCar.TestClient/NucuCar.TestClient.csproj +++ b/NucuCar.TestClient/NucuCar.TestClient.csproj @@ -15,6 +15,7 @@ + diff --git a/NucuCar.TestClient/Telemetry/AzureTelemetryPublishCmd.cs b/NucuCar.TestClient/Telemetry/AzureTelemetryPublishCmd.cs index a967e0c..6f817ad 100644 --- a/NucuCar.TestClient/Telemetry/AzureTelemetryPublishCmd.cs +++ b/NucuCar.TestClient/Telemetry/AzureTelemetryPublishCmd.cs @@ -1,69 +1,70 @@ -// ReSharper disable UnusedAutoPropertyAccessor.Global - -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using CommandLine; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using NucuCar.Domain.Telemetry; - -namespace NucuCar.TestClient.Telemetry -{ - public class AzureTelemetryPublishCmd - { - [Verb("azure-telemetry-publish", HelpText = "Test the publishing telemetry using Microsoft Azure IoT Hub.")] - public class AzureTelemetryPublishOptions - { - [Option('c', "connectionString", Required = true, - HelpText = "The publisher's connection string. Get it from the Device.")] - public string PublisherConnectionString { get; set; } - - [Option('m', "message", Required = true, HelpText = "The message to publish")] - public string PublisherJsonMessage { get; set; } - } - - private class DummyTelemeter : ITelemeter - { - private readonly Dictionary _dummyTelemeterData; - - public DummyTelemeter(Dictionary dummyData) - { - _dummyTelemeterData = dummyData; - } - - public string GetIdentifier() - { - return "DummyTelemeter"; - } - - public Dictionary GetTelemetryData() - { - return _dummyTelemeterData; - } - - public bool IsTelemetryEnabled() - { - return true; - } - } - - public static async Task RunAzurePublisherTelemetryTest(AzureTelemetryPublishOptions opts) - { - var logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) - .CreateLogger(); - - var telemetryPublisher = TelemetryPublisherFactory.Create(TelemetryPublisherType.Azure, - opts.PublisherConnectionString, "NucuCar.TestClient", logger); - - var anonymousTelemeter = - new DummyTelemeter( - JsonConvert.DeserializeObject>(opts.PublisherJsonMessage)); - - - logger.LogInformation($"Publishing message: {opts.PublisherJsonMessage}"); - telemetryPublisher.RegisterTelemeter(anonymousTelemeter); - await telemetryPublisher.PublishAsync(CancellationToken.None); - } - } +// ReSharper disable UnusedAutoPropertyAccessor.Global + +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using CommandLine; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using NucuCar.Domain.Telemetry; +using NucuCar.Telemetry; + +namespace NucuCar.TestClient.Telemetry +{ + public class AzureTelemetryPublishCmd + { + [Verb("azure-telemetry-publish", HelpText = "Test the publishing telemetry using Microsoft Azure IoT Hub.")] + public class AzureTelemetryPublishOptions + { + [Option('c', "connectionString", Required = true, + HelpText = "The publisher's connection string. Get it from the Device.")] + public string PublisherConnectionString { get; set; } + + [Option('m', "message", Required = true, HelpText = "The message to publish")] + public string PublisherJsonMessage { get; set; } + } + + private class DummyTelemeter : ITelemeter + { + private readonly Dictionary _dummyTelemeterData; + + public DummyTelemeter(Dictionary dummyData) + { + _dummyTelemeterData = dummyData; + } + + public string GetIdentifier() + { + return "DummyTelemeter"; + } + + public Dictionary GetTelemetryData() + { + return _dummyTelemeterData; + } + + public bool IsTelemetryEnabled() + { + return true; + } + } + + public static async Task RunAzurePublisherTelemetryTest(AzureTelemetryPublishOptions opts) + { + var logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) + .CreateLogger(); + + var telemetryPublisher = TelemetryPublisherFactory.Create(TelemetryPublisherType.Azure, + opts.PublisherConnectionString, "NucuCar.TestClient", logger); + + var anonymousTelemeter = + new DummyTelemeter( + JsonConvert.DeserializeObject>(opts.PublisherJsonMessage)); + + + logger.LogInformation($"Publishing message: {opts.PublisherJsonMessage}"); + telemetryPublisher.RegisterTelemeter(anonymousTelemeter); + await telemetryPublisher.PublishAsync(CancellationToken.None); + } + } } \ No newline at end of file diff --git a/NucuCar.UnitTests/NucuCar.Domain.Tests/Telemetry/TelemetryPublisherFactoryTest.cs b/NucuCar.UnitTests/NucuCar.Domain.Tests/Telemetry/TelemetryPublisherFactoryTest.cs index e917086..17afc64 100644 --- a/NucuCar.UnitTests/NucuCar.Domain.Tests/Telemetry/TelemetryPublisherFactoryTest.cs +++ b/NucuCar.UnitTests/NucuCar.Domain.Tests/Telemetry/TelemetryPublisherFactoryTest.cs @@ -1,5 +1,6 @@ using System; using NucuCar.Domain.Telemetry; +using NucuCar.Telemetry; using Xunit; namespace NucuCar.UnitTests.NucuCar.Domain.Tests.Telemetry diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680WorkerTests.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680WorkerTests.cs index 41a69d0..bf02d44 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680WorkerTests.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/EnvironmentSensor.Tests/Bme680WorkerTests.cs @@ -5,7 +5,7 @@ using Microsoft.Extensions.Options; using Moq; using NucuCar.Sensors; using NucuCar.Sensors.Environment; -using NucuCar.Sensors.Telemetry; +using NucuCar.Telemetry; using NucuCarSensorsProto; using Xunit; diff --git a/NucuCar.UnitTests/NucuCar.UnitTests.csproj b/NucuCar.UnitTests/NucuCar.UnitTests.csproj index da1d499..a5efc47 100644 --- a/NucuCar.UnitTests/NucuCar.UnitTests.csproj +++ b/NucuCar.UnitTests/NucuCar.UnitTests.csproj @@ -18,6 +18,7 @@ + diff --git a/NucuCar.sln b/NucuCar.sln index 7b09eb3..0c4e409 100644 --- a/NucuCar.sln +++ b/NucuCar.sln @@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NucuCar.Domain", "NucuCar.D EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NucuCar.UnitTests", "NucuCar.UnitTests\NucuCar.UnitTests.csproj", "{C6F07921-1052-4945-911E-F328A622F229}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NucuCar.Telemetry", "NucuCar.Telemetry\NucuCar.Telemetry.csproj", "{C20407F3-AB62-4590-B4FF-A0DCFCFA232B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,5 +32,9 @@ Global {C6F07921-1052-4945-911E-F328A622F229}.Debug|Any CPU.Build.0 = Debug|Any CPU {C6F07921-1052-4945-911E-F328A622F229}.Release|Any CPU.ActiveCfg = Release|Any CPU {C6F07921-1052-4945-911E-F328A622F229}.Release|Any CPU.Build.0 = Release|Any CPU + {C20407F3-AB62-4590-B4FF-A0DCFCFA232B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C20407F3-AB62-4590-B4FF-A0DCFCFA232B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C20407F3-AB62-4590-B4FF-A0DCFCFA232B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C20407F3-AB62-4590-B4FF-A0DCFCFA232B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal