From 37b131eb2dc58f10cdd0ef3a38614f8a5b242514 Mon Sep 17 00:00:00 2001 From: Patrick Ritchie Date: Tue, 5 Dec 2023 00:31:04 -0500 Subject: [PATCH] Added MTConnectAgentProcessor base class and added Log() method for processors --- .../MTConnectAgentApplication.cs | 18 +++++++ .../NLog.config | 7 +++ .../Agents/IMTConnectAgentProcessor.cs | 9 ++++ .../Agents/MTConnectAgentProcessor.cs | 48 +++++++++++++++++++ .../Agents/MTConnectAgentProcessors.cs | 9 ++++ 5 files changed, 91 insertions(+) create mode 100644 libraries/MTConnect.NET-Common/Agents/MTConnectAgentProcessor.cs diff --git a/agent/MTConnect.NET-Applications-Agents/MTConnectAgentApplication.cs b/agent/MTConnect.NET-Applications-Agents/MTConnectAgentApplication.cs index 0b6abcd3..f15f8ff9 100644 --- a/agent/MTConnect.NET-Applications-Agents/MTConnectAgentApplication.cs +++ b/agent/MTConnect.NET-Applications-Agents/MTConnectAgentApplication.cs @@ -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 _deviceConfigurationWatchers = new List(); @@ -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; @@ -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); diff --git a/agent/MTConnect.NET-Applications-Agents/NLog.config b/agent/MTConnect.NET-Applications-Agents/NLog.config index 84a8abad..f2825069 100644 --- a/agent/MTConnect.NET-Applications-Agents/NLog.config +++ b/agent/MTConnect.NET-Applications-Agents/NLog.config @@ -23,6 +23,10 @@ + + + @@ -43,6 +47,9 @@ + + + diff --git a/libraries/MTConnect.NET-Common/Agents/IMTConnectAgentProcessor.cs b/libraries/MTConnect.NET-Common/Agents/IMTConnectAgentProcessor.cs index d578dac7..63c2f7f4 100644 --- a/libraries/MTConnect.NET-Common/Agents/IMTConnectAgentProcessor.cs +++ b/libraries/MTConnect.NET-Common/Agents/IMTConnectAgentProcessor.cs @@ -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); diff --git a/libraries/MTConnect.NET-Common/Agents/MTConnectAgentProcessor.cs b/libraries/MTConnect.NET-Common/Agents/MTConnectAgentProcessor.cs new file mode 100644 index 00000000..f0ad30b1 --- /dev/null +++ b/libraries/MTConnect.NET-Common/Agents/MTConnectAgentProcessor.cs @@ -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); + } + } +} diff --git a/libraries/MTConnect.NET-Common/Agents/MTConnectAgentProcessors.cs b/libraries/MTConnect.NET-Common/Agents/MTConnectAgentProcessors.cs index ebe974f3..bc4226a6 100644 --- a/libraries/MTConnect.NET-Common/Agents/MTConnectAgentProcessors.cs +++ b/libraries/MTConnect.NET-Common/Agents/MTConnectAgentProcessors.cs @@ -4,6 +4,7 @@ using MTConnect.Assets; using MTConnect.Configurations; using MTConnect.Input; +using MTConnect.Logging; using System; using System.Collections.Generic; @@ -21,6 +22,8 @@ public class MTConnectAgentProcessors public event EventHandler ProcessorLoaded; + public event MTConnectLogEventHandler LogReceived; + public MTConnectAgentProcessors(IAgentApplicationConfiguration configuration) { @@ -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(); @@ -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)