Skip to content

Commit

Permalink
Bug fixes, docs and additional logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
barnstee committed Jan 21, 2024
1 parent 2db397d commit 7caadc5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
27 changes: 21 additions & 6 deletions Applications/ADXClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace UACloudTwin
using Kusto.Ingest;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Opc.Ua;
using System;
using System.Collections.Generic;
Expand All @@ -23,7 +22,7 @@ namespace UACloudTwin
using UACloudTwin.Models;

// Requires
// .create table DTDL_models (context:string, id:string, type:string, displayname: string, description:string, comment:string, contenttype:string, contentid:string, writable:bool, schema:dynamic, stringcontentname:string, contentdisplayname: string, contentdescription:string, contentcomment:string, target:string, extends: string, schemas:string)
// .create table DTDL_models (context:string, id:string, type:string, displayname: string, description:string, comment:string, contenttype:string, contentid:string, writable:bool, schema:dynamic, contentname:string, contentdisplayname: string, contentdescription:string, contentcomment:string, target:string, extends: string, schemas:string)
// to be run on ADX cluster first!

public class ADXClient : IDigitalTwinClient
Expand All @@ -37,9 +36,23 @@ public class ADXClient : IDigitalTwinClient

private static readonly ColumnMapping[] _jsonDTDLMapping = new ColumnMapping[]
{
// TODO
new ColumnMapping { ColumnName = "EventText", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.EventText" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "Properties", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.Properties" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "context", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.context" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "id", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.id" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "type", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.type" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "displayname", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.displayname" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "description", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.description" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "comment", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.comment" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "contenttype", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.contenttype" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "contentid", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.contentid" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "writable", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.writable" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "schema", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.schema" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "contentname", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.contentname" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "contentdisplayname", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.contentdisplayname" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "contentdescription", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.contentdescription" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "contentcomment", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.contentcomment" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "target", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.target" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "extends", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.extends" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
new ColumnMapping { ColumnName = "schemas", Properties = new Dictionary<string, string>{ { MappingConsts.Path, "$.schemas" }, { MappingConsts.TransformationMethod, CsvFromJsonStream_TransformationMethod.None.FastToString() } } },
};

public ADXClient(Microsoft.Extensions.Logging.ILogger<ADTClient> logger)
Expand Down Expand Up @@ -169,8 +182,9 @@ public void UploadTwinModels()
};

ADXIngest(JsonConvert.SerializeObject(newModel));
_logger.LogInformation("Uploaded DTDL content " + content.name + " of model " + model.id);

if (model.extends.Count > 1)
if (model.extends?.Count > 1)
{
for (int i = 1; i < model.extends.Count; i++)
{
Expand All @@ -196,6 +210,7 @@ public void UploadTwinModels()
};

ADXIngest(JsonConvert.SerializeObject(newModel1));
_logger.LogInformation("Uploaded additional DTDL extend " + model.extends[i] + " of model " + model.id);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions Applications/Models/DTDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace UACloudTwin.Models
{
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

public class DTDL
Expand Down Expand Up @@ -39,7 +38,7 @@ public class Content

public bool writable { get; set; }

public JObject schema { get; set; }
public object schema { get; set; }

public string name { get; set; }

Expand Down Expand Up @@ -72,7 +71,7 @@ public class FlattenedModel

public bool writable { get; set; }

public JObject schema { get; set; }
public object schema { get; set; }

public string contentname { get; set; }

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ The following environment variables **can optionally** be defined:
* ADT_HOSTNAME - the hostname of the Azure Digital Twins instance UA Cloud Twin should connect to
* USE_ISA95_EQUIPMENT_MODELS - Use the ISA95 equipment models (equipment and equipment property) for the mapping from OPC UA metadata to ISA95
* USE_ADX - Use Azure Data Explorer for storing the digital twin graph instead of Azure Digital Twins service
* ADX_INSTANCE_URL - URL of ADX cluster
* ADX_DB_NAME - ADX database name
* ADX_INSTANCE_URL - URL of ADX cluster for DTDL models
* ADX_DB_NAME - ADX database name for DTDL models
* ADX_TABLE_NAME - ADX table name for DTDL models

Alternatively, if an Azure IoT Hub or Azure Event Hubs are used for the broker, the Azure Event Hub connection string can be specified in the UI to avoid the need to specify the above environment variables. The Azure Event Hub connection string can be read in the Azure Portal for IoT Hub under Built-in Endpoints -> Event Hub-compatible endpoint and for Azure Event Hubs under Shared Access Policies -> RootManageSharedAccessKey -> Connection string-primary key.

Expand Down

0 comments on commit 7caadc5

Please sign in to comment.