Skip to content

Commit

Permalink
Updated to add a FormatError to MTConnectHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Mar 1, 2024
1 parent bf242b2 commit 2db33be
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 90 deletions.
16 changes: 12 additions & 4 deletions clients/MTConnect.NET-Client-HTTP/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MTConnect.Input;
using MTConnect.Formatters;
using MTConnect.Input;
using MTConnect.Observations;

namespace MTConnect.Clients.HTTP
Expand All @@ -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) =>
{
Expand All @@ -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}");
}
}
}
Expand All @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions libraries/MTConnect.NET-Common/Formatters/FormatReadResult.cs
Original file line number Diff line number Diff line change
@@ -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<T>
public struct FormatReadResult<T> : IFormatReadResult
{
public T Content { get; set; }

public Type ContentType => typeof(T);

public bool Success { get; set; }

public IEnumerable<string> Messages { get; set; }
Expand Down
23 changes: 23 additions & 0 deletions libraries/MTConnect.NET-Common/Formatters/IFormatReadResult.cs
Original file line number Diff line number Diff line change
@@ -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<string> Messages { get; }

IEnumerable<string> Warnings { get; }

IEnumerable<string> Errors { get; }

double ResponseDuration { get; }
}
}
41 changes: 31 additions & 10 deletions libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpAssetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using MTConnect.Assets;
using MTConnect.Errors;
using MTConnect.Formatters;
using MTConnect.Http;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -132,6 +133,11 @@ private void Init()
/// </summary>
public event EventHandler<IErrorResponseDocument> MTConnectError;

/// <summary>
/// Raised when a Document Formatting Error is received
/// </summary>
public event EventHandler<IFormatReadResult> FormatError;

/// <summary>
/// Raised when an Connection Error occurs
/// </summary>
Expand Down Expand Up @@ -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);
}
}

Expand Down
19 changes: 18 additions & 1 deletion libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -220,6 +221,11 @@ private void Init()
/// </summary>
public event EventHandler<IErrorResponseDocument> MTConnectError;

/// <summary>
/// Raised when a Document Formatting Error is received
/// </summary>
public event EventHandler<IFormatReadResult> FormatError;

/// <summary>
/// Raised when an Connection Error occurs
/// </summary>
Expand Down Expand Up @@ -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();
Expand All @@ -456,6 +463,7 @@ public async Task<IDevicesResponseDocument> 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);
Expand All @@ -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();
Expand All @@ -495,6 +504,7 @@ public async Task<IStreamsResponseDocument> 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);
Expand All @@ -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();
Expand All @@ -534,6 +545,7 @@ public async Task<IStreamsResponseDocument> 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);
Expand All @@ -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();
Expand All @@ -573,6 +586,7 @@ public async Task<IAssetsResponseDocument> 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);
Expand All @@ -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();
Expand All @@ -612,6 +627,7 @@ public async Task<IAssetsResponseDocument> 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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -1008,7 +1025,7 @@ private async void CheckAssetChanged(IEnumerable<IObservation> 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)
Expand Down
35 changes: 28 additions & 7 deletions libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClientStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +77,8 @@ public MTConnectHttpClientStream(string url, string documentFormat = DocumentFor

public event EventHandler<IErrorResponseDocument> ErrorReceived;

public event EventHandler<IFormatReadResult> FormatError;

public event EventHandler<Exception> InternalError;

public event EventHandler<Exception> ConnectionError;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Loading

0 comments on commit 2db33be

Please sign in to comment.