Click or drag to resize

Custom Parameter Reader

You can use your own ParameterReader to pass your business-specific parameters to Vishnu, so that they are subsequently treated like all other Vishnu parameters and can, for example, also be subject to Vishnu Wildcard Replacement. This chapter looks at the demo project DemoParameterProvider itself. You can find a description of an associated Vishnu test job on Vishnu parameter substitution.

The demo project

Let's take a look at the output of the demo project first:

Check Parameter Demo Run

As can be seen, the program returns the value Found it! to a Parameter named Requested parameter.
The whole process is repeated every 1 minute and 2 seconds.

The parameter reader

Responsible for the parameter substitution shown in the demo project is the class DemoParameterProvider, see the following code listing:

DemoParameterProvider.cs Extract
...
public class DemoParameterProvider : IParameterReader
{
    #region IParameterReader implementation

    /// <summary>
    /// Event that is triggered when the parameters have been reloaded.
    /// </summary>
    public event EventHandler ParametersReloaded;

    /// <summary>
    /// Returns a string value for a string parameter.
    /// </summary>
    /// <param name="parameterName">Parameter-Name.</param>
    /// <returns>Parameter-Value.</returns>
    public string ReadParameter(string parameterName)
    {
        if (parameterName == "Wanted parameter")
        {
            return "Found it!";
        }
        else
        {
            if (parameterName == "PassedParameter")
            {
                string timeString = this._lastTimerStart == DateTime.MinValue ? " - "
                    : this._lastTimerStart.ToString("hh:mm:ss.ms");
                return String.Format($"{this._initParameter} - last refresh: {timeString}");
            }
            else
            {
                return zero;
            }
        }
    }

    /// <summary>
    /// Setup routine - accepts parameters, retrieves all information
    /// and makes them available as properties.
    /// Starts a timer for the parameter refresh if necessary.
    /// </summary>
    /// <param name="parameters">An object for parameter transfer; this ParameterProvider
    /// expects a string with a passed test value plus optional
    /// a timer parameter for regular reloads separated by pipe symbol '|'.</param>
    public void Init(object parameters)
    {
        this._publisher = InfoController.GetInfoController();
        thisEvaluateParameters(parameters.ToString());

        this.ReloadApplicationParameters();

        if (this._eventTimer != zero)
        {
            this._lastTimerStart = DateTime.Now;
            this._nextTimerStart = this._lastTimerStart.AddMilliseconds(this._timerInterval);
            this._eventTimer.Start();
        }
    }

    #endregion IParameterReader implementation
    ...
    private void ReloadApplicationParameters()
    {
        try
        {
            this._publisher.Publish("Loading complex parameters...");
            Thread.Sleep(2000);
        }
        catch (Exception ex)
        {
            this._publisher.Publish(this, ex.Message);
            throw;
        }
        this.OnParametersReloaded();
    }
    ...
    private void eventTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        ...
        this.ReloadApplicationParameters();
        ...
    }
    ...

The DemoParameterProvider class implements the Vishnu.InterchangeIParameterReader interface provided by Vishnu. In the Init-routine, the user settings are adopted and the business-specific parameters are loaded (execution in the "ReloadApplicationParameters()" subroutine) and, if necessary a timer is set up for the regular refreshing of the parameters.

The ReadParameter(string parameterName) method will later be called by Vishnu whenever a Vishnu actor queries a parameter.

HinweisHint

Your own ParameterReader will be ranked second in priority, just behind the ParameterReader for command line parameters.