/* All user scripts should derive from the base "Application" class */ require ("Application"); require ("Time"); /* 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 WriteElapsedTime Application { //the value that causes the tag to be in condition. inCondition = 1; } /* Use methods to create functions outside the 'main line'. */ method WriteElapsedTime.valueChanged (monitoredTag, elapsedSeconds) { //currently this is checking for a boolean. You can change the IF statement to use it for an integer if( value == .inCondition) { setprop(symbol(monitoredTag), #oldtime, PointMetadata(monitoredTag).timestamp); } else { if(getprop(symbol(monitoredTag), #oldtime) != nil) { //finding the difference in days and then multiplying by 86400 to convert to seconds. local timediff = (PointMetadata(monitoredTag).timestamp - getprop(symbol(monitoredTag), #oldtime)) * 86400; local timediffRound = number(format("%6.3f", timediff)); //only write to the elapsed time tag if the tag is in condition for more than 5 seconds. if(timediffRound > 5) { datahub_write(elapsedSeconds, timediffRound); } } } } /* Write the 'main line' of the program here. */ method WriteElapsedTime.constructor () { //default:AlarmToWatch is the tag that you wish to monitor how long it is in condition. //default:ElapsedTime is the tag that holds the value of how long the value was in condition. .OnChange(#$default:AlarmToWatch, `(@self).valueChanged("default:AlarmToWatch", "default:ElapsedTime")); } /* Any code to be run when the program gets shut down. */ method WriteElapsedTime.destructor () { } /* 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 (WriteElapsedTime);