Comparing strings
nhighton
Posts: 18
Hello all,
what I'm trying to do is compare a string received from a UPS (after parsing it) with TIME on my NI4100. I don't want only the current time to be checked against the string, but within a 3 minute window.
So if TIME == 08:59:00 and string from UPS contains: 09:01:00, do something.
I'm just wondering what approach people have taken previously, in a channel event's STRING, I have:
I was thinking COMPARE_STRING, but I can't use TIME to compare the string to because of my 2 minute gap requirement. The only way I can think to do this is to extract the TIME_TO_SECOND, MINUTE and HOUR and store the values in variables, then use this to compare to the string.
By the way, after my REMOVE_STRING, the buffer will look something like:
06/21/2011 09:05:15 UPS: On Batter power in response to an input power problem.
I know there will be some more tweaks to do, all I'm looking for at the moment is an efficient way to compare the time from a string to my master's time.
Thanks in advance,
Nick
what I'm trying to do is compare a string received from a UPS (after parsing it) with TIME on my NI4100. I don't want only the current time to be checked against the string, but within a 3 minute window.
So if TIME == 08:59:00 and string from UPS contains: 09:01:00, do something.
I'm just wondering what approach people have taken previously, in a channel event's STRING, I have:
ACTIVE (FIND_STRING (UPS1_BUFFER,'UPS: On battery power',1)): { REMOVE_STRING(UPS1_BUFFER,"LEFT_STRING(UPS1_BUFFER,FIND_STRING(UPS1_BUFFER,'UPS: On battery power',1)-18)",1) IF (FIND_STRING(UPS1_BUFFER,DATE,1)) { //CODE REQUIRED! } }
I was thinking COMPARE_STRING, but I can't use TIME to compare the string to because of my 2 minute gap requirement. The only way I can think to do this is to extract the TIME_TO_SECOND, MINUTE and HOUR and store the values in variables, then use this to compare to the string.
By the way, after my REMOVE_STRING, the buffer will look something like:
06/21/2011 09:05:15 UPS: On Batter power in response to an input power problem.
I know there will be some more tweaks to do, all I'm looking for at the moment is an efficient way to compare the time from a string to my master's time.
Thanks in advance,
Nick
0
Comments
First off you'll need to put both the time returned from the device and the current time into a single comparable format - I'd recommend using true's unix time library to create a couple of timestamps. From there just check the absolute value of their difference and see if it lies within your bounds.
yeah I can see how that'd work but it seems a bit long-winded for what I need. How about extracting hour, minute and second from TIME, storing them in variables before concatenating them back together but with wild cards where appropriate.
I haven't written anything comprehensive yet with it being Monday, but I was thinking pretty much a time parsing function that takes into account the 20 second difference between 50 seconds and 10 seconds. Is that unix timestamp code capable of this?
My reasoning here being that an offset in accuracy down to about 20 seconds is no big deal.
Thanks,
Nick
IF(COMPARE_STRING(UPS1_BUFFER,nTIME))
{
DO STUFF
}
Where nTIME is the time reconcatenated (is that a word?) but with a wildcard in place of seconds. This way, if it misses one instance where the above IF should evaulate to true, it will catch the next.
This event is regularly scheduled anyway. An AMX SNMP module/APC Link Language would make this so much easier.
However, in your case you could probably make a quick version that'll get the gig done by taking the TIME_TO_HOUR and TIME_TO_MINUTE commands and making an integer of the minutes since 12AM midnight.
n_decimal_Time=(Time_To_Hour(time)*60) + TIME_TO_MINUTE(time)
This will give you the minutes in a day since midnight and it's easy math at that point to get the plus or minus 3 minute window.
Believe me, getting the same reliable functionality by using the text is nightmarish. You end up with so many conditionals it'll make your head hurt.
The only problem with my bandade solution is that it won't work right around midnight since your time will be going from 1439 to 0. So, you'll have to do a little conditional logic around that time to deal with the jump.
@ericmedley
Modulus should help with the wrap around.
@nhighton
Unix time stamps are the number of seconds elapsed since midnight January 1, 1970 (UTC) so any math involved in comparing them is really straight forward.
yes it is based on device feedback, but I have to 'poke' the device to make it speak - based on a schedule. I've got it so that the event only triggers once so it's just the time string comparison I needed some advice about.
IF ((TIME_TO_SECONDS(TIME) % 14 == 0) AND nFLAG == 0)
{
WAIT 6
nFLAG = 1
//CODE BLOCK
}
...So the code runs at 14 28 42 56 seconds each minute.
Eric Medley, that system seems good to me. As Phreak said, not as much logic to mess around with apart from the start & end of the 1439 minutes.
I think that might be about it, once again thanks AMX Forums
Nick
This suddenly seems like a taxing IF - yet contains all the checks required before the queries are sent. The rest of the code on the master isn't too heavy ~1900 lines. 4 UPSs that need to be checked between 09:00 and 21:30 every 20 seconds = 9,000 queries and parsing feedback per day. The particular use of AMX here is purely for AV show control, there's an array of 4 Christie 16K projectors (1 UPS for each).
Is this too taxing to put in define_program based on my brief description?
Thanks again,
Nick
In the application you're describing I personally would use a timeline. I'd make a polling TL and check the for the time slots you describe and then do them as events.
But, that's just me...
Here's an example of what I mean by stepping. Basically check 1 condition and if that passes check another. If you're looking for a specfic hour for instance first check for the hour match and then if the hours matches then check for a minute match but don't check for both on every pass. If your checking for seconds in the day match then check for that match before any further conditions are tested.
If I recall we tested to see if the IF's exit comparsions on the first false evaluation and I think we determined it didn't. So "if(nHour == 12 && nMinute == 15)" doesn't exit if the hour isn't 12 and it will still check the minutes even though the first condition has already evaluated as false. I think spire_jeff ran that test and it would be worth a search to confirm my recollection.
So in this function which is triggered by a timeline running every 500ms I step my conditions or qualify one condition at a time in an attempt to make it as effecient as possible. In the code below I first check my seconds and if that passes I'll check my minute and then hours and so on.
if((time > '10:00:00' && time < '10:03:00'))
{
// do sumpun
}
Paul