Skip to content

Commit

Permalink
Update Json Input Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Dec 12, 2023
1 parent 5c1f630 commit 45321bf
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 69 deletions.
4 changes: 2 additions & 2 deletions libraries/MTConnect.NET-Common/Formatters/IInputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public interface IInputFormatter

FormatWriteResult Format(IEnumerable<IObservationInput> observations, IEnumerable<KeyValuePair<string, string>> options = null);

FormatWriteResult Format(IAssetInput asset, IEnumerable<KeyValuePair<string, string>> options = null);
FormatWriteResult Format(IEnumerable<IAssetInput> assets, IEnumerable<KeyValuePair<string, string>> options = null);


FormatReadResult<IDevice> CreateDevice(byte[] content, IEnumerable<KeyValuePair<string, string>> options = null);

FormatReadResult<IEnumerable<IObservationInput>> CreateObservations(byte[] content, IEnumerable<KeyValuePair<string, string>> options = null);

FormatReadResult<IAsset> CreateAsset(string assetType, byte[] content, IEnumerable<KeyValuePair<string, string>> options = null);
FormatReadResult<IEnumerable<IAsset>> CreateAssets(byte[] content, IEnumerable<KeyValuePair<string, string>> options = null);
}
}
10 changes: 5 additions & 5 deletions libraries/MTConnect.NET-Common/Formatters/InputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static FormatWriteResult Format(string documentFormatterId, IEnumerable<I
return result;
}

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

Expand All @@ -83,7 +83,7 @@ public static FormatWriteResult Format(string documentFormatterId, IAssetInput a
if (formatter != null)
{
// Create the string representation of the Entity using the Formatter
result = formatter.Format(asset, options);
result = formatter.Format(assets, options);
}

stpw.Stop();
Expand Down Expand Up @@ -119,17 +119,17 @@ public static FormatReadResult<IEnumerable<IObservationInput>> CreateObservation
return FormatReadResult<IEnumerable<IObservationInput>>.Error(null, $"Input Formatter Not found for \"{documentFormatterId}\"");
}

public static FormatReadResult<IAsset> CreateAsset(string documentFormatterId, string assetType, byte[] content)
public static FormatReadResult<IEnumerable<IAsset>> CreateAssets(string documentFormatterId, byte[] content)
{
// Get the Formatter with the specified ID
var formatter = GetFormatter(documentFormatterId);
if (formatter != null)
{
// Create the Entity using the Formatter
return formatter.CreateAsset(assetType, content);
return formatter.CreateAssets(content);
}

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public class JsonComponentConfigurationParametersAsset
public IEnumerable<JsonParameterSet> ParameterSets { get; set; }


public JsonComponentConfigurationParametersAsset() { }
public JsonComponentConfigurationParametersAsset()
{
Type = ComponentConfigurationParametersAsset.TypeId;
}

public JsonComponentConfigurationParametersAsset(IComponentConfigurationParametersAsset asset)
{
Expand All @@ -59,7 +62,6 @@ public IComponentConfigurationParametersAsset ToComponentConfigurationParameters
var asset = new ComponentConfigurationParametersAsset();

asset.AssetId = AssetId;
asset.Type = Type;
asset.Timestamp = Timestamp;
asset.DeviceUuid = DeviceUuid;
asset.Removed = Removed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public class JsonCuttingToolAsset
public JsonCuttingToolArchetypeReference CuttingToolArchetypeReference { get; set; }


public JsonCuttingToolAsset() { }
public JsonCuttingToolAsset()
{
Type = CuttingToolAsset.TypeId;
}

public JsonCuttingToolAsset(ICuttingToolAsset asset)
{
Expand Down Expand Up @@ -78,7 +81,6 @@ public ICuttingToolAsset ToCuttingToolAsset()
var asset = new CuttingToolAsset();

asset.AssetId = AssetId;
asset.Type = Type;
asset.Timestamp = Timestamp;
asset.DeviceUuid = DeviceUuid;
asset.Removed = Removed;
Expand Down
6 changes: 4 additions & 2 deletions libraries/MTConnect.NET-JSON/Assets/Files/JsonFileAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ public class JsonFileAsset
public DateTime? ModificationTime { get; set; }


public JsonFileAsset() { }
public JsonFileAsset()
{
Type = FileAsset.TypeId;
}

public JsonFileAsset(IFileAsset asset)
{
Expand Down Expand Up @@ -139,7 +142,6 @@ public IFileAsset ToFileAsset()
var asset = new FileAsset();

asset.AssetId = AssetId;
asset.Type = Type;
asset.Timestamp = Timestamp;
asset.DeviceUuid = DeviceUuid;
asset.Removed = Removed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public class JsonQIFDocumentWrapperAsset
public string QIFDocument { get; set; }


public JsonQIFDocumentWrapperAsset() { }
public JsonQIFDocumentWrapperAsset()
{
Type = QIFDocumentWrapperAsset.TypeId;
}

public JsonQIFDocumentWrapperAsset(IQIFDocumentWrapperAsset asset)
{
Expand All @@ -65,7 +68,6 @@ public IQIFDocumentWrapperAsset ToQIFDocumentWrapperAsset()
var asset = new QIFDocumentWrapperAsset();

asset.AssetId = AssetId;
asset.Type = Type;
asset.Timestamp = Timestamp;
asset.DeviceUuid = DeviceUuid;
asset.Removed = Removed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ public class JsonRawMaterialAsset
public JsonMaterial Material { get; set; }


public JsonRawMaterialAsset() { }
public JsonRawMaterialAsset()
{
Type = RawMaterialAsset.TypeId;
}

public JsonRawMaterialAsset(IRawMaterialAsset asset)
{
Expand Down Expand Up @@ -122,7 +125,6 @@ public IRawMaterialAsset ToRawMaterialAsset()
var asset = new RawMaterialAsset();

asset.AssetId = AssetId;
asset.Type = Type;
asset.Timestamp = Timestamp;
asset.DeviceUuid = DeviceUuid;
asset.Removed = Removed;
Expand Down
70 changes: 18 additions & 52 deletions libraries/MTConnect.NET-JSON/Formatters/JsonInputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Assets;
using MTConnect.Assets.CuttingTools;
using MTConnect.Assets.Files;
using MTConnect.Assets.Json.CuttingTools;
using MTConnect.Assets.Json.Files;
using MTConnect.Assets.Json.QIF;
using MTConnect.Assets.Json.RawMaterials;
using MTConnect.Assets.QIF;
using MTConnect.Assets.RawMaterials;
using MTConnect.Devices;
using MTConnect.Devices.Json;
using MTConnect.Input;
using MTConnect.Mqtt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;

namespace MTConnect.Formatters
Expand Down Expand Up @@ -57,25 +48,18 @@ public FormatWriteResult Format(IEnumerable<IObservationInput> observations, IEn
return FormatWriteResult.Error();
}

public FormatWriteResult Format(IAssetInput asset, IEnumerable<KeyValuePair<string, string>> options = null)
public FormatWriteResult Format(IEnumerable<IAssetInput> assets, IEnumerable<KeyValuePair<string, string>> options = null)
{
if (asset != null)
if (!assets.IsNullOrEmpty())
{
byte[] bytes;

switch (asset.Type)
{
case "CuttingTool": bytes = JsonFunctions.ConvertBytes(new JsonCuttingToolAsset(asset as CuttingToolAsset)); break;
case "File": bytes = JsonFunctions.ConvertBytes(new JsonFileAsset(asset as FileAsset)); break;
case "QIFDocumentWrapper": bytes = JsonFunctions.ConvertBytes(new JsonQIFDocumentWrapperAsset(asset as QIFDocumentWrapperAsset)); break;
case "RawMaterial": bytes = JsonFunctions.ConvertBytes(new JsonRawMaterialAsset(asset as RawMaterialAsset)); break;

default: bytes = JsonFunctions.ConvertBytes(asset); break;
}

if (bytes != null)
var jsonAssetGroup = new JsonInputAssetGroup(assets);
if (jsonAssetGroup != null)
{
return FormatWriteResult.Successful(bytes, ContentType);
var bytes = JsonFunctions.ConvertBytes(jsonAssetGroup);
if (bytes != null)
{
return FormatWriteResult.Successful(bytes, ContentType);
}
}
}

Expand Down Expand Up @@ -126,52 +110,34 @@ public FormatReadResult<IEnumerable<IObservationInput>> CreateObservations(byte[
return new FormatReadResult<IEnumerable<IObservationInput>>();
}

public FormatReadResult<IAsset> CreateAsset(string assetType, byte[] content, IEnumerable<KeyValuePair<string, string>> options = null)
public FormatReadResult<IEnumerable<IAsset>> CreateAssets(byte[] content, IEnumerable<KeyValuePair<string, string>> options = null)
{
var messages = new List<string>();
var warnings = new List<string>();
var errors = new List<string>();

IAsset asset = null;
IEnumerable<IAsset> assets = null;

// Read Document
if (!string.IsNullOrEmpty(assetType) && content != null)
if (content != null)
{
try
{
// Convert from UTF8 bytes
var json = Encoding.UTF8.GetString(content);
if (!string.IsNullOrEmpty(json))
var jsonAssetGroup = JsonSerializer.Deserialize<JsonInputAssetGroup>(content);
if (jsonAssetGroup != null)
{
switch (assetType)
{
case "CuttingTool":
asset = JsonSerializer.Deserialize<JsonCuttingToolAsset>(json).ToCuttingToolAsset();
break;

case "File":
asset = JsonSerializer.Deserialize<JsonFileAsset>(json).ToFileAsset();
break;

case "QIFDocumentWrapper":
asset = JsonSerializer.Deserialize<JsonQIFDocumentWrapperAsset>(json).ToQIFDocumentWrapperAsset();
break;

case "RawMaterial":
asset = JsonSerializer.Deserialize<JsonRawMaterialAsset>(json).ToRawMaterialAsset();
break;
}
assets = JsonInputAssetGroup.ToAssets(jsonAssetGroup);
}
}
catch (Exception ex)
{
errors.Add(ex.Message);

}
}

var success = asset != null;
var success = assets != null;

return new FormatReadResult<IAsset>(asset, success, messages, warnings, errors);
return new FormatReadResult<IEnumerable<IAsset>>(assets, success, messages, warnings, errors);
}


Expand Down
Loading

0 comments on commit 45321bf

Please sign in to comment.