Search in sources :

Example 1 with OneTimeTrigger

use of com.serotonin.timer.OneTimeTrigger in project ma-modules-public by infiniteautomation.

the class MaintenanceEventRT method createTrigger.

public TimerTrigger createTrigger(boolean activeTrigger) {
    if (vo.getScheduleType() == MaintenanceEventVO.TYPE_MANUAL)
        return null;
    if (vo.getScheduleType() == MaintenanceEventVO.TYPE_CRON) {
        try {
            if (activeTrigger)
                return new CronTimerTrigger(vo.getActiveCron());
            return new CronTimerTrigger(vo.getInactiveCron());
        } catch (ParseException e) {
            // Should never happen, so wrap and rethrow
            throw new ShouldNeverHappenException(e);
        }
    }
    if (vo.getScheduleType() == MaintenanceEventVO.TYPE_ONCE) {
        DateTime dt;
        if (activeTrigger)
            dt = new DateTime(vo.getActiveYear(), vo.getActiveMonth(), vo.getActiveDay(), vo.getActiveHour(), vo.getActiveMinute(), vo.getActiveSecond(), 0);
        else
            dt = new DateTime(vo.getInactiveYear(), vo.getInactiveMonth(), vo.getInactiveDay(), vo.getInactiveHour(), vo.getInactiveMinute(), vo.getInactiveSecond(), 0);
        return new OneTimeTrigger(new Date(dt.getMillis()));
    }
    int month = vo.getActiveMonth();
    int day = vo.getActiveDay();
    int hour = vo.getActiveHour();
    int minute = vo.getActiveMinute();
    int second = vo.getActiveSecond();
    if (!activeTrigger) {
        month = vo.getInactiveMonth();
        day = vo.getInactiveDay();
        hour = vo.getInactiveHour();
        minute = vo.getInactiveMinute();
        second = vo.getInactiveSecond();
    }
    StringBuilder expression = new StringBuilder();
    expression.append(second).append(' ');
    expression.append(minute).append(' ');
    if (vo.getScheduleType() == MaintenanceEventVO.TYPE_HOURLY)
        expression.append("* * * ?");
    else {
        expression.append(hour).append(' ');
        if (vo.getScheduleType() == MaintenanceEventVO.TYPE_DAILY)
            expression.append("* * ?");
        else if (vo.getScheduleType() == MaintenanceEventVO.TYPE_WEEKLY)
            expression.append("? * ").append(weekdays[day]);
        else {
            if (day > 0)
                expression.append(day);
            else if (day == -1)
                expression.append('L');
            else
                expression.append(-day).append('L');
            if (vo.getScheduleType() == MaintenanceEventVO.TYPE_MONTHLY)
                expression.append(" * ?");
            else
                expression.append(' ').append(month).append(" ?");
        }
    }
    CronTimerTrigger cronTrigger;
    try {
        cronTrigger = new CronTimerTrigger(expression.toString());
    } catch (ParseException e) {
        // Should never happen, so wrap and rethrow
        throw new ShouldNeverHappenException(e);
    }
    return cronTrigger;
}
Also used : OneTimeTrigger(com.serotonin.timer.OneTimeTrigger) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 2 with OneTimeTrigger

use of com.serotonin.timer.OneTimeTrigger in project ma-modules-public by infiniteautomation.

the class ScheduledEventRT method createTrigger.

public TimerTrigger createTrigger(boolean activeTrigger) {
    if (!activeTrigger && !vo.isReturnToNormal())
        return null;
    if (vo.getScheduleType() == ScheduledEventVO.TYPE_CRON) {
        try {
            if (activeTrigger)
                return new CronTimerTrigger(vo.getActiveCron());
            return new CronTimerTrigger(vo.getInactiveCron());
        } catch (ParseException e) {
            // Should never happen, so wrap and rethrow
            throw new ShouldNeverHappenException(e);
        }
    }
    if (vo.getScheduleType() == ScheduledEventVO.TYPE_ONCE) {
        DateTime dt;
        if (activeTrigger)
            dt = new DateTime(vo.getActiveYear(), vo.getActiveMonth(), vo.getActiveDay(), vo.getActiveHour(), vo.getActiveMinute(), vo.getActiveSecond(), 0);
        else
            dt = new DateTime(vo.getInactiveYear(), vo.getInactiveMonth(), vo.getInactiveDay(), vo.getInactiveHour(), vo.getInactiveMinute(), vo.getInactiveSecond(), 0);
        return new OneTimeTrigger(new Date(dt.getMillis()));
    }
    int month = vo.getActiveMonth();
    int day = vo.getActiveDay();
    int hour = vo.getActiveHour();
    int minute = vo.getActiveMinute();
    int second = vo.getActiveSecond();
    if (!activeTrigger) {
        month = vo.getInactiveMonth();
        day = vo.getInactiveDay();
        hour = vo.getInactiveHour();
        minute = vo.getInactiveMinute();
        second = vo.getInactiveSecond();
    }
    StringBuilder expression = new StringBuilder();
    expression.append(second).append(' ');
    expression.append(minute).append(' ');
    if (vo.getScheduleType() == ScheduledEventVO.TYPE_HOURLY)
        expression.append("* * * ?");
    else {
        expression.append(hour).append(' ');
        if (vo.getScheduleType() == ScheduledEventVO.TYPE_DAILY)
            expression.append("* * ?");
        else if (vo.getScheduleType() == ScheduledEventVO.TYPE_WEEKLY)
            expression.append("? * ").append(weekdays[day]);
        else {
            if (day > 0)
                expression.append(day);
            else if (day == -1)
                expression.append('L');
            else
                expression.append(-day).append('L');
            if (vo.getScheduleType() == ScheduledEventVO.TYPE_MONTHLY)
                expression.append(" * ?");
            else
                expression.append(' ').append(month).append(" ?");
        }
    }
    CronTimerTrigger cronTrigger;
    try {
        cronTrigger = new CronTimerTrigger(expression.toString());
    } catch (ParseException e) {
        // Should never happen, so wrap and rethrow
        throw new ShouldNeverHappenException(e);
    }
    return cronTrigger;
}
Also used : OneTimeTrigger(com.serotonin.timer.OneTimeTrigger) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Aggregations

ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 CronTimerTrigger (com.serotonin.timer.CronTimerTrigger)2 OneTimeTrigger (com.serotonin.timer.OneTimeTrigger)2 ParseException (java.text.ParseException)2 Date (java.util.Date)2 DateTime (org.joda.time.DateTime)2