Skip to content

Commit

Permalink
Added Python Processor
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Nov 8, 2023
1 parent af634c5 commit d9606f6
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class MTConnectAgentApplication : IMTConnectAgentApplication

protected readonly Logger _applicationLogger = LogManager.GetLogger("application-logger");
protected readonly Logger _agentLogger = LogManager.GetLogger("agent-logger");
protected readonly Logger _agentMetricLogger = LogManager.GetLogger("agent--metric-logger");
protected readonly Logger _agentMetricLogger = LogManager.GetLogger("agent-metric-logger");
protected readonly Logger _agentValidationLogger = LogManager.GetLogger("agent-validation-logger");

private readonly List<DeviceConfigurationFileWatcher> _deviceConfigurationWatchers = new List<DeviceConfigurationFileWatcher>();
Expand Down Expand Up @@ -390,6 +390,11 @@ public void StartAgent(IAgentApplicationConfiguration configuration, bool verbos
_agentLogger.Warn($"No Devices Found : Reading from : {configuration.Devices}");
}

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

OnStartAgentBeforeLoad(devices, initializeDataItems);
_modules.StartBeforeLoad();

Expand All @@ -414,11 +419,6 @@ public void StartAgent(IAgentApplicationConfiguration configuration, bool verbos
FileIndex.ToFile(FileIndex.DataItemsFileName, FileIndex.Create(_mtconnectAgent.DataItemIndexes));
}

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

// Start Agent
_mtconnectAgent.Start();

Expand Down Expand Up @@ -459,6 +459,7 @@ public void StopAgent()
OnStopAgent();

if (_modules != null) _modules.Stop();
if (_processors != null) _processors.Dispose();

_started = false;
}
Expand Down
68 changes: 46 additions & 22 deletions src/MTConnect.NET-Common/Agents/MTConnectAgentProcessors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Assets;
using MTConnect.Configurations;
using MTConnect.Observations.Input;
using System;
using System.Collections.Concurrent;
Expand All @@ -14,6 +15,14 @@ public class MTConnectAgentProcessors
private static readonly ConcurrentDictionary<string, IMTConnectAgentProcessor> _processors = new ConcurrentDictionary<string, IMTConnectAgentProcessor>();


private readonly IAgentApplicationConfiguration _configuration;


public MTConnectAgentProcessors(IAgentApplicationConfiguration configuration)
{
_configuration = configuration;
}

public void Load()
{
InitializeProcessors();
Expand All @@ -23,16 +32,27 @@ public void Load()
{
foreach (var processorType in processorTypes)
{
try
var configurationTypeId = GetConfigurationTypeId(processorType);
if (configurationTypeId != null)
{
// Create new Instance of the Controller and add to cached dictionary
var processor = (IMTConnectAgentProcessor)Activator.CreateInstance(processorType, new object[] { });
var processorConfigurations = _configuration.GetProcessors(configurationTypeId);
if (!processorConfigurations.IsNullOrEmpty())
{
foreach (var processorConfiguration in processorConfigurations)
{
try
{
// Create new Instance of the Controller and add to cached dictionary
var processor = (IMTConnectAgentProcessor)Activator.CreateInstance(processorType, new object[] { processorConfiguration });

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

_processors.TryAdd(processorId, processor);
_processors.TryAdd(processorId, processor);
}
catch { }
}
}
}
catch { }
}
}
}
Expand All @@ -50,13 +70,6 @@ public IObservationInput Process(ProcessObservation observation)
}
}


//outputObservation = new ObservationInput();
//outputObservation.DeviceKey = observation.DataItem.Device.Uuid;
//outputObservation.DataItemKey = observation.DataItem.Id;
//outputObservation.Values = observation.Values;
//outputObservation.Timestamp = observation.Timestamp.ToUnixTime();

return outputObservation;
}

Expand All @@ -77,15 +90,10 @@ public IAsset Process(IAsset asset)

public void Dispose()
{
//if (!_processors.IsNullOrEmpty())
//{
// foreach (var processor in _processors)
// {
// processor.Value.Stop();
// }

// _processors.Clear();
//}
if (!_processors.IsNullOrEmpty())
{
_processors.Clear();
}
}


Expand Down Expand Up @@ -116,5 +124,21 @@ private static void InitializeProcessors()
}
}
}

private static string GetConfigurationTypeId(Type type)
{
if (type != null)
{
try
{
var configurationTypeIdField = type.GetField("ConfigurationTypeId");

return configurationTypeIdField.GetValue(null)?.ToString();
}
catch { }
}

return null;
}
}
}
Loading

0 comments on commit d9606f6

Please sign in to comment.