From 2db33beadb683141ef5d1de337c275859ed9a323 Mon Sep 17 00:00:00 2001 From: Patrick Ritchie Date: Fri, 1 Mar 2024 01:20:49 -0500 Subject: [PATCH] Updated to add a FormatError to MTConnectHttpClient --- clients/MTConnect.NET-Client-HTTP/Program.cs | 16 +++++-- .../Formatters/FormatReadResult.cs | 7 ++- .../Formatters/IFormatReadResult.cs | 23 ++++++++++ .../Clients/MTConnectHttpAssetClient.cs | 41 ++++++++++++----- .../Clients/MTConnectHttpClient.cs | 19 +++++++- .../Clients/MTConnectHttpClientStream.cs | 35 ++++++++++++--- .../Clients/MTConnectHttpCurrentClient.cs | 45 ++++++++++++++----- .../Clients/MTConnectHttpProbeClient.cs | 41 ++++++++++++----- .../Clients/MTConnectHttpSampleClient.cs | 45 ++++++++++++++----- .../XmlResponseDocumentFormatter.cs | 26 +++++++---- .../Streams/XmlStreamsResponseDocument.cs | 23 ---------- 11 files changed, 231 insertions(+), 90 deletions(-) create mode 100644 libraries/MTConnect.NET-Common/Formatters/IFormatReadResult.cs diff --git a/clients/MTConnect.NET-Client-HTTP/Program.cs b/clients/MTConnect.NET-Client-HTTP/Program.cs index d9b508bb..8b0ece42 100644 --- a/clients/MTConnect.NET-Client-HTTP/Program.cs +++ b/clients/MTConnect.NET-Client-HTTP/Program.cs @@ -1,4 +1,5 @@ -using MTConnect.Input; +using MTConnect.Formatters; +using MTConnect.Input; using MTConnect.Observations; namespace MTConnect.Clients.HTTP @@ -16,12 +17,13 @@ static void Main(string[] args) static void DocumentClient() { //var client = new MTConnectHttpClient("http://mtconnect.mazakcorp.com/", 5719); - //var client = new MTConnectHttpClient("localhost", 5000); - var client = new MTConnectHttpClient("localhost", 5001); + var client = new MTConnectHttpClient("localhost", 5000); + //var client = new MTConnectHttpClient("localhost", 5001); client.Interval = 100; //client.Heartbeat = 0; client.ClientStarted += (s, args) => { Console.WriteLine("Client Started"); }; client.ClientStopped += (s, args) => { Console.WriteLine("Client Stopped"); }; + client.FormatError += (s, args) => { Console.WriteLine($"Format Error : {args.ContentType.Name} : {args.Messages?.FirstOrDefault()}"); }; client.ProbeReceived += (s, response) => { @@ -37,6 +39,9 @@ static void DocumentClient() foreach (var observation in componentStream.Observations) { Console.WriteLine($"Observation Received : {observation.DataItemId} : {string.Join(";", observation.Values.Select(o => o.Value))}"); + + var validationResult = observation.Validate(); + Console.WriteLine($"Observation Validation : {observation.DataItemId} : {validationResult.IsValid} : {validationResult.Message}"); } } } @@ -61,7 +66,10 @@ static void DocumentClient() client.AssetsReceived += (s, response) => { - Console.WriteLine(response.Assets.Count()); + foreach (var asset in response.Assets) + { + Console.WriteLine($"Asset Received : {asset.AssetId}"); + } }; client.Start(); diff --git a/libraries/MTConnect.NET-Common/Formatters/FormatReadResult.cs b/libraries/MTConnect.NET-Common/Formatters/FormatReadResult.cs index 9e78b70a..7ecb8a4d 100644 --- a/libraries/MTConnect.NET-Common/Formatters/FormatReadResult.cs +++ b/libraries/MTConnect.NET-Common/Formatters/FormatReadResult.cs @@ -1,14 +1,17 @@ -// Copyright (c) 2023 TrakHound Inc., All Rights Reserved. +// Copyright (c) 2024 TrakHound Inc., All Rights Reserved. // TrakHound Inc. licenses this file to you under the MIT license. +using System; using System.Collections.Generic; namespace MTConnect.Formatters { - public struct FormatReadResult + public struct FormatReadResult : IFormatReadResult { public T Content { get; set; } + public Type ContentType => typeof(T); + public bool Success { get; set; } public IEnumerable Messages { get; set; } diff --git a/libraries/MTConnect.NET-Common/Formatters/IFormatReadResult.cs b/libraries/MTConnect.NET-Common/Formatters/IFormatReadResult.cs new file mode 100644 index 00000000..6529e302 --- /dev/null +++ b/libraries/MTConnect.NET-Common/Formatters/IFormatReadResult.cs @@ -0,0 +1,23 @@ +// Copyright (c) 2024 TrakHound Inc., All Rights Reserved. +// TrakHound Inc. licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; + +namespace MTConnect.Formatters +{ + public interface IFormatReadResult + { + Type ContentType { get; } + + bool Success { get; } + + IEnumerable Messages { get; } + + IEnumerable Warnings { get; } + + IEnumerable Errors { get; } + + double ResponseDuration { get; } + } +} \ No newline at end of file diff --git a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpAssetClient.cs b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpAssetClient.cs index 95a0d437..e8484adb 100644 --- a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpAssetClient.cs +++ b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpAssetClient.cs @@ -3,6 +3,7 @@ using MTConnect.Assets; using MTConnect.Errors; +using MTConnect.Formatters; using MTConnect.Http; using System; using System.Collections.Generic; @@ -132,6 +133,11 @@ private void Init() /// public event EventHandler MTConnectError; + /// + /// Raised when a Document Formatting Error is received + /// + public event EventHandler FormatError; + /// /// Raised when an Connection Error occurs /// @@ -385,20 +391,35 @@ private IAssetsResponseDocument ReadDocument(HttpResponseMessage response, Strea var stream = MTConnectHttpResponse.HandleContentEncoding(contentEncoding, documentStream); if (stream != null && stream.Position > 0) stream.Seek(0, SeekOrigin.Begin); - // Process MTConnectAssets Document - var document = Formatters.ResponseDocumentFormatter.CreateAssetsResponseDocument(DocumentFormat.ToString(), stream).Content; - if (document != null) + var formatResult = ResponseDocumentFormatter.CreateAssetsResponseDocument(DocumentFormat, stream); + if (formatResult.Success) { - return document; + // Process MTConnectDevices Document + var document = formatResult.Content; + if (document != null) + { + return document; + } + else + { + // Process MTConnectError Document (if MTConnectStreams fails) + var errorFormatResult = ResponseDocumentFormatter.CreateErrorResponseDocument(DocumentFormat, stream); + if (errorFormatResult.Success) + { + var errorDocument = errorFormatResult.Content; + if (errorDocument != null) MTConnectError?.Invoke(this, errorDocument); + } + else + { + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, errorFormatResult); + } + } } else { - // Process MTConnectError Document (if MTConnectAssets fails) - var errorDocument = Formatters.ResponseDocumentFormatter.CreateErrorResponseDocument(DocumentFormat.ToString(), stream).Content; - if (errorDocument != null) - { - MTConnectError?.Invoke(this, errorDocument); - } + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, formatResult); } } diff --git a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs index 547e8ca9..38852eec 100644 --- a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs +++ b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs @@ -4,6 +4,7 @@ using MTConnect.Assets; using MTConnect.Devices; using MTConnect.Errors; +using MTConnect.Formatters; using MTConnect.Http; using MTConnect.Observations; using MTConnect.Streams; @@ -220,6 +221,11 @@ private void Init() /// public event EventHandler MTConnectError; + /// + /// Raised when a Document Formatting Error is received + /// + public event EventHandler FormatError; + /// /// Raised when an Connection Error occurs /// @@ -433,6 +439,7 @@ public IDevicesResponseDocument GetProbe() client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return client.Get(); @@ -456,6 +463,7 @@ public async Task GetProbeAsync(CancellationToken canc client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return await client.GetAsync(cancellationToken); @@ -472,6 +480,7 @@ public IStreamsResponseDocument GetCurrent(long at = 0, string path = null) client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return client.Get(); @@ -495,6 +504,7 @@ public async Task GetCurrentAsync(CancellationToken ca client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return await client.GetAsync(cancellationToken); @@ -511,6 +521,7 @@ public IStreamsResponseDocument GetSample(long from = 0, long to = 0, int count client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return client.Get(); @@ -534,6 +545,7 @@ public async Task GetSampleAsync(CancellationToken can client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return await client.GetAsync(cancellationToken); @@ -550,6 +562,7 @@ public IAssetsResponseDocument GetAssets(long count = 100) client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return client.Get(); @@ -573,6 +586,7 @@ public async Task GetAssetsAsync(CancellationToken canc client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return await client.GetAsync(cancellationToken); @@ -589,6 +603,7 @@ public IAssetsResponseDocument GetAsset(string assetId) client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return client.Get(); @@ -612,6 +627,7 @@ public async Task GetAssetAsync(string assetId, Cancell client.ContentEncodings = ContentEncodings; client.ContentType = ContentType; client.MTConnectError += (s, doc) => MTConnectError?.Invoke(this, doc); + client.FormatError += (s, r) => FormatError?.Invoke(this, r); client.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); client.InternalError += (s, ex) => InternalError?.Invoke(this, ex); return await client.GetAsync(cancellationToken); @@ -730,6 +746,7 @@ private async Task Worker() _stream.Stopped += (s, o) => StreamStopped?.Invoke(this, url); _stream.DocumentReceived += (s, doc) => ProcessSampleDocument(doc, _stop.Token); _stream.ErrorReceived += (s, doc) => ProcessSampleError(doc); + _stream.FormatError += (s, r) => FormatError?.Invoke(this, r); _stream.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex); _stream.InternalError += (s, ex) => InternalError?.Invoke(this, ex); @@ -1008,7 +1025,7 @@ private async void CheckAssetChanged(IEnumerable observations, Can { if (observations != null && observations.Count() > 0) { - var assetsChanged = observations.Where(o => o.Type == Devices.DataItems.AssetChangedDataItem.TypeId); + var assetsChanged = observations.Where(o => o.Type.ToUnderscoreUpper() == Devices.DataItems.AssetChangedDataItem.TypeId); if (assetsChanged != null) { foreach (var assetChanged in assetsChanged) diff --git a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClientStream.cs b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClientStream.cs index bf77e365..4125a290 100644 --- a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClientStream.cs +++ b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClientStream.cs @@ -2,6 +2,7 @@ // TrakHound Inc. licenses this file to you under the MIT license. using MTConnect.Errors; +using MTConnect.Formatters; using MTConnect.Http; using MTConnect.Streams; using System; @@ -76,6 +77,8 @@ public MTConnectHttpClientStream(string url, string documentFormat = DocumentFor public event EventHandler ErrorReceived; + public event EventHandler FormatError; + public event EventHandler InternalError; public event EventHandler ConnectionError; @@ -347,17 +350,35 @@ protected virtual void ProcessResponseBody(Stream responseBody, string contentEn var stream = MTConnectHttpResponse.HandleContentEncoding(contentEncoding, responseBody); if (stream.Position > 0) stream.Seek(0, SeekOrigin.Begin); - // Process MTConnectDevices Document - var document = Formatters.ResponseDocumentFormatter.CreateStreamsResponseDocument(_documentFormat, stream).Content; - if (document != null) + var formatResult = ResponseDocumentFormatter.CreateStreamsResponseDocument(_documentFormat, stream); + if (formatResult.Success) { - DocumentReceived?.Invoke(this, document); + // Process MTConnectDevices Document + var document = formatResult.Content; + if (document != null) + { + DocumentReceived?.Invoke(this, document); + } + else + { + // Process MTConnectError Document (if MTConnectStreams fails) + var errorFormatResult = ResponseDocumentFormatter.CreateErrorResponseDocument(_documentFormat, stream); + if (errorFormatResult.Success) + { + var errorDocument = errorFormatResult.Content; + if (errorDocument != null) ErrorReceived?.Invoke(this, errorDocument); + } + else + { + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, errorFormatResult); + } + } } else { - // Process MTConnectError Document (if MTConnectStreams fails) - var errorDocument = Formatters.ResponseDocumentFormatter.CreateErrorResponseDocument(_documentFormat, stream).Content; - if (errorDocument != null) ErrorReceived?.Invoke(this, errorDocument); + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, formatResult); } } } diff --git a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpCurrentClient.cs b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpCurrentClient.cs index 73ffd24b..a18b905f 100644 --- a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpCurrentClient.cs +++ b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpCurrentClient.cs @@ -2,6 +2,7 @@ // TrakHound Inc. licenses this file to you under the MIT license. using MTConnect.Errors; +using MTConnect.Formatters; using MTConnect.Http; using MTConnect.Streams; using System; @@ -135,6 +136,11 @@ public MTConnectHttpCurrentClient(string hostname, int port, string device = nul /// public event EventHandler MTConnectError; + /// + /// Raised when a Document Formatting Error is received + /// + public event EventHandler FormatError; + /// /// Raised when an Connection Error occurs /// @@ -160,7 +166,7 @@ public IStreamsResponseDocument Get() request.RequestUri = CreateUri(); // Add 'Accept' HTTP Header - var contentType = Formatters.ResponseDocumentFormatter.GetContentType(DocumentFormat); + var contentType = ResponseDocumentFormatter.GetContentType(DocumentFormat); if (!string.IsNullOrEmpty(contentType)) { request.Headers.Add(HttpHeaders.Accept, contentType); @@ -219,7 +225,7 @@ public async Task GetAsync(CancellationToken cancellat request.RequestUri = CreateUri(); // Add 'Accept' HTTP Header - var contentType = Formatters.ResponseDocumentFormatter.GetContentType(DocumentFormat); + var contentType = ResponseDocumentFormatter.GetContentType(DocumentFormat); if (!string.IsNullOrEmpty(contentType)) { request.Headers.Add(HttpHeaders.Accept, contentType); @@ -373,20 +379,35 @@ private IStreamsResponseDocument ReadDocument(HttpResponseMessage response, Stre var stream = MTConnectHttpResponse.HandleContentEncoding(contentEncoding, documentStream); if (stream != null && stream.Position > 0) stream.Seek(0, SeekOrigin.Begin); - // Process MTConnectStreams Document - var document = Formatters.ResponseDocumentFormatter.CreateStreamsResponseDocument(DocumentFormat.ToString(), stream).Content; - if (document != null) + var formatResult = ResponseDocumentFormatter.CreateStreamsResponseDocument(DocumentFormat, stream); + if (formatResult.Success) { - return document; + // Process MTConnectDevices Document + var document = formatResult.Content; + if (document != null) + { + return document; + } + else + { + // Process MTConnectError Document (if MTConnectStreams fails) + var errorFormatResult = ResponseDocumentFormatter.CreateErrorResponseDocument(DocumentFormat, stream); + if (errorFormatResult.Success) + { + var errorDocument = errorFormatResult.Content; + if (errorDocument != null) MTConnectError?.Invoke(this, errorDocument); + } + else + { + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, errorFormatResult); + } + } } else { - // Process MTConnectError Document (if MTConnectDevices fails) - var errorDocument = Formatters.ResponseDocumentFormatter.CreateErrorResponseDocument(DocumentFormat.ToString(), stream).Content; - if (errorDocument != null) - { - MTConnectError?.Invoke(this, errorDocument); - } + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, formatResult); } } diff --git a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpProbeClient.cs b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpProbeClient.cs index 69f306d5..d40d3664 100644 --- a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpProbeClient.cs +++ b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpProbeClient.cs @@ -3,6 +3,7 @@ using MTConnect.Devices; using MTConnect.Errors; +using MTConnect.Formatters; using MTConnect.Http; using System; using System.Collections.Generic; @@ -167,6 +168,11 @@ public MTConnectHttpProbeClient(string hostname, int port, string device = null, /// public event EventHandler MTConnectError; + /// + /// Raised when a Document Formatting Error is received + /// + public event EventHandler FormatError; + /// /// Raised when an Connection Error occurs /// @@ -406,20 +412,35 @@ private IDevicesResponseDocument ReadDocument(HttpResponseMessage response, Stre var stream = MTConnectHttpResponse.HandleContentEncoding(contentEncoding, documentStream); if (stream != null && stream.Position > 0) stream.Seek(0, SeekOrigin.Begin); - // Process MTConnectDevices Document - var document = Formatters.ResponseDocumentFormatter.CreateDevicesResponseDocument(DocumentFormat.ToString(), stream).Content; - if (document != null) + var formatResult = ResponseDocumentFormatter.CreateDevicesResponseDocument(DocumentFormat, stream); + if (formatResult.Success) { - return document; + // Process MTConnectDevices Document + var document = formatResult.Content; + if (document != null) + { + return document; + } + else + { + // Process MTConnectError Document (if MTConnectStreams fails) + var errorFormatResult = ResponseDocumentFormatter.CreateErrorResponseDocument(DocumentFormat, stream); + if (errorFormatResult.Success) + { + var errorDocument = errorFormatResult.Content; + if (errorDocument != null) MTConnectError?.Invoke(this, errorDocument); + } + else + { + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, errorFormatResult); + } + } } else { - // Process MTConnectError Document (if MTConnectDevices fails) - var errorDocument = Formatters.ResponseDocumentFormatter.CreateErrorResponseDocument(DocumentFormat.ToString(), stream).Content; - if (errorDocument != null) - { - MTConnectError?.Invoke(this, errorDocument); - } + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, formatResult); } } diff --git a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpSampleClient.cs b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpSampleClient.cs index 3cfce697..10c58491 100644 --- a/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpSampleClient.cs +++ b/libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpSampleClient.cs @@ -2,6 +2,7 @@ // TrakHound Inc. licenses this file to you under the MIT license. using MTConnect.Errors; +using MTConnect.Formatters; using MTConnect.Http; using MTConnect.Streams; using System; @@ -153,6 +154,11 @@ public MTConnectHttpSampleClient(string hostname, int port, string device = null /// public event EventHandler MTConnectError; + /// + /// Raised when a Document Formatting Error is received + /// + public event EventHandler FormatError; + /// /// Raised when an Connection Error occurs /// @@ -178,7 +184,7 @@ public IStreamsResponseDocument Get() request.RequestUri = CreateUri(); // Add 'Accept' HTTP Header - var contentType = Formatters.ResponseDocumentFormatter.GetContentType(DocumentFormat); + var contentType = ResponseDocumentFormatter.GetContentType(DocumentFormat); if (!string.IsNullOrEmpty(contentType)) { request.Headers.Add(HttpHeaders.Accept, contentType); @@ -236,7 +242,7 @@ public async Task GetAsync(CancellationToken cancel) request.RequestUri = CreateUri(); // Add 'Accept' HTTP Header - var contentType = Formatters.ResponseDocumentFormatter.GetContentType(DocumentFormat); + var contentType = ResponseDocumentFormatter.GetContentType(DocumentFormat); if (!string.IsNullOrEmpty(contentType)) { request.Headers.Add(HttpHeaders.Accept, contentType); @@ -404,20 +410,35 @@ private IStreamsResponseDocument ReadDocument(HttpResponseMessage response, Stre var stream = MTConnectHttpResponse.HandleContentEncoding(contentEncoding, documentStream); if (stream != null && stream.Position > 0) stream.Seek(0, SeekOrigin.Begin); - // Process MTConnectDevices Document - var document = Formatters.ResponseDocumentFormatter.CreateStreamsResponseDocument(DocumentFormat.ToString(), stream).Content; - if (document != null) + var formatResult = ResponseDocumentFormatter.CreateStreamsResponseDocument(DocumentFormat, stream); + if (formatResult.Success) { - return document; + // Process MTConnectDevices Document + var document = formatResult.Content; + if (document != null) + { + return document; + } + else + { + // Process MTConnectError Document (if MTConnectStreams fails) + var errorFormatResult = ResponseDocumentFormatter.CreateErrorResponseDocument(DocumentFormat, stream); + if (errorFormatResult.Success) + { + var errorDocument = errorFormatResult.Content; + if (errorDocument != null) MTConnectError?.Invoke(this, errorDocument); + } + else + { + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, errorFormatResult); + } + } } else { - // Process MTConnectError Document (if MTConnectDevices fails) - var errorDocument = Formatters.ResponseDocumentFormatter.CreateErrorResponseDocument(DocumentFormat.ToString(), stream).Content; - if (errorDocument != null) - { - MTConnectError?.Invoke(this, errorDocument); - } + // Raise Format Error + if (FormatError != null) FormatError.Invoke(this, formatResult); } } diff --git a/libraries/MTConnect.NET-XML/Formatters/XmlResponseDocumentFormatter.cs b/libraries/MTConnect.NET-XML/Formatters/XmlResponseDocumentFormatter.cs index 797b522e..9c540197 100644 --- a/libraries/MTConnect.NET-XML/Formatters/XmlResponseDocumentFormatter.cs +++ b/libraries/MTConnect.NET-XML/Formatters/XmlResponseDocumentFormatter.cs @@ -15,6 +15,7 @@ using System.IO; using System.Linq; using System.Text.Json; +using System.Xml; namespace MTConnect.Formatters.Xml { @@ -253,6 +254,8 @@ public FormatReadResult CreateDevicesResponseDocument( public FormatReadResult CreateStreamsResponseDocument(Stream content, IEnumerable> options = null) { + IStreamsResponseDocument document = null; + var success = false; var messages = new List(); var warnings = new List(); var errors = new List(); @@ -284,17 +287,22 @@ public FormatReadResult CreateStreamsResponseDocument( } } - byte[] bytes; - using (var memoryStream = new MemoryStream()) + try { - content.CopyTo(memoryStream); - bytes = memoryStream.ToArray(); + if (content != null && content.Length > 0) + { + using (var xmlReader = XmlReader.Create(content)) + { + document = XmlStreamsResponseDocument.ReadXml(xmlReader); + success = document != null; + } + } } - - // Read Document - var document = XmlStreamsResponseDocument.FromXml(bytes); - var success = document != null; - + catch (Exception ex) + { + messages.Add(ex.Message); + } + return new FormatReadResult(document, success, messages, warnings, errors); } diff --git a/libraries/MTConnect.NET-XML/Streams/XmlStreamsResponseDocument.cs b/libraries/MTConnect.NET-XML/Streams/XmlStreamsResponseDocument.cs index ab03bcf9..53bb9bf4 100644 --- a/libraries/MTConnect.NET-XML/Streams/XmlStreamsResponseDocument.cs +++ b/libraries/MTConnect.NET-XML/Streams/XmlStreamsResponseDocument.cs @@ -16,29 +16,6 @@ public static class XmlStreamsResponseDocument #region "Read" - public static IStreamsResponseDocument FromXml(byte[] xmlBytes) - { - if (xmlBytes != null && xmlBytes.Length > 0) - { - try - { - // Clean whitespace and Encoding Marks (BOM) - var bytes = XmlFunctions.SanitizeBytes(xmlBytes); - - using (var memoryReader = new MemoryStream(bytes)) - { - using (var xmlReader = XmlReader.Create(memoryReader)) - { - return ReadXml(xmlReader); - } - } - } - catch { } - } - - return null; - } - public static IStreamsResponseDocument ReadXml(XmlReader reader) { var document = new StreamsResponseDocument();