From 44cf3b06bf971bde5d7a24872c6bf56fbab7e2d0 Mon Sep 17 00:00:00 2001 From: Patrick Ritchie Date: Tue, 31 Oct 2023 22:00:12 -0400 Subject: [PATCH] Update SysML Import --- .../CppAgentJsonClient.cs | 53 +++++- .../Json-cppagent/Templates/Samples.scriban | 2 +- .../Clients/MTConnectHttpClient.cs | 10 + .../Streams/JsonDataSetEntries.cs | 88 +++++++++ .../Streams/JsonEventDataSet.cs | 65 +++---- .../Streams/JsonEventTable.cs | 70 +++---- .../Streams/JsonObservation.cs | 36 ++-- .../Streams/JsonSampleDataSet.cs | 63 +++--- .../Streams/JsonSampleTable.cs | 69 +++---- .../Streams/JsonSampleTimeSeries.cs | 83 ++++---- .../Streams/JsonSamples.g.cs | 180 +++++++++--------- .../Streams/JsonTableEntries.cs | 100 ++++++++++ .../Streams/JsonTimeSeriesSamples.cs | 82 ++++++++ 13 files changed, 600 insertions(+), 301 deletions(-) create mode 100644 src/MTConnect.NET-JSON-cppagent/Streams/JsonDataSetEntries.cs create mode 100644 src/MTConnect.NET-JSON-cppagent/Streams/JsonTableEntries.cs create mode 100644 src/MTConnect.NET-JSON-cppagent/Streams/JsonTimeSeriesSamples.cs diff --git a/applications/Clients/MTConnect.NET-Client-Example-01/CppAgentJsonClient.cs b/applications/Clients/MTConnect.NET-Client-Example-01/CppAgentJsonClient.cs index 321e5b6d..e6d81bcf 100644 --- a/applications/Clients/MTConnect.NET-Client-Example-01/CppAgentJsonClient.cs +++ b/applications/Clients/MTConnect.NET-Client-Example-01/CppAgentJsonClient.cs @@ -1,4 +1,5 @@ using MTConnect.Clients; +using MTConnect.Observations; var agentUrl = "localhost:5001"; @@ -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) => { diff --git a/build/MTConnect.NET-SysML-Import/Json-cppagent/Templates/Samples.scriban b/build/MTConnect.NET-SysML-Import/Json-cppagent/Templates/Samples.scriban index ad300754..d59e767c 100644 --- a/build/MTConnect.NET-SysML-Import/Json-cppagent/Templates/Samples.scriban +++ b/build/MTConnect.NET-SysML-Import/Json-cppagent/Templates/Samples.scriban @@ -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(); diff --git a/src/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs b/src/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs index 25925246..eaa33b7f 100644 --- a/src/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs +++ b/src/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs @@ -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) { diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonDataSetEntries.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonDataSetEntries.cs new file mode 100644 index 00000000..ace05d5a --- /dev/null +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonDataSetEntries.cs @@ -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 Entries { get; set; } + + public int Count { get; set; } + + public bool IsUnavailable { get; set; } + + + public JsonDataSetEntries() { } + + public JsonDataSetEntries(bool isUnavailable) + { + IsUnavailable = isUnavailable; + } + + public JsonDataSetEntries(Dictionary entries) + { + Entries = entries; + Count = entries != null ? entries.Count : 0; + } + + + public class JsonDataSetEntriesConverter : JsonConverter + { + 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>(); + 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); + } + } + } + } +} diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonEventDataSet.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonEventDataSet.cs index a696f327..1bb90780 100644 --- a/src/MTConnect.NET-JSON-cppagent/Streams/JsonEventDataSet.cs +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonEventDataSet.cs @@ -2,14 +2,9 @@ // 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 @@ -17,8 +12,7 @@ namespace MTConnect.Streams.Json public class JsonEventDataSet : JsonObservation { [JsonPropertyName("value")] - [JsonConverter(typeof(JsonEventDataSetValueConverter))] - public Dictionary Entries { get; set; } + public JsonDataSetEntries Entries { get; set; } [JsonPropertyName("count")] public long? Count { get; set; } @@ -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; + } } } } @@ -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; + } } } } @@ -91,9 +104,9 @@ public IEventDataSetObservation ToObservation(string type) observation.CompositionId = CompositionId; observation.ResetTriggered = ResetTriggered.ConvertEnum(); - if (Entries != null) + if (Entries != null && !Entries.IsUnavailable) { - observation.Entries = CreateDataSetEntries(Entries); + observation.Entries = CreateDataSetEntries(Entries.Entries); } else { @@ -102,29 +115,5 @@ public IEventDataSetObservation ToObservation(string type) return observation; } - - - public class JsonEventDataSetValueConverter : JsonConverter> - { - public override Dictionary Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.StartObject) - { - var obj = JsonObject.Parse(ref reader); - return obj.Deserialize>(); - } - else - { - reader.Skip(); // Unavailable - } - - return null; - } - - public override void Write(Utf8JsonWriter writer, Dictionary value, JsonSerializerOptions options) - { - //writer.WriteStringValue(dateTimeValue.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)); - } - } } } \ No newline at end of file diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonEventTable.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonEventTable.cs index 85e83eb5..9bc6d975 100644 --- a/src/MTConnect.NET-JSON-cppagent/Streams/JsonEventTable.cs +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonEventTable.cs @@ -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.Linq; -using System.Text.Json; -using System.Text.Json.Nodes; using System.Text.Json.Serialization; -using static MTConnect.Streams.Json.JsonEventDataSet; namespace MTConnect.Streams.Json { public class JsonEventTable : JsonObservation { [JsonPropertyName("value")] - [JsonConverter(typeof(JsonEventTableValueConverter))] - public Dictionary> Entries { get; set; } + public JsonTableEntries Entries { get; set; } [JsonPropertyName("count")] public long? Count { get; set; } @@ -45,8 +39,18 @@ public JsonEventTable(IObservation observation, bool categoryOutput = false, boo // Table Entries if (observation is EventTableObservation) { - Entries = CreateTableEntries(((EventTableObservation)observation).Entries); - Count = !Entries.IsNullOrEmpty() ? Entries.Count() : 0; + var tableObservation = (EventTableObservation)observation; + + if (!tableObservation.Entries.IsNullOrEmpty()) + { + Entries = CreateTableEntries(tableObservation.Entries); + Count = Entries != null ? Entries.Count : 0; + } + else + { + Entries = new JsonTableEntries(true); + Count = 0; + } } } } @@ -66,13 +70,22 @@ public JsonEventTable(IObservationOutput observation) NativeCode = observation.GetValue(ValueKeys.NativeCode); AssetType = observation.GetValue(ValueKeys.AssetType); - // DataSet Entries + // Table Entries if (observation.Representation == DataItemRepresentation.TABLE) { - var dataSetObservation = new EventTableObservation(); - dataSetObservation.AddValues(observation.Values); - Entries = CreateTableEntries(dataSetObservation.Entries); - Count = !Entries.IsNullOrEmpty() ? Entries.Count() : 0; + var tableObservation = new EventTableObservation(); + tableObservation.AddValues(observation.Values); + + if (!tableObservation.Entries.IsNullOrEmpty()) + { + Entries = CreateTableEntries(tableObservation.Entries); + Count = Entries != null ? Entries.Count : 0; + } + else + { + Entries = new JsonTableEntries(true); + Count = 0; + } } } } @@ -91,9 +104,9 @@ public IEventTableObservation ToObservation(string type) observation.CompositionId = CompositionId; observation.ResetTriggered = ResetTriggered.ConvertEnum(); - if (Entries != null) + if (Entries != null && !Entries.IsUnavailable) { - observation.Entries = CreateTableEntries(Entries); + observation.Entries = CreateTableEntries(Entries.Entries); } else { @@ -102,28 +115,5 @@ public IEventTableObservation ToObservation(string type) return observation; } - - public class JsonEventTableValueConverter : JsonConverter>> - { - public override Dictionary> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.StartObject) - { - var obj = JsonObject.Parse(ref reader); - return obj.Deserialize>>(); - } - else - { - reader.Skip(); // Unavailable - } - - return null; - } - - public override void Write(Utf8JsonWriter writer, Dictionary> value, JsonSerializerOptions options) - { - //writer.WriteStringValue(dateTimeValue.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)); - } - } } } \ No newline at end of file diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonObservation.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonObservation.cs index 2e5fd486..d9c38aa8 100644 --- a/src/MTConnect.NET-JSON-cppagent/Streams/JsonObservation.cs +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonObservation.cs @@ -1,6 +1,7 @@ // Copyright (c) 2023 TrakHound Inc., All Rights Reserved. // TrakHound Inc. licenses this file to you under the MIT license. +using MTConnect.NET_JSON_cppagent.Streams; using MTConnect.Observations; using System; using System.Collections.Generic; @@ -22,9 +23,6 @@ public class JsonObservation [JsonPropertyName("representation")] public string Representation { get; set; } - //[JsonIgnore] - //public string Type { get; set; } - [JsonPropertyName("subType")] public string SubType { get; set; } @@ -43,18 +41,6 @@ public class JsonObservation [JsonPropertyName("resetTriggered")] public string ResetTriggered { get; set; } - //[JsonPropertyName("value")] - //public string Value { get; set; } - - //[JsonPropertyName("samples")] - //public IEnumerable Samples { get; set; } - - //[JsonPropertyName("value")] - //public IEnumerable Entries { get; set; } - - //[JsonPropertyName("count")] - //public long? Count { get; set; } - [JsonPropertyName("nativeCode")] public string NativeCode { get; set; } @@ -62,7 +48,7 @@ public class JsonObservation public string AssetType { get; set; } - public static Dictionary CreateDataSetEntries(IEnumerable entries) + public static JsonDataSetEntries CreateDataSetEntries(IEnumerable entries) { if (!entries.IsNullOrEmpty()) { @@ -81,7 +67,8 @@ public static Dictionary CreateDataSetEntries(IEnumerable CreateDataSetEntries(Dictionary> CreateTableEntries(IEnumerable entries) + public static JsonTableEntries CreateTableEntries(IEnumerable entries) { if (!entries.IsNullOrEmpty()) { @@ -132,7 +119,7 @@ public static Dictionary> CreateTableEntries( jsonEntries.Add(entry.Key, cells); } } - return jsonEntries; + return new JsonTableEntries(jsonEntries); } return null; @@ -160,5 +147,16 @@ public static IEnumerable CreateTableEntries(Dictionary samples) + { + if (!samples.IsNullOrEmpty()) + { + return new JsonTimeSeriesSamples(samples); + } + + return null; + } } } \ No newline at end of file diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleDataSet.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleDataSet.cs index 0d40c4a4..9c534636 100644 --- a/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleDataSet.cs +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleDataSet.cs @@ -2,13 +2,9 @@ // 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.Linq; -using System.Text.Json.Nodes; -using System.Text.Json; using System.Text.Json.Serialization; namespace MTConnect.Streams.Json @@ -16,8 +12,7 @@ namespace MTConnect.Streams.Json public class JsonSampleDataSet : JsonObservation { [JsonPropertyName("value")] - [JsonConverter(typeof(JsonSampleDataSetValueConverter))] - public Dictionary Entries { get; set; } + public JsonDataSetEntries Entries { get; set; } [JsonPropertyName("count")] public long? Count { get; set; } @@ -44,8 +39,18 @@ public JsonSampleDataSet(IObservation observation, bool categoryOutput = false, // DataSet Entries if (observation is SampleDataSetObservation) { - Entries = CreateDataSetEntries(((SampleDataSetObservation)observation).Entries); - Count = !Entries.IsNullOrEmpty() ? Entries.Count() : 0; + var dataSetObservation = (SampleDataSetObservation)observation; + + if (!dataSetObservation.Entries.IsNullOrEmpty()) + { + Entries = CreateDataSetEntries(dataSetObservation.Entries); + Count = Entries != null ? Entries.Count : 0; + } + else + { + Entries = new JsonDataSetEntries(true); + Count = 0; + } } } } @@ -70,8 +75,17 @@ public JsonSampleDataSet(IObservationOutput observation) { var dataSetObservation = new SampleDataSetObservation(); 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; + } } } } @@ -89,32 +103,17 @@ public ISampleDataSetObservation ToObservation(string type) observation.SubType = SubType; observation.CompositionId = CompositionId; observation.ResetTriggered = ResetTriggered.ConvertEnum(); - observation.Entries = CreateDataSetEntries(Entries); - return observation; - } - - public class JsonSampleDataSetValueConverter : JsonConverter> - { - public override Dictionary Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + if (Entries != null && !Entries.IsUnavailable) { - if (reader.TokenType == JsonTokenType.StartObject) - { - var obj = JsonObject.Parse(ref reader); - return obj.Deserialize>(); - } - else - { - reader.Skip(); // Unavailable - } - - return null; + observation.Entries = CreateDataSetEntries(Entries.Entries); } - - public override void Write(Utf8JsonWriter writer, Dictionary value, JsonSerializerOptions options) + else { - //writer.WriteStringValue(dateTimeValue.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)); + observation.AddValue(ValueKeys.Result, Observation.Unavailable); } + + return observation; } } } \ No newline at end of file diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleTable.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleTable.cs index cf91dbd4..a435bf83 100644 --- a/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleTable.cs +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleTable.cs @@ -2,13 +2,9 @@ // 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.Linq; -using System.Text.Json; -using System.Text.Json.Nodes; using System.Text.Json.Serialization; namespace MTConnect.Streams.Json @@ -16,8 +12,7 @@ namespace MTConnect.Streams.Json public class JsonSampleTable : JsonObservation { [JsonPropertyName("value")] - [JsonConverter(typeof(JsonSampleTableValueConverter))] - public Dictionary> Entries { get; set; } + public JsonTableEntries Entries { get; set; } [JsonPropertyName("count")] public long? Count { get; set; } @@ -44,8 +39,18 @@ public JsonSampleTable(IObservation observation, bool categoryOutput = false, bo // Table Entries if (observation is SampleTableObservation) { - Entries = CreateTableEntries(((SampleTableObservation)observation).Entries); - Count = !Entries.IsNullOrEmpty() ? Entries.Count() : 0; + var tableObservation = (SampleTableObservation)observation; + + if (!tableObservation.Entries.IsNullOrEmpty()) + { + Entries = CreateTableEntries(tableObservation.Entries); + Count = Entries != null ? Entries.Count : 0; + } + else + { + Entries = new JsonTableEntries(true); + Count = 0; + } } } } @@ -65,13 +70,22 @@ public JsonSampleTable(IObservationOutput observation) NativeCode = observation.GetValue(ValueKeys.NativeCode); AssetType = observation.GetValue(ValueKeys.AssetType); - // DataSet Entries + // Table Entries if (observation.Representation == DataItemRepresentation.TABLE) { - var dataSetObservation = new SampleTableObservation(); - dataSetObservation.AddValues(observation.Values); - Entries = CreateTableEntries(dataSetObservation.Entries); - Count = !Entries.IsNullOrEmpty() ? Entries.Count() : 0; + var tableObservation = new SampleTableObservation(); + tableObservation.AddValues(observation.Values); + + if (!tableObservation.Entries.IsNullOrEmpty()) + { + Entries = CreateTableEntries(tableObservation.Entries); + Count = Entries != null ? Entries.Count : 0; + } + else + { + Entries = new JsonTableEntries(true); + Count = 0; + } } } } @@ -90,9 +104,9 @@ public ISampleTableObservation ToObservation(string type) observation.CompositionId = CompositionId; observation.ResetTriggered = ResetTriggered.ConvertEnum(); - if (Entries != null) + if (Entries != null && !Entries.IsUnavailable) { - observation.Entries = CreateTableEntries(Entries); + observation.Entries = CreateTableEntries(Entries.Entries); } else { @@ -101,28 +115,5 @@ public ISampleTableObservation ToObservation(string type) return observation; } - - public class JsonSampleTableValueConverter : JsonConverter>> - { - public override Dictionary> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.StartObject) - { - var obj = JsonObject.Parse(ref reader); - return obj.Deserialize>>(); - } - else - { - reader.Skip(); // Unavailable - } - - return null; - } - - public override void Write(Utf8JsonWriter writer, Dictionary> value, JsonSerializerOptions options) - { - //writer.WriteStringValue(dateTimeValue.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)); - } - } } } \ No newline at end of file diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleTimeSeries.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleTimeSeries.cs index 5a6b9918..d425672a 100644 --- a/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleTimeSeries.cs +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonSampleTimeSeries.cs @@ -2,13 +2,10 @@ // 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.Linq; -using System.Text.Json; -using System.Text.Json.Nodes; using System.Text.Json.Serialization; namespace MTConnect.Streams.Json @@ -16,11 +13,13 @@ namespace MTConnect.Streams.Json public class JsonSampleTimeSeries : JsonObservation { [JsonPropertyName("value")] - [JsonConverter(typeof(JsonSampleTimeSeriesValueConverter))] - public IEnumerable Samples { get; set; } + public JsonTimeSeriesSamples Samples { get; set; } - [JsonPropertyName("count")] - public long? Count { get; set; } + [JsonPropertyName("sampleCount")] + public long? SampleCount { get; set; } + + [JsonPropertyName("sampleRate")] + public double? SampleRate { get; set; } public JsonSampleTimeSeries() { } @@ -45,8 +44,18 @@ public JsonSampleTimeSeries(IObservation observation, bool categoryOutput = fals if (observation is SampleTimeSeriesObservation) { var timeseriesObservation = (SampleTimeSeriesObservation)observation; - Samples = timeseriesObservation.Samples; - Count = timeseriesObservation.SampleCount; + + if (!timeseriesObservation.Samples.IsNullOrEmpty()) + { + Samples = CreateTimeSeriesSamples(timeseriesObservation.Samples); + SampleCount = Samples != null ? Samples.Count : 0; + SampleRate = timeseriesObservation.SampleRate; + } + else + { + Samples = new JsonTimeSeriesSamples(true); + SampleCount = 0; + } } } } @@ -66,13 +75,23 @@ public JsonSampleTimeSeries(IObservationOutput observation) NativeCode = observation.GetValue(ValueKeys.NativeCode); AssetType = observation.GetValue(ValueKeys.AssetType); - // DataSet Entries - if (observation.Representation == DataItemRepresentation.TABLE) + // TimeSeries Samples + if (observation.Representation == DataItemRepresentation.TIME_SERIES) { var timeseriesObservation = new SampleTimeSeriesObservation(); timeseriesObservation.AddValues(observation.Values); - Samples = timeseriesObservation.Samples; - Count = timeseriesObservation.SampleCount; + + if (!timeseriesObservation.Samples.IsNullOrEmpty()) + { + Samples = CreateTimeSeriesSamples(timeseriesObservation.Samples); + SampleCount = Samples != null ? Samples.Count : 0; + SampleRate = timeseriesObservation.SampleRate; + } + else + { + Samples = new JsonTimeSeriesSamples(true); + SampleCount = 0; + } } } } @@ -91,12 +110,17 @@ public ISampleTimeSeriesObservation ToObservation(string type) observation.CompositionId = CompositionId; observation.ResetTriggered = ResetTriggered.ConvertEnum(); - if (!Samples.IsNullOrEmpty()) + if (SampleRate != null) observation.SampleRate = SampleRate.Value; + + if (Samples != null && !Samples.IsUnavailable) { - var samples = Samples.ToArray(); - for (var i = 0; i < samples.Length - 1; i++) + if (!Samples.Samples.IsNullOrEmpty()) { - observation.AddValue(ValueKeys.CreateTimeSeriesValueKey(i), samples[i]); + var samples = Samples.Samples.ToArray(); + for (var i = 0; i < samples.Length; i++) + { + observation.AddValue(ValueKeys.CreateTimeSeriesValueKey(i), samples[i]); + } } } else @@ -106,28 +130,5 @@ public ISampleTimeSeriesObservation ToObservation(string type) return observation; } - - public class JsonSampleTimeSeriesValueConverter : JsonConverter> - { - public override IEnumerable Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.StartObject) - { - var obj = JsonObject.Parse(ref reader); - return obj.Deserialize>(); - } - else - { - reader.Skip(); // Unavailable - } - - return null; - } - - public override void Write(Utf8JsonWriter writer, IEnumerable value, JsonSerializerOptions options) - { - //writer.WriteStringValue(dateTimeValue.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)); - } - } } } \ No newline at end of file diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonSamples.g.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonSamples.g.cs index 48d5e9bc..1d759536 100644 --- a/src/MTConnect.NET-JSON-cppagent/Streams/JsonSamples.g.cs +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonSamples.g.cs @@ -1654,7 +1654,7 @@ public JsonSamples(IEnumerable observations) { IEnumerable typeObservations; // Add Acceleration - typeObservations = observations.Where(o => o.Type == AccelerationDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AccelerationDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -1703,7 +1703,7 @@ public JsonSamples(IEnumerable observations) // Add AccumulatedTime - typeObservations = observations.Where(o => o.Type == AccumulatedTimeDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AccumulatedTimeDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -1752,7 +1752,7 @@ public JsonSamples(IEnumerable observations) // Add Amperage - typeObservations = observations.Where(o => o.Type == AmperageDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AmperageDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -1801,7 +1801,7 @@ public JsonSamples(IEnumerable observations) // Add AmperageAC - typeObservations = observations.Where(o => o.Type == AmperageACDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AmperageACDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -1850,7 +1850,7 @@ public JsonSamples(IEnumerable observations) // Add AmperageDC - typeObservations = observations.Where(o => o.Type == AmperageDCDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AmperageDCDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -1899,7 +1899,7 @@ public JsonSamples(IEnumerable observations) // Add Angle - typeObservations = observations.Where(o => o.Type == AngleDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AngleDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -1948,7 +1948,7 @@ public JsonSamples(IEnumerable observations) // Add AngularAcceleration - typeObservations = observations.Where(o => o.Type == AngularAccelerationDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AngularAccelerationDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -1997,7 +1997,7 @@ public JsonSamples(IEnumerable observations) // Add AngularDeceleration - typeObservations = observations.Where(o => o.Type == AngularDecelerationDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AngularDecelerationDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2046,7 +2046,7 @@ public JsonSamples(IEnumerable observations) // Add AngularVelocity - typeObservations = observations.Where(o => o.Type == AngularVelocityDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AngularVelocityDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2095,7 +2095,7 @@ public JsonSamples(IEnumerable observations) // Add AssetUpdateRate - typeObservations = observations.Where(o => o.Type == AssetUpdateRateDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AssetUpdateRateDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2144,7 +2144,7 @@ public JsonSamples(IEnumerable observations) // Add AxisFeedrate - typeObservations = observations.Where(o => o.Type == AxisFeedrateDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == AxisFeedrateDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2193,7 +2193,7 @@ public JsonSamples(IEnumerable observations) // Add BatteryCapacity - typeObservations = observations.Where(o => o.Type == BatteryCapacityDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == BatteryCapacityDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2242,7 +2242,7 @@ public JsonSamples(IEnumerable observations) // Add BatteryCharge - typeObservations = observations.Where(o => o.Type == BatteryChargeDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == BatteryChargeDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2291,7 +2291,7 @@ public JsonSamples(IEnumerable observations) // Add CapacityFluid - typeObservations = observations.Where(o => o.Type == CapacityFluidDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == CapacityFluidDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2340,7 +2340,7 @@ public JsonSamples(IEnumerable observations) // Add CapacitySpatial - typeObservations = observations.Where(o => o.Type == CapacitySpatialDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == CapacitySpatialDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2389,7 +2389,7 @@ public JsonSamples(IEnumerable observations) // Add ChargeRate - typeObservations = observations.Where(o => o.Type == ChargeRateDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == ChargeRateDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2438,7 +2438,7 @@ public JsonSamples(IEnumerable observations) // Add Concentration - typeObservations = observations.Where(o => o.Type == ConcentrationDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == ConcentrationDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2487,7 +2487,7 @@ public JsonSamples(IEnumerable observations) // Add Conductivity - typeObservations = observations.Where(o => o.Type == ConductivityDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == ConductivityDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2536,7 +2536,7 @@ public JsonSamples(IEnumerable observations) // Add CuttingSpeed - typeObservations = observations.Where(o => o.Type == CuttingSpeedDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == CuttingSpeedDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2585,7 +2585,7 @@ public JsonSamples(IEnumerable observations) // Add Deceleration - typeObservations = observations.Where(o => o.Type == DecelerationDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DecelerationDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2634,7 +2634,7 @@ public JsonSamples(IEnumerable observations) // Add Density - typeObservations = observations.Where(o => o.Type == DensityDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DensityDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2683,7 +2683,7 @@ public JsonSamples(IEnumerable observations) // Add DepositionAccelerationVolumetric - typeObservations = observations.Where(o => o.Type == DepositionAccelerationVolumetricDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DepositionAccelerationVolumetricDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2732,7 +2732,7 @@ public JsonSamples(IEnumerable observations) // Add DepositionDensity - typeObservations = observations.Where(o => o.Type == DepositionDensityDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DepositionDensityDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2781,7 +2781,7 @@ public JsonSamples(IEnumerable observations) // Add DepositionMass - typeObservations = observations.Where(o => o.Type == DepositionMassDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DepositionMassDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2830,7 +2830,7 @@ public JsonSamples(IEnumerable observations) // Add DepositionRateVolumetric - typeObservations = observations.Where(o => o.Type == DepositionRateVolumetricDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DepositionRateVolumetricDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2879,7 +2879,7 @@ public JsonSamples(IEnumerable observations) // Add DepositionVolume - typeObservations = observations.Where(o => o.Type == DepositionVolumeDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DepositionVolumeDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2928,7 +2928,7 @@ public JsonSamples(IEnumerable observations) // Add DewPoint - typeObservations = observations.Where(o => o.Type == DewPointDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DewPointDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -2977,7 +2977,7 @@ public JsonSamples(IEnumerable observations) // Add Diameter - typeObservations = observations.Where(o => o.Type == DiameterDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DiameterDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3026,7 +3026,7 @@ public JsonSamples(IEnumerable observations) // Add DischargeRate - typeObservations = observations.Where(o => o.Type == DischargeRateDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DischargeRateDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3075,7 +3075,7 @@ public JsonSamples(IEnumerable observations) // Add Displacement - typeObservations = observations.Where(o => o.Type == DisplacementDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DisplacementDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3124,7 +3124,7 @@ public JsonSamples(IEnumerable observations) // Add DisplacementAngular - typeObservations = observations.Where(o => o.Type == DisplacementAngularDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DisplacementAngularDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3173,7 +3173,7 @@ public JsonSamples(IEnumerable observations) // Add DisplacementLinear - typeObservations = observations.Where(o => o.Type == DisplacementLinearDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == DisplacementLinearDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3222,7 +3222,7 @@ public JsonSamples(IEnumerable observations) // Add ElectricalEnergy - typeObservations = observations.Where(o => o.Type == ElectricalEnergyDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == ElectricalEnergyDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3271,7 +3271,7 @@ public JsonSamples(IEnumerable observations) // Add EquipmentTimer - typeObservations = observations.Where(o => o.Type == EquipmentTimerDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == EquipmentTimerDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3320,7 +3320,7 @@ public JsonSamples(IEnumerable observations) // Add FillLevel - typeObservations = observations.Where(o => o.Type == FillLevelDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == FillLevelDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3369,7 +3369,7 @@ public JsonSamples(IEnumerable observations) // Add Flow - typeObservations = observations.Where(o => o.Type == FlowDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == FlowDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3418,7 +3418,7 @@ public JsonSamples(IEnumerable observations) // Add FollowingError - typeObservations = observations.Where(o => o.Type == FollowingErrorDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == FollowingErrorDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3467,7 +3467,7 @@ public JsonSamples(IEnumerable observations) // Add FollowingErrorAngular - typeObservations = observations.Where(o => o.Type == FollowingErrorAngularDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == FollowingErrorAngularDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3516,7 +3516,7 @@ public JsonSamples(IEnumerable observations) // Add FollowingErrorLinear - typeObservations = observations.Where(o => o.Type == FollowingErrorLinearDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == FollowingErrorLinearDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3565,7 +3565,7 @@ public JsonSamples(IEnumerable observations) // Add Frequency - typeObservations = observations.Where(o => o.Type == FrequencyDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == FrequencyDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3614,7 +3614,7 @@ public JsonSamples(IEnumerable observations) // Add GlobalPosition - typeObservations = observations.Where(o => o.Type == GlobalPositionDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == GlobalPositionDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3663,7 +3663,7 @@ public JsonSamples(IEnumerable observations) // Add GravitationalAcceleration - typeObservations = observations.Where(o => o.Type == GravitationalAccelerationDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == GravitationalAccelerationDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3712,7 +3712,7 @@ public JsonSamples(IEnumerable observations) // Add GravitationalForce - typeObservations = observations.Where(o => o.Type == GravitationalForceDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == GravitationalForceDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3761,7 +3761,7 @@ public JsonSamples(IEnumerable observations) // Add HumidityAbsolute - typeObservations = observations.Where(o => o.Type == HumidityAbsoluteDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == HumidityAbsoluteDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3810,7 +3810,7 @@ public JsonSamples(IEnumerable observations) // Add HumidityRelative - typeObservations = observations.Where(o => o.Type == HumidityRelativeDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == HumidityRelativeDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3859,7 +3859,7 @@ public JsonSamples(IEnumerable observations) // Add HumiditySpecific - typeObservations = observations.Where(o => o.Type == HumiditySpecificDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == HumiditySpecificDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3908,7 +3908,7 @@ public JsonSamples(IEnumerable observations) // Add Length - typeObservations = observations.Where(o => o.Type == LengthDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == LengthDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -3957,7 +3957,7 @@ public JsonSamples(IEnumerable observations) // Add Level - typeObservations = observations.Where(o => o.Type == LevelDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == LevelDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4006,7 +4006,7 @@ public JsonSamples(IEnumerable observations) // Add LinearForce - typeObservations = observations.Where(o => o.Type == LinearForceDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == LinearForceDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4055,7 +4055,7 @@ public JsonSamples(IEnumerable observations) // Add Load - typeObservations = observations.Where(o => o.Type == LoadDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == LoadDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4104,7 +4104,7 @@ public JsonSamples(IEnumerable observations) // Add Mass - typeObservations = observations.Where(o => o.Type == MassDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == MassDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4153,7 +4153,7 @@ public JsonSamples(IEnumerable observations) // Add ObservationUpdateRate - typeObservations = observations.Where(o => o.Type == ObservationUpdateRateDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == ObservationUpdateRateDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4202,7 +4202,7 @@ public JsonSamples(IEnumerable observations) // Add Openness - typeObservations = observations.Where(o => o.Type == OpennessDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == OpennessDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4251,7 +4251,7 @@ public JsonSamples(IEnumerable observations) // Add Orientation - typeObservations = observations.Where(o => o.Type == OrientationDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == OrientationDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4300,7 +4300,7 @@ public JsonSamples(IEnumerable observations) // Add PathFeedrate - typeObservations = observations.Where(o => o.Type == PathFeedrateDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PathFeedrateDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4349,7 +4349,7 @@ public JsonSamples(IEnumerable observations) // Add PathFeedratePerRevolution - typeObservations = observations.Where(o => o.Type == PathFeedratePerRevolutionDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PathFeedratePerRevolutionDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4398,7 +4398,7 @@ public JsonSamples(IEnumerable observations) // Add PathPosition - typeObservations = observations.Where(o => o.Type == PathPositionDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PathPositionDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4447,7 +4447,7 @@ public JsonSamples(IEnumerable observations) // Add PH - typeObservations = observations.Where(o => o.Type == PHDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PHDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4496,7 +4496,7 @@ public JsonSamples(IEnumerable observations) // Add Position - typeObservations = observations.Where(o => o.Type == PositionDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PositionDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4545,7 +4545,7 @@ public JsonSamples(IEnumerable observations) // Add PositionCartesian - typeObservations = observations.Where(o => o.Type == PositionCartesianDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PositionCartesianDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4594,7 +4594,7 @@ public JsonSamples(IEnumerable observations) // Add PowerFactor - typeObservations = observations.Where(o => o.Type == PowerFactorDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PowerFactorDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4643,7 +4643,7 @@ public JsonSamples(IEnumerable observations) // Add Pressure - typeObservations = observations.Where(o => o.Type == PressureDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PressureDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4692,7 +4692,7 @@ public JsonSamples(IEnumerable observations) // Add PressureAbsolute - typeObservations = observations.Where(o => o.Type == PressureAbsoluteDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PressureAbsoluteDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4741,7 +4741,7 @@ public JsonSamples(IEnumerable observations) // Add PressurizationRate - typeObservations = observations.Where(o => o.Type == PressurizationRateDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == PressurizationRateDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4790,7 +4790,7 @@ public JsonSamples(IEnumerable observations) // Add ProcessTimer - typeObservations = observations.Where(o => o.Type == ProcessTimerDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == ProcessTimerDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4839,7 +4839,7 @@ public JsonSamples(IEnumerable observations) // Add Resistance - typeObservations = observations.Where(o => o.Type == ResistanceDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == ResistanceDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4888,7 +4888,7 @@ public JsonSamples(IEnumerable observations) // Add RotaryVelocity - typeObservations = observations.Where(o => o.Type == RotaryVelocityDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == RotaryVelocityDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4937,7 +4937,7 @@ public JsonSamples(IEnumerable observations) // Add SettlingError - typeObservations = observations.Where(o => o.Type == SettlingErrorDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == SettlingErrorDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -4986,7 +4986,7 @@ public JsonSamples(IEnumerable observations) // Add SettlingErrorAngular - typeObservations = observations.Where(o => o.Type == SettlingErrorAngularDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == SettlingErrorAngularDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5035,7 +5035,7 @@ public JsonSamples(IEnumerable observations) // Add SettlingErrorLinear - typeObservations = observations.Where(o => o.Type == SettlingErrorLinearDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == SettlingErrorLinearDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5084,7 +5084,7 @@ public JsonSamples(IEnumerable observations) // Add SoundLevel - typeObservations = observations.Where(o => o.Type == SoundLevelDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == SoundLevelDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5133,7 +5133,7 @@ public JsonSamples(IEnumerable observations) // Add SpindleSpeed - typeObservations = observations.Where(o => o.Type == SpindleSpeedDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == SpindleSpeedDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5182,7 +5182,7 @@ public JsonSamples(IEnumerable observations) // Add Strain - typeObservations = observations.Where(o => o.Type == StrainDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == StrainDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5231,7 +5231,7 @@ public JsonSamples(IEnumerable observations) // Add Temperature - typeObservations = observations.Where(o => o.Type == TemperatureDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == TemperatureDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5280,7 +5280,7 @@ public JsonSamples(IEnumerable observations) // Add Tension - typeObservations = observations.Where(o => o.Type == TensionDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == TensionDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5329,7 +5329,7 @@ public JsonSamples(IEnumerable observations) // Add Tilt - typeObservations = observations.Where(o => o.Type == TiltDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == TiltDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5378,7 +5378,7 @@ public JsonSamples(IEnumerable observations) // Add Torque - typeObservations = observations.Where(o => o.Type == TorqueDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == TorqueDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5427,7 +5427,7 @@ public JsonSamples(IEnumerable observations) // Add Velocity - typeObservations = observations.Where(o => o.Type == VelocityDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == VelocityDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5476,7 +5476,7 @@ public JsonSamples(IEnumerable observations) // Add Viscosity - typeObservations = observations.Where(o => o.Type == ViscosityDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == ViscosityDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5525,7 +5525,7 @@ public JsonSamples(IEnumerable observations) // Add VoltAmpere - typeObservations = observations.Where(o => o.Type == VoltAmpereDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == VoltAmpereDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5574,7 +5574,7 @@ public JsonSamples(IEnumerable observations) // Add VoltAmpereReactive - typeObservations = observations.Where(o => o.Type == VoltAmpereReactiveDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == VoltAmpereReactiveDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5623,7 +5623,7 @@ public JsonSamples(IEnumerable observations) // Add Voltage - typeObservations = observations.Where(o => o.Type == VoltageDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == VoltageDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5672,7 +5672,7 @@ public JsonSamples(IEnumerable observations) // Add VoltageAC - typeObservations = observations.Where(o => o.Type == VoltageACDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == VoltageACDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5721,7 +5721,7 @@ public JsonSamples(IEnumerable observations) // Add VoltageDC - typeObservations = observations.Where(o => o.Type == VoltageDCDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == VoltageDCDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5770,7 +5770,7 @@ public JsonSamples(IEnumerable observations) // Add VolumeFluid - typeObservations = observations.Where(o => o.Type == VolumeFluidDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == VolumeFluidDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5819,7 +5819,7 @@ public JsonSamples(IEnumerable observations) // Add VolumeSpatial - typeObservations = observations.Where(o => o.Type == VolumeSpatialDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == VolumeSpatialDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5868,7 +5868,7 @@ public JsonSamples(IEnumerable observations) // Add Wattage - typeObservations = observations.Where(o => o.Type == WattageDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == WattageDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5917,7 +5917,7 @@ public JsonSamples(IEnumerable observations) // Add XDimension - typeObservations = observations.Where(o => o.Type == XDimensionDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == XDimensionDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -5966,7 +5966,7 @@ public JsonSamples(IEnumerable observations) // Add YDimension - typeObservations = observations.Where(o => o.Type == YDimensionDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == YDimensionDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); @@ -6015,7 +6015,7 @@ public JsonSamples(IEnumerable observations) // Add ZDimension - typeObservations = observations.Where(o => o.Type == ZDimensionDataItem.TypeId); + typeObservations = observations.Where(o => o.Type == ZDimensionDataItem.TypeId && o.Representation == DataItemRepresentation.VALUE); if (!typeObservations.IsNullOrEmpty()) { var jsonObservations = new List(); diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonTableEntries.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonTableEntries.cs new file mode 100644 index 00000000..7be00fa9 --- /dev/null +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonTableEntries.cs @@ -0,0 +1,100 @@ +// 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(JsonTableEntriesConverter))] + public class JsonTableEntries + { + public Dictionary> Entries { get; set; } + + public int Count { get; set; } + + public bool IsUnavailable { get; set; } + + + public JsonTableEntries() { } + + public JsonTableEntries(bool isUnavailable) + { + IsUnavailable = isUnavailable; + } + + public JsonTableEntries(Dictionary> entries) + { + Entries = entries; + Count = entries != null ? entries.Count : 0; + } + + + public class JsonTableEntriesConverter : JsonConverter + { + public override bool HandleNull => true; + + + public override JsonTableEntries Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.StartObject) + { + var obj = JsonObject.Parse(ref reader); + var entries = obj.Deserialize>>(); + return new JsonTableEntries(entries); + } + else + { + reader.Skip(); // Unavailable + } + + return null; + } + + public override void Write(Utf8JsonWriter writer, JsonTableEntries value, JsonSerializerOptions options) + { + if (value != null && !value.IsUnavailable) + { + writer.WriteStartObject(); + + if (!value.Entries.IsNullOrEmpty()) + { + foreach (var entry in value.Entries) + { + writer.WritePropertyName(entry.Key); + + if (!entry.Value.IsNullOrEmpty()) + { + writer.WriteStartObject(); + + foreach (var cell in entry.Value) + { + if (cell.Value.IsNumeric()) + { + writer.WriteNumber(cell.Key, cell.Value.ToDouble()); + } + else + { + writer.WriteString(cell.Key, cell.Value?.ToString()); + } + } + + writer.WriteEndObject(); + } + } + } + + writer.WriteEndObject(); + } + else + { + writer.WriteStringValue(Observation.Unavailable); + } + } + } + } +} diff --git a/src/MTConnect.NET-JSON-cppagent/Streams/JsonTimeSeriesSamples.cs b/src/MTConnect.NET-JSON-cppagent/Streams/JsonTimeSeriesSamples.cs new file mode 100644 index 00000000..338689b9 --- /dev/null +++ b/src/MTConnect.NET-JSON-cppagent/Streams/JsonTimeSeriesSamples.cs @@ -0,0 +1,82 @@ +// 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.Linq; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace MTConnect.NET_JSON_cppagent.Streams +{ + [JsonConverter(typeof(JsonTimeSeriesSamplesConverter))] + public class JsonTimeSeriesSamples + { + public IEnumerable Samples { get; set; } + + public int Count { get; set; } + + public bool IsUnavailable { get; set; } + + + public JsonTimeSeriesSamples() { } + + public JsonTimeSeriesSamples(bool isUnavailable) + { + IsUnavailable = isUnavailable; + } + + public JsonTimeSeriesSamples(IEnumerable samples) + { + Samples = samples; + Count = samples != null ? samples.Count() : 0; + } + + + public class JsonTimeSeriesSamplesConverter : JsonConverter + { + public override bool HandleNull => true; + + + public override JsonTimeSeriesSamples Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.StartArray) + { + var obj = JsonObject.Parse(ref reader); + var entries = obj.Deserialize>(); + return new JsonTimeSeriesSamples(entries); + } + else + { + reader.Skip(); // Unavailable + } + + return null; + } + + public override void Write(Utf8JsonWriter writer, JsonTimeSeriesSamples value, JsonSerializerOptions options) + { + if (value != null && !value.IsUnavailable) + { + writer.WriteStartArray(); + + if (!value.Samples.IsNullOrEmpty()) + { + foreach (var sample in value.Samples) + { + writer.WriteNumberValue(sample); + } + } + + writer.WriteEndArray(); + } + else + { + writer.WriteStringValue(Observation.Unavailable); + } + } + } + } +}