Table of Contents

Create a Module

Creating a module is easy.

Choose a Base Type

There are some useful classes for working with LisRT modules.

Name Interface Class type Description
LisrtModule ILisrtModule Abstract base Inherit from this class to create a new module. It contains virtual and abstract methods and properties. For example the TreeItem or Icon can be overridden.
CompositeModule ICompositeModule Base class If you need submodules, then inherit from this class. Pass submodules to the constructor or add them to the SubModules property.
InitializableCompositeModule ICompositeModule, IInitializable Abstract base Inherits from the CompositeModule and has an implementation for the exit method. You can use the InitializeSubModules() method to initialize the submodules (just submodules, not recursive).
ModuleCollection ILisrtModuleCollection Collection A collection for ILisrtModule implementations. Used for the SubModules.
ModuleExtensions Extensions Contains extensions methods for ILisrtModule implementations. Mainly for recursive navigation through the module tree.

Interfaces

There are some interfaces which can be used for a module to have default effects.

ILisrtModule

This is the base interface for a module. Important properties are:

  • Enabled: If false, shown as inactive in the module tree and functions will have no effect.
  • Icon: A representative icon for the module tree

IHasDeviceLabel

This is a interface for devices which can have a device label (e.g. electrical or hydraulic components). Important properties are:

  • DeviceLabel: Additional property for log files and shown on the module tree item
  • InstanceID: Additional property for log files

IInitializable

This is an interface for modules that have to be initialized, for example, if a communication channel must be opened.

The initialization state is shown in the module tree:

  • NotInitialized: Cancel
  • IsInitializing: Clock
  • Initialized: Checkmark green
  • Failed: Block Error
Tip

Initialization should normally start from top to bottom and from the inner to outer module. But you have the full control and can arrange an initialization order in your root station module.

Initialize Modules Recursively

Tip

Deinitialize a tree with the ExitRecursively(ILisrtModule, bool) extension method.

IHasServiceView

Implement this interface if your module has a service view.

Example

This is an example implementation of a module using these features:

This is the standard implementation of a LisRT module:

public class MyDeviceModule : LisrtModule, IInitializable
{
    private InitializationState _initializationState;
    private int _myConfiguration;
    private string _myState;

    public MyDeviceModule(string name)
        : base(name)
    {
    }

    // This is a configuration property that is loaded from a configuration file.
    [Configuration(0)]
    public int MyConfiguration
    {
        get { return _myConfiguration; }
        set
        {
            if (_myConfiguration != value)
            {
                _myConfiguration = value;
                NotifyOfPropertyChange();
            }
        }
    }

    // This is a property that holds the state of the module
    public string MyState
    {
        get { return _myState; }
        private set
        {
            if (_myState != value)
            {
                _myState = value;
                NotifyOfPropertyChange();
            }
        }
    }

    // Function of IInitializable interface
    public InitializationState InitializationState
    {
        get { return _initializationState; }
        private set
        {
            if (_initializationState != value)
            {
                _initializationState = value;
                NotifyOfPropertyChange();
            }
        }
    }

    // Function of IInitializable interface
    public void Initialize()
    {
        try
        {
            if (Enabled)
            {
                // Execute here your initialization code.
            }

            InitializationState = InitializationState.Initialized;
        }
        catch (Exception ex)
        {
            ex.AddModuleInformation(this);
            InitializationState = InitializationState.Failed;
            throw;
        }
    }

    // Function of IInitializable interface
    public void Exit()
    {
        try
        {
            if (Enabled)
            {
                // Execute here your exit code.
            }
        }
        catch (Exception ex)
        {
            ex.AddModuleInformation(this);
            throw;
        }
        finally
        {
            InitializationState = InitializationState.Failed;
        }
    }

    // This is a method of the module
    public void Do()
    {
        try
        {
            ThrowExceptionIfNotInitialized();
            if (Enabled)
            {
                // Do stuff the method has to do
            }
        }
        catch (Exception ex)
        {
            ex.AddModuleInformation(this);
            throw;
        }
    }

    private void ThrowExceptionIfNotInitialized()
    {
        if (InitializationState != InitializationState.Initialized)
            throw new NotInitializedException(this);
    }
}