Learn how to create your own AutomationServer plugins.
Create a new VisualStudio class library project and add the “Reitec.AutomationServer.Interfaces” as reference.
Every plugin must have at least two classes a job definition class and a job handler class. The job definition class is some kind of description of the job. You can define the job type name and the job properties the user can change for each job.
Here is an example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Reitec.AutomationServer.Interfaces; namespace Reitec.AutomationServer.TestJob { [JobDefinition] public class TestJobDefinition : IJobDefinition { private IPropertyFactory _propertyFactory; public TestJobDefinition() { } public string DisplayName { get { return "AutomationServer Test Job"; } } public string JobDefinitionId { get { return "TESTJOB"; } } public List<IProperty> Properties { get; private set; } = new List<IProperty>(); public void Init(IPropertyFactory propertyFactory) { _propertyFactory = propertyFactory; InitProperties(); } private void InitProperties() { IPropertyNumeric numericProperty; numericProperty = _propertyFactory.CreateNumeric("ExecutionTime", "Execution Time", 30); numericProperty.MinValue = 1; numericProperty.MaxValue = 86400; numericProperty.DecimalPlaces = 0; this.Properties.Add(numericProperty); } public override string ToString() { return this.DisplayName; } } }
A job definition class is recoginzed automatically by the “JobDefinition” class attribute. After the class is instantiated the AutomationServer calls the “Init” function and provides a reference to the property factory.
You can provide as much properties as you need. There a multiple datatypes available. The example shows the creation of simple numeric value.
The “Execute” function of job handler class will be called when the job is executed. A job object and a cancellationToken are provided as parameters.
Here is an example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Reitec.AutomationServer.Interfaces; using System.Threading; namespace Reitec.AutomationServer.TestJob { [JobHandler] public class TestJobHandler : IJobHandler { public bool CanExectue(string jobType) { return string.Equals(jobType, "TESTJOB", StringComparison.OrdinalIgnoreCase); } public bool Execute(IJob job, CancellationToken cancellationToken, out string result) { bool success = false; string executionResult = ""; try { IPropertyValue executionTimeValue = job.PropertyValues.Where(v => v.PropertyName == "ExecutionTime").FirstOrDefault(); if (executionTimeValue != null) { double executionTime = (double)executionTimeValue.Value; int count = 0; do { Thread.Sleep(1000); count += 1; } while ((count < executionTime) & !cancellationToken.IsCancellationRequested); if (cancellationToken.IsCancellationRequested) { executionResult = "Cancled."; } else { success = true; executionResult = "OK"; } } } catch (Exception ex) { executionResult = ex.Message; } result = executionResult; return success; } } }
A job handler class is recoginzed automatically by the “JobHandler” class attribute. The property values for this job are stored in the “PropertyValues” property of the job object. Any messages can be return to the AutomationServer over the “result” parameter. When the job was successfully executed return “true” otherwise “false”.
To deploy your plugin just copy the assembly in the “Reitec.AutomationServer” program folder and run the AutomationServer. You should now be able to create new jobs.