Skip to content

Commit

Permalink
Updated Python Processor module
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Dec 5, 2023
1 parent 37b131e commit 739287d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
6 changes: 0 additions & 6 deletions MTConnect.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docker", "Docker", "{400652E9-7061-4D72-B2CF-F70280341B79}"
ProjectSection(SolutionItems) = preProject
docker\Dockerfile = docker\Dockerfile
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MTConnect.NET-AgentModule-MqttBroker", "agent\Modules\MTConnect.NET-MqttBroker-Module\MTConnect.NET-AgentModule-MqttBroker.csproj", "{8687FC7A-07E0-4377-9460-7C9261CCB656}"
EndProject
Global
Expand Down Expand Up @@ -422,7 +417,6 @@ Global
{04171268-E71D-45F5-9B14-C325AB522BD1} = {C3791CDD-9446-4584-AA80-0C41FCB63B88}
{3B6E6874-ADB2-4C59-9E65-C13693466920} = {4558F2F3-D5B7-49DE-AC9C-8E778C4D3585}
{619557F6-A8D5-4995-9D96-0E3174221FC6} = {4558F2F3-D5B7-49DE-AC9C-8E778C4D3585}
{400652E9-7061-4D72-B2CF-F70280341B79} = {0712EA0C-EC0D-42EE-9CDD-148893A018F8}
{8687FC7A-07E0-4377-9460-7C9261CCB656} = {0F01D2F5-0CFA-4DAE-A0FD-5733D291F6FB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
// TrakHound Inc. licenses this file to you under the MIT license.

using MTConnect.Agents;
using MTConnect.Assets;
using MTConnect.Configurations;
using MTConnect.Input;
using NLog;
using System;
using System.Collections.Generic;
using System.IO;

namespace MTConnect.Processors
{
public class MTConnectPythonProcessor : IMTConnectAgentProcessor
public class Processor : MTConnectAgentProcessor
{
public const string ConfigurationTypeId = "python";
private const int DefaultUpdateInterval = 2000;
Expand All @@ -21,22 +19,21 @@ public class MTConnectPythonProcessor : IMTConnectAgentProcessor
private const string _defaultDirectory = "processors";
private const string _defaultExtension = ".py";

private readonly Logger _logger = LogManager.GetLogger("python-processor-logger");
private readonly Microsoft.Scripting.Hosting.ScriptEngine _pythonEngine;
private readonly Dictionary<string, Func<ProcessObservation, ProcessObservation>> _functions = new Dictionary<string, Func<ProcessObservation, ProcessObservation>>();
private readonly PythonProcessorConfiguration _configuration;
private readonly ProcessorConfiguration _configuration;
private readonly object _lock = new object();

private FileSystemWatcher _watcher;
private System.Timers.Timer _watcherTimer;
private bool _update = false;


public MTConnectPythonProcessor(object configuration)
public Processor(object configuration)
{
_pythonEngine = IronPython.Hosting.Python.CreateEngine();
_configuration = AgentApplicationConfiguration.GetConfiguration<PythonProcessorConfiguration>(configuration);
if (_configuration == null) _configuration = new PythonProcessorConfiguration();
_configuration = AgentApplicationConfiguration.GetConfiguration<ProcessorConfiguration>(configuration);
if (_configuration == null) _configuration = new ProcessorConfiguration();

Load();

Expand Down Expand Up @@ -75,19 +72,19 @@ private void LoadEngine(string file)
var process = scope.GetVariable<Func<ProcessObservation, ProcessObservation>>(_functionName);
if (process != null)
{
_logger.Info($"[Python-Processor] : Script Loaded : {file}");
Log(Logging.MTConnectLogLevel.Information, $"[Python-Processor] : Script Loaded : {file}");

AddFunction(file, process);
}
}
catch (Exception ex)
{
_logger.Error($"[Python-Processor] : Error Loading Script : {file} : {ex.Message}");
Log(Logging.MTConnectLogLevel.Error, $"[Python-Processor] : Error Loading Script : {file} : {ex.Message}");
}
}


public IObservationInput Process(ProcessObservation observation)
protected override IObservationInput OnProcess(ProcessObservation observation)
{
ProcessObservation outputObservation = observation;

Expand All @@ -111,7 +108,7 @@ public IObservationInput Process(ProcessObservation observation)
}
catch (Exception ex)
{
_logger.Error($"[Python-Processor] : Process Error : {functionKey} : {ex.Message}");
Log(Logging.MTConnectLogLevel.Error, $"[Python-Processor] : Process Error : {functionKey} : {ex.Message}");
}
}
}
Expand All @@ -121,7 +118,7 @@ public IObservationInput Process(ProcessObservation observation)
}
catch (Exception ex)
{
_logger.Error($"[Python-Processor] : Error During Process : {ex.Message}");
Log(Logging.MTConnectLogLevel.Error, $"[Python-Processor] : Error During Process : {ex.Message}");
}

if (outputObservation != null)
Expand All @@ -139,11 +136,6 @@ public IObservationInput Process(ProcessObservation observation)
}
}

public IAsset Process(IAsset asset)
{
return asset;
}


private void AddFunction(string filePath, Func<ProcessObservation, ProcessObservation> function)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace MTConnect.Processors
{
public class PythonProcessorConfiguration
public class ProcessorConfiguration
{
[JsonPropertyName("directory")]
public string Directory { get; set; }
Expand Down

0 comments on commit 739287d

Please sign in to comment.