use of com.serotonin.timer.CronTimerTrigger in project ma-core-public by infiniteautomation.
the class DeltamationCommon method getCronTrigger.
/**
* Delta implementation of com.serotonin.m2m2.Common.getCronTrigger()
* Adds an "every" parameter and overloaded methods
*
* @param every
* @param periodType
* @param delaySeconds
* @return
*/
public static CronTimerTrigger getCronTrigger(int every, int periodType, int delaySeconds) {
// could use constraint
if (every <= 0) {
every = 1;
}
int delayMinutes = 0;
if (delaySeconds >= 60) {
delayMinutes = delaySeconds / 60;
delaySeconds %= 60;
if (delayMinutes >= 60)
delayMinutes = 59;
}
try {
switch(periodType) {
case TimePeriods.MILLISECONDS:
throw new ShouldNeverHappenException("Can't create a cron trigger for milliseconds");
case TimePeriods.SECONDS:
return new CronTimerTrigger("*/" + Integer.toString(every) + " * * * * ?");
case TimePeriods.MINUTES:
return new CronTimerTrigger(delaySeconds + " */" + Integer.toString(every) + " * * * ?");
case TimePeriods.HOURS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " */" + Integer.toString(every) + " * * ?");
case TimePeriods.DAYS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 */" + Integer.toString(every) + " * ?");
case TimePeriods.WEEKS:
// TODO cant do every on weeks
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 ? * MON");
case TimePeriods.MONTHS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 */" + Integer.toString(every) + " ?");
case TimePeriods.YEARS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 JAN ? */" + Integer.toString(every));
default:
throw new ShouldNeverHappenException("Invalid cron period type: " + periodType);
}
} catch (ParseException e) {
throw new ShouldNeverHappenException(e);
}
}
Aggregations