Skip to content

Commit

Permalink
Update SysML Import
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Oct 29, 2023
1 parent 595b061 commit 3273eb3
Show file tree
Hide file tree
Showing 31 changed files with 10,752 additions and 374 deletions.
4 changes: 4 additions & 0 deletions build/MTConnect.NET-SysML-Import/CSharp/DataItemType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public static DataItemType Create(MTConnectDataItemType importModel)
exportModel.Units = UnitsHelper.Get(exportModel.Units);
}

exportModel.Id += "DataItem";
exportModel.Name += "DataItem";
if (exportModel.ParentName != null && exportModel.ParentName != "DataItem") exportModel.ParentName += "DataItem";

return exportModel;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public static InterfaceDataItemType Create(MTConnectInterfaceDataItemType import
}
}

exportModel.Id += "DataItem";
exportModel.Name += "DataItem";
if (exportModel.ParentName != null && exportModel.ParentName != "DataItem") exportModel.ParentName += "DataItem";

return exportModel;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MTConnect.SysML.Models.Devices;

namespace MTConnect.SysML.Json_cppagent
{
public class DataItemsModel
{
public List<MTConnectDataItemType> Types { get; set; } = new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static void Render(MTConnectModel mtconnectModel, string outputPath)
if (mtconnectModel != null && !string.IsNullOrEmpty(outputPath))
{
WriteComponents(mtconnectModel, outputPath);
WriteEvents(mtconnectModel, outputPath);
WriteSamples(mtconnectModel, outputPath);
}
}

Expand All @@ -20,7 +22,6 @@ private static void WriteComponents(MTConnectModel mtconnectModel, string output
var components = mtconnectModel.DeviceInformationModel.Components.Types;
foreach (var component in components.OrderBy(o => o.Type)) componentsModel.Types.Add(component);


var templateFilename = $"Components.scriban";
var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "json-cppagent", "templates", templateFilename);
if (File.Exists(templatePath))
Expand Down Expand Up @@ -51,5 +52,81 @@ private static void WriteComponents(MTConnectModel mtconnectModel, string output
}
}
}

private static void WriteEvents(MTConnectModel mtconnectModel, string outputPath)
{
var dataItemsModel = new DataItemsModel();

var dataItems = mtconnectModel.DeviceInformationModel.DataItems.Types;
foreach (var dataItem in dataItems.Where(o => o.Category == "EVENT").OrderBy(o => o.Type)) dataItemsModel.Types.Add(dataItem);

var templateFilename = $"Events.scriban";
var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "json-cppagent", "templates", templateFilename);
if (File.Exists(templatePath))
{
try
{
var templateContents = File.ReadAllText(templatePath);
if (templateContents != null)
{
var template = Template.Parse(templateContents);
var result = template.Render(dataItemsModel);
if (result != null)
{
var resultPath = "Streams/JsonEvents";
resultPath = Path.Combine(outputPath, resultPath);
resultPath = $"{resultPath}.g.cs";

var resultDirectory = Path.GetDirectoryName(resultPath);
if (!Directory.Exists(resultDirectory)) Directory.CreateDirectory(resultDirectory);

File.WriteAllText(resultPath, result);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

private static void WriteSamples(MTConnectModel mtconnectModel, string outputPath)
{
var dataItemsModel = new DataItemsModel();

var dataItems = mtconnectModel.DeviceInformationModel.DataItems.Types;
foreach (var dataItem in dataItems.Where(o => o.Category == "SAMPLE").OrderBy(o => o.Type)) dataItemsModel.Types.Add(dataItem);

var templateFilename = $"Samples.scriban";
var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "json-cppagent", "templates", templateFilename);
if (File.Exists(templatePath))
{
try
{
var templateContents = File.ReadAllText(templatePath);
if (templateContents != null)
{
var template = Template.Parse(templateContents);
var result = template.Render(dataItemsModel);
if (result != null)
{
var resultPath = "Streams/JsonSamples";
resultPath = Path.Combine(outputPath, resultPath);
resultPath = $"{resultPath}.g.cs";

var resultDirectory = Path.GetDirectoryName(resultPath);
if (!Directory.Exists(resultDirectory)) Directory.CreateDirectory(resultDirectory);

File.WriteAllText(resultPath, result);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Devices;
using MTConnect.Devices.DataItems;
using MTConnect.Observations;
using MTConnect.Observations.Output;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;

namespace MTConnect.Streams.Json
{
public class JsonEvents
{
[JsonIgnore]
public List<IObservation> Observations
{
get
{
var l = new List<IObservation>();

{{- for type in types }}
if (!{{type.name}}.IsNullOrEmpty()) foreach (var x in {{type.name}}) l.Add(x.ToObservation({{type.name}}DataItem.TypeId));
if (!{{type.name}}DataSet.IsNullOrEmpty()) foreach (var x in {{type.name}}DataSet) l.Add(x.ToObservation({{type.name}}DataItem.TypeId));
if (!{{type.name}}Table.IsNullOrEmpty()) foreach (var x in {{type.name}}Table) l.Add(x.ToObservation({{type.name}}DataItem.TypeId));
{{ end }}

return l;
}
}

{{- for type in types }}
[JsonPropertyName("{{type.name}}")]
public IEnumerable<JsonEventValue> {{type.name}} { get; set; }

[JsonPropertyName("{{type.name}}DataSet")]
public IEnumerable<JsonEventDataSet> {{type.name}}DataSet { get; set; }

[JsonPropertyName("{{type.name}}Table")]
public IEnumerable<JsonEventTable> {{type.name}}Table { get; set; }

{{ end }}

public JsonEvents() { }

public JsonEvents(IEnumerable<IObservationOutput> observations)
{
if (observations != null)
{
if (!observations.IsNullOrEmpty())
{
IEnumerable<IObservationOutput> typeObservations;

{{- for type in types }}
// Add {{type.name}}
typeObservations = observations.Where(o => o.Type == {{type.name}}DataItem.TypeId && o.Representation == DataItemRepresentation.VALUE);
if (!typeObservations.IsNullOrEmpty())
{
var jsonObservations = new List<JsonEventValue>();
foreach (var observation in typeObservations)
{
jsonObservations.Add(new JsonEventValue(observation));
}
{{type.name}} = jsonObservations;
}

// Add {{type.name}}DataSet
typeObservations = observations.Where(o => o.Type == {{type.name}}DataItem.TypeId && o.Representation == DataItemRepresentation.DATA_SET);
if (!typeObservations.IsNullOrEmpty())
{
var jsonObservations = new List<JsonEventDataSet>();
foreach (var observation in typeObservations)
{
jsonObservations.Add(new JsonEventDataSet(observation));
}
{{type.name}}DataSet = jsonObservations;
}

// Add {{type.name}}Table
typeObservations = observations.Where(o => o.Type == {{type.name}}DataItem.TypeId && o.Representation == DataItemRepresentation.TABLE);
if (!typeObservations.IsNullOrEmpty())
{
var jsonObservations = new List<JsonEventTable>();
foreach (var observation in typeObservations)
{
jsonObservations.Add(new JsonEventTable(observation));
}
{{type.name}}Table = jsonObservations;
}

{{ end }}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Devices;
using MTConnect.Devices.DataItems;
using MTConnect.Observations;
using MTConnect.Observations.Output;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;

namespace MTConnect.Streams.Json
{
public class JsonSamples
{
[JsonIgnore]
public List<IObservation> Observations
{
get
{
var l = new List<IObservation>();

{{- for type in types }}
if (!{{type.name}}.IsNullOrEmpty()) foreach (var x in {{type.name}}) l.Add(x.ToObservation({{type.name}}DataItem.TypeId));
if (!{{type.name}}DataSet.IsNullOrEmpty()) foreach (var x in {{type.name}}DataSet) l.Add(x.ToObservation({{type.name}}DataItem.TypeId));
{{ end }}

return l;
}
}

{{- for type in types }}
[JsonPropertyName("{{type.name}}")]
public IEnumerable<JsonSampleValue> {{type.name}} { get; set; }

[JsonPropertyName("{{type.name}}DataSet")]
public IEnumerable<JsonSampleDataSet> {{type.name}}DataSet { get; set; }

{{ end }}

public JsonSamples() { }

public JsonSamples(IEnumerable<IObservationOutput> observations)
{
if (observations != null)
{
if (!observations.IsNullOrEmpty())
{
IEnumerable<IObservationOutput> typeObservations;

{{- for type in types }}
// Add {{type.name}}
typeObservations = observations.Where(o => o.Type == {{type.name}}DataItem.TypeId);
if (!typeObservations.IsNullOrEmpty())
{
var jsonObservations = new List<JsonSampleValue>();
foreach (var observation in typeObservations)
{
jsonObservations.Add(new JsonSampleValue(observation));
}
{{type.name}} = jsonObservations;
}

// Add {{type.name}}DataSet
typeObservations = observations.Where(o => o.Type == {{type.name}}DataItem.TypeId && o.Representation == DataItemRepresentation.DATA_SET);
if (!typeObservations.IsNullOrEmpty())
{
var jsonObservations = new List<JsonSampleDataSet>();
foreach (var observation in typeObservations)
{
jsonObservations.Add(new JsonSampleDataSet(observation));
}
{{type.name}}DataSet = jsonObservations;
}

{{ end }}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
<None Update="CSharp\Templates\Observations.Observation.scriban">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Json-cppagent\Templates\Events.scriban">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Json-cppagent\Templates\Samples.scriban">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Json-cppagent\Templates\Components.scriban">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
2 changes: 2 additions & 0 deletions build/MTConnect.NET-SysML-Import/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

var mtconnectModel = MTConnectModel.Parse(xmlPath);


//var jsonOptions = new JsonSerializerOptions();
//jsonOptions.WriteIndented = true;
//jsonOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault;
Expand All @@ -17,6 +18,7 @@
//var json = JsonSerializer.Serialize(mtconnectModel, options: jsonOptions);
//await File.WriteAllTextAsync(@"C:\temp\mtconnect-model.json", json);


RenderCommonClasses();
RenderJsonComponents();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MTConnectVersionDataItem : DataItem
{
public const DataItemCategory CategoryId = DataItemCategory.EVENT;
public const string TypeId = "MTCONNECT_VERSION";
public const string NameId = "";
public const string NameId = "mtconnectVersion";

public new const string DescriptionText = "Reference version of the MTConnect Standard supported by the adapter.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public FormattedDocumentWriteResult Format(ref IStreamsResponseOutputDocument do
var indentOutput = GetFormatterOption<bool>(options, "indentOutput");
var jsonOptions = indentOutput ? JsonFunctions.IndentOptions : JsonFunctions.DefaultOptions;

var json = JsonSerializer.SerializeToUtf8Bytes(new JsonStreamsDocument(document), jsonOptions);
var json = JsonSerializer.SerializeToUtf8Bytes(new JsonStreamsResponseDocument(document), jsonOptions);
if (!json.IsNullOrEmpty())
{
return FormattedDocumentWriteResult.Successful(json, ContentType);
Expand Down Expand Up @@ -96,7 +96,7 @@ public FormattedDocumentReadResult<IDevicesResponseDocument> CreateDevicesRespon
public FormattedDocumentReadResult<IStreamsResponseDocument> CreateStreamsResponseDocument(byte[] content, IEnumerable<KeyValuePair<string, string>> options = null)
{
// Read Document
var document = JsonSerializer.Deserialize<JsonStreamsDocument>(content);
var document = JsonSerializer.Deserialize<JsonStreamsResponseDocument>(content);
var success = document != null;

return new FormattedDocumentReadResult<IStreamsResponseDocument>(document.ToStreamsDocument(), success);
Expand Down
Loading

0 comments on commit 3273eb3

Please sign in to comment.