Search in sources :

Example 11 with CronTimerTrigger

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);
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException)

Aggregations

CronTimerTrigger (com.serotonin.timer.CronTimerTrigger)11 ParseException (java.text.ParseException)8 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)7 DateTime (org.joda.time.DateTime)4 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 User (com.serotonin.m2m2.vo.User)3 InvalidArgumentException (com.serotonin.InvalidArgumentException)2 JsonException (com.serotonin.json.JsonException)2 OneTimeTrigger (com.serotonin.timer.OneTimeTrigger)2 IOException (java.io.IOException)2 Date (java.util.Date)2 JsonArray (com.serotonin.json.type.JsonArray)1 JsonValue (com.serotonin.json.type.JsonValue)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 UserDao (com.serotonin.m2m2.db.dao.UserDao)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ReportDao (com.serotonin.m2m2.reports.ReportDao)1 ReportVO (com.serotonin.m2m2.reports.vo.ReportVO)1 TimeoutTask (com.serotonin.m2m2.util.timeout.TimeoutTask)1