Skip to content

Commit

Permalink
Update SysML Import
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Nov 1, 2023
1 parent a88381f commit 44cf3b0
Show file tree
Hide file tree
Showing 13 changed files with 600 additions and 301 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MTConnect.Clients;
using MTConnect.Observations;

var agentUrl = "localhost:5001";

Expand All @@ -13,7 +14,57 @@
};
client.ObservationReceived += (sender, observation) =>
{
Console.WriteLine($"Observation Received : {observation.DataItemId} = {observation.GetValue("Result")} @ {observation.Timestamp}");
switch (observation.Representation)
{
case MTConnect.Devices.DataItemRepresentation.VALUE:
Console.WriteLine($"Observation Received : {observation.DataItemId} = {observation.GetValue("Result")} @ {observation.Timestamp}");
break;

case MTConnect.Devices.DataItemRepresentation.DATA_SET:
if (!observation.IsUnavailable)
{
var entries = DataSetObservation.GetEntries(observation.Values);
foreach (var entry in entries)
{
Console.WriteLine($"Observation Received : {observation.DataItemId} : DATA_SET : {entry.Key} = {entry.Value} @ {observation.Timestamp}");
}
}
else
{
Console.WriteLine($"Observation Received : {observation.DataItemId} = {observation.GetValue("Result")} @ {observation.Timestamp}");
}
break;

case MTConnect.Devices.DataItemRepresentation.TABLE:
if (!observation.IsUnavailable)
{
var entries = TableObservation.GetEntries(observation.Values);
foreach (var entry in entries)
{
foreach (var cell in entry.Cells)
{
Console.WriteLine($"Observation Received : {observation.DataItemId} : TABLE : {entry.Key} : {cell.Key} = {cell.Value} @ {observation.Timestamp}");
}
}
}
else
{
Console.WriteLine($"Observation Received : {observation.DataItemId} = {observation.GetValue("Result")} @ {observation.Timestamp}");
}
break;

case MTConnect.Devices.DataItemRepresentation.TIME_SERIES:
if (!observation.IsUnavailable)
{
var samples = TimeSeriesObservation.GetSamples(observation.Values).ToList();
Console.WriteLine($"Observation Received : {observation.DataItemId} : TIME_SERIES : {string.Join(" ", samples)} @ {observation.Timestamp}");
}
else
{
Console.WriteLine($"Observation Received : {observation.DataItemId} = {observation.GetValue("Result")} @ {observation.Timestamp}");
}
break;
}
};
client.AssetReceived += (sender, asset) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace MTConnect.Streams.Json

{{- for type in types }}
// Add {{type.name}}
typeObservations = observations.Where(o => o.Type == {{type.name}}DataItem.TypeId);
typeObservations = observations.Where(o => o.Type == {{type.name}}DataItem.TypeId && o.Representation == DataItemRepresentation.VALUE);
if (!typeObservations.IsNullOrEmpty())
{
var jsonObservations = new List<JsonSampleValue>();
Expand Down
10 changes: 10 additions & 0 deletions src/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,16 @@ private IComponentStream ProcessComponentStream(string deviceUuid, IComponentStr
{
foreach (var inputObservation in inputComponentStream.Observations)
{




Console.WriteLine($"{inputObservation.DataItemId} = {inputObservation.Representation}");





var dataItem = GetCachedDataItem(deviceUuid, inputObservation.DataItemId);
if (dataItem != null)
{
Expand Down
88 changes: 88 additions & 0 deletions src/MTConnect.NET-JSON-cppagent/Streams/JsonDataSetEntries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Observations;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace MTConnect.NET_JSON_cppagent.Streams
{
[JsonConverter(typeof(JsonDataSetEntriesConverter))]
public class JsonDataSetEntries
{
public Dictionary<string, object> Entries { get; set; }

public int Count { get; set; }

public bool IsUnavailable { get; set; }


public JsonDataSetEntries() { }

public JsonDataSetEntries(bool isUnavailable)
{
IsUnavailable = isUnavailable;
}

public JsonDataSetEntries(Dictionary<string, object> entries)
{
Entries = entries;
Count = entries != null ? entries.Count : 0;
}


public class JsonDataSetEntriesConverter : JsonConverter<JsonDataSetEntries>
{
public override bool HandleNull => true;


public override JsonDataSetEntries Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.StartObject)
{
var obj = JsonObject.Parse(ref reader);
var entries = obj.Deserialize<Dictionary<string, object>>();
return new JsonDataSetEntries(entries);
}
else
{
reader.Skip(); // Unavailable
}

return null;
}

public override void Write(Utf8JsonWriter writer, JsonDataSetEntries value, JsonSerializerOptions options)
{
if (value != null && !value.IsUnavailable)
{
writer.WriteStartObject();

if (!value.Entries.IsNullOrEmpty())
{
foreach (var entry in value.Entries)
{
if (entry.Value.IsNumeric())
{
writer.WriteNumber(entry.Key, entry.Value.ToDouble());
}
else
{
writer.WriteString(entry.Key, entry.Value?.ToString());
}
}
}

writer.WriteEndObject();
}
else
{
writer.WriteStringValue(Observation.Unavailable);
}
}
}
}
}
65 changes: 27 additions & 38 deletions src/MTConnect.NET-JSON-cppagent/Streams/JsonEventDataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Devices;
using MTConnect.NET_JSON_cppagent.Streams;
using MTConnect.Observations;
using MTConnect.Observations.Output;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace MTConnect.Streams.Json
{
public class JsonEventDataSet : JsonObservation
{
[JsonPropertyName("value")]
[JsonConverter(typeof(JsonEventDataSetValueConverter))]
public Dictionary<string, object> Entries { get; set; }
public JsonDataSetEntries Entries { get; set; }

[JsonPropertyName("count")]
public long? Count { get; set; }
Expand All @@ -45,8 +39,18 @@ public JsonEventDataSet(IObservation observation, bool categoryOutput = false, b
// DataSet Entries
if (observation is EventDataSetObservation)
{
Entries = CreateDataSetEntries(((EventDataSetObservation)observation).Entries);
Count = !Entries.IsNullOrEmpty() ? Entries.Count() : 0;
var dataSetObservation = (EventDataSetObservation)observation;

if (!dataSetObservation.Entries.IsNullOrEmpty())
{
Entries = CreateDataSetEntries(dataSetObservation.Entries);
Count = Entries != null ? Entries.Count : 0;
}
else
{
Entries = new JsonDataSetEntries(true);
Count = 0;
}
}
}
}
Expand All @@ -71,8 +75,17 @@ public JsonEventDataSet(IObservationOutput observation)
{
var dataSetObservation = new EventDataSetObservation();
dataSetObservation.AddValues(observation.Values);
Entries = CreateDataSetEntries(dataSetObservation.Entries);
Count = !Entries.IsNullOrEmpty() ? Entries.Count() : 0;

if (!dataSetObservation.Entries.IsNullOrEmpty())
{
Entries = CreateDataSetEntries(dataSetObservation.Entries);
Count = Entries != null ? Entries.Count : 0;
}
else
{
Entries = new JsonDataSetEntries(true);
Count = 0;
}
}
}
}
Expand All @@ -91,9 +104,9 @@ public IEventDataSetObservation ToObservation(string type)
observation.CompositionId = CompositionId;
observation.ResetTriggered = ResetTriggered.ConvertEnum<ResetTriggered>();

if (Entries != null)
if (Entries != null && !Entries.IsUnavailable)
{
observation.Entries = CreateDataSetEntries(Entries);
observation.Entries = CreateDataSetEntries(Entries.Entries);
}
else
{
Expand All @@ -102,29 +115,5 @@ public IEventDataSetObservation ToObservation(string type)

return observation;
}


public class JsonEventDataSetValueConverter : JsonConverter<Dictionary<string, object>>
{
public override Dictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.StartObject)
{
var obj = JsonObject.Parse(ref reader);
return obj.Deserialize<Dictionary<string, object>>();
}
else
{
reader.Skip(); // Unavailable
}

return null;
}

public override void Write(Utf8JsonWriter writer, Dictionary<string, object> value, JsonSerializerOptions options)
{
//writer.WriteStringValue(dateTimeValue.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture));
}
}
}
}
Loading

0 comments on commit 44cf3b0

Please sign in to comment.