Skip to content

Commit

Permalink
Added Logging to Modules and Processors
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Nov 8, 2023
1 parent 9d89411 commit 18a0abb
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\MTConnect.NET-HTTP\MTConnect.NET-HTTP.csproj" />
<ProjectReference Include="..\..\src\MTConnect.NET-SHDR\MTConnect.NET-SHDR.csproj" />
<ProjectReference Include="..\..\src\MTConnect.NET-XML\MTConnect.NET-XML.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public MTConnectHttpServerModule(IMTConnectAgentBroker mtconnectAgent, object co
public void StartAfterLoad()
{
// Intialize the Http Server
_httpServer = new MTConnectHttpAgentServer(_configuration, _mtconnectAgent);
_httpServer = new MTConnectShdrHttpAgentServer(_configuration, _mtconnectAgent);

_httpServer.ServerStarted += HttpListenerStarted;
_httpServer.ServerStopped += HttpListenerStopped;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Agents;
using MTConnect.Assets;
using MTConnect.Configurations;
using MTConnect.Devices;
using MTConnect.Devices.DataItems;
using MTConnect.Formatters;
using MTConnect.Servers;
using MTConnect.Shdr;
using System;
using System.Linq;
using System.Text;

namespace MTConnect.Modules.Http
{
/// <summary>
/// An Http Web Server for processing MTConnect REST Api Requests that supports SHDR Put and Post requests
/// </summary>
public class MTConnectShdrHttpAgentServer : MTConnectHttpAgentServer
{
public MTConnectShdrHttpAgentServer(HttpModuleConfiguration configuration, IMTConnectAgentBroker mtconnectAgent) : base(configuration, mtconnectAgent) { }


//protected override bool OnObservationInput(string deviceKey, string dataItemKey, string input)
protected override bool OnObservationInput(MTConnectObservationInputArgs args)
{
// Get the Devices Document from the Agent
var devicesDocument = _mtconnectAgent.GetDevicesResponseDocument(args.DeviceKey);
if (devicesDocument != null && !devicesDocument.Devices.IsNullOrEmpty())
{
// Get the first Device (should only be one Device)
var device = devicesDocument.Devices.FirstOrDefault();
if (device != null)
{
// Get the DataItem based on the Key
var dataItem = device.GetDataItemByKey(args.DataItemKey);
if (dataItem != null)
{
// Construct an SHDR Line using the DataItemId and the Input string from Http
var shdrLine = $"|{dataItem.Id}|{args.Value}";

if (dataItem.Category == DataItemCategory.CONDITION)
{
var condition = ShdrFaultState.FromString(shdrLine);
if (condition != null) _mtconnectAgent.AddObservation(device.Uuid, condition);
}
else if (dataItem.Type == Devices.DataItems.MessageDataItem.TypeId)
{
var message = ShdrMessage.FromString(shdrLine);
if (message != null) _mtconnectAgent.AddObservation(device.Uuid, message);
}
else if (dataItem.Representation == DataItemRepresentation.TABLE)
{
var table = ShdrTable.FromString(shdrLine);
if (table != null) _mtconnectAgent.AddObservation(device.Uuid, table);
}
else if (dataItem.Representation == DataItemRepresentation.DATA_SET)
{
var dataSet = ShdrDataSet.FromString(shdrLine);
if (dataSet != null) _mtconnectAgent.AddObservation(device.Uuid, dataSet);
}
else if (dataItem.Representation == DataItemRepresentation.TIME_SERIES)
{
var timeSeries = ShdrTimeSeries.FromString(shdrLine);
if (timeSeries != null) _mtconnectAgent.AddObservation(device.Uuid, timeSeries);
}
else
{
var dataItems = ShdrDataItem.FromString(shdrLine);
if (!dataItems.IsNullOrEmpty()) _mtconnectAgent.AddObservations(device.Uuid, dataItems);
}
}
else
{
//if (_mtconnectAgent.InvalidObservationAdded != null)
//{
// _mtconnectAgent.InvalidObservationAdded.Invoke(deviceKey, dataItemKey, new ValidationResult(false, $"DataItemKey \"{dataItemKey}\" not Found in Device"));
//}
}

return true;
}
else
{
//if (_mtconnectAgent.InvalidObservationAdded != null)
//{
// _mtconnectAgent.InvalidObservationAdded.Invoke(deviceKey, dataItemKey, new ValidationResult(false, $"Device \"{deviceKey}\" not Found"));
//}
}
}

return false;
}

//protected override bool OnAssetInput(string assetId, string deviceKey, string assetType, byte[] requestBytes, string documentFormat = DocumentFormat.XML)
protected override bool OnAssetInput(MTConnectAssetInputArgs args)
{
if (!string.IsNullOrEmpty(args.DeviceKey) && !string.IsNullOrEmpty(args.AssetType))
{
//var asset = Assets.Xml.XmlAsset.FromXml(assetType, );
var result = EntityFormatter.CreateAsset(args.DocumentFormat, args.AssetType, ReadRequestBody(args.RequestBody));
if (result.Success)
{
var asset = (Asset)result.Entity;
asset.AssetId = args.AssetId;
asset.Timestamp = asset.Timestamp > DateTime.MinValue ? asset.Timestamp : DateTime.Now;
return _mtconnectAgent.AddAsset(args.DeviceKey, asset);
}
}

return false;
}

private byte[] ReadRequestBody(byte[] bytes)
{
if (bytes != null)
{
try
{
return Encoding.Convert(Encoding.ASCII, Encoding.UTF8, bytes);
}
catch { }
}

return null;
}
}
}
12 changes: 12 additions & 0 deletions src/MTConnect.NET-Applications-Agents/MTConnectAgentApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public void StartAgent(IAgentApplicationConfiguration configuration, bool verbos

// Initialize Agent Modules
_modules = new MTConnectAgentModules(configuration, _mtconnectAgent);
_modules.ModuleLoaded += ModuleLoaded;
_modules.Load();

// Read Indexes for Buffer
Expand Down Expand Up @@ -392,6 +393,7 @@ public void StartAgent(IAgentApplicationConfiguration configuration, bool verbos

// Initilialize Processors
_processors = new MTConnectAgentProcessors(configuration);
_processors.ProcessorLoaded += ProcessorLoaded;
_processors.Load();
_mtconnectAgent.ProcessObservationFunction = _processors.Process;

Expand Down Expand Up @@ -619,6 +621,16 @@ private void PrintHelp()

#region "Logging"

private void ModuleLoaded(object sender, IMTConnectAgentModule module)
{
_applicationLogger.Debug($"[Application] : Module Loaded : " + module.GetType().Name);
}

private void ProcessorLoaded(object sender, IMTConnectAgentProcessor processor)
{
_applicationLogger.Debug($"[Application] : Processor Loaded : " + processor.GetType().Name);
}

private void DevicesRequested(string deviceName)
{
_agentLogger.Debug($"[Agent] : MTConnectDevices Requested : " + deviceName);
Expand Down
5 changes: 5 additions & 0 deletions src/MTConnect.NET-Common/Agents/MTConnectAgentModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class MTConnectAgentModules
private readonly IMTConnectAgentBroker _mtconnectAgent;


public event EventHandler<IMTConnectAgentModule> ModuleLoaded;


public MTConnectAgentModules(IAgentApplicationConfiguration configuration, IMTConnectAgentBroker mtconnectAgent)
{
_configuration = configuration;
Expand Down Expand Up @@ -48,6 +51,8 @@ public void Load()

var moduleId = Guid.NewGuid().ToString();

if (ModuleLoaded != null) ModuleLoaded.Invoke(this, module);

lock (_lock) _modules.Add(moduleId, module);
}
catch { }
Expand Down
5 changes: 5 additions & 0 deletions src/MTConnect.NET-Common/Agents/MTConnectAgentProcessors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class MTConnectAgentProcessors
private readonly IAgentApplicationConfiguration _configuration;


public event EventHandler<IMTConnectAgentProcessor> ProcessorLoaded;


public MTConnectAgentProcessors(IAgentApplicationConfiguration configuration)
{
_configuration = configuration;
Expand Down Expand Up @@ -49,6 +52,8 @@ public void Load()

var processorId = Guid.NewGuid().ToString();

if (ProcessorLoaded != null) ProcessorLoaded.Invoke(this, processor);

lock (_lock) _processors.Add(processorId, processor);
}
catch { }
Expand Down
17 changes: 0 additions & 17 deletions src/MTConnect.NET-Common/IMTConnectController.cs

This file was deleted.

17 changes: 0 additions & 17 deletions src/MTConnect.NET-Common/IMTConnectDataSource.cs

This file was deleted.

4 changes: 0 additions & 4 deletions src/MTConnect.NET-Common/MTConnect.NET-Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,10 @@
<ItemGroup>
<Compile Remove="Buffers\IMTConnectDeviceBuffer.cs" />
<Compile Remove="Buffers\MTConnectDeviceBuffer.cs" />
<Compile Remove="IMTConnectController.cs" />
<Compile Remove="IMTConnectDataSource.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Buffers\IMTConnectDeviceBuffer.cs" />
<None Include="Buffers\MTConnectDeviceBuffer.cs" />
<None Include="IMTConnectController.cs" />
<None Include="IMTConnectDataSource.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

<ItemGroup>
<PackageReference Include="IronPython.StdLib" Version="3.4.1" />
<PackageReference Include="NLog" Version="5.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 18a0abb

Please sign in to comment.