Skip to content

Commit

Permalink
Added new InputFormatter to format input data
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Dec 10, 2023
1 parent 0add24b commit 09b4919
Show file tree
Hide file tree
Showing 30 changed files with 972 additions and 700 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static IEnumerable<DeviceConfiguration> FromFile(string filePath, string
if (contents != null)
{
// Read ResponseDocument Format
var devicesDocument = Formatters.ResponseDocumentFormatter.CreateDevicesResponseDocument(documentFormatterId, contents).Document;
var devicesDocument = Formatters.ResponseDocumentFormatter.CreateDevicesResponseDocument(documentFormatterId, contents).Content;
if (devicesDocument != null && devicesDocument.Devices != null && devicesDocument.Devices.Count() > 0)
{
var devices = new List<DeviceConfiguration>();
Expand All @@ -93,7 +93,7 @@ public static IEnumerable<DeviceConfiguration> FromFile(string filePath, string
else
{
// Read Single Entity Format
var device = Formatters.EntityFormatter.CreateDevice(documentFormatterId, contents).Entity;
var device = Formatters.EntityFormatter.CreateDevice(documentFormatterId, contents).Content;
if (device != null)
{
return new List<DeviceConfiguration> { new DeviceConfiguration(device, rootPath) };
Expand Down Expand Up @@ -136,7 +136,7 @@ public static async Task<IEnumerable<DeviceConfiguration>> FromFileAsync(string
if (contents != null)
{
// Read ResponseDocument Format
var devicesDocument = Formatters.ResponseDocumentFormatter.CreateDevicesResponseDocument(documentFormatterId, contents).Document;
var devicesDocument = Formatters.ResponseDocumentFormatter.CreateDevicesResponseDocument(documentFormatterId, contents).Content;
if (devicesDocument != null && devicesDocument.Devices != null && devicesDocument.Devices.Count() > 0)
{
var devices = new List<DeviceConfiguration>();
Expand All @@ -151,7 +151,7 @@ public static async Task<IEnumerable<DeviceConfiguration>> FromFileAsync(string
else
{
// Read Single Entity Format
var device = Formatters.EntityFormatter.CreateDevice(documentFormatterId, contents).Entity;
var device = Formatters.EntityFormatter.CreateDevice(documentFormatterId, contents).Content;
if (device != null)
{
return new List<DeviceConfiguration> { new DeviceConfiguration(device, rootPath) };
Expand Down
96 changes: 18 additions & 78 deletions libraries/MTConnect.NET-Common/Formatters/EntityFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public static string GetContentType(string documentFormatterId)
}


public static FormattedEntityWriteResult Format(string documentFormatterId, IDevice device, IEnumerable<KeyValuePair<string, string>> options = null)
public static FormatWriteResult Format(string documentFormatterId, IDevice device, IEnumerable<KeyValuePair<string, string>> options = null)
{
var stpw = Stopwatch.StartNew();

FormattedEntityWriteResult result = FormattedEntityWriteResult.Error();
FormatWriteResult result = FormatWriteResult.Error();

// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
Expand All @@ -52,71 +52,11 @@ public static FormattedEntityWriteResult Format(string documentFormatterId, IDev
return result;
}

//public static FormattedEntityWriteResult Format(string documentFormatterId, IComponent component, IEnumerable<KeyValuePair<string, string>> options = null)
//{
// var stpw = Stopwatch.StartNew();

// FormattedEntityWriteResult result = FormattedEntityWriteResult.Error();

// // Get the Formatter with the specified ID
// var formatter = GetFormatter(documentFormatterId);
// if (formatter != null)
// {
// // Create the string representation of the Entity using the Formatter
// result = formatter.Format(component, options);
// }

// stpw.Stop();
// result.ResponseDuration = stpw.GetElapsedMilliseconds();

// return result;
//}

//public static FormattedEntityWriteResult Format(string documentFormatterId, IComposition composition, IEnumerable<KeyValuePair<string, string>> options = null)
//{
// var stpw = Stopwatch.StartNew();

// FormattedEntityWriteResult result = FormattedEntityWriteResult.Error();

// // Get the Formatter with the specified ID
// var formatter = GetFormatter(documentFormatterId);
// if (formatter != null)
// {
// // Create the string representation of the Entity using the Formatter
// result = formatter.Format(composition, options);
// }

// stpw.Stop();
// result.ResponseDuration = stpw.GetElapsedMilliseconds();

// return result;
//}

//public static FormattedEntityWriteResult Format(string documentFormatterId, IDataItem dataItem, IEnumerable<KeyValuePair<string, string>> options = null)
//{
// var stpw = Stopwatch.StartNew();

// FormattedEntityWriteResult result = FormattedEntityWriteResult.Error();

// // Get the Formatter with the specified ID
// var formatter = GetFormatter(documentFormatterId);
// if (formatter != null)
// {
// // Create the string representation of the Entity using the Formatter
// result = formatter.Format(dataItem, options);
// }

// stpw.Stop();
// result.ResponseDuration = stpw.GetElapsedMilliseconds();

// return result;
//}

public static FormattedEntityWriteResult Format(string documentFormatterId, IObservation observation, IEnumerable<KeyValuePair<string, string>> options = null)
public static FormatWriteResult Format(string documentFormatterId, IObservation observation, IEnumerable<KeyValuePair<string, string>> options = null)
{
var stpw = Stopwatch.StartNew();

FormattedEntityWriteResult result = FormattedEntityWriteResult.Error();
FormatWriteResult result = FormatWriteResult.Error();

// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
Expand All @@ -132,11 +72,11 @@ public static FormattedEntityWriteResult Format(string documentFormatterId, IObs
return result;
}

public static FormattedEntityWriteResult Format(string documentFormatterId, IEnumerable<IObservation> observations, IEnumerable<KeyValuePair<string, string>> options = null)
public static FormatWriteResult Format(string documentFormatterId, IEnumerable<IObservation> observations, IEnumerable<KeyValuePair<string, string>> options = null)
{
var stpw = Stopwatch.StartNew();

FormattedEntityWriteResult result = FormattedEntityWriteResult.Error();
FormatWriteResult result = FormatWriteResult.Error();

// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
Expand All @@ -152,11 +92,11 @@ public static FormattedEntityWriteResult Format(string documentFormatterId, IEnu
return result;
}

public static FormattedEntityWriteResult Format(string documentFormatterId, IAsset asset, IEnumerable<KeyValuePair<string, string>> options = null)
public static FormatWriteResult Format(string documentFormatterId, IAsset asset, IEnumerable<KeyValuePair<string, string>> options = null)
{
var stpw = Stopwatch.StartNew();

FormattedEntityWriteResult result = FormattedEntityWriteResult.Error();
FormatWriteResult result = FormatWriteResult.Error();

// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
Expand All @@ -173,7 +113,7 @@ public static FormattedEntityWriteResult Format(string documentFormatterId, IAss
}


public static FormattedEntityReadResult<IDevice> CreateDevice(string documentFormatterId, byte[] content)
public static FormatReadResult<IDevice> CreateDevice(string documentFormatterId, byte[] content)
{
// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
Expand All @@ -183,10 +123,10 @@ public static FormattedEntityReadResult<IDevice> CreateDevice(string documentFor
return formatter.CreateDevice(content);
}

return FormattedEntityReadResult<IDevice>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
return FormatReadResult<IDevice>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
}

public static FormattedEntityReadResult<IComponent> CreateComponent(string documentFormatterId, byte[] content)
public static FormatReadResult<IComponent> CreateComponent(string documentFormatterId, byte[] content)
{
// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
Expand All @@ -196,10 +136,10 @@ public static FormattedEntityReadResult<IComponent> CreateComponent(string docum
return formatter.CreateComponent(content);
}

return FormattedEntityReadResult<IComponent>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
return FormatReadResult<IComponent>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
}

public static FormattedEntityReadResult<IComposition> CreateComposition(string documentFormatterId, byte[] content)
public static FormatReadResult<IComposition> CreateComposition(string documentFormatterId, byte[] content)
{
// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
Expand All @@ -209,10 +149,10 @@ public static FormattedEntityReadResult<IComposition> CreateComposition(string d
return formatter.CreateComposition(content);
}

return FormattedEntityReadResult<IComposition>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
return FormatReadResult<IComposition>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
}

public static FormattedEntityReadResult<IDataItem> CreateDataItem(string documentFormatterId, byte[] content)
public static FormatReadResult<IDataItem> CreateDataItem(string documentFormatterId, byte[] content)
{
// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
Expand All @@ -222,10 +162,10 @@ public static FormattedEntityReadResult<IDataItem> CreateDataItem(string documen
return formatter.CreateDataItem(content);
}

return FormattedEntityReadResult<IDataItem>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
return FormatReadResult<IDataItem>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
}

public static FormattedEntityReadResult<IAsset> CreateAsset(string documentFormatterId, string assetType, byte[] content)
public static FormatReadResult<IAsset> CreateAsset(string documentFormatterId, string assetType, byte[] content)
{
// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
Expand All @@ -235,7 +175,7 @@ public static FormattedEntityReadResult<IAsset> CreateAsset(string documentForma
return formatter.CreateAsset(assetType, content);
}

return FormattedEntityReadResult<IAsset>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
return FormatReadResult<IAsset>.Error(null, $"Entity Formatter Not found for \"{documentFormatterId}\"");
}


Expand Down
80 changes: 80 additions & 0 deletions libraries/MTConnect.NET-Common/Formatters/FormatReadResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using System.Collections.Generic;

namespace MTConnect.Formatters
{
public struct FormatReadResult<T>
{
public T Content { get; set; }

public bool Success { get; set; }

public IEnumerable<string> Messages { get; set; }

public IEnumerable<string> Warnings { get; set; }

public IEnumerable<string> Errors { get; set; }

public double ResponseDuration { get; set; }


public FormatReadResult(T content, bool success = true, IEnumerable<string> messages = null, IEnumerable<string> warnings = null, IEnumerable<string> errors = null)
{
Content = content;
Success = success;
Messages = messages;
Warnings = warnings;
Errors = errors;
ResponseDuration = 0;
}


public static FormatReadResult<T> Successful(T content, string message = null)
{
var messages = new List<string>();
if (!string.IsNullOrEmpty(message)) messages = new List<string> { message };

return new FormatReadResult<T>(content, true, messages);
}

public static FormatReadResult<T> Successful(T content, IEnumerable<string> messages)
{
return new FormatReadResult<T>(content, true, messages);
}


public static FormatReadResult<T> Warning(T content, string warning = null)
{
var warnings = new List<string>();
if (!string.IsNullOrEmpty(warning)) warnings = new List<string> { warning };

return new FormatReadResult<T>(content, true, null, warnings);
}

public static FormatReadResult<T> Warning(T content, IEnumerable<string> warnings)
{
return new FormatReadResult<T>(content, true, null, warnings);
}


public static FormatReadResult<T> Error(T content, string error = null)
{
var errors = new List<string>();
if (!string.IsNullOrEmpty(error)) errors = new List<string> { error };

return new FormatReadResult<T>(content, false, null, null, errors);
}

public static FormatReadResult<T> Error(T content, IEnumerable<string> errors)
{
return new FormatReadResult<T>(content, false, null, null, errors);
}

public static FormatReadResult<T> Error(IEnumerable<string> errors = null)
{
return new FormatReadResult<T>(default, false, null, null, errors);
}
}
}
83 changes: 83 additions & 0 deletions libraries/MTConnect.NET-Common/Formatters/FormatWriteResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using System.Collections.Generic;

namespace MTConnect.Formatters
{
public struct FormatWriteResult
{
public byte[] Content { get; set; }

public string ContentType { get; set; }

public bool Success { get; set; }

public IEnumerable<string> Messages { get; set; }

public IEnumerable<string> Warnings { get; set; }

public IEnumerable<string> Errors { get; set; }

public double ResponseDuration { get; set; }


public FormatWriteResult(byte[] content, string contentType, bool success = true, IEnumerable<string> messages = null, IEnumerable<string> warnings = null, IEnumerable<string> errors = null)
{
Content = content;
ContentType = contentType;
Success = success;
Messages = messages;
Warnings = warnings;
Errors = errors;
ResponseDuration = 0;
}


public static FormatWriteResult Successful(byte[] content, string contentType, string message = null)
{
var messages = new List<string>();
if (!string.IsNullOrEmpty(message)) messages = new List<string> { message };

return new FormatWriteResult(content, contentType, true, messages);
}

public static FormatWriteResult Successful(byte[] content, string contentType, IEnumerable<string> messages)
{
return new FormatWriteResult(content, contentType, true, messages);
}


public static FormatWriteResult Warning(byte[] content, string contentType, string warning = null)
{
var warnings = new List<string>();
if (!string.IsNullOrEmpty(warning)) warnings = new List<string> { warning };

return new FormatWriteResult(content, contentType, true, null, warnings);
}

public static FormatWriteResult Warning(byte[] content, string contentType, IEnumerable<string> warnings)
{
return new FormatWriteResult(content, contentType, true, null, warnings);
}


public static FormatWriteResult Error(byte[] content, string contentType, string error = null)
{
var errors = new List<string>();
if (!string.IsNullOrEmpty(error)) errors = new List<string> { error };

return new FormatWriteResult(content, contentType, false, null, null, errors);
}

public static FormatWriteResult Error(byte[] content, string contentType, IEnumerable<string> errors)
{
return new FormatWriteResult(content, contentType, false, null, null, errors);
}

public static FormatWriteResult Error(IEnumerable<string> errors = null)
{
return new FormatWriteResult(null, null, false, null, null, errors);
}
}
}
Loading

0 comments on commit 09b4919

Please sign in to comment.