From 6eb96626673eaf853d5902a112ae13c2cfb96812 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:35:34 -0800 Subject: [PATCH 01/19] apply style changes --- .../DigitalTwinsClientWrapper.cs | 20 +++++++++++++++---- .../src/core/DigitalTwinsConnectorService.cs | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs index 181814e..9fe2d9c 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs @@ -59,7 +59,7 @@ public DigitalTwinsClientWrapper(DigitalTwinsClient client, ILoggerReturns a task for updating a digital twin instance. public async Task UpdateDigitalTwinAsync(string modelID, string instanceID, string instancePropertyPath, string data) { - List dataTypes = new List() { typeof(Double), typeof(Boolean), typeof(Int32) }; + List dataTypes = new() { typeof(double), typeof(bool), typeof(int) }; var jsonPatchDocument = new JsonPatchDocument(); foreach (Type type in dataTypes) @@ -73,19 +73,31 @@ public async Task UpdateDigitalTwinAsync(string modelID, string instanceID, stri { instancePropertyPath = "$/{instancePropertyPath}"; } + // Once we're able to parse the data string to a type // we append it to the jsonPatchDocument jsonPatchDocument.AppendAdd(instancePropertyPath, value); // First UpdateDigitalTwinAsync call may block due to initial authorization. await _client.UpdateDigitalTwinAsync(instanceID, jsonPatchDocument); - _logger.LogInformation($"Successfully set instance {instanceID}{instancePropertyPath} based on model {modelID} to {data}"); + _logger.LogInformation( + "Successfully set instance {InstanceID}{InstancePropertyPath} based on model {ModelID} to {Data}", + instanceID, + instancePropertyPath, + modelID, + data); return; } catch (RequestFailedException ex) { - _logger.LogError($"Cannot set instance {instanceID}{instancePropertyPath} based on model {modelID} to {data} due to {ex.Message}"); - throw ex; + _logger.LogError( + "Cannot set instance {InstanceID}{InstancePropertyPath} based on model {ModelID} to {Data} due to {Message}", + instanceID, + instancePropertyPath, + modelID, + data, + ex.Message); + throw; } // Try to parse string data with the next type if we're unsuccessful. catch (Exception ex) when (ex is NotSupportedException || ex is ArgumentException || ex is FormatException) diff --git a/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnectorService.cs b/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnectorService.cs index ae6882f..44d3990 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnectorService.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnectorService.cs @@ -43,7 +43,7 @@ public override async Task UpdateDigitalTwin(UpdateDi } catch (Exception ex) { - _logger.LogError(ex.Message); + _logger.LogError("Error updating digital twin: {Message}", ex.Message); throw; } From 7fa615e2d5a5f8625103006db480e6c0dd134c8e Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:46:17 -0800 Subject: [PATCH 02/19] a few more changes --- .../DigitalTwinsClientWrapper.cs | 10 +++++++--- .../src/core/DigitalTwinsConnectorService.cs | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs index 9fe2d9c..f7e3e8c 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs @@ -106,9 +106,13 @@ public async Task UpdateDigitalTwinAsync(string modelID, string instanceID, stri } } - string errorMessage = $"Failed to parse {data}. Cannot set instance {instanceID}{instancePropertyPath} based on model {modelID} to {data}"; - _logger.LogError(errorMessage); - throw new NotSupportedException(errorMessage); + _logger.LogError( + "Failed to parse data. Cannot set instance {InstanceID}{InstancePropertyPath} based on model {ModelID} to {Data}", + instanceID, + instancePropertyPath, + modelID, + data); + throw new NotSupportedException(); } } } diff --git a/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnectorService.cs b/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnectorService.cs index 44d3990..696f7ad 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnectorService.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnectorService.cs @@ -43,7 +43,7 @@ public override async Task UpdateDigitalTwin(UpdateDi } catch (Exception ex) { - _logger.LogError("Error updating digital twin: {Message}", ex.Message); + _logger.LogError("Error updating digital twin: {ExceptionType}: {Message}", ex.GetType(), ex.Message); throw; } From 9572c4423a730595ab88c5baf1d0ff4b46d050a6 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:49:22 -0800 Subject: [PATCH 03/19] updated some doc comments --- .../src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs index f7e3e8c..7c9986a 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs @@ -56,6 +56,8 @@ public DigitalTwinsClientWrapper(DigitalTwinsClient client, ILoggerthe digital twin instance ID. /// the property path of a digital twin instance to update. /// the data used to update a digital twin instance's property. + /// Rethrown if the client throws this exception + /// Thrown if the data parameter could not be parsed /// Returns a task for updating a digital twin instance. public async Task UpdateDigitalTwinAsync(string modelID, string instanceID, string instancePropertyPath, string data) { From b23007d5e08ac9eeb01bc6b7dc84b59af35bb9b4 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 11:36:35 -0800 Subject: [PATCH 04/19] add workflow and aplly more suggestions --- .github/workflows/dotnet-ci.yml | 11 ++++ .../DigitalTwinsClientWrapper.cs | 2 +- .../src/core/Program.cs | 2 +- .../mqtt_connector/azure_function/src/run.cs | 63 +++++++++++-------- .../tests/MQTTConnectorAzureFunction.Tests.cs | 59 +++++++++++------ 5 files changed, 90 insertions(+), 47 deletions(-) diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index ede5ecc..f204fc6 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -31,6 +31,17 @@ jobs: run: ./cloud_connectors/azure/digital_twins_connector/build.sh - name: Build MQTT Connector's Azure Function run: dotnet build cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj + - name: Run .NET Code Analysis + uses: dotnet/code-analysis@v1 + id: net-code-analysis + with: + projects: cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj; + cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj; + cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj; + cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj; + cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.csproj + build-breaking: true + all-categories: latest-recommended - name: Digital Twins Connector Tests run: dotnet test cloud_connectors/azure/digital_twins_connector/tests/**/*.csproj - name: MQTT Connector's Azure Function Tests diff --git a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs index 7c9986a..8a321c1 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs @@ -28,7 +28,7 @@ public class DigitalTwinsClientWrapper /// /// the path. /// Returns true if the path starts with a slash, otherwise false. - private bool DoesPathStartsWithSlash(string path) + private static bool DoesPathStartsWithSlash(string path) { return path.StartsWith('/'); } diff --git a/cloud_connectors/azure/digital_twins_connector/src/core/Program.cs b/cloud_connectors/azure/digital_twins_connector/src/core/Program.cs index 695692e..45cef5f 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/core/Program.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/core/Program.cs @@ -29,7 +29,7 @@ static void Main(string[] args) string adtInstanceUrl = adtInstanceConfig.AzureDigitalTwinsInstanceUrl; var credential = new DefaultAzureCredential(); - DigitalTwinsClient client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credential); + DigitalTwinsClient client = new(new Uri(adtInstanceUrl), credential); ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole(c => { diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs index 3fe8523..3d8d087 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; -using System.ComponentModel; +using System.Text.Json.Serialization; using System.Threading.Tasks; using Azure; @@ -20,10 +20,17 @@ namespace Microsoft.ESDV.CloudConnector.Azure { /// This class contains the info to target an Azure Digital Twin instance. /// public class DigitalTwinsInstance { - public string model_id { get; set; } - public string instance_id { get; set; } - public string instance_property_path { get; set; } - public string data { get; set; } + [JsonPropertyName("model_id")] + public string ModelId { get; set; } + + [JsonPropertyName("instance_id")] + public string InstanceId { get; set; } + + [JsonPropertyName("instance_property_path")] + public string InstancePropertyPath { get; set; } + + [JsonPropertyName("data")] + public string Data { get; set; } } public class MQTTConnectorAzureFunction { @@ -32,7 +39,8 @@ public class MQTTConnectorAzureFunction { private const string KEYVAULT_SETTINGS = "KEYVAULT_SETTINGS"; // Maps a string data type name to its concrete data type. - private static readonly Dictionary dataTypeNameToConverterMap = new Dictionary { + private static readonly Dictionary dataTypeNameToConverterMap = new() + { { "int", typeof(int) }, { "double", typeof(double) }, { "boolean", typeof(bool) } @@ -57,10 +65,11 @@ public static bool DoesPathStartsWithSlash(string path) { /// /// the name of the data type. /// Returns a task for updating a digital twin instance. - public Type GetDataTypeFromString(string dataTypeName) { + public static Type GetDataTypeFromString(string dataTypeName) { if (!dataTypeNameToConverterMap.ContainsKey(dataTypeName)) { throw new NotSupportedException($"No conversion for {dataTypeName}"); } + return dataTypeNameToConverterMap[dataTypeName]; } @@ -71,31 +80,35 @@ public Type GetDataTypeFromString(string dataTypeName) { /// the digital twin instance to update. /// the name of the data type. /// Returns a task for updating a digital twin instance. - public async Task UpdateDigitalTwinAsync(DigitalTwinsClient client, DigitalTwinsInstance instance, string dataTypeName = "double") { - JsonPatchDocument jsonPatchDocument = new JsonPatchDocument(); + public static async Task UpdateDigitalTwinAsync(DigitalTwinsClient client, DigitalTwinsInstance instance, string dataTypeName = "double") { + JsonPatchDocument jsonPatchDocument = new(); - try { + try + { // Get the concrete data type of an instance's data based on its string data type name // then uses that concrete data type to change the data from string to its concrete data type. Type dataType = GetDataTypeFromString(dataTypeName); - dynamic convertedDataToType = Convert.ChangeType(instance.data, dataType); + dynamic convertedDataToType = Convert.ChangeType(instance.Data, dataType); - if (!DoesPathStartsWithSlash(instance.instance_property_path)) + if (!DoesPathStartsWithSlash(instance.InstancePropertyPath)) { - instance.instance_property_path = $"/{instance.instance_property_path}"; + instance.InstancePropertyPath = $"/{instance.InstancePropertyPath}"; } - jsonPatchDocument.AppendAdd(instance.instance_property_path, convertedDataToType); + jsonPatchDocument.AppendAdd(instance.InstancePropertyPath, convertedDataToType); } - catch (Exception ex) when (ex is NotSupportedException || ex is InvalidCastException || ex is FormatException) { - throw new NotSupportedException($"Cannot convert {instance.data}. {ex.Message}"); + catch (Exception ex) when (ex is NotSupportedException || ex is InvalidCastException || ex is FormatException) + { + throw new NotSupportedException($"Cannot convert {instance.Data}. {ex.Message}"); } - try { - await client.UpdateDigitalTwinAsync(instance.instance_id, jsonPatchDocument); + try + { + await client.UpdateDigitalTwinAsync(instance.InstanceId, jsonPatchDocument); } - catch(RequestFailedException ex) { - string errorMessage = @$"Cannot set instance {instance.instance_id}{instance.instance_property_path} - based on model {instance.model_id} to {instance.data} due to {ex.Message}"; + catch(RequestFailedException ex) + { + string errorMessage = @$"Cannot set instance {instance.InstanceId}{instance.InstancePropertyPath} + based on model {instance.ModelId} to {instance.Data} due to {ex.Message}"; throw new NotSupportedException(errorMessage); } } @@ -112,12 +125,12 @@ public async Task Run([EventGridTrigger] CloudEvent cloudEvent) { DigitalTwinsInstance instance = cloudEvent.Data.ToObjectFromJson(); try { - DefaultAzureCredential credential = new DefaultAzureCredential(); + DefaultAzureCredential credential = new(); string adt_instance_url = Environment.GetEnvironmentVariable(KEYVAULT_SETTINGS, EnvironmentVariableTarget.Process); - DigitalTwinsClient client = new DigitalTwinsClient(new Uri(adt_instance_url), credential); + DigitalTwinsClient client = new(new Uri(adt_instance_url), credential); await UpdateDigitalTwinAsync(client, instance); - _logger.LogInformation(@$"Successfully set instance {instance.instance_id}{instance.instance_property_path} - based on model {instance.model_id} to {instance.data}"); + _logger.LogInformation(@$"Successfully set instance {instance.InstanceId}{instance.InstancePropertyPath} + based on model {instance.ModelId} to {instance.Data}"); } catch (Exception ex) { _logger.LogError(ex.Message); diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs b/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs index 19bde42..01fbd39 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. // SPDX-License-Identifier: MIT +using System.Text.Json; using Azure.DigitalTwins.Core; using Microsoft.Extensions.Logging; using Moq; @@ -22,49 +23,67 @@ public void Setup() _connector = new MQTTConnectorAzureFunction(new Mock>().Object); _instance = new DigitalTwinsInstance { - model_id = "some-model", - instance_id = "some-instance", - instance_property_path = "some-instance-property", - data = null + ModelId = "some-model", + InstanceId = "some-instance", + InstancePropertyPath = "some-instance-property", + Data = null }; } [Test] public void ConvertStringToDataType_ShouldSucceed() { - Assert.That(_connector.GetDataTypeFromString("int"), Is.EqualTo(typeof(int))); - Assert.That(_connector.GetDataTypeFromString("double"), Is.EqualTo(typeof(double))); - Assert.That(_connector.GetDataTypeFromString("boolean"), Is.EqualTo(typeof(bool))); - Assert.Throws(() => _connector.GetDataTypeFromString("invalid-converter")); + Assert.Multiple(() => + { + Assert.That(MQTTConnectorAzureFunction.GetDataTypeFromString("int"), Is.EqualTo(typeof(int))); + Assert.That(MQTTConnectorAzureFunction.GetDataTypeFromString("double"), Is.EqualTo(typeof(double))); + Assert.That(MQTTConnectorAzureFunction.GetDataTypeFromString("boolean"), Is.EqualTo(typeof(bool))); + }); + Assert.Throws(() => MQTTConnectorAzureFunction.GetDataTypeFromString("invalid-converter")); } [Test] public async Task UpdateDigitalTwinAsync_ShouldSucceed() { - _instance.data = "44.5"; - await _connector.UpdateDigitalTwinAsync(_client, _instance, "double"); + _instance.Data = "44.5"; + await MQTTConnectorAzureFunction.UpdateDigitalTwinAsync(_client, _instance, "double"); Assert.Pass(); - _instance.data = "44"; - await _connector.UpdateDigitalTwinAsync(_client, _instance, "int"); + _instance.Data = "44"; + await MQTTConnectorAzureFunction.UpdateDigitalTwinAsync(_client, _instance, "int"); Assert.Pass(); - _instance.data = "true"; - await _connector.UpdateDigitalTwinAsync(_client, _instance, "boolean"); + _instance.Data = "true"; + await MQTTConnectorAzureFunction.UpdateDigitalTwinAsync(_client, _instance, "boolean"); Assert.Pass(); } [Test] public void UpdateDigitalTwinAsync_ThrowNotSupported() { - _instance.data = null; - Assert.ThrowsAsync(async () => await _connector.UpdateDigitalTwinAsync(_client, _instance)); + _instance.Data = null; + Assert.ThrowsAsync(async () => await MQTTConnectorAzureFunction.UpdateDigitalTwinAsync(_client, _instance)); + + _instance.Data = "test1234"; + Assert.ThrowsAsync(async () => await MQTTConnectorAzureFunction.UpdateDigitalTwinAsync(_client, _instance, "invalid-converter")); + + _instance.Data = ""; + Assert.ThrowsAsync(async () => await MQTTConnectorAzureFunction.UpdateDigitalTwinAsync(_client, _instance, "double")); + } + + [Test] + public void CanDeserializeDigitalTwinsInstance() + { + string input = @"{ + ""model_id"": ""some-model"", + ""instance_id"": ""some-instance"", + ""instance_property_path"": ""some-instance-property"", + ""data"": ""42"" + }"; - _instance.data = "test1234"; - Assert.ThrowsAsync(async () => await _connector.UpdateDigitalTwinAsync(_client, _instance, "invalid-converter")); + BinaryData data = BinaryData.FromString(input); - _instance.data = ""; - Assert.ThrowsAsync(async () => await _connector.UpdateDigitalTwinAsync(_client, _instance, "double")); + Assert.DoesNotThrow(() => data.ToObjectFromJson()); } } } From 71e19304f71241f3a4ac1608227fd1a7dc8c030f Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 11:39:15 -0800 Subject: [PATCH 05/19] fix workflow --- .github/workflows/dotnet-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index f204fc6..ffa6c5a 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -32,7 +32,7 @@ jobs: - name: Build MQTT Connector's Azure Function run: dotnet build cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj - name: Run .NET Code Analysis - uses: dotnet/code-analysis@v1 + uses: dotnet/code-analysis@main id: net-code-analysis with: projects: cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj; From 80f3b183d29081f5e0bf4cf67a0f286299a31a65 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 12:18:48 -0800 Subject: [PATCH 06/19] version varaible --- .github/workflows/dotnet-ci.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index ffa6c5a..9ebc953 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -8,16 +8,19 @@ on: branches: - main +env: + DOTNET_VERSION: 6.0.119 + jobs: static_code_analysis: runs-on: ubuntu-20.04 steps: - name: Checkout repository and submodules uses: actions/checkout@v3 - - name: Install .NET 6.0.119 + - name: Install .NET $DOTNET_VERSION uses: actions/setup-dotnet@v3 with: - dotnet-version: 6.0.119 + dotnet-version: $DOTNET_VERSION global-json-file: cloud_connectors/azure/digital_twins_connector/global.json - name: Cache NuGet dependencies uses: actions/cache@v3 @@ -35,6 +38,7 @@ jobs: uses: dotnet/code-analysis@main id: net-code-analysis with: + dotnet-version: $DOTNET_VERSION projects: cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj; cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj; cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj; From 52a64215b39648d30a143fbd177275e61cb4aaf6 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 12:21:14 -0800 Subject: [PATCH 07/19] try to fix syntax --- .github/workflows/dotnet-ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index 9ebc953..b40cf51 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -17,10 +17,10 @@ jobs: steps: - name: Checkout repository and submodules uses: actions/checkout@v3 - - name: Install .NET $DOTNET_VERSION + - name: Install .NET ${DOTNET_VERSION} uses: actions/setup-dotnet@v3 with: - dotnet-version: $DOTNET_VERSION + dotnet-version: ${DOTNET_VERSION} global-json-file: cloud_connectors/azure/digital_twins_connector/global.json - name: Cache NuGet dependencies uses: actions/cache@v3 @@ -38,7 +38,7 @@ jobs: uses: dotnet/code-analysis@main id: net-code-analysis with: - dotnet-version: $DOTNET_VERSION + dotnet-version: ${DOTNET_VERSION} projects: cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj; cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj; cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj; From 4450633eb0c792a2862760315608013820002606 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 12:22:54 -0800 Subject: [PATCH 08/19] try to fix syntax --- .github/workflows/dotnet-ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index b40cf51..9afe42a 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -17,10 +17,10 @@ jobs: steps: - name: Checkout repository and submodules uses: actions/checkout@v3 - - name: Install .NET ${DOTNET_VERSION} + - name: Install .NET ${{ env.DOTNET_VERSION }} uses: actions/setup-dotnet@v3 with: - dotnet-version: ${DOTNET_VERSION} + dotnet-version: ${{ env.DOTNET_VERSION }} global-json-file: cloud_connectors/azure/digital_twins_connector/global.json - name: Cache NuGet dependencies uses: actions/cache@v3 @@ -38,7 +38,7 @@ jobs: uses: dotnet/code-analysis@main id: net-code-analysis with: - dotnet-version: ${DOTNET_VERSION} + dotnet-version: ${{ env.DOTNET_VERSION }} projects: cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj; cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj; cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj; From a0ae94eeee12d4678ebf413c914ac4d864637478 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 12:26:46 -0800 Subject: [PATCH 09/19] try installing older .net for workflow --- .github/workflows/dotnet-ci.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index 9afe42a..a7ae476 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -10,6 +10,7 @@ on: env: DOTNET_VERSION: 6.0.119 + CODE_ANALYSIS_DOTNET_VERSION: 3.1.x jobs: static_code_analysis: @@ -17,10 +18,12 @@ jobs: steps: - name: Checkout repository and submodules uses: actions/checkout@v3 - - name: Install .NET ${{ env.DOTNET_VERSION }} + - name: Install .NET uses: actions/setup-dotnet@v3 with: - dotnet-version: ${{ env.DOTNET_VERSION }} + dotnet-version: | + ${{ env.DOTNET_VERSION }} + ${{ env.CODE_ANALYSIS_DOTNET_VERSION }} global-json-file: cloud_connectors/azure/digital_twins_connector/global.json - name: Cache NuGet dependencies uses: actions/cache@v3 @@ -38,7 +41,6 @@ jobs: uses: dotnet/code-analysis@main id: net-code-analysis with: - dotnet-version: ${{ env.DOTNET_VERSION }} projects: cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj; cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj; cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj; From 870f631c17e9b424db02dc35abfaeae03c0c3792 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 13:00:43 -0800 Subject: [PATCH 10/19] bake analysis into build since workflow is unmaintained --- .github/workflows/dotnet-ci.yml | 22 +++++-------------- cloud_connectors/azure/.globalconfig | 6 +++++ .../azure/digital_twins_connector/build.sh | 6 ++--- .../DigitalTwinsClientWrapper.cs | 2 +- .../DigitalTwinsClientWrapper.csproj | 1 + .../src/core/DigitalTwinsConnector.csproj | 1 + .../DigitalTwinsClientWrapper.Tests.cs | 4 ++-- .../DigitalTwinsClientWrapper.Tests.csproj | 1 + .../azure_function/src/function.csproj | 1 + .../mqtt_connector/azure_function/src/run.cs | 3 ++- .../tests/MQTTConnectorAzureFunction.Tests.cs | 4 ++-- .../MQTTConnectorAzureFunction.Tests.csproj | 1 + 12 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 cloud_connectors/azure/.globalconfig diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index a7ae476..3ffce7a 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -10,7 +10,6 @@ on: env: DOTNET_VERSION: 6.0.119 - CODE_ANALYSIS_DOTNET_VERSION: 3.1.x jobs: static_code_analysis: @@ -18,12 +17,10 @@ jobs: steps: - name: Checkout repository and submodules uses: actions/checkout@v3 - - name: Install .NET + - name: Install .NET ${{ env.DOTNET_VERSION }} uses: actions/setup-dotnet@v3 with: - dotnet-version: | - ${{ env.DOTNET_VERSION }} - ${{ env.CODE_ANALYSIS_DOTNET_VERSION }} + dotnet-version: ${{ env.DOTNET_VERSION }} global-json-file: cloud_connectors/azure/digital_twins_connector/global.json - name: Cache NuGet dependencies uses: actions/cache@v3 @@ -36,18 +33,9 @@ jobs: - name: Build Digital Twins Connector run: ./cloud_connectors/azure/digital_twins_connector/build.sh - name: Build MQTT Connector's Azure Function - run: dotnet build cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj - - name: Run .NET Code Analysis - uses: dotnet/code-analysis@main - id: net-code-analysis - with: - projects: cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj; - cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj; - cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj; - cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj; - cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.csproj - build-breaking: true - all-categories: latest-recommended + run: | + dotnet build cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj -warnaserror + && dotnet build cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.csproj -warnaserror - name: Digital Twins Connector Tests run: dotnet test cloud_connectors/azure/digital_twins_connector/tests/**/*.csproj - name: MQTT Connector's Azure Function Tests diff --git a/cloud_connectors/azure/.globalconfig b/cloud_connectors/azure/.globalconfig new file mode 100644 index 0000000..53317ae --- /dev/null +++ b/cloud_connectors/azure/.globalconfig @@ -0,0 +1,6 @@ +# Global analyzer config for .NET projects +is_global = true + +# The way to address this rule is not well documented and appears to be very complex, +# so it's left as a suggestion for now +dotnet_diagnostic.CA1848.severity = suggestion \ No newline at end of file diff --git a/cloud_connectors/azure/digital_twins_connector/build.sh b/cloud_connectors/azure/digital_twins_connector/build.sh index 9b1b69f..8566180 100755 --- a/cloud_connectors/azure/digital_twins_connector/build.sh +++ b/cloud_connectors/azure/digital_twins_connector/build.sh @@ -7,6 +7,6 @@ # Set the current directory to the directory of this script. cd "$(dirname "$0")" -dotnet build src/core/DigitalTwinsConnector.csproj -dotnet build src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj -dotnet build tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj \ No newline at end of file +dotnet build src/core/DigitalTwinsConnector.csproj -warnaserror +dotnet build src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj -warnaserror +dotnet build tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj -warnaserror \ No newline at end of file diff --git a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs index 8a321c1..058a7ec 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs @@ -99,7 +99,7 @@ public async Task UpdateDigitalTwinAsync(string modelID, string instanceID, stri modelID, data, ex.Message); - throw; + throw ex; } // Try to parse string data with the next type if we're unsuccessful. catch (Exception ex) when (ex is NotSupportedException || ex is ArgumentException || ex is FormatException) diff --git a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj index 6f27d36..4242c4d 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj +++ b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.csproj @@ -3,6 +3,7 @@ net6.0 enable + latest-recommended diff --git a/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj b/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj index 88cae81..6ba07ce 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj +++ b/cloud_connectors/azure/digital_twins_connector/src/core/DigitalTwinsConnector.csproj @@ -4,6 +4,7 @@ Exe net6.0 enable + latest-recommended diff --git a/cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.cs b/cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.cs index 09be11f..bfe43bf 100644 --- a/cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.cs +++ b/cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.cs @@ -23,7 +23,7 @@ public void Setup() } [Test] - public async Task UpdateDigitalTwinAsync_ShouldSucceed() + public async Task UpdateDigitalTwinAsyncShouldSucceed() { const string modelID = "some-model"; const string instanceID = "some-instance"; @@ -33,7 +33,7 @@ public async Task UpdateDigitalTwinAsync_ShouldSucceed() } [Test] - public void UpdateDigitalTwinAsync_ThrowNotSupported() + public void UpdateDigitalTwinAsyncThrowNotSupported() { const string modelID = "some-model"; const string instanceID = "some-instance"; diff --git a/cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj b/cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj index b0409a1..20b3fec 100644 --- a/cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj +++ b/cloud_connectors/azure/digital_twins_connector/tests/DigitalTwinsClientWrapper.Tests/DigitalTwinsClientWrapper.Tests.csproj @@ -4,6 +4,7 @@ net6.0 enable false + latest-recommended diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj b/cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj index 9f0674c..323b768 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj +++ b/cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj @@ -2,6 +2,7 @@ net6.0 v4 + latest-recommended diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs index 3d8d087..ec01ad3 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Text.Json.Serialization; using System.Threading.Tasks; @@ -88,7 +89,7 @@ public static async Task UpdateDigitalTwinAsync(DigitalTwinsClient client, Digit // Get the concrete data type of an instance's data based on its string data type name // then uses that concrete data type to change the data from string to its concrete data type. Type dataType = GetDataTypeFromString(dataTypeName); - dynamic convertedDataToType = Convert.ChangeType(instance.Data, dataType); + dynamic convertedDataToType = Convert.ChangeType(instance.Data, dataType, CultureInfo.InvariantCulture); if (!DoesPathStartsWithSlash(instance.InstancePropertyPath)) { diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs b/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs index 01fbd39..d5a459f 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs @@ -43,7 +43,7 @@ public void ConvertStringToDataType_ShouldSucceed() } [Test] - public async Task UpdateDigitalTwinAsync_ShouldSucceed() + public async Task UpdateDigitalTwinAsyncShouldSucceed() { _instance.Data = "44.5"; await MQTTConnectorAzureFunction.UpdateDigitalTwinAsync(_client, _instance, "double"); @@ -59,7 +59,7 @@ public async Task UpdateDigitalTwinAsync_ShouldSucceed() } [Test] - public void UpdateDigitalTwinAsync_ThrowNotSupported() + public void UpdateDigitalTwinAsyncThrowNotSupported() { _instance.Data = null; Assert.ThrowsAsync(async () => await MQTTConnectorAzureFunction.UpdateDigitalTwinAsync(_client, _instance)); diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.csproj b/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.csproj index 24c4e2d..ac71991 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.csproj +++ b/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.csproj @@ -4,6 +4,7 @@ net6.0 enable false + latest-recommended From 97ec2e0cfc254fa70c6bc12b1bdfc4b26ba623ec Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 13:23:46 -0800 Subject: [PATCH 11/19] fix remaining error --- .../azure/mqtt_connector/azure_function/src/run.cs | 4 ++-- .../azure_function/tests/MQTTConnectorAzureFunction.Tests.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs index ec01ad3..3ebc66f 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs @@ -67,11 +67,11 @@ public static bool DoesPathStartsWithSlash(string path) { /// the name of the data type. /// Returns a task for updating a digital twin instance. public static Type GetDataTypeFromString(string dataTypeName) { - if (!dataTypeNameToConverterMap.ContainsKey(dataTypeName)) { + if (!dataTypeNameToConverterMap.TryGetValue(dataTypeName, out Type value)) { throw new NotSupportedException($"No conversion for {dataTypeName}"); } - return dataTypeNameToConverterMap[dataTypeName]; + return value; } /// diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs b/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs index d5a459f..5521487 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.cs @@ -31,7 +31,7 @@ public void Setup() } [Test] - public void ConvertStringToDataType_ShouldSucceed() + public void ConvertStringToDataTypeShouldSucceed() { Assert.Multiple(() => { From 40619f9c85d80e4de1ba47055a8a374be73e77d1 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 13:25:49 -0800 Subject: [PATCH 12/19] fix workflow --- .github/workflows/dotnet-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index 3ffce7a..507f217 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -35,7 +35,7 @@ jobs: - name: Build MQTT Connector's Azure Function run: | dotnet build cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj -warnaserror - && dotnet build cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.csproj -warnaserror + dotnet build cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.csproj -warnaserror - name: Digital Twins Connector Tests run: dotnet test cloud_connectors/azure/digital_twins_connector/tests/**/*.csproj - name: MQTT Connector's Azure Function Tests From f614593267e3b2aa7d3674dbfd7f9e3b70a00639 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 13:31:24 -0800 Subject: [PATCH 13/19] more updates --- .../src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs index 058a7ec..8a321c1 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/DigitalTwinsClientWrapper/DigitalTwinsClientWrapper.cs @@ -99,7 +99,7 @@ public async Task UpdateDigitalTwinAsync(string modelID, string instanceID, stri modelID, data, ex.Message); - throw ex; + throw; } // Try to parse string data with the next type if we're unsuccessful. catch (Exception ex) when (ex is NotSupportedException || ex is ArgumentException || ex is FormatException) From bb7a4e811a97ecdbcdaa84887a73838ad9810dec Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 13:32:58 -0800 Subject: [PATCH 14/19] fix workflow --- .github/workflows/dotnet-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index 507f217..5001959 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -35,7 +35,7 @@ jobs: - name: Build MQTT Connector's Azure Function run: | dotnet build cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj -warnaserror - dotnet build cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.csproj -warnaserror + dotnet build cloud_connectors/azure/mqtt_connector/azure_function/testsMQTTConnectorAzureFunction.Tests.csproj -warnaserror - name: Digital Twins Connector Tests run: dotnet test cloud_connectors/azure/digital_twins_connector/tests/**/*.csproj - name: MQTT Connector's Azure Function Tests From 65142f79c632e06d669a38ff968133e0429c58f6 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 13:34:07 -0800 Subject: [PATCH 15/19] fix workflow --- .github/workflows/dotnet-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dotnet-ci.yml b/.github/workflows/dotnet-ci.yml index 5001959..9fe1dbf 100644 --- a/.github/workflows/dotnet-ci.yml +++ b/.github/workflows/dotnet-ci.yml @@ -35,7 +35,7 @@ jobs: - name: Build MQTT Connector's Azure Function run: | dotnet build cloud_connectors/azure/mqtt_connector/azure_function/src/function.csproj -warnaserror - dotnet build cloud_connectors/azure/mqtt_connector/azure_function/testsMQTTConnectorAzureFunction.Tests.csproj -warnaserror + dotnet build cloud_connectors/azure/mqtt_connector/azure_function/tests/MQTTConnectorAzureFunction.Tests.csproj -warnaserror - name: Digital Twins Connector Tests run: dotnet test cloud_connectors/azure/digital_twins_connector/tests/**/*.csproj - name: MQTT Connector's Azure Function Tests From 26286c1f069eb99ecb4ff0b735f0a4e582e89f69 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 14:06:30 -0800 Subject: [PATCH 16/19] clean up formatting --- .../src/core/Program.cs | 1 + .../mqtt_connector/azure_function/src/run.cs | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cloud_connectors/azure/digital_twins_connector/src/core/Program.cs b/cloud_connectors/azure/digital_twins_connector/src/core/Program.cs index 45cef5f..aabd46e 100644 --- a/cloud_connectors/azure/digital_twins_connector/src/core/Program.cs +++ b/cloud_connectors/azure/digital_twins_connector/src/core/Program.cs @@ -36,6 +36,7 @@ static void Main(string[] args) c.TimestampFormat = "[yyyy-MM-ddTHH:mm::ssZ] "; c.UseUtcTimestamp = true; })); + loggerFactory.CreateLogger("Main").LogInformation("Started the Azure Digital Twins Connector"); // Instantiate the DigitalTwinClient first before adding it as a service for dependency injection. diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs index 3ebc66f..5e48beb 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs @@ -20,7 +20,8 @@ namespace Microsoft.ESDV.CloudConnector.Azure { /// /// This class contains the info to target an Azure Digital Twin instance. /// - public class DigitalTwinsInstance { + public class DigitalTwinsInstance + { [JsonPropertyName("model_id")] public string ModelId { get; set; } @@ -34,7 +35,8 @@ public class DigitalTwinsInstance { public string Data { get; set; } } - public class MQTTConnectorAzureFunction { + public class MQTTConnectorAzureFunction + { private readonly ILogger _logger; private const string KEYVAULT_SETTINGS = "KEYVAULT_SETTINGS"; @@ -57,7 +59,8 @@ public MQTTConnectorAzureFunction(ILogger logger) /// /// the path. /// Returns true if the path starts with a slash, otherwise false. - public static bool DoesPathStartsWithSlash(string path) { + public static bool DoesPathStartsWithSlash(string path) + { return path.StartsWith('/'); } @@ -66,8 +69,10 @@ public static bool DoesPathStartsWithSlash(string path) { /// /// the name of the data type. /// Returns a task for updating a digital twin instance. - public static Type GetDataTypeFromString(string dataTypeName) { - if (!dataTypeNameToConverterMap.TryGetValue(dataTypeName, out Type value)) { + public static Type GetDataTypeFromString(string dataTypeName) + { + if (!dataTypeNameToConverterMap.TryGetValue(dataTypeName, out Type value)) + { throw new NotSupportedException($"No conversion for {dataTypeName}"); } @@ -81,7 +86,8 @@ public static Type GetDataTypeFromString(string dataTypeName) { /// the digital twin instance to update. /// the name of the data type. /// Returns a task for updating a digital twin instance. - public static async Task UpdateDigitalTwinAsync(DigitalTwinsClient client, DigitalTwinsInstance instance, string dataTypeName = "double") { + public static async Task UpdateDigitalTwinAsync(DigitalTwinsClient client, DigitalTwinsInstance instance, string dataTypeName = "double") + { JsonPatchDocument jsonPatchDocument = new(); try @@ -122,10 +128,12 @@ public static async Task UpdateDigitalTwinAsync(DigitalTwinsClient client, Digit /// An exception is thrown if the Azure Digital Twin client cannot update an instance. /// [FunctionName("MQTTConnectorAzureFunction")] - public async Task Run([EventGridTrigger] CloudEvent cloudEvent) { + public async Task Run([EventGridTrigger] CloudEvent cloudEvent) + { DigitalTwinsInstance instance = cloudEvent.Data.ToObjectFromJson(); - try { + try + { DefaultAzureCredential credential = new(); string adt_instance_url = Environment.GetEnvironmentVariable(KEYVAULT_SETTINGS, EnvironmentVariableTarget.Process); DigitalTwinsClient client = new(new Uri(adt_instance_url), credential); @@ -133,7 +141,8 @@ public async Task Run([EventGridTrigger] CloudEvent cloudEvent) { _logger.LogInformation(@$"Successfully set instance {instance.InstanceId}{instance.InstancePropertyPath} based on model {instance.ModelId} to {instance.Data}"); } - catch (Exception ex) { + catch (Exception ex) + { _logger.LogError(ex.Message); throw; } From 1fc3ade67f2de84be2dde2aa83d72464404ea699 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 20:08:08 -0800 Subject: [PATCH 17/19] Update cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs Co-authored-by: jorchiu <122116059+jorchiu@users.noreply.github.com> --- .../azure/mqtt_connector/azure_function/src/run.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs index 5e48beb..2891fff 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs @@ -68,7 +68,8 @@ public static bool DoesPathStartsWithSlash(string path) /// Gets the data type from a data type name. /// /// the name of the data type. - /// Returns a task for updating a digital twin instance. + /// Thrown if the data type is not supported. + /// Returns a task for updating a digital twin instance. public static Type GetDataTypeFromString(string dataTypeName) { if (!dataTypeNameToConverterMap.TryGetValue(dataTypeName, out Type value)) From 677692c5854dd09592449847a0d84eece6e294bc Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Mon, 20 Nov 2023 20:11:04 -0800 Subject: [PATCH 18/19] fix format --- cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs index 2891fff..45306f8 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs @@ -69,7 +69,7 @@ public static bool DoesPathStartsWithSlash(string path) /// /// the name of the data type. /// Thrown if the data type is not supported. - /// Returns a task for updating a digital twin instance. + /// Returns a task for updating a digital twin instance. public static Type GetDataTypeFromString(string dataTypeName) { if (!dataTypeNameToConverterMap.TryGetValue(dataTypeName, out Type value)) From e70fe0cbe05720ff4c32b920839232974d46e7c6 Mon Sep 17 00:00:00 2001 From: William Lyles <26171886+wilyle@users.noreply.github.com> Date: Tue, 21 Nov 2023 10:39:11 -0800 Subject: [PATCH 19/19] comment cleanup --- .../mqtt_connector/azure_function/src/run.cs | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs index 45306f8..c2b54fe 100644 --- a/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs +++ b/cloud_connectors/azure/mqtt_connector/azure_function/src/run.cs @@ -22,26 +22,50 @@ namespace Microsoft.ESDV.CloudConnector.Azure { /// public class DigitalTwinsInstance { + /// + /// The Azure Digital Twins model ID + /// [JsonPropertyName("model_id")] public string ModelId { get; set; } + /// + /// The Azure Digital Twins instance ID + /// [JsonPropertyName("instance_id")] public string InstanceId { get; set; } + /// + /// The Azure Digital Twins instance property path + /// [JsonPropertyName("instance_property_path")] public string InstancePropertyPath { get; set; } + /// + /// The data to synchronize + /// [JsonPropertyName("data")] public string Data { get; set; } } + /// + /// Azure function for use with the MQTT connector. + /// Reads data from an event grid and forwards it to Azure Digital Twins. + /// public class MQTTConnectorAzureFunction { + /// + /// The logger for this function + /// private readonly ILogger _logger; + /// + /// The environment variable name for the keyvault settings + /// private const string KEYVAULT_SETTINGS = "KEYVAULT_SETTINGS"; - // Maps a string data type name to its concrete data type. + /// + /// Maps a string data type name to its concrete data type. + /// private static readonly Dictionary dataTypeNameToConverterMap = new() { { "int", typeof(int) }, @@ -49,6 +73,10 @@ public class MQTTConnectorAzureFunction { "boolean", typeof(bool) } }; + /// + /// Create a new MQTTConnectorAzureFunction + /// + /// The logger to use public MQTTConnectorAzureFunction(ILogger logger) { _logger = logger; @@ -67,7 +95,7 @@ public static bool DoesPathStartsWithSlash(string path) /// /// Gets the data type from a data type name. /// - /// the name of the data type. + /// the name of the data type. /// Thrown if the data type is not supported. /// Returns a task for updating a digital twin instance. public static Type GetDataTypeFromString(string dataTypeName) @@ -85,7 +113,7 @@ public static Type GetDataTypeFromString(string dataTypeName) /// /// the Azure Digital Twins client. /// the digital twin instance to update. - /// the name of the data type. + /// the name of the data type. Defaults to "double". /// Returns a task for updating a digital twin instance. public static async Task UpdateDigitalTwinAsync(DigitalTwinsClient client, DigitalTwinsInstance instance, string dataTypeName = "double") {