NX masters do something at a specific time
George Krietsepis
Posts: 284
Dear All,
since AMX suggests the DEFINE_PROGRAM section to not be used any more, how should we make the code to do something at a specific time ?
For example:
//Older master NI
DEFINE_PROGRAM
if ( TIME = '19:00:00')
//do something
How can this be implemented on the new NX master ?
Thanks,
George
since AMX suggests the DEFINE_PROGRAM section to not be used any more, how should we make the code to do something at a specific time ?
For example:
//Older master NI
DEFINE_PROGRAM
if ( TIME = '19:00:00')
//do something
How can this be implemented on the new NX master ?
Thanks,
George
0
Comments
if ( TIME = '19:00:00')
//do something
is likely to occasionally fail no matter where you put it. You are asking to check the time for a 100'th of a second match. If you don't happen to be checking at that hundreth, that is, if you are busy at that precise moment, you miss it.
To avoid missing anything, is it possible to check a range of time ? For example from '19:00:00' to '19:00:02'
I feel the timeline suggestion is the best idea to handle this. However, I think you'll find things work better if you convert the time to an integer. the way I do this for events firing with 1 minute accuracy is to convert the time to X minutes past midnight. You use the following formula. nDigital_Time= ((abs_value(time_to_hour(time))*60) + abs_value(time_to_minute(time)) )
While this may seem a bit of overkill - If you do it it allows you to now use all kinds of math operators to be pretty clever in choosing times and/or time ranges. For example:
If I want to turn on my Christmas lights at 5:30PM and the off at 12:00AM I can create a range of time from 1050 minutes up to midnight (which is zero)
For example: 5:30PM (17:30 military) is (17 * 60 or 1020) plus 30 minutes past midnight. (1050)
I can use a simple "If time is >= 1050" turn the lights on if not already on" routine. The nice thing about this method is not only does it work to make the timed event but also will update the system right after a reboot or if something happens to the light that shuts them off by accident. I've gotten where I can do the math pretty easily in my head but a simple calculator is hand for more oddball times.
There are cases also where I also bring the accuracy down to the second. However, this results in a number too big for a standard integer and have to flop over to using a LONG. But the principle is the same. I've even written a little function that allows me to send in the time in human readable form and it converts it to a decimal-past-midnight value for me.
Like I say - at first glance it seems like a lot of work. But, having the availability of simple math operators to handle time is so clean and powerful it's worth the trouble.
I also use this method for more complex systems. I did a system for a hotel that had several audio zones and several background music sources that they wanted to schedule when a zone went on or off, what music source to play, and at what volume to come on at and also the abiltiy to just raise/lower the volume at a given time. To make matters more complex, they also wanted all this to be editable on the fly.
So, I just made the whole mess into a minutes-past-midnight system and made an unordered event list of 'stuff to happen' The entire thing is nothing more than equals/greater-than/less-than math operators built into the logic. They can have as many events as they want doing whatever they want without dealing with the crazy logic and string chopping of using human-readable time.
My timeline runs once a minute to create the digital time value and at each fire then checks for timed events.
timeline_event[TL] //monitor event x every 1 minute
{
stack_var integer currentTime
currentTime = time_to_hour(time)*60 + time_to_minute(time)
switch ( timeline.sequence ) //every minute
{
case 1:
{
if ( eventTime = currentTime )
//do something
}
}
}
George
Well sure there is, but what exactly do you want to do - how often and how many times?
Let's say, for example, you want an event routine to start at 19:00 hours. At that time you want something to fire 5 times every 15 seconds. I would in this case create another timeline that runs the routine as desired (5 events at 15 second intervals) and simply launch that timeline routine from my timed events timeline.
Another method of getting there might be to go ahead and make your 1 minute timeline actually run more than once per minute and just trap events you only need to happen once but let the ticking of the timeline time out an event like you describe.
Since the thing your describe is simply an event with multiple steps I would think making it its own separate routine that is easily called whenever you want to run the whole thing would make more logical sense. so using your code example:
Set a 1 minute recurring timeline, In it call your event check.
In the event check, if actual time is less than a minute AFTER a trigger time, trigger.
Stack and dynamically manage as many triggers in your event check as you need.
You have a full minute to get to the timeline and still catch your event. You don't need a DONE flag, because you won't be back to check again within the accepted time window.
Paul
So you must either trigger on a RANGE of time, or more simply as I suggest, "Is it AFTER the Trigger time now (and within a guard time that assures you won't trigger twice)?
John,
I'm confused as to what you're saying here. "19:00:00" is 7pm, no minutes, and no seconds; therefore, isn't the statement "TIME='19:00:00'" true for full second?
--D
Same caveat however, be sure you are not going to miss the precise match you seek. If, due to system activity, you don't get around to checking at the exact moment, you don't get what you expect.