Home AMX User Forum AMXForums Archive Threads Tips and Tricks

Run daily at specific time?

Hi there, What is the best method to run a Netinx function daily at a specific time?

Comments

  • ericmedleyericmedley Posts: 4,177
    the'TIME" function gives you a text string with the time in the format 'hh:mm:ss'

    so a simple

    if(time='12:30:54')
    {
    // do something
    }

    will get the gig done. bear in mind, that this statement will be true several thousand times in mainline. So, you'll need to put something in there to make it fire only once. Or better yet do it up in an event or function.
  • bobbob Posts: 296
    I'll define a channel or use a variable. Thanks, Eric!
  • mpullinmpullin Posts: 949
    I would suggest against putting a statement like if(time='12:30:54') in your DEFINE_PROGRAM because your processor is doing a string comparison every tick, not very efficient. If you put
    wait 600{
         if(COMPARE_STRING('12:30:??',"time") daily_function();
    }
    
    the comparison will only happen once every minute and daily_function will run whenever it's between 12:30 and 12:31. This assumes you need accuracy within one minute, you could just as easily do it for tens of seconds:
    wait 100{
         if(COMPARE_STRING('12:30:5?',"time") daily_function();
    }
    
  • bobbob Posts: 296
    I had done the following, but it is still a string comparison every tick...

    IF ((TIME == '16:10:00') AND (nDaily))
    {
    nDaily = FALSE; // execute only once
    DailyFunction();
    }
    ELSE
    nDaily = TRUE;
  • jjamesjjames Posts: 2,908
    What about putting your code within a wait that's executed every 58 or 59 seconds? This way it's not evaluated every "tick".
  • viningvining Posts: 4,368
    Here's an include file I use to handle most time functions that I'll need in most jobs. There's some good stuff in there and the fnUpdate_Time() function I use to track time. It tickes every second via a timeline but in DEF PROG there's a wait 10 commented out that could call this function if you don't use a timeline. Then there are functions with in this function to things based on time. I just copied this from a current job so there are events in some of these functions that should be ignored.
    PROGRAM_NAME='VAV_Date_&_Time'
    
    DEFINE_CONSTANT //DST                           <<<*************************  CHANGE SETTING PER JOB *********************************>>>
    //Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn.[1] 
    //Starting in 2007, Daylight Saving Time (DTS) has been extended by one month. Therefore, the start date of DTS in the United States has been
    //changed to the 2nd Sunday in March and the end date of DTS has been changed to the 1st Sunday in November
    
    //If Daylight Savings Time is currently being observed (between spring and fall), set this item to "1". If Daylight Savings Time
    //is not currently being observed (between fall and spring) or is not observed in your geographic location, set this item to "0".
    //This item is used to correctly calculate the times for sunrise and sunset.
    
    INTEGER DST_OBSERVED		= 1 ;  		//1 for yes and 0 for no 
    INTEGER DST_OFFSET		= 1 ;  		//number of hours time is offset 
    INTEGER DST_BEGIN_MONTH		= 3 ;  		//March (months 1-12 January to December)
    INTEGER DST_END_MONTH		= 11 ; 		//November (months 1-12 January to December)
    INTEGER DST_BEGIN_DAY		= 1 ;  		//Sunday (day of week 1-7 Sunday to Saturday)
    INTEGER DST_END_DAY		= 1 ;  		//Sunday (day of week 1-7 Sunday to Saturday)
    INTEGER DST_BEGINDAY_OCCURANCE	= 2 ;  		//Number of occurances of that day in that month (2nd Sunday)
    INTEGER DST_ENDDAY_OCCURANCE	= 1 ;  		//Number of occurances of that day in that month (1st Sunday)
    CHAR DST_BEGIN_TIME[]		= '02:00:00' ;	//Time change occurs 
    CHAR DST_END_TIME[]		= '02:00:00' ;	//Time change occurs 
    
    DEFINE_CONSTANT //ASTRO_CLOCK LAT/LONG          <<<*************************  CHANGE SETTING PER JOB *********************************>>>
    
    INTEGER CLKMGR_DST		= 1 ;     	//1 for ON and 0 for OFF
    INTEGER CLKMGR_DST_OFFSET_HR	= 1 ;     	//1 for 1 hour
    INTEGER CLKMGR_DST_OFFSET_MIN	= 0 ;     	//0 for 0 minutes
    INTEGER CLKMGR_DST_OFFSET_SEC	= 0 ;     	//0 for 0 seconds
    INTEGER CLKMGR_RESYNC_PERIOD	= 240 ;     	//in minutes 480 = 8 hours, choices: 5 min, 15 min, 2 hour, 4 hours & (8 hours?)
    					        //the web interface doesn't show an 8 hour option but the keyword example only shows
    					        //an hour hour example. So I choose the highest the web gui lists 4 hours/240 minutes. 
    
    DOUBLE CLKMGR_LAT		= -73.42 ;	//Longitude - Longitude in Degrees.Fraction of Degrees. West longitudes must be negative.
    DOUBLE CLKMGR_LONG		= 41.98 ; 	//Latitude - Latitude in Degrees.Fraction of Degrees. South latitudes must be negative 
    DOUBLE CLKMGR_OFF_FROM_GMT	= -5.0 ;  	//HoursFromGMT - Number of hours from GMT. Hours West of GMT can be entered as 
    
    CHAR CLKMGR_TIME_ZONE[]		= 'UTC-05:00' ;	//Input string must have the correct format: UTC[+|-]HH:MM
    CHAR CLKMGR_ACTIVESERVER[] 	= '132.163.4.101' ; 
    
    DEFINE_CONSTANT //TIME CONSTANTS Days of Week & Month of Year
    
    SUNDAY        	= 1 ;
    MONDAY        	= 2 ;
    TUESDAY       	= 3 ;
    WEDNESDAY     	= 4 ;
    THURSDAY      	= 5 ;
    FRIDAY        	= 6 ;
    SATURDAY      	= 7 ;
    
    JANUARY		= 1 ;
    FEBRUARY	= 2 ;
    MARCH		= 3 ;
    APRIL		= 4 ;
    MAY		= 5 ;
    JUNE		= 6 ;
    JULY 		= 7 ;
    AUGUST		= 8 ;
    SEPTEMBER	= 9 ;
    OCTOBER		= 10 ;
    NOVEMBER	= 11 ;
    DECEMBER	= 12 ;
    
    FIRST           = 1 ;
    SECOND          = 2 ;
    THIRD           = 3 ;
    FOURTH          = 4 ;
    FIFTH           = 5 ;
    
    EXPIRES_IN_SECONDS      	= 10 ; 
    
    LONG NUM_SECONDS_DAY	    	= 86400 ;
    CHAR DAY_OF_WEEK_ABREV[7][3]   = {'SUN','MON','TUE','WED','THU','FRI','SAT'}
    CHAR DAY_OF_WEEK_FULL[7][9]    = {'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'}
    
    CHAR MONTH_OF_YR_ABREV[12][3]   = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'} ; 
    CHAR MONTH_OF_YR_FULL[12][9]    = {'January','February','March','April','May','June','July','August','September','October','November','December'} ; 
    CHAR DAYS_MONTHs[12]        	= {31   ,28   ,31   ,30   ,31   ,30   ,31   ,31   ,30   ,31   ,30   ,31} ;
    CHAR DAYS_LEAPMONTHs[12]    	= {31   ,29   ,31   ,30   ,31   ,30   ,31   ,31   ,30   ,31   ,30   ,31} ;
    
    DEFINE_CONSTANT //UI FEEDBACK EVENTS
    
    FB_TIME_TIME       	= 1 ;
    FB_TIME_SUNRISE		= 2 ;
    FB_TIME_SUNSET		= 3 ;
    FB_TIME_DST		= 4 ;
    FB_TIME_HOLIDAY		= 5 ;
    
    DEFINE_TYPE     //TIME STUFF
    
    STRUCTURE _sMyTime
    
         {
         INTEGER 	nYear ;
         INTEGER 	nMonth ;
         INTEGER 	n24Hour ;
         INTEGER 	n12Hour ;
         INTEGER 	nMinute ;
         INTEGER 	nSecond ;
         INTEGER 	nDayOfMonth ;
         INTEGER 	nDayOfWeek ;
         INTEGER  	nDayOccurance[7] ;
         LONG	nTime ;
         CHAR	c24Time[8] ;
         CHAR	c12Time[8] ;
         INTEGER 	nWeekOfMonth ;
         INTEGER 	nPlusDays ;
         INTEGER 	nDaytime ;
         INTEGER    nTimePM_flag ;
         CHAR       cAMPM[2] ;
         LONG     	nSunriseTime ;
         LONG     	nSunSetTime ;
         CHAR 	cSunrise[10] ;
         CHAR 	cSunset[10] ;
         INTEGER 	nSunset24Hour ;
         INTEGER 	nSunset12Hour ;
         INTEGER 	nSunsetMinute ;
         INTEGER 	nSunsetSecond ;
         INTEGER 	nSunriseHour ;
         INTEGER 	nSunriseMinute ;
         INTEGER 	nSunriseSecond ;
         CHAR     	cHoliday[24] ;
         CHAR     	cDay[3] ;
         CHAR     	cDate[8] ;
         CHAR 	cWebCurDateTime[29] ;
         CHAR 	cWebExpDateTime[29] ;
         }
         
    DEFINE_TYPE     //DST
    
    STRUCTURE _sMyDST
    
         {
         INTEGER nDST_Active ;
         INTEGER nOffset ;
         INTEGER nObserved ;
         INTEGER nBEG_Month ;
         INTEGER nEND_Month ;
         INTEGER nBEG_Day ;
         INTEGER nEND_Day ;
         INTEGER nBEG_DAY_Occurance ;
         INTEGER nEND_DAY_Occurance ;
         }
    
    DEFINE_TYPE     //CLKMGR_TIMEOFFSET_STRUCT FROM NETLINX.AXI FOR REFERNCE ONLY
    
    (*    
    STRUCTURE CLKMGR_TIMEOFFSET_STRUCT
         
         {
         INTEGER     HOURS;
         INTEGER     MINUTES;
         INTEGER     SECONDS;
         }
    *)
    
    DEFINE_VARIABLE //STRUCTURES
    
    PERSISTENT _sMyTime sMyTime ;
    VOLATILE   _sMyDST sMyDST ;
    
    VOLATILE INTEGER nTimeWait_Force = 1 ;
    VOLATILE INTEGER nTime_DeBug = 1 ;
    
    DEFINE_FUNCTION LONG fnConv_24HrTimeToSeconds(CHAR iTime[])
    
         {
         RETURN ((((ATOI(REMOVE_STRING(iTime,"':'",1)) * 60) + ATOI(REMOVE_STRING(iTime,"':'",1))) * 60) + ATOI(iTime)) ;
         }
         
    DEFINE_FUNCTION CHAR[8] fnConv_SecondsTo24HrTime(LONG iTimeInSeconds)
    
         {
         RETURN   "right_string("'0',ITOA(iTimeInSeconds / 3600)",2),':',
    		    right_string("'0',ITOA((iTimeInSeconds % 3600) / 60)",2),':',
    			 right_string("'0',ITOA((iTimeInSeconds % 3600) % 60)",2)" ;
         }
         
    DEFINE_FUNCTION CHAR[30] fnDo_24hrTimeAddition(CHAR iLDate[],CHAR i24HourTime[],LONG iSecondsAdded)     
         
         {
         STACK_VAR INTEGER nYear ;       //change to all stack_var's
         STACK_VAR INTEGER nMonth ;
         STACK_VAR INTEGER nLeap ;
         STACK_VAR CHAR c24HrTime[8] ;
         STACK_VAR CHAR cNewDate[10] ;
         STACK_VAR LONG nDay ;
         STACK_VAR LONG nDaysOver ;
         STACK_VAR LONG nSecondsRemain ;
         STACK_VAR LONG nTotalTimeSeconds ;
         
         nTotalTimeSeconds = fnConv_24HrTimeToSeconds(i24HourTime) + iSecondsAdded ;
         nMonth = atoi("REMOVE_STRING(iLDATE,"'/'",1)") ;
         nDay   = atoi("REMOVE_STRING(iLDATE,"'/'",1)") ;
         nYear  = atoi("iLDATE") ;
         
         if(nTotalTimeSeconds > NUM_SECONDS_DAY)
    	  {
    	  STACK_VAR INTEGER i ;
    	  
    	  nDaysOver = nTotalTimeSeconds / NUM_SECONDS_DAY ;
    	  nSecondsRemain = nTotalTimeSeconds % NUM_SECONDS_DAY ;
    	  if(!(nYear % 4))//http://en.wikipedia.org/wiki/Leap_year
    	       {
    	       nLeap = 1 ;
    	       if(!(nYear % 100))//double check these rules?????? 
    		    {
    		    if(nYear % 400)
    			 {
    			 nLeap = 0 ;
    			 }
    		    }
    	       }
    	  if(nLeap)
    	       {
    	       if((nDaysOver + nDay) > DAYS_LEAPMONTHs[nMonth])
    		    {
    		    nDaysOver = nDaysOver - (DAYS_LEAPMONTHs[nMonth] - nDay) ;
    		    nMonth++ ;
    		    
    		    for(nMonth = nMonth ; nDaysOver > DAYS_LEAPMONTHs[nMonth] ; nMonth++)
    			 {
    			 nDaysOver = (nDaysOver - DAYS_LEAPMONTHs[nMonth]) ;
    			 if(nMonth == 12)
    			      {
    			      nMonth = 1 ;
    			      nYear ++ ;
    			      }
    			 }
    		    nDay = nDaysOver ;
    		    }
    	       else
    		    {
    		    nDay = nDaysOver + nDay ;
    		    }
    	       }
    	  else
    	       {
    	       if((nDaysOver + nDay) > DAYS_MONTHs[nMonth])
    		    {
    		    nDaysOver = nDaysOver - (DAYS_MONTHs[nMonth] - nDay) ;
    		    nMonth++ ;
    		    
    		   for(nMonth = nMonth ; nDaysOver > DAYS_MONTHs[nMonth] ; nMonth++)
    			 {
    			 nDaysOver = (nDaysOver - DAYS_MONTHs[nMonth]) ;
    			 if(nMonth == 12)
    			      {
    			      nMonth = 1 ;
    			      nYear ++ ;
    			      }
    			 }
    		    nDay = nDaysOver ;
    		    }
    	       else
    		    {
    		    nDay = nDaysOver + nDay ;
    		    }
    	       }
    	  c24HrTime = fnConv_SecondsTo24HrTime(nSecondsRemain) ; 
    	  }
         else
    	  {
    	  c24HrTime = fnConv_SecondsTo24HrTime(nTotalTimeSeconds) ;
    	  }
         cNewDate = "right_string("'0',ITOA(nMonth)",2),'/',right_string("'0',ITOA(nDay)",2),'/',right_string("'0',ITOA(nYear)",2)" ;
    	   
         RETURN "DAY_OF_WEEK_ABREV[TYPE_CAST(DAY_OF_WEEK(cNewDate))],', ',itoa(nDay),' ',MONTH_OF_YR_ABREV[nMonth],' ',itoa(nYear),' ',c24HrTime" ;
         }
         
    DEFINE_FUNCTION fnFB_Time(INTEGER iUI_Indx,INTEGER iEvent) //<< SET VT ADDRESS CHANNELS  CHANGE SETTING PER JOB **********************>>>
    
         {
         SWITCH(iEvent)
    	  {
    	  CASE FB_TIME_TIME:
    	       {
    	       //panels can handle time on their own 
    	       }
    	  CASE FB_TIME_SUNRISE:
    	       {
    	       fnFB_TimeSend_VT(iUI_Indx,1,"'Sunrise at ',itoa(sMyTime.nSunriseHour),':',itoa(sMyTime.nSunriseMinute),' AM'") ;
    	       }
    	  CASE FB_TIME_SUNSET:
    	       {
    	       fnFB_TimeSend_VT(iUI_Indx,2,"'Sunset at ',itoa(sMyTime.nSunset12Hour),':',itoa(sMyTime.nSunsetMinute),' PM'") ;
    	       }
    	  CASE FB_TIME_DST:
    	       {
    	       if(sMyDST.nDST_Active)
    		    {
    		    //fnFB_TimeSend_VT(iUI_Indx,??,"'DST: IN EFFECT'") ; 
    		    }
    	       else
    		    {
    		    //fnFB_TimeSend_VT(iUI_Indx,??,"'DST: NOT IN EFFECT'") ; 
    		    }
    	       }
    	  CASE FB_TIME_HOLIDAY:
    	       {
    	       fnFB_TimeSend_VT(iUI_Indx,4,"sMyTime.cHoliday") ;
    	       }
    	  }
         }
         
    DEFINE_FUNCTION fnFB_TimeSend_VT(INTEGER iUI_Indx,INTEGER iCHAN,CHAR iStrMSG[500]) 
         
         {
         STACK_VAR INTEGER n ;
         STACK_VAR INTEGER nTPCount ; 
         STACK_VAR INTEGER nLoopStart ; 
         
         if(iUI_Indx)
    	  {
    	  nTPCount = iUI_Indx ;
    	  nLoopStart = iUI_Indx ;
    	  }
         else
    	  {
    	  nTPCount = NUM_UIs_IN_SYSTEM ;
    	  nLoopStart = 1 ;
    	  }
    	  
         //fnTime_DeBug("'SEND_VT: ',iStrMSG,' :DEBUG <',itoa(__LINE__),'>'") ;
         
         for(n = nLoopStart ; n <= nTPCount ; n++)
    	  {
    	  SWITCH(nUI_TypeArry[n])
    	       {
    	       CASE UI_TYPE_G4:
    	       CASE UI_TYPE_R4:
    	       CASE UI_TYPE_MIO_DMS:
    	       (* no need for uni strings!!!
    		    {
    		    STACK_VAR WIDECHAR cSTRING1[500] ;
    		    STACK_VAR CHAR cSTRING2[500] ;
    		    
    		    cSTRING1 = WC_DECODE(iStrMSG,WC_FORMAT_UTF8,1) ; 
    		    cSTRING2 = WC_ENCODE(cSTRING1,WC_FORMAT_TP,1) ;
    		    //fnTime_DeBug("'^UNI- TP INDX: ',itoa(n),', CHNL: ',itoa(iCHAN),' :DEBUG <',ITOA(__LINE__),'>'") ;
    		    SEND_COMMAND dvUI_Arry[n], "'^UNI-',itoa(iCHAN),',0,',cSTRING2" ; 
    		    }
    	       *)
    	       CASE UI_TYPE_G3:
    	       CASE UI_TYPE_iPHONE://uni not supported yet		
    	       CASE UI_TYPE_iPAD:		
    	       CASE UI_TYPE_iTOUCH:	
    		    {
    		    //fnTime_DeBug("'^TXT- TP INDX: ',itoa(n),', CHNL: ',itoa(iCHAN),' :DEBUG <',ITOA(__LINE__),'>'") ;
    		    SEND_COMMAND dvUI_Arry[n], "'TEXT',itoa(iCHAN),'-',iStrMSG" ; 
    		    }
    	       CASE UI_TYPE_METKP:
    	       CASE UI_TYPE_UNKNOWN:
    	       CASE UI_TYPE_VIRTUAL:
    		    {
    		    //DO NOTHING
    		    }
    	       }
    	  }
    	  
         RETURN ;
         }
    
    DEFINE_FUNCTION fnTime_DeBug(CHAR iStr[])
    
         {
         if(nTime_DeBug)
    	  {
    	  STACK_VAR CHAR cCopyStr[1024] ;
    	  STACK_VAR INTEGER nLineCount ;
    	  
    	  cCopyStr = iStr ;
    	  
    	  nLineCount ++ ;
    	  WHILE(length_string(cCopyStr) > 100)
    	       {
    	       SEND_STRING 0,"'TIME DEBUG (',itoa(nLineCount),'): ',get_buffer_string(cCopyStr,80)" ;
    	       nLineCount ++ ;
    	       }
    	  if(length_string(cCopyStr))
    	       {
    	       SEND_STRING 0,"'TIME DEBUG (',itoa(nLineCount),'): ',cCopyStr" ;
    	       }
    	  }
         
         RETURN ;
         } 
         
    DEFINE_FUNCTION fnGet_DST_State() 
         
         {//don't care about the hour of day it actually occurs!
         fnTime_DeBug("'fnCheckDST_Rule(): Checking DST State :DEBUG <',ITOA(__LINE__),'>'") ;
         SELECT
    	  {
    	  ACTIVE(sMyTime.nMonth < DST_BEGIN_MONTH && sMyTime.nMonth > DST_END_MONTH):
    	       {//quick elimination
    	       sMyDST.nDST_Active = 0 ;
    	       }
    	  ACTIVE(sMyTime.nMonth > DST_BEGIN_MONTH && sMyTime.nMonth < DST_END_MONTH):
    	       {//quick elimination
    	       sMyDST.nDST_Active = 1 ;
    	       }
    	  ACTIVE(sMyTime.nMonth == DST_BEGIN_MONTH): 
    	       {
    	       if(sMyTime.nDayOccurance[sMyDST.nBEG_Day] >= sMyDST.nBEG_DAY_Occurance)
    		    {
    		    sMyDST.nDST_Active = 1 ;
    		    }
    	       else
    		    {
    		    sMyDST.nDST_Active = 0 ;
    		    }
    	       }
    	  ACTIVE(sMyTime.nMonth == DST_END_MONTH): 
    	       {
    	       if(sMyTime.nDayOccurance[sMyDST.nEND_Day] >= sMyDST.nEND_DAY_Occurance)
    		    {
    		    sMyDST.nDST_Active = 0 ;
    		    }
    	       else
    		    {
    		    sMyDST.nDST_Active = 1 ;
    		    }
    	       }
    	  ACTIVE(TRUE):
    	       {
    	       fnTime_DeBug("'fnCheckDST_Rule(): DST Check Error! :DEBUG <',ITOA(__LINE__),'>'") ;
    	       
    	       RETURN ;
    	       }
    	  } 
         if(sMyDST.nDST_Active)
    	  {
    	  fnTime_DeBug("'fnCheckDST_Rule(): DST is Active! :DEBUG <',ITOA(__LINE__),'>'") ;
    	  }
         else
    	  {
    	  fnTime_DeBug("'fnCheckDST_Rule(): DST is In-Active! :DEBUG <',ITOA(__LINE__),'>'") ;
    	  }
    	  
         fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_DST) ;
    	  
         RETURN ;
         }
         
    DEFINE_FUNCTION fnGet_AstroClock()
    
         {
         LOCAL_VAR INTEGER nTimeMngr_SyncPeriod ;//local just in case I want to view/testing, just get rid of these and the calls that use them
         LOCAL_VAR SINTEGER nTimeMngr_Result ;
         LOCAL_VAR INTEGER nTimeMngr_DST ;
         LOCAL_VAR CHAR cTimeMngr_TmZn[6] ;
         
         fnTime_DeBug("'fnAstro_Clock(): Running ArtoClock! :DEBUG <',ITOA(__LINE__),'>'") ;
         
         nTimeMngr_SyncPeriod = CLKMGR_GET_RESYNC_PERIOD() ;
         cTimeMngr_TmZn = CLKMGR_GET_TIMEZONE() ;
         nTimeMngr_DST = CLKMGR_IS_DAYLIGHTSAVINGS_ON() ;
         
         if(sMyDST.nObserved)
    	  {
    	  fnGet_DST_State() ;
    	  }
         
         //SINTEGER ASTRO_CLOCK(DOUBLE Longitude,DOUBLE Latitude,DOUBLE HoursFromGMT,CHAR[] Date,CHAR[] Sunrise,CHAR[] Sunset)
         nTimeMngr_Result = ASTRO_CLOCK(CLKMGR_LAT,CLKMGR_LONG,CLKMGR_OFF_FROM_GMT,LDATE,sMyTime.cSunrise,sMyTime.cSunset) ;
             
         fnTime_DeBug("'fnAstro_Clock(): Sunset Time = ',sMyTime.cSunset,' :DEBUG <',ITOA(__LINE__),'>'") ;
         sMyTime.nSunset24Hour = atoi(left_string(sMyTime.cSunset,2)) ;
         sMyTime.nSunsetMinute = atoi(mid_string(sMyTime.cSunset,4,2)) ;
         sMyTime.nSunsetSecond = atoi(right_string(sMyTime.cSunset,2)) ;
         sMyTime.nSunsetTime   =  ((((sMyTime.nSunset24Hour * 60) + sMyTime.nSunsetMinute) * 60) + sMyTime.nSunsetSecond) ;
         if(sMyTime.nSunset24Hour >= 12)
              {
    	  sMyTime.nSunset12Hour = sMyTime.nSunset24Hour - 12 ;
    	  }
         else//yeah, I know!
    	  {
    	  sMyTime.nSunset12Hour = sMyTime.nSunset24Hour ;
    	  }
         fnTime_DeBug("'fnAstro_Clock(): Sunrise Time = ',sMyTime.cSunrise,' :DEBUG <',ITOA(__LINE__),'>'") ;
         sMyTime.nSunriseHour   = atoi(left_string(sMyTime.cSunrise,2)) ;
         sMyTime.nSunriseMinute = atoi(mid_string(sMyTime.cSunrise,4,2)) ;
         sMyTime.nSunriseSecond = atoi(right_string(sMyTime.cSunrise,2)) ;
         sMyTime.nSunriseTime   = ((((sMyTime.nSunriseHour * 60) + sMyTime.nSunriseMinute) * 60) + sMyTime.nSunriseSecond) ;
         fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_SUNRISE) ;
         fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_SUNSET) ;
         }
         
    DEFINE_FUNCTION fnGet_DayOccurrences()
    
         {
         if(sMyTime.nDayOfWeek && sMyTime.nDayOfMonth)
    	  {
    	  STACK_VAR INTEGER i ;
    	  STACK_VAR INTEGER nD_O_W ;
    	  
    	  sMyTime.nWeekOfMonth = sMyTime.nDayOfMonth / 7 ;
    	  sMyTime.nPlusDays  = sMyTime.nDayOfMonth % 7 ;
    	  
    	  for(i = 1 ; i <= 7 ; i ++)//first erase stored values 
    	       {
    	       sMyTime.nDayOccurance[i] = 0 ;
    	       }
    	
    	  nD_O_W = sMyTime.nDayOfWeek ;
    	  sMyTime.nDayOccurance[nD_O_W] ++ ;
    	  
    	  for(i = sMyTime.nDayOfMonth ; i > 1 ; i --)
    	       {
    	       if(nD_O_W > 1) 
    		    {
    		    nD_O_W --  ;
    		    }		    
    	       else 
    		    {
    		    nD_O_W = 7 ;
    		    }
    	       sMyTime.nDayOccurance[nD_O_W] ++ ;
    	       }
    	  fnTime_DeBug("'fnFindDayOccurences(): The First Day of the Month= ',DAY_OF_WEEK_ABREV[nD_O_W],' (',itoa(nD_O_W),') :DEBUG <',ITOA(__LINE__),'>'") ;
    	  fnTime_DeBug("'fnFindDayOccurences(): Weeks= ',itoa(sMyTime.nWeekOfMonth),', Days= ',itoa(sMyTime.nPlusDays),
    			 ', Day= ',itoa((sMyTime.nWeekOfMonth * 7) + sMyTime.nPlusDays),' of ',MONTH_OF_YR_ABREV[sMyTime.nMonth],' :DEBUG <',ITOA(__LINE__),'>'") ;
    
    	  }
         else
    	  {
    	  fnTime_DeBug("'fnFindDayOccurences(): Error! No Value in sMyTime.nDayOfWeek or sMyTime.nDayOfMonth :DEBUG <',ITOA(__LINE__),'>'") ;
    	  }
         }
         
    DEFINE_FUNCTION fnGet_Holiday()
    
         {
         sMyTime.cHoliday = '' ;
         
         SWITCH(sMyTime.nMonth)
    	  {
    	  CASE JANUARY:
    	       {
    	       SELECT
    		    {
    		    ACTIVE(sMyTime.nDayOfMonth == 1):
    			 {
    			 sMyTime.cHoliday = 'New Years Day' ;
    			 }
    		    ACTIVE(sMyTime.nDayOccurance[MONDAY] == THIRD && sMyTime.nDayOfWeek == MONDAY):
    			 {
    			 sMyTime.cHoliday = "'M.L.King',39,'s Birthday'" ;
    			 }
    		    }
    	       }
    	  CASE FEBRUARY:
    	       {
    	       SELECT
    		    {
    		    ACTIVE(sMyTime.nDayOfMonth == 2):
    			 {
    			 sMyTime.cHoliday = 'Ground Hog Day' ;
    			 }
    		    ACTIVE(sMyTime.nDayOfMonth == 14):
    			 {
    			 sMyTime.cHoliday = "'Valentine',39,'s Day'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOfMonth == 22):
    			 {
    			 sMyTime.cHoliday = "'G. Washington',39,'s Birthday'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOccurance[MONDAY] == THIRD && sMyTime.nDayOfWeek == MONDAY):
    			 {
    			 sMyTime.cHoliday = "'President',39,'s Day'" ;
    			 }
    		    }
    	       }
    	  CASE MARCH:
    	       {
    	       SELECT
    		    {
    		    ACTIVE(sMyTime.nDayOfMonth == 9):
    			 {
    			 sMyTime.cHoliday = "'Employee Appreciation Day'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOfMonth == 17):
    			 {
    			 sMyTime.cHoliday = "'ST Patrick',39,'s Day'" ;
    			 }
    		    }
    	       //EASTER?
    	       }
    	  CASE APRIL:
    	       {
    	       SELECT
    		    {
    		    ACTIVE(sMyTime.nDayOfMonth == 1):
    			 {
    			 sMyTime.cHoliday = "'April Fool',39,'s Day'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOfMonth == 14):
    			 {
    			 sMyTime.cHoliday = "'Valentine',39,'s Day'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOccurance[MONDAY] == THIRD && sMyTime.nDayOfWeek == MONDAY):
    			 {
    			 sMyTime.cHoliday = "'Patriot',39,'s Day'" ;
    			 }
    		    }
    	       }
    	  CASE MAY:
    	       {
    	       SELECT
    		    {
    		    ACTIVE(sMyTime.nDayOccurance[SUNDAY] == SECOND && sMyTime.nDayOfWeek == SUNDAY):
    			 {
    			 sMyTime.cHoliday = "'Mother',39,'s Day'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOccurance[SATURDAY] == THIRD && sMyTime.nDayOfWeek == SATURDAY):
    			 {
    			 sMyTime.cHoliday = "'Armed Forces Day'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOccurance[MONDAY] == FOURTH && sMyTime.nDayOfMonth > 24 && sMyTime.nDayOfWeek == MONDAY):
    			 {
    			 sMyTime.cHoliday = 'Memorial Day' ;
    			 }
    		    ACTIVE(sMyTime.nDayOccurance[MONDAY] == FIFTH && sMyTime.nDayOfWeek == MONDAY):
    			 {
    			 sMyTime.cHoliday = 'Memorial Day' ;
    			 }
    		    }
    	       }
    	  CASE JUNE:
    	       {
    	       SELECT
    		    {
    		    ACTIVE(sMyTime.nDayOfMonth == 14):
    			 {
    			 sMyTime.cHoliday = "'Flag Day'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOfMonth == 21):
    			 {
    			 sMyTime.cHoliday = "'Summer Solstice'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOccurance[SUNDAY] == THIRD && sMyTime.nDayOfWeek == SUNDAY):
    			 {
    			 sMyTime.cHoliday = "'Father',39,'s Day'" ;
    			 } 
    		    }
    	       }
    	  CASE JULY:
    	       {
    	       if(sMyTime.nDayOfMonth == 4)
    		    {
    		    sMyTime.cHoliday = 'Independence Day' ;
    		    }
    	       }
    	  CASE AUGUST:
    	       {
    	       if(sMyTime.nDayOccurance[SUNDAY] == FIRST && sMyTime.nDayOfWeek == SUNDAY)
    		    {
    		    sMyTime.cHoliday = "'Friendship Day'" ;
    		    } 
    	       }
    	  CASE SEPTEMBER:
    	       {
    	       if(sMyTime.nDayOccurance[MONDAY] == FIRST && sMyTime.nDayOfWeek == MONDAY)
    		    {
    		    sMyTime.cHoliday = 'Labor Day' ;
    		    }
    	       }
    	  CASE OCTOBER:
    	       {
    	       if(sMyTime.nDayOfMonth == 31)
    		    {
    		    sMyTime.cHoliday = 'Halloween' ;
    		    }
    	       }
    	  CASE NOVEMBER:
    	       {
    	       SELECT
    		    {
    		    ACTIVE(sMyTime.nDayOfMonth == 1):
    			 {
    			 sMyTime.cHoliday = "'All Saint',39,'s Day'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOfMonth == 11):
    			 {
    			 sMyTime.cHoliday = "'Veteran',39,'s Day'" ;
    			 }
    		    ACTIVE(sMyTime.nDayOccurance[THURSDAY] == FOURTH && sMyTime.nDayOfWeek == THURSDAY):
    			 {
    			 sMyTime.cHoliday = 'Thanksgiving Day' ;
    			 }
    		    }
    	       }
    	  CASE DECEMBER:
    	       {
    	       SELECT
    		    {
    		    ACTIVE(sMyTime.nDayOfMonth >= 1 && sMyTime.nDayOfMonth <= 23):
    			 {
    			 sMyTime.cHoliday = "ITOA(25 - sMyTime.nDayOfMonth),' days to Christmas'" ;
    			 }
    		    (*
    		    ACTIVE(nMY_DayOfMonth == 21):
    			 {
    			 cHoliday = 'Winter Solstice' ;
    			 }
    		    *)
    		    ACTIVE(sMyTime.nDayOfMonth == 24):
    			 {
    			 sMyTime.cHoliday = 'Christmas Eve' ;
    			 }
    		    ACTIVE(sMyTime.nDayOfMonth == 25):
    			 {
    			 sMyTime.cHoliday = 'Christmas Day' ;
    			 }
    		    ACTIVE(sMyTime.nDayOfMonth == 31):
    			 {
    			 sMyTime.cHoliday = "'New Year',39,'s Eve'" ;
    			 }
    		    }
    	       }
    	  }
         if(length_string(sMyTime.cHoliday))
    	  {
    	  sMyTime.cHoliday = "'"',sMyTime.cHoliday,'"'" ;
    	  fnTime_DeBug("'fnFindHolidays(): Today is ',sMyTime.cHoliday,' :DEBUG <',ITOA(__LINE__),'>'") ;
    	  }
         else
    	  {
    	  fnTime_DeBug("'fnFindHolidays(): Today is not a "supported" holiday! :DEBUG <',ITOA(__LINE__),'>'") ;
    	  }
    	  
         fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_HOLIDAY) ;
         
         RETURN ;
         }
         
    DEFINE_FUNCTION fnGet_WebDATE_TIME()
    
         {
         sMyTime.cWebCurDateTime = "fnDo_24hrTimeAddition(LDate,TIME,0),' EST'" ;
         sMyTime.cWebExpDateTime = "fnDo_24hrTimeAddition(LDate,TIME,EXPIRES_IN_SECONDS),' EST'" ;
        
         RETURN ;
         }
         
    DEFINE_FUNCTION fnRun_D_O_W_Events(INTEGER iDayOfWeek)   //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {
         STACK_VAR INTEGER cCopyDOW ;
         
         cCopyDOW  = iDayOfWeek ;
         //SELECT ACTIVE or SWITCH CASE to do something
         }     
         
    DEFINE_FUNCTION fnRun_D_O_M_Events(INTEGER iDayOfMonth)  //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {//uncomment call in fnUpdate_Time()
         STACK_VAR INTEGER cCopyDOM ;
         
         cCopyDOM  = iDayOfMonth ;
         //SELECT ACTIVE or SWITCH CASE to do something
         }
         
    DEFINE_FUNCTION fnRun_DailyEvents()                      //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {
         //fnDailyEmailTest() ;
         fnGet_Holiday() ;
         fnGet_AstroClock() ;
         //SELECT ACTIVE or SWITCH CASE to do something
         }
         
    DEFINE_FUNCTION fnRun_HourEvents(INTEGER iHour)          //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {
         STACK_VAR INTEGER cCopyHour ;
         
         cCopyHour  = iHour ;
         //SELECT ACTIVE or SWITCH CASE to do something
         }
    
    DEFINE_FUNCTION fnRun_MinuteEvents(INTEGER iMinute)      //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {
         STACK_VAR INTEGER cCopyMinute ;
         
         cCopyMinute  = iMinute ;
         
         
         #WARN 'CKECK ALARM COMMENTED OUT inn date & time.axi'
        // fnAC_CheckAlarmClocks() ;
         
         
         fnCntDwnPirTmrs() ;
         if(nHAI_DataIn_Q)
    	  {
    	  fnHAI_SendNextInQ() ; //ITOA(iMinutes%60)
    	  }
         if(!cCopyMinute || !(cCopyMinute % 10))
    	  {
    	  fnQueryHAI_Temps() ;
    	  }
        // fnQueryHAI_Temps() ;  //????????
         
         //SELECT ACTIVE or SWITCH CASE to do something
         } 
    
    DEFINE_FUNCTION fnRun_SecondEvents(INTEGER iSecond)      //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {//uncomment call in fnUpdate_Time()
         STACK_VAR INTEGER cCopySecond ;
         
         cCopySecond  = iSecond ;
         //SELECT ACTIVE or SWITCH CASE to do something
         }
         
    DEFINE_FUNCTION fnRun_StartUpCode() 
    
         {
         LOCAL_VAR CLKMGR_TIMEOFFSET_STRUCT sMyCLKMGR_TIMEOFFSET ;
         
         fnTime_DeBug("'fnRunStartUpCode(): Run Start Up! :DEBUG <',ITOA(__LINE__),'>'") ;
         nTimeWait_Force = 1 ;
         
         sMyDST.nObserved = DST_OBSERVED ;
         
         if(sMyDST.nObserved)
    	  {
    	  sMyDST.nOffset	= DST_OFFSET ;
    	  sMyDST.nBEG_Month	= DST_BEGIN_MONTH ;
    	  sMyDST.nEND_Month     = DST_END_MONTH ;
    	  sMyDST.nBEG_Day       = DST_BEGIN_DAY ;
    	  sMyDST.nEND_Day 	= DST_END_DAY ;
    	  sMyDST.nBEG_DAY_Occurance = DST_BEGINDAY_OCCURANCE ;
    	  sMyDST.nEND_DAY_Occurance = DST_ENDDAY_OCCURANCE ;
    	  }     
         
         WAIT 300//3000
    	  {
    	  fnTime_DeBug("'fnRunStartUpCode(): Run Start Up Wait items! :DEBUG <',ITOA(__LINE__),'>'") ;
    	  CLKMGR_SET_CLK_SOURCE(CLKMGR_MODE_NETWORK) ; // or CLKMGR_MODE_STANDALONE
    	  CLKMGR_SET_ACTIVE_TIMESERVER (CLKMGR_ACTIVESERVER) ;
    	  CLKMGR_SET_TIMEZONE(CLKMGR_TIME_ZONE) ;
    	  CLKMGR_SET_DAYLIGHTSAVINGS_MODE(TRUE) ;
    	  CLKMGR_SET_START_DAYLIGHTSAVINGS_RULE("'occurrence:',itoa(DST_BEGINDAY_OCCURANCE),',',itoa(DST_BEGIN_DAY),',',itoa(DST_BEGIN_MONTH),',',DST_BEGIN_TIME" ) ;
    	  CLKMGR_SET_END_DAYLIGHTSAVINGS_RULE("'occurrence:',itoa(DST_ENDDAY_OCCURANCE),',',itoa(DST_END_DAY),',',itoa(DST_END_MONTH),',',DST_END_TIME" ) ;
    	  CLKMGR_SET_RESYNC_PERIOD(CLKMGR_RESYNC_PERIOD) ;
    	  
    	  sMyCLKMGR_TIMEOFFSET.HOURS   = CLKMGR_DST_OFFSET_HR ;
    	  sMyCLKMGR_TIMEOFFSET.MINUTES = CLKMGR_DST_OFFSET_MIN ;
    	  sMyCLKMGR_TIMEOFFSET.SECONDS = CLKMGR_DST_OFFSET_SEC ;
    	  CLKMGR_SET_DAYLIGHTSAVINGS_OFFSET(sMyCLKMGR_TIMEOFFSET) ;
    	  
    	  nTimeWait_Force = 2 ;
    	  fnUpdate_Time() ;
    	  }
         }
         
    DEFINE_FUNCTION fnUpdate_Time()
         
         {
         if(sMyTime.nSecond != TYPE_CAST(TIME_TO_SECOND(TIME)) || nTimeWait_Force)
    	  {
    	  sMyTime.nSecond = TYPE_CAST(TIME_TO_SECOND(TIME)) ;
    	  sMyTime.c24Time = TIME ;
    	  
    	  if(sMyTime.nMinute != TYPE_CAST(TIME_TO_MINUTE(TIME)) || nTimeWait_Force)
    	       {
    	       sMyTime.nMinute = TYPE_CAST(TIME_TO_MINUTE(TIME)) ;
    	       sMyTime.nTime = (((sMyTime.n24Hour * 60) + sMyTime.nMinute) * 60) ;
    	       if(sMyTime.nTime >= sMyTime.nSunriseTime && sMyTime.nTime < sMyTime.nSunsetTime)
    		    {
    		    sMyTime.nDaytime = 1 ;
    		    }
    	       else
    		    {
    		    sMyTime.nDaytime = 0 ;
    		    }			 
    	       if(sMyTime.n24Hour != TYPE_CAST(TIME_TO_HOUR(TIME)) || nTimeWait_Force)
    		    {
    		    sMyTime.n24Hour = TYPE_CAST(TIME_TO_HOUR(TIME)) ;
    		    if(sMyTime.n24Hour >= 12)
    			 {
    			 sMyTime.cAMPM = 'PM' ;
    			 sMyTime.nTimePM_flag = 1 ;
    			 if(sMyTime.n24Hour > 12)
    			      {
    			      sMyTime.n12Hour = sMyTime.n24Hour - 12 ;
    			      }
    			 }
    		    else
    			 {
    			 sMyTime.cAMPM = 'AM' ;
    			 sMyTime.nTimePM_flag = 0 ;
    			 sMyTime.n12Hour = sMyTime.n24Hour ;
    			 }
    		    sMyTime.c12Time = "itoa(sMyTime.n12Hour),':',RIGHT_STRING("'0',itoa(sMyTime.nMinute)",2),':',RIGHT_STRING("'0',itoa(sMyTime.nSecond)",2)" ;
    		    if(sMyTime.nDayOfMonth != TYPE_CAST(DATE_TO_DAY(LDATE)) || nTimeWait_Force)
    			 {
    			 sMyTime.nDayOfMonth = TYPE_CAST(DATE_TO_DAY(LDATE)) ;
    			 sMyTime.nDayOfWeek = TYPE_CAST(DAY_OF_WEEK(LDATE)) ;
    			 if(sMyTime.nMonth != TYPE_CAST(DATE_TO_MONTH(LDATE)))//no force here.  would erase stored values.
    			      {
    			      sMyTime.nMonth = TYPE_CAST(DATE_TO_MONTH(LDATE)) ;
    			      if(sMyTime.nYear != TYPE_CAST(DATE_TO_YEAR(LDATE)))
    				   {
    				   sMyTime.nYear = TYPE_CAST(DATE_TO_YEAR(LDATE)) ;
    				   }
    			      }
    			 if(sMyTime.cDay != Day || nTimeWait_Force)
    			      {
    			      sMyTime.cDay = Day ;
    			      if (sMyTime.cDate != Date)
    				   {
    				   sMyTime.cDate = Date ;
    				   }
    			      }
    			 fnGet_DayOccurrences() ;
    			 fnRun_D_O_W_Events(sMyTime.nDayOfWeek) ;
    			 fnRun_D_O_M_Events(sMyTime.nDayOfMonth) ;
    			 fnRun_DailyEvents() ;
    			 }
    		    fnRun_HourEvents(sMyTime.n24Hour) ;
    		    }
    	       else
    		    {
    		    sMyTime.c12Time = "itoa(sMyTime.n12Hour),':',RIGHT_STRING("'0',itoa(sMyTime.nMinute)",2),':',RIGHT_STRING("'0',itoa(sMyTime.nSecond)",2)" ;
    		    }
    	       fnRun_MinuteEvents(sMyTime.nMinute) ;
    	       }
    	  else
    	       {
    	       sMyTime.c12Time = "itoa(sMyTime.n12Hour),':',RIGHT_STRING("'0',itoa(sMyTime.nMinute)",2),':',RIGHT_STRING("'0',itoa(sMyTime.nSecond)",2)" ;
    	       }
    	  fnRun_SecondEvents(sMyTime.nSecond) ;
    	  //fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_TIME) ; //IF ANYTHING NEEDS TIME UPDATES
    	  }
         nTimeWait_Force  = 0 ;
         }
              
    DEFINE_START    //RUN START UP CODE
         
    fnRun_StartUpCode() ;
         
    DEFINE_PROGRAM  //CALL fnUpdate_Time() if you don't have a timeline elsewhere to call it every second
    (*
    WAIT 10
         {
         fnUpdate_Time() ;
         }
    *)
    
  • mpullinmpullin Posts: 949
    jjames wrote: »
    What about putting your code within a wait that's executed every 58 or 59 seconds? This way it's not evaluated every "tick".
    Yes, that is another solution, you have to set a flag though, like bob did, so that the event doesn't fire twice, say, at 12:34:01 and 12:34:59.
  • ericmedleyericmedley Posts: 4,177
    I do all my feedback and timed events like this in a timeline myself and don't put anything in mainline anymore. (DEFINE_PROGRAM) My mainlines are pretty much empty.

    I tend to give advice on the forum with the 'theory' or 'concept of how to do something' in mind. At no time should anyone take any of my code seriously. I certainly don't.
  • jjamesjjames Posts: 2,908
    ericmedley wrote: »
    I tend to give advice on the forum with the 'theory' or 'concept of how to do something' in mind. At no time should anyone take any of my code seriously. I certainly don't.
    Where's the "Like" button?
  • Time

    I had a similar situation, I needed to power off the projectors at certain times, either you can read this link or try this code. I know it works and is a simple code, just do not forget to put wait, because if you don't it will send the same string like 10000 time and it might freeze the device, like in my case the projector would go into stand by.

    http://www.amxforums.com/showthread.php?3470-Commands-at-Certain-Times&highlight=COMPARE_STRING

    (***********************************************************)
    (* THE ACTUAL PROGRAM GOES BELOW *)
    (***********************************************************)
    DEFINE_PROGRAM

    (******************Shutdown**************)
    IF (TIME='22:30:00')
    {
    wait 11
    SEND_STRING dvProjector,"$02,'POF',$03";
    }
    (***********************************************************)
    (* END OF PROGRAM *)
    (* DO NOT PUT ANY CODE BELOW THIS COMMENT *)
    (***********************************************************)
  • the8thstthe8thst Posts: 470
    This is what I do:
    DEFINE_PROGRAM
    
    	wait 45 'vstat_time_compare_wait' {
    		IF (compare_string(time, cVstatTime)) {
    			CANCEL_WAIT 'REINIT VSTATS'
    			
    			WAIT 50 'REINIT VSTATS' {
    				HVAC_Auto_Refresh()
    			}
    		}
    	}
    
Sign In or Register to comment.