Introduction

All services are in the Robin.Engine.Services.Interfaces namespace, you can add a "using" directive for ease of use

using Robin.Engine.Services.Interfaces;

To use the service, you need to add it to the constructor of the main action class (the class that inherits BaseRobinAction). 

public class MyCustomAction : BaseRobinAction
{
    private readonly IConfigurationService _configurationService;
    public MyCustomAction(IActionLogger logger, IConfigurationService configurationService) : base(logger)
    {
        _configurationService = configurationService;
    }

	public override IDictionary<string, object> Execute(IDictionary<string, object> parameters)
	{
    	return null;
	}
}

ActionLogger

Logging service is in the parent class, available from each action by the name Logger.

There are 5 levels of logging: Debug, Info, Warn, Error and Business. Each time the logger is called, the logging threshold is checked at the script level (set in the application where the robot is launched from) and at the action level (set in the studio). Business level logs are always logged. 

There are overloads with a formatted string, such a string will get into the studio log in the same form.

There are overloads with a callback, which will be called if the message level is not below the set logging thresholds. To add a formatted string, you need to add a pair with the "message" key to the dictionary.

The log is passed from the action to the session initiator application (Studio, Player, etc.) and to the robot log file.  

Methods

void Debug(string message);

void Debug(Func<IDictionary<string, string>> parameters);

void Info(string message);

void Info(Func<IDictionary<string, string>> parameters);

void Warn(string message);

void Warn(Func<IDictionary<string, string>> parameters);

void Error(string message);

void Error(Func<IDictionary<string, string>> parameters);

void Business(Func<IDictionary<string, string>> parameters);

Examples of use

using System.Collections.Generic;
using Robin.ActionSDK;
using Robin.Engine.Services.Interfaces;

namespace Robin.SdkExamples
{
    public class MyCustomAction : BaseRobinAction
    {
        public MyCustomAction(IActionLogger logger) : base(logger)
        {
        }

        public override IDictionary<string, object> Execute(IDictionary<string, object> parameters)
        {
            var infoMessage = (string)parameters["infoMessage"];
            var debugMessage = (string)parameters["debugMessage"];

            Logger.Debug(() => new Dictionary<string, string>
            {
                {"message", debugMessage}
            });
            Logger.Info($"Получено сообщение {infoMessage}");
            
            return null;
        }
    }
}

ConfigurationService

Service for retrieving configuration parameters located in the file %LOCALAPPDATA%\robin\env.cfg

Methods

// Получение конфигурационного значения по ключу
object GetConfigValue(string key);

// Получение всех имеющихся в конфигурационном файле ключей по регулярному выражению
List<string> GetKeys(string pattern);

Examples of use

using System.Collections.Generic;
using System.Linq;
using Robin.ActionSDK;
using Robin.Engine.Services.Interfaces;

namespace Robin.SdkExamples
{
    public class GetConfigValue : BaseRobinAction
    {
        private readonly IConfigurationService _configurationService;

        public GetConfigValue(IActionLogger logger, IConfigurationService configurationService) : base(logger)
        {
            _configurationService = configurationService;
        }

        public override IDictionary<string, object> Execute(IDictionary<string, object> parameters)
        {
            var key = (string)parameters["key"];
            var pattern = (string)parameters["pattern"];

            var valueByKey = _configurationService.GetConfigValue(key);
            var allKeys = _configurationService.GetKeys(pattern);

            return new Dictionary<string, object>
            {
                { "value", valueByKey },
                { "keys", allKeys.Cast<object>().ToList() }
            };
        }
    }
}

ConverterService 

Service for obtaining a presentation value for an object (not to be confused with conversion to a string). The service returns the value of the object presented in an easy-to-read form.

Methods

string GetDisplayValue(object nativeObject);

Examples of use

using System.Collections.Generic;
using Robin.ActionSDK;
using Robin.Engine.Services.Interfaces;

namespace Robin.SdkExamples
{
    public class CheckCollection : BaseRobinAction
    {
        private readonly IConverterService _converterService;

        public CheckCollection(IActionLogger logger, IConverterService converterService) : base(logger)
        {
            _converterService = converterService;
        }

        public override IDictionary<string, object> Execute(IDictionary<string, object> parameters)
        {
            var collection = (List<object>)parameters["collection"];

            var displayValue = _converterService.GetDisplayValue(collection);

            return new Dictionary<string, object>
            {
                {"displayValue", displayValue }
            };
        }
    }
}

DisposeService

A service for releasing unmanaged resources. The Dispose method will be called when the runtime system terminates, even if the robot terminates with an error.

Methods

void RegisterResource(IDisposable disposableResource);

Examples of use

Пример IDisposableService
using System;
using System.Collections.Generic;
using Robin.ActionSDK;
using Robin.Engine.Services.Interfaces;
using Robin.Types;

namespace Robin.SdkExamples
{
    public class OpenFile : BaseRobinAction
    {
        private readonly IDisposeService _disposeService;

        public OpenFile(IActionLogger logger, IDisposeService disposeService) : base(logger)
        {
            _disposeService = disposeService;
        }

        public override IDictionary<string, object> Execute(IDictionary<string, object> parameters)
        {
            var filePath = ((RobinFilePath)parameters["filePath"]).Value;

            var file = new File(filePath);
            file.Open();

            _disposeService.RegisterResource(file);

            return new Dictionary<string, object> { { "file", file } };
        }
    }
   
    internal class File : IDisposable
    {
        private readonly string _filePath;
        private bool _isDisposed;

        public File(string filePath)
        {
            _filePath = filePath;
        }

        public void Open()  { }

        public bool TryReadNextLine(out string line) { }

        public void Close() { }

        public void Dispose()
        {
            if (_isDisposed) return;
            Close();
            _isDisposed = true;
        }
    }
}

ResourcesFolderService

Service to get the full path to the robot's resources folder, this folder contains files that were added to the robot in the Studio. 

After adding a file to the resources in the Studio and using it in the action parameters, the file name is passed to the action. To get the full path, you need to concatenate the path to the resources folder and the file name. The path to this folder is different for each launch. 

Methods

string GetResourcesFolderPath();

Examples of use

using System.Collections.Generic;
using System.IO;
using Robin.ActionSDK;
using Robin.ActionSDK.Exceptions;
using Robin.Engine.Services.Interfaces;
using Robin.Types;

namespace Robin.SdkExamples
{
    public class ReadFile : BaseRobinAction
    {
        private readonly IResourcesFolderService _resourcesFolderService;

        public ReadFile(IActionLogger logger, IResourcesFolderService resourcesFolderService) : base(logger)
        {
            _resourcesFolderService = resourcesFolderService;
        }

        public override IDictionary<string, object> Execute(IDictionary<string, object> parameters)
        {
            var fileName = ((RobinFilePath)parameters["fileName"]).Value;

            if (!TryResolveFilePath(fileName, out var filePath))
                throw new RobinException($"Файл не найден {fileName}", "Robin.Exception.FileNotFound");

            var text = File.ReadAllText(filePath);
            return new Dictionary<string, object>
            {
                {"text", text}
            };
        }

        private bool TryResolveFilePath(string fileName, out string filePath)
        {
            filePath = fileName;
            if (File.Exists(fileName))
                return true;

            filePath = Path.Combine(_resourcesFolderService.GetResourcesFolderPath(), fileName);
            return File.Exists(filePath);
        }
    }
}
  • Нет меток