LogFile.g

LogFile.g — logs data to a text file when a point changes value.

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 shows how to log data to a text file. It uses a trigger point 
 * to signal an alarm condition (non-zero), which causes a value to be written 
 * to the file. 
 * 
 * To use this script with your points, replace 'default:triggerpt' in the LogFile
 * class with your trigger point, and replace 'default:loggedpt' with the point 
 * whose value you wish to log.  You can also change the name of the log file.
 */

/* All user scripts should derive from the base "Application" class */

require ("Application");

/* Get the Gamma library functions and methods for ODBC and/or
 * Windows programming.  Uncomment either or both.
 */

require ("WindowsSupport");
//require ("ODBCSupport");

/* Applications share the execution thread and the global name
 * space, so we create a class that contains all of the functions
 * and variables for the application.  This does two things:
 *   1) creates a private name space for the application, and
 *   2) allows you to re-load the application to create either
 *      a new unique instance or multiple instances without
 *      damaging an existing running instance.
 */

class LogFile Application
{
    // The trigger point whose value determines when logging takes place.
    trigger = #$default:triggerpt;
	
    // The point whose value gets logged.
    logged = #$default:loggedpt;
	
    // The name of the log file.
    log_file_name = "c:/tmp/logfile.txt";
	
    // The file handle to the open file
    log_file;
}

/*  
 * This method writes the trigger value and the logged point value
 * for alarm conditions and non-alarm conditions.  The first argument
 * is the actual value of the trigger point, and the second is the
 * symbolic name of the point to be logged.
 */
method LogFile.AlarmOccurred(triggervalue, !logpoint)
{
    local    value = eval (logpoint);
    if (triggervalue != 0)
    {
       	writec (.log_file, format ("Alarm:   %-20s = %10g\n", string(logpoint), value));
       	princ ("Alarm condition: ", logpoint, ", ", value, "\n");
    }
    else
    {
       	writec (.log_file, format ("Cleared: %-20s = %10g\n", string(logpoint), value));
       	princ ("No alarm: ", logpoint, ", ", value, "\n");
    }
    flush (.log_file);
}

/* Write the 'main line' of the program here. */
method LogFile.constructor ()
{
    /* If the trigger and logged points don't exist in the DataHub, create them.*/
    datahub_command (string ("(create ", .trigger, " 1)"), 1);
    datahub_command (string ("(create ", .logged, " 1)"), 1);
	
    /* Attempt to open the log file. */
    .log_file = open (.log_file_name, "a");
    if (!.log_file)
    {
       	MessageBox (0, string ("Could not open alarm log file: ", .log_file_name),
       	                      "Error opening file", 0);
    }
    else
    {
       	/* Log the data whenever the trigger point changes. */
       	.OnChange (.trigger, `(@self).AlarmOccurred (value, @.logged));
    }
}

/* Any code to be run when the program gets shut down. */
method LogFile.destructor ()
{
    if (.log_file)
        close (.log_file);
}

/* Start the program by instantiating the class.  If your
 * constructor code does not create a persistent reference to
 * the instance (self), then it will be destroyed by the
 * garbage collector soon after creation.  If you do not want
 * this to happen, assign the instance to a global variable, or
 * create a static data member in your class to which you assign
 * 'self' during the construction process.  ApplicationSingleton()
 * does this for you automatically. */
ApplicationSingleton (LogFile);