Wednesday, April 11, 2018

EB GUIDE Monitor Scripts – Top Tips and Tricks – Part I

Henry Moser

Henry Moser is a lead architect for EB GUIDE. This blog post is a two part series. Part I will showcase scripting examples for EB GUIDE Monitor. Part II will demonstrate how to set up an EB GUIDE Monitor scripting environment with Visual Studio to debug EB GUIDE Monitor scripts.

With EB GUIDE 6.5, the EB GUIDE Monitor underwent some significant changes. Now based on C#, we’ve introduced a new ‘SCRIPTING’ component that allows you to easily execute a script as well as load it (refer to image below). The choice of a script editor is left to you. You can not only write scripts that you can use to test your HMI model but also connect EB GUIDE Monitor easily to Visual Studio to debug your scripts.

These scripts give you access and the ability to manipulate a running EB GUIDE target framework. The process is very simple – load a single C# file containing your test scripts and run its script methods in EB GUIDE Monitor. With these methods, you have complete control over the EB GUIDE Monitor API and consequently, control over running GTF instances. This means you can read and write the Datapool, fire events, wait for Datapool changes, wait for Event occurrences, and request a screenshot from the target framework. All changes within an opened script library file are detected automatically by the EB GUIDE Monitor. The EB GUIDE Monitor reloads and recompiles the file if something has changed.

 

EB GUIDE Monitor with the loaded scripts

As you’ll notice in the image above, the SCRIPT OUTPUT section shows you if a script was succcessfully loaded. The script is compiled just in time. Errors such as compile errors are also listed here.

Tips and Tricks – Example Templates

A valid EB GUIDE Script file contains the following components:

  • A public class
  • A using statement for class “Elektrobit.Guide.Monitor.Scripting.MonitorContext
  • A using statement for class “Threading.Tasks
  • At least one public async method with a single parameter “IMonitorContext” and return value “Task

The following examples give you an overview of the API for accessing and manipulating a running target framework. The EB GUIDE Monitor script API is based on Microsoft .NET “Task Parallel Library”. For more information on Microsoft’s .NET “Task Parallel Library”, visit https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming.

 

Basics

Template for a valid script file

namespace MyProject
{
   using System.Threading.Tasks;

   using Elektrobit.Guide.Monitor.Scripting.MonitorContext;

   public class ScriptingClass
   {
      public async Task MyFirstMethod(IMonitorContext monitor)
      {
         // add script code here
      }
   }
}

 

Template for a valid script file

public async Task PrintMessage(IMonitorContext monitor)
{
     await monitor.Write(“Hello World”);
}

 

Events

Fire an event

public async Task FireEvent(IMonitorContext monitor)
{
   await monitor.FireEvent(“nextView”);
}

 

Fire an event with parameter

public async Task FireEventWithParameter(IMonitorContext monitor)
{
   await monitor.FireEvent(“setBool”, true);
}

 

Wait for an event

public async Task WaitForEvent(IMonitorContext monitor)
{
   var ev = await monitor.WaitForEvent(“nextView”);
   await monitor.Write(“Even occured: ” + ev.EventModel.Name);
}

 

Wait for an event and evaluate the parameters

public async Task WaitForEventWithParameters(IMonitorContext monitor)
{
   var ev = await monitor.WaitForEvent(“setBool”);

   bool mv1 = ev[“value”]; // read parameter via name
   bool mv2 = ev[0]; // read the parameter via index

   await monitor.Write(“Parameter ‘value’ is: ” + mv1);
   await monitor.Write(“Parameter [0] is: ” + mv2);
}

 

Datapool

Write a datapool value

public async Task WriteDpValue(IMonitorContext monitor)
{
   await monitor.WriteDatapool(“Boolean 1”, true);
}

 

Read basic types from datapool

public async Task ReadDatapoolValue(IMonitorContext monitor)
{
   bool boolValue = await monitor.ReadDatapool(“Boolean 1”);
   string stringValue = await monitor.ReadDatapool(“String 1”);
   int integerValue = await monitor.ReadDatapool(“Integer 1”);
   float floatValue = await monitor.ReadDatapool(“Float 1”);

   await monitor.Write(“Boolean: ” + boolValue);
   await monitor.Write(“String: ” + stringValue);
   await monitor.Write(“Integer: ” + integerValue);
   await monitor.Write(“Float: ” + floatValue);
}

 

Read color value

namespace MyProject
{
   using System.Threading.Tasks;
   using System.Windows.Media; // necessary for Color type!

   using Elektrobit.Guide.Monitor.Scripting.MonitorContext;

   public class ScriptingClass
   {
      public async Task ReadColor(IMonitorContext monitor)
      {
         Color colorValue = await monitor.ReadDatapool(“Color 1”);
         await monitor.Write(“Boolean: ” + colorValue);
      }
   }
}

 

Advanced

Capture a screenshot

public async Task CaptureScreenshot(IMonitorContext monitor)
{
   // make sure remote framebuffer is enabled in profile
   var sceneId = 0;
   await monitor.CaptureScreenshot(sceneId, @”d:\image.png”);
}

 

Check for cancellation

public async Task CountTo10(IMonitorContext monitor)
{
   for (var i = 0; i < 10; i++)
   {
      await monitor.Write(“Hello World: ” + i);
      await Task.Delay(1000, monitor.CancellationToken);

      monitor.CancellationToken.ThrowIfCancellationRequested();
   }
}

 

Wait with timeout

public async Task WaitForEventWithTimeout(IMonitorContext monitor)
{
   // Disclaimer:
   // this is just one of many opportunities provided by
   // the .NET’s “Task Parallel Library”

   var eventWaitTask = monitor.WaitForEvent(“nextView”);

   await Task.WhenAny(eventWaitTask, Task.Delay(5000));

   if (!eventWaitTask.IsCompleted)
   {
      return;
   }

   await monitor.Write(“event occured”);
}

 

Start modeling now!

As always, we are constantly looking for ways to improve the tool.

Download the latest version of the tool, try it out and send us your feedback.

Also, don’t forget to visit our Resources section to download examples, review tutorials and read user documentation.

As always, get in touch with us if you have questions or feedback.