Logic Module Development

Logic modules have the following features:

  • Receive current, historical data and events at the fastest possible speed for processing.
  • Record data and events in archives.
  • Control commands coming to Server.
  • Send commands.

Let's look at the development of the logical part and user interface of a simple module, which will be named ModAbc. To develop a complex module, learn and use the source code of existing modules on GitHub as examples.

Logic Implementation

Create a new project based on the Class Library template. Enter the project name ModAbc.Logic, and select the .NET 8.0 framework.

Add dependencies to the ScadaCommon.dll, ScadaCommon.Log.dll and ScadaServerCommon.dll libraries. Binary files of the libraries can be found in the Rapid SCADA installation directory, or compiled from source code.

Double click a project node in Solution Explorer to open the project file ModAbc.Logic.csproj and edit its properties as shown below.

<PropertyGroup>
  <TargetFramework>net8.0</TargetFramework>
  <ImplicitUsings>enable</ImplicitUsings>
  <Nullable>disable</Nullable>
  <RootNamespace>Scada.Server.Modules.ModAbc.Logic</RootNamespace>
</PropertyGroup>

Create a ModAbcLogic class and copy the code below. This class implements the logic of the module. Note that the namespace and class name must contain the ModAbc module code. Explore the source code of the ModuleLogic base class and the IServerContext interface to learn about the capabilities available when implementing module logic.

using Scada.Data.Models;

namespace Scada.Server.Modules.ModAbc.Logic
{
    public class ModAbcLogic : ModuleLogic
    {
        private const int InputChannel = 105;
        private const int OutputChannel = 104;
        private const int UserID = 11;
        private const double Threshold = 10.0;

        private bool high = false;
        private bool low = false;

        public ModAbcLogic(IServerContext serverContext)
            : base(serverContext)
        {
        }

        public override string Code => "ModAbc";

        public override void OnServiceStart()
        {
            Log.WriteAction("Модуль ModAbc запущен");
        }

        public override void OnServiceStop()
        {
            Log.WriteAction("Модуль ModAbc остановлен");
        }

        public override void OnIteration()
        {
            CnlData curData = ServerContext.GetCurrentData(InputChannel);

            if (curData.IsDefined)
            {
                if (curData.Val >= Threshold)
                {
                    if (!high)
                    {
                        ServerContext.SendCommand(new TeleCommand(OutputChannel, 1, UserID));
                        high = true;
                    }
                }
                else
                {
                    high = false;
                }

                if (curData.Val < Threshold)
                {
                    if (!low)
                    {
                        ServerContext.SendCommand(new TeleCommand(OutputChannel, 0, UserID));
                        low = true;
                    }
                }
                else
                {
                    low = false;
                }
            }
        }
    }
}

Build the project and copy ModAbc.Logic.dll to the Server modules directory ScadaServer\Mod

Interface Implementation

Create a new project based on the Windows Forms Class Library template. Enter the project name ModAbc.View, and select the .NET 8.0 framework.

Add dependencies to the ScadaCommon.dll, ScadaCommon.Forms.dll, ScadaCommon.Log.dll and ScadaServerCommon.dll libraries.

Double click a project node in Solution Explorer to open the project file ModAbc.View.csproj and edit its properties as shown below.

<PropertyGroup>
  <TargetFramework>net8.0-windows</TargetFramework>
  <Nullable>disable</Nullable>
  <UseWindowsForms>true</UseWindowsForms>
  <ImplicitUsings>enable</ImplicitUsings>
  <RootNamespace>Scada.Server.Modules.ModAbc.View</RootNamespace>
</PropertyGroup>

Create a ModAbcView class and copy the code below. This class implements the user interface of the module. Note that the namespace and class name must contain the ModAbc module code. In the example, there is actually no user interface for the module, however, a minimal implementation of the interface is required so that the module can be used in the Administrator application. View the source code of the ModuleView base class to learn about the available features.

namespace Scada.Server.Modules.ModAbc.View
{
    public class ModAbcView : ModuleView
    {
        public override string Name => "Модуль ABC";
        public override string Descr => "Простой пример модуля";
    }
}

Build the project and copy ModAbc.View.dll to the Administrator libraries directory ScadaAdmin\Lib

Run Module

Start the Administrator application or restart it if it is open. Create and open a copy of the HelloWorld project, then find the developed module in the Server > Modules section. Select the module and make sure its description is displayed correctly. If an error occurs when displaying a module description, there is most likely an inaccuracy in the namespace or class names of the module user interface.

Activate the ModAbc module and upload the project for execution. Information about the start and stop of the module should be displayed in the Server log. When the value of channel 105 passes through the threshold 10 specified by the constant, commands 0 or 1 are sent to channel 104.