Subscribe to Present Value Changes and Print to Console Results

var datacount = 0;

var result = subscribeValues("System1.ManagementView:ManagementView.FieldNetworks.GmsBacnetNetwork_1.Hardware.903/903-Simulator_Devices_903.903/0-Local_IO.903/1-AO_1","Present_Value",onPresentValueChanged);

console("{0}",result);

 

function onPresentValueChanged(object,values){

    datacount++;

    var time = timeStamp();

    var propChanged = values.Present_Value.value;

    console("{0},{1}",time, propChanged);

    if(datacount>=20){

        unsubscribeValues(result);

        console("Unsubscribe executed");

    }

}

function timeStamp() {

    // Create a date object with the current time

    var now = new Date();

    // Create an array with the current hour, minute and second

    var time = [ now.getHours(), now.getMinutes(), now.getSeconds() ];

    // If seconds and minutes are less than 10, add a zero

    for ( var i = 1; i < 3; i++ ) {

        if ( time[i] < 10 ) {

            time[i] = "0" + time[i];

        }

    }

    return time.join(":");

}