Script Ver2 event usage example

<< Click to Display Table of Contents >>

Manual > Monitoring system construction guide > Building server logic > Executing your own logic in the background (SC2) >

Script Ver2 event usage example

overview

Here we will introduce examples of how to use Script Ver2 events using samples.

 

Action used: Script Ver2 Action

 

 

 

Download the sample

The creation examples introduced on this page will be explained based on samples.

 

The sample can be downloaded from below.

 

Server configuration file: script2_sample01.txt

Script file: script.txt

 

In this sample, you can check the behavior of the following events.

 

OnInitialize / OnTerminate / OnTimer / OnTagValueChanged / OnHeartBeat / OnExpressionValueChanged

 

 

hint

For details about each event and method used in the sample, please refer to the Control Reference.

 

 

Operation check

Load the sample server configuration file from FA-Server and bring the application online (yellow arrow).

After going online, the script's operation results will be output as a log to Output01.

 

c_action_0085

 

 

Commentary

Below is an explanation of the processing performed by the script written in the sample script file "script.txt".

 

1)OnInitialize Event

 
The OnInitialize event is called only once immediately after online execution. It is used when writing script initialization processing, etc.

 

Example script

event OnInitialize()
{
	::SvsDump("OnInitialize:" & ::SvsGetActionName());
	::AddTag(c("U01.F01.T01", "U01.F01.T02", "U01.F01.T03"));
	var vExpressionName = c("EX01","EX02");
	var vExpression = c("(U01.F01.T04 && U01.F01.T05) ||
                              U01.F01.T06" ,"U01.F01.T04 || U01.F01.T05 || U01.F01.T06");
	::AddExpression(vExpressionName, vExpression);
	::SetTimer(0, 5000, -1);
}

 

Processing content

i.The SvsDump method outputs a message to the Output log. Here, it outputs the message that the OnInitialize event was executed.

ii.Use the AddTag method to register the tags you want to use.

iii.Register an expression using the AddExpression method. When the value of an expression registered with the AddExpression method changes, the OnExpressionValueChanged event occurs.

iv.SetTimer sets the timer. In this example, a timer of 5000 milliseconds (5 seconds) is set with an infinite number of executions (-1), so the OnTimer event will occur repeatedly every 5 seconds.

 

 

hint

The SvsDump method allows you to output arbitrary messages to the Output log. This method is very useful for debugging SC2.

 

 

2)OnTerminate event

 
The OnTerminate event is executed when offline. Use it when you want to perform some processing on termination.

 

Example script

event OnTerminate()
{
	::SvsDump("OnTerminate:" & ::SvsGetActionName());
}

 

Processing content

The SvsDump method logs that the OnTerminate event occurred.

 

 

3)OnTimer Event

 
OnTimer event occurs when the timer set by the SetTimer method expires. In this example, the 5-second periodic event set by the SetTimer method occurs within the OnInitialize event.

 

Example script

event OnTimer(timerid,counter)
{
	::SvsDump("OnTimer Id:" & ::CStr(timerid) & " counter:" & ::CStr(counter));
}

 

Processing content

The SvsDump method logs that the OnTimer event has occurred.

 

 

4)OnTagValueChanged event

 
The OnTagValueChanged event occurs when the value of a tag registered with AddTag changes. Use this when you want to detect and process changes in the tag value.

 

In this sample, the three tags "U01.F01.T01", "U01.F01.T02", and "U01.F01.T03" are registered with AddTag within the OnInitialize event, so whenever the value of any of these tags changes, the OnTagValueChanged event will occur.

 

Example script

event OnTagValueChanged(tagname,value)
{
	::SvsDump("OnTagValueChanged(Tag:" & tagname & " Value:" & ::CStr(value) & ")");

	if(value){
		if(tagname == "U01.F01.T01"){
			::WriteVal("U01.F01.T03", 1);
		}else if(tagname == "U01.F01.T02"){
			::WriteVal("U01.F01.T03", 0);
		}
	}                                                 
}

 

Processing content

i.The SvsDump method logs that the OnTagValueChanged event occurred.

ii.If the tag whose value has changed is "U01.F01.T01", write 1 to "U01.F01.T03". Also, if the tag whose value has changed is "U01.F01.T02", write 0 to "U01.F01.T03".

 

 

5)OnHeartBeat Event

 
OnHeartBeat event occurs every time a script Ver.2 action is called. It is used in combination with Scheduled event etc. when you want to process at regular intervals. In the sample, the action is executed at 1-second intervals, so the OnHeartBeat event is generated every second.

 

Example script

event OnHeartBeat()
{
	SvsDump("OnHeartBeat:" + SvsGetActionName());
}

 

Processing content

The SvsDump method logs that the OnHeartBeat event has occurred.
 

 

hint

If you want to implement a process in a script that is executed periodically, you can use the OnHeartBeat event or the OnTimer event with the SetTimer method. We recommend using the OnTimer event with the SetTimer method to implement a periodic process unless there is a special reason. The OnHeartBeat event is always called every time an action is executed by an event, so the execution period depends on the timing of calling this action from the FA-Server event. On the other hand, using SetTimer has the advantage that multiple timers with different periods can be set at the same time. The idea is to use the OnTimer event to implement a periodic process, and use the OnHeartBeat event if you want to execute a script at a time specified by FA-Server's Scheduled event, etc., so choose according to your purpose.

 

 

6)OnExpressionValueChanged event

 
The OnExpressionValueChanged event occurs when the value of an expression registered using AddExpression changes.

 

In this sample, two expressions are registered within the OnInitialize event: "(U01.F01.T04 && U01.F01.T05) || U01.F01.T06" and "U01.F01.T04 || U01.F01.T05 || U01.F01.T06". When the values of these expressions change, an event will occur.

 

Example script

event OnExpressionValueChanged(name,value,firstevent)
{
	::SvsDump("OnExpressionValueChanged(Name:" & ::CStr(name) & 
             " Value:" & ::CStr(value) & " First:" & ::CStr(firstevent) & ")");
}

 

Processing content

The SvsDump method logs what happened when OnExpressionValueChanged occurred.

 

 

 

Setup Procedure

Tag configuration

 

1.Please add tags to be used in script Ver2.
 
As an example, let's say that a virtual device unit "U01" has been added and the following tags have been registered.

 

U01.F01.T01

U01.F01.T02

U01.F01.T03

U01.F01.T04

U01.F01.T05

U01.F01.T06

 

 


Action Settings

 

1.Right-click Action to display the menu and select "Add" - "Action".
 
c_action_0081

 

2.Add script Ver2 action.
 
c_action_0082
 
As an example, let's assume that an action named "A01" has been added.

 

3.Open the properties of the action you added and select the script file.
 
c_action_0083
 
In a good practice, you would write the process description of the script to be executed in the script file beforehand.
 
As an example, select "script.txt" included in the sample.
 

 


Event Settings

 

1.Register the action you added to the event.
 

c_action_0084

 

 

As an example, select Periodic event and add Periodic event with a 1 second period named "E01".

 

c_action_0354

 

In the execution action of Periodic event"E01", register the call of the Script Ver2 action "A01" that you just created. No parameters need to be specified.

 

c_action_0355

 

With this setting, the script Ver2 action "A01" will be executed at one second intervals.