Skip to content

Commit

Permalink
Added MTConnectAgentProcessor base class and added Log() method for p…
Browse files Browse the repository at this point in the history
…rocessors
  • Loading branch information
PatrickRitchie committed Dec 5, 2023
1 parent cbb53e2 commit 37b131e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class MTConnectAgentApplication : IMTConnectAgentApplication
protected readonly Logger _agentMetricLogger = LogManager.GetLogger("agent-metric-logger");
protected readonly Logger _agentValidationLogger = LogManager.GetLogger("agent-validation-logger");
protected readonly Logger _moduleLogger = LogManager.GetLogger("module-logger");
protected readonly Logger _processorLogger = LogManager.GetLogger("processor-logger");

private readonly List<DeviceConfigurationFileWatcher> _deviceConfigurationWatchers = new List<DeviceConfigurationFileWatcher>();

Expand Down Expand Up @@ -401,6 +402,7 @@ public void StartAgent(IAgentApplicationConfiguration configuration, bool verbos
// Initilialize Processors
_processors = new MTConnectAgentProcessors(configuration);
_processors.ProcessorLoaded += ProcessorLoaded;
_processors.LogReceived += ProcessorLogReceived;
_processors.Load();
_mtconnectAgent.ProcessObservationFunction = _processors.Process;

Expand Down Expand Up @@ -658,6 +660,22 @@ private void ProcessorLoaded(object sender, IMTConnectAgentProcessor processor)
_applicationLogger.Info($"[Application] : Processor Loaded : " + processor.GetType().Name);
}

private void ProcessorLogReceived(object sender, MTConnectLogLevel logLevel, string message)
{
if (!string.IsNullOrEmpty(message))
{
switch (logLevel)
{
case MTConnectLogLevel.Fatal: _processorLogger.Fatal(message); break;
case MTConnectLogLevel.Error: _processorLogger.Error(message); break;
case MTConnectLogLevel.Warning: _processorLogger.Warn(message); break;
case MTConnectLogLevel.Information: _processorLogger.Info(message); break;
case MTConnectLogLevel.Debug: _processorLogger.Debug(message); break;
case MTConnectLogLevel.Trace: _processorLogger.Trace(message); break;
}
}
}

private void DevicesRequested(string deviceName)
{
_agentLogger.Debug($"[Agent] : MTConnectDevices Requested : " + deviceName);
Expand Down
7 changes: 7 additions & 0 deletions agent/MTConnect.NET-Applications-Agents/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<target xsi:type="File" name="module-file" fileName="logs\module-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

<!--Processor Log File-->
<target xsi:type="File" name="processor-file" fileName="logs\processor-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

</targets>

<!-- rules to map from logger name to target -->
Expand All @@ -43,6 +47,9 @@
<!--Module Logger-->
<logger name="module-logger" minlevel="Info" writeTo="module-file" final="true" />

<!--Processor Logger-->
<logger name="processor-logger" minlevel="Info" writeTo="processor-file" final="true" />

<!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<logger name="System.Net.Http.*" maxlevel="Info" final="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@

using MTConnect.Assets;
using MTConnect.Input;
using MTConnect.Logging;

namespace MTConnect.Agents
{
public interface IMTConnectAgentProcessor
{
string Id { get; }

string Description { get; }


public event MTConnectLogEventHandler LogReceived;


IObservationInput Process(ProcessObservation observation);

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

using MTConnect.Assets;
using MTConnect.Input;
using MTConnect.Logging;

namespace MTConnect.Agents
{
public abstract class MTConnectAgentProcessor : IMTConnectAgentProcessor
{
public string Id { get; set; }

public string Description { get; set; }


public event MTConnectLogEventHandler LogReceived;


public IObservationInput Process(ProcessObservation observation) => OnProcess(observation);

public IAsset Process(IAsset asset) => OnProcess(asset);


protected virtual IObservationInput OnProcess(ProcessObservation observation)
{
if (observation != null)
{
var observationInput = new ObservationInput();
observationInput.DeviceKey = observation.DataItem?.Device?.Uuid;
observationInput.DataItemKey = observation.DataItem?.Id;
observationInput.Timestamp = observation.Timestamp.ToUnixTime();
observationInput.Values = observation.Values;
return observationInput;
}

return null;
}

protected virtual IAsset OnProcess(IAsset asset) => asset;


protected void Log(MTConnectLogLevel logLevel, string message)
{
if (LogReceived != null) LogReceived.Invoke(this, logLevel, message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using MTConnect.Assets;
using MTConnect.Configurations;
using MTConnect.Input;
using MTConnect.Logging;
using System;
using System.Collections.Generic;

Expand All @@ -21,6 +22,8 @@ public class MTConnectAgentProcessors

public event EventHandler<IMTConnectAgentProcessor> ProcessorLoaded;

public event MTConnectLogEventHandler LogReceived;


public MTConnectAgentProcessors(IAgentApplicationConfiguration configuration)
{
Expand Down Expand Up @@ -49,6 +52,7 @@ public void Load()
{
// Create new Instance of the Controller and add to cached dictionary
var processor = (IMTConnectAgentProcessor)Activator.CreateInstance(processorType, new object[] { processorConfiguration });
processor.LogReceived += HandleProcessorLogReceived;

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

Expand Down Expand Up @@ -144,6 +148,11 @@ private static void InitializeProcessors()
}
}

private void HandleProcessorLogReceived(object sender, MTConnectLogLevel logLevel, string message)
{
if (LogReceived != null) LogReceived.Invoke(sender, logLevel, message);
}

private static string GetConfigurationTypeId(Type type)
{
if (type != null)
Expand Down

0 comments on commit 37b131e

Please sign in to comment.