This shows you the differences between two versions of the page.
— |
automationserver:api [2019/05/20 12:59] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== API ====== | ||
+ | Learn how to create your own AutomationServer plugins. | ||
+ | |||
+ | ===== Requirements ===== | ||
+ | * .NET Framework 4 | ||
+ | |||
+ | ===== Step-by-Step Example===== | ||
+ | ==== New VisualStudio Project ==== | ||
+ | Create a new VisualStudio class library project and add the " | ||
+ | |||
+ | {{ : | ||
+ | \\ | ||
+ | |||
+ | ==== Create a Job Definition Class ==== | ||
+ | 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: | ||
+ | <code csharp> | ||
+ | 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 " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public string JobDefinitionId | ||
+ | { | ||
+ | get | ||
+ | { | ||
+ | return " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public List< | ||
+ | |||
+ | public void Init(IPropertyFactory propertyFactory) | ||
+ | { | ||
+ | _propertyFactory = propertyFactory; | ||
+ | |||
+ | InitProperties(); | ||
+ | } | ||
+ | |||
+ | private void InitProperties() | ||
+ | { | ||
+ | IPropertyNumeric numericProperty; | ||
+ | |||
+ | numericProperty = _propertyFactory.CreateNumeric(" | ||
+ | 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 " | ||
+ | You can provide as much properties as you need. There a multiple datatypes available. The example shows the creation of simple numeric value. \\ | ||
+ | |||
+ | ==== Create a Job Handler Class ==== | ||
+ | The " | ||
+ | |||
+ | Here is an example: | ||
+ | |||
+ | <code csharp> | ||
+ | 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, | ||
+ | } | ||
+ | |||
+ | public bool Execute(IJob job, CancellationToken cancellationToken, | ||
+ | { | ||
+ | bool success = false; | ||
+ | string executionResult = ""; | ||
+ | |||
+ | try | ||
+ | { | ||
+ | IPropertyValue executionTimeValue = job.PropertyValues.Where(v => v.PropertyName == " | ||
+ | |||
+ | 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 = " | ||
+ | else | ||
+ | { success = true; executionResult = " | ||
+ | } | ||
+ | } | ||
+ | catch (Exception ex) | ||
+ | { | ||
+ | executionResult = ex.Message; | ||
+ | } | ||
+ | |||
+ | result = executionResult; | ||
+ | return success; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | A job handler class is recoginzed automatically by the " | ||
+ | |||
+ | ==== Deployment ==== | ||
+ | To deploy your plugin just copy the assembly in the " | ||
+ | |||
+ | <note tip>The job queue is saved in a xml file called " | ||