User Tools

Site Tools


automationserver:api

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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 "Reitec.AutomationServer.Interfaces" as reference.
 +
 +{{ :automationserver:api:vs_addreference.png?200 |}} 
 +\\
 +
 +==== 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 "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;
 +        }
 +    }
 +}
 +
 +</code>
 +
 +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. \\
 +
 +==== Create a Job Handler Class ====
 +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:
 +
 +<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, "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;
 +        }
 +    }
 +}
 +
 +</code>
 +
 +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". \\
 +
 +==== Deployment ====
 +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.
 +
 +<note tip>The job queue is saved in a xml file called "jobqueue.database" in the program data folder (%programdata%) </note>
  
automationserver/api.txt ยท Last modified: 2019/05/20 12:59 (external edit)