Forum Discussion

scrier's avatar
scrier
Contributor
14 years ago

Wait for a region.compare to occur with max time.

Is there some way to get the current time as Unix time from any of the variables. I want to write a method to check for a region to occur during a specified time window. The aqDateTime dowsn't seem to support this function. And the Regions.Compare doesn't seem to support a timeout.



function waitUntilImageIsInStateA(maxTimeInSeconds) {

var TimeNow = aqDateTime.now();

var maxTime = TimeNow + maxTimeInSeconds;

while( maxTime > TimeNow ) {

if( Regions.Compare("Image" Aliases.QObject.lblNavigation, false, false, true, 0, lmNone ) {

Log.Message(":: TEST :: State A Occured after " + aqDateTime.now() - TimeNow + " seconds.");

return true;

}

}

Log.Warning("Timeout occured waiting for Checkpoint");

return false;

}



The following code executes but does not work as I would like, the variable added is as far as I know not in seconds, maybe milliseconds as it seems to be around 5 seconds it's waiting. Although the Regions.Compare does occur but does not triggere the method successully.

4 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    The reason being is that TimeNow is a snapshot of the time you captured at the moment you called aqDateTime.Now().  TimeNow is not a reference to the function aqDateTime.Now() but is simply the value returned when that function is called.



    Adding a single line (see the line in bold) will do what you ask because it reassigned "TimeNow" each time through the loop to make sure that the time is compared in real time.



    You'll also want to change the way you are doing your maxTime assignment.



    function waitUntilImageIsInStateA(maxTimeInSeconds) 


    {


        var TimeNow = aqDateTime.Now();


        var maxTime = aqDateTime.AddSeconds(aqDateTime.Now(), maxTimeInSeconds);


        while( maxTime > TimeNow ) 


        {


            if (Regions.Compare("Image", Aliases.QObject.lblNavigation, false, false, true, 0, lmNone))


            { Log.Message(":: TEST :: State A Occured after " + aqDateTime.Now() - TimeNow + " seconds.");


            return true;


            }


            TimeNow = aqDateTime.Now();


        }


        Log.Warning("Timeout occured waiting for Checkpoint");


        return false;


    }
  • Hi Andreas,


    Robert is right, to calculate the maxTime value, use the aqDateTime.AddSeconds method.


    Also, I have modified your function a bit. Now, it works as you expect. If the checkpoint has passed, it returns True and posts the time the checkpoint required. If the checkpoint has failed or if it does not work until the specified timeout elapses, the method returns False.




    function waitUntilImageIsInStateA(maxTimeInSeconds)

    {

        var TimeNow = aqDateTime.now();

        var maxTime = aqDateTime.AddSeconds(TimeNow,maxTimeInSeconds)

        while(maxTime > TimeNow) 

        {

              if( Regions.Compare("Image", Aliases.QObject.lblNavigation, false, false, true, 0, lmNone ) )

              {

                     var Time = aqDateTime.now()- TimeNow; 

                     Log.Message(":: TEST :: State A Occurred after " + Time + " milliseconds.");



                         //or using another way:

        

                     var Hours = aqDateTime.GetHours(aqDateTime.now()) - aqDateTime.GetHours(TimeNow) ;    

                     var Minutes = aqDateTime.GetMinutes(aqDateTime.now()) - aqDateTime.GetMinutes(TimeNow) ;

                     var Seconds = aqDateTime.GetSeconds(aqDateTime.now()) - aqDateTime.GetSeconds(TimeNow) ;

                     Log.Message(":: TEST :: State A Occurred after "+ Hours + "Hours" + Minutes + "Minutes" + Seconds + "Seconds");

       

                      return true;

              }

              else

              {

                    Log.Warning("Checkpoint failed or timeout elapsed while waiting for the checkpoint");

                    return false;

              }

        }

    }


    In this script, I have demonstrated two ways to calculate the difference between the two time values. The first one is the same as you used:




    var Time = aqDateTime.now() - TimeNow;

    Log.Message(":: TEST :: State A Occured after " + Time + " milliseconds.");


    In this case, the result value is the amount of milliseconds.


    Another way is to calculate the difference between hours, minutes and seconds separately:




    var Hours = aqDateTime.GetHours(aqDateTime.now()) - aqDateTime.GetHours(TimeNow) ;    

    var Minutes = aqDateTime.GetMinutes(aqDateTime.now()) - aqDateTime.GetMinutes(TimeNow) ;

    var Seconds = aqDateTime.GetSeconds(aqDateTime.now()) - aqDateTime.GetSeconds(TimeNow) ;

    Log.Message(":: TEST :: State A Occurred after "+ Hours + "Hours" + Minutes + "Minutes" + Seconds + "Seconds");

  • Thank you for your response, one more question though, is it possible to get milliseconds as well? Or is that in another object?
  • Hi,


    You can also use methods of the HISUtils.StopWatch program object to check for a region checkpoint to pass during the specified time interval. This allows you to calculate how long the verification takes up to milliseconds.


    function waitUntilImageIsInStateA(maxTimeInSeconds) 

    {

        var TimeNow = aqDateTime.now();

        var maxTime = aqDateTime.AddSeconds(TimeNow,maxTimeInSeconds)

        var StopWatchObj = HISUtils.StopWatch;

        StopWatchObj.Start();

        while(maxTime > TimeNow) 

        {

              if( Regions.Compare("Image", Aliases.QObject.lblNavigation, false, false, true, 0, lmNone ) )

              {

                    StopWatchObj.Stop();

                    Log.Message("Checkpoint passed. Execution time: " + StopWatchObj.ToString()); 

                    return true;

              }

              else

              {

                    Log.Warning("Checkpoint failed or timeout elapsed while waiting for the checkpoint");

                    return false;

              }

        }

    }