Get historical data from script Ver2

<< Click to Display Table of Contents >>

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

Get historical data from script Ver2

overview

By using Historical data control, you can connect from script Ver2 to each action with server functionality, such as summary action, logger action, and Historical data server action, and obtain historical data (time series data).

 

Action used: Script Ver2 Action

 

 

 

Download the sample

The creation examples shown on this page are provided with samples.

 

The sample can be downloaded from below.

 

Server configuration file: script2_sample03.txt

Script file: script.txt

 

 

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_0090

 

 

Commentary

In this sample, the script Ver2 action is registered as "A01" and the logger (CSV) action is registered as "A02." The logger action logs 10 tags from U01.F01.SD0000 to SD0009 on the virtual device, while the script retrieves the chronological log data generated by the logger at 5-second intervals and displays it in the Output log.

 

Below we will explain how to access the Historical data server functionality from a script to retrieve historical data.

 

1)Initial Processing

 

The initial processing is performed in the OnInitialize event. The important point here is to use the "new" operator in the initial processing to generate the HistoricalData control object.

 

Example script

event OnInitialize()
{
	::SvsDump("OnInitialize:" + ::SvsGetActionName());
	objHistData = new HistoricalData;
	objHistData.HDSRequestType = "RAWDATA";
	objHistData.AddField(c("A02.U01_F01_SD0000","A02.U01_F01_SD0001",
			"A02.U01_F01_SD0002","A02.U01_F01_SD0003","A02.U01_F01_SD0004"));
	this.SetTimer(0, 5000, -1);
}

 

Processing content

i.Use the new operator to generate a HistoricalData control object.

ii.Specify "RAWDATA" for the HDSRequestType property. By specifying "RAWDATA", you will be able to retrieve raw data where each row of data has the same time stamp.

iii.Specify the data source to be acquired using the AddField method. In this example, to acquire time series data via the logger action "A02", specify a field defined in the logger action, such as "A02.U01_F01_SD0000".

iv.SetTimer sets a timer. When the timer expires, an OnTimer event occurs. Here, we set an unlimited timer with a 5-second period.

 

 

2)Historical data acquisition process

 
Historical data is obtained within the OnTimer event. Time series data is obtained via the HistoricalData control and output to the Output log.

 

Example script

event OnTimer(timerid,counter)
{
	::SvsDump("OnTimer Id:" + ::CStr(timerid) + " counter:" + ::CStr(counter));
	var vCount, vDateTime, vData;

	if(!objHistData.ReadHistoricalData("future", T, "past", T, 10, vCount, vDateTime, vData)){
		::SvsDump("Fail to read historical data. Msg:" + objHistData.GetLastError());
		return;
	}

	::SvsDump("Count:" + ::CStr(vCount));
	::SvsDump("DateTime:" + ::CScript(vDateTime));
	::SvsDump("Data:" + ::CScript(vData));
}

 

Processing content

i.Log data (time series data) is obtained using the ReadHistoricalData method of the HistoricalData control. The range of data you want to obtain is specified in the method's arguments. As an example, we will specify "future" to "past" to search all data, but since the maximum number of records is 10, only the most recent 10 records can be obtained. Also, if data acquisition fails, the GetLastError method is used to output the details of the error to the log.

ii.The number of acquired data items, the date and time of the acquired data, and the acquired data are output to the log.

 

 

hint

"future" and "past" specify the reference time. "future" represents the future, and "past" represents the past. By specifying a date and time for these arguments, you can obtain historical data by specifying a time range.

 

 

Setup Procedure

In this sample, logging is performed using the logger action. The history data logged by the logger action is retrieved from the script via the logger action's Historical data server function.

 

Action Settings

 

1.Register the script Ver2 action "A01" and the logger (CSV) action "A02".
 
c_action_0088
 

2.Open the properties of the Script Ver2 action "A01" and select the script file.
 
c_action_0083

 

 

3.Configure logging for the logger action "A02." Note that we will not explain the procedure for configuring the logger action here.

 

 


Event Settings
 

1.Add Periodic event with a 1 second cycle and register A01 and A02 in that order as execution actions.
 
c_action_0089
 

With this setting, A01 and A02 will be processed in order when a single Periodic event occurs.