ConnectionTrack.g

ConnectionTrack.g — changes a point when a connection is made or broken.

Code

[Note]

The code for this and other example scripts can be found in the DataHub distribution archive, typically at one of these locations:

    C:\Program Files\Cogent\OPC DataHub\scripts\

    C:\Program Files\Cogent\Cascade DataHub\scripts\

Please refer to Section 3.1, “How to Run a Script” for more information on using scripts.

/*
 * This script watches the quality on an indicator point and sets
 * an output to 0 if the quality is NOT CONNECTED and 1 otherwise.
 * This effectively produces a synthetic point that changes according
 * to whether a data connection to the source of the indicator point
 * is made or broken.
 *
 * To use this script:
 * 1) Adjust the poll_rate to the desired number of seconds.  This can
 *    be fractional, such as 0.25.
 * 2) Set track_time to t or nil.  If set to t, the time stamp of the
 *    indicator point will be updated on each poll, causing a value
 *    change event to all attached clients, OPC servers, etc.
 * 3) In the constructor, make one or more calls to .BeginTracking
 *    to set up a mapping between the point being watched and the
 *    indicator point.  The watched_point 
 * 
 * Once this script is running, you can use the output point as a
 * trigger to send email, write to a database, update a PLC, write
 * to Excel or perform some other custom action through scripting.
 */

require ("Application");

class ConnectionTrack Application
{
    poll_rate = 1;		// polling rate in seconds
    track_time = nil;	// set to t to adjust the output time stamp on each poll
}

method ConnectionTrack.BeginTracking(indicator_point, output_point)
{
    datahub_command(format("(create %a 1)", indicator_point), 1);
    datahub_command(format("(create %a 1)", output_point), 1);
    .TimerEvery(.poll_rate, `(@self).CheckQuality(@indicator_point, @output_point));
}

method ConnectionTrack.CheckQuality(!indicator_point, !output_point)
{
    local	quality = PointMetadata(indicator_point).quality;
    local	active = 1;
    if (quality == OPC_QUALITY_NOT_CONNECTED)
        active = 0;
    datahub_write(string(output_point), active, .track_time);
}

method ConnectionTrack.constructor ()
{
    .BeginTracking(#$default:indicator, #$default:active);
    // default:indicator = the point you want to monitor
    // default:active = the point used to trigger the notification
}

ApplicationSingleton (ConnectionTrack);