Search in sources :

Example 1 with TimerTrigger

use of org.eclipse.smarthome.model.rule.rules.TimerTrigger in project smarthome by eclipse.

the class RuleTriggerManager method addRule.

/**
 * Adds a given rule to the mapping tables
 *
 * @param rule the rule to add
 */
public synchronized void addRule(Rule rule) {
    for (EventTrigger t : rule.getEventtrigger()) {
        // add the rule to the lookup map for the trigger kind
        if (t instanceof SystemOnStartupTrigger) {
            systemStartupTriggeredRules.add(rule);
        } else if (t instanceof SystemOnShutdownTrigger) {
            systemShutdownTriggeredRules.add(rule);
        } else if (t instanceof CommandEventTrigger) {
            CommandEventTrigger ceTrigger = (CommandEventTrigger) t;
            Set<Rule> rules = commandEventTriggeredRules.get(ceTrigger.getItem());
            if (rules == null) {
                rules = new HashSet<Rule>();
                commandEventTriggeredRules.put(ceTrigger.getItem(), rules);
            }
            rules.add(rule);
        } else if (t instanceof GroupMemberCommandEventTrigger) {
            GroupMemberCommandEventTrigger gmceTrigger = (GroupMemberCommandEventTrigger) t;
            Set<Rule> rules = commandEventTriggeredRules.get(GROUP_NAME_PREFIX + gmceTrigger.getGroup());
            if (rules == null) {
                rules = new HashSet<Rule>();
                commandEventTriggeredRules.put(GROUP_NAME_PREFIX + gmceTrigger.getGroup(), rules);
            }
            rules.add(rule);
        } else if (t instanceof UpdateEventTrigger) {
            UpdateEventTrigger ueTrigger = (UpdateEventTrigger) t;
            Set<Rule> rules = updateEventTriggeredRules.get(ueTrigger.getItem());
            if (rules == null) {
                rules = new HashSet<Rule>();
                updateEventTriggeredRules.put(ueTrigger.getItem(), rules);
            }
            rules.add(rule);
        } else if (t instanceof GroupMemberUpdateEventTrigger) {
            GroupMemberUpdateEventTrigger gmueTrigger = (GroupMemberUpdateEventTrigger) t;
            Set<Rule> rules = updateEventTriggeredRules.get(GROUP_NAME_PREFIX + gmueTrigger.getGroup());
            if (rules == null) {
                rules = new HashSet<Rule>();
                updateEventTriggeredRules.put(GROUP_NAME_PREFIX + gmueTrigger.getGroup(), rules);
            }
            rules.add(rule);
        } else if (t instanceof ChangedEventTrigger) {
            ChangedEventTrigger ceTrigger = (ChangedEventTrigger) t;
            Set<Rule> rules = changedEventTriggeredRules.get(ceTrigger.getItem());
            if (rules == null) {
                rules = new HashSet<Rule>();
                changedEventTriggeredRules.put(ceTrigger.getItem(), rules);
            }
            rules.add(rule);
        } else if (t instanceof GroupMemberChangedEventTrigger) {
            GroupMemberChangedEventTrigger gmceTrigger = (GroupMemberChangedEventTrigger) t;
            Set<Rule> rules = changedEventTriggeredRules.get(GROUP_NAME_PREFIX + gmceTrigger.getGroup());
            if (rules == null) {
                rules = new HashSet<Rule>();
                changedEventTriggeredRules.put(GROUP_NAME_PREFIX + gmceTrigger.getGroup(), rules);
            }
            rules.add(rule);
        } else if (t instanceof TimerTrigger) {
            try {
                createTimer(rule, (TimerTrigger) t);
                timerEventTriggeredRules.add(rule);
            } catch (SchedulerException e) {
                logger.error("Cannot create timer for rule '{}': {}", rule.getName(), e.getMessage());
            }
        } else if (t instanceof EventEmittedTrigger) {
            EventEmittedTrigger eeTrigger = (EventEmittedTrigger) t;
            Set<Rule> rules = triggerEventTriggeredRules.get(eeTrigger.getChannel());
            if (rules == null) {
                rules = new HashSet<Rule>();
                triggerEventTriggeredRules.put(eeTrigger.getChannel(), rules);
            }
            rules.add(rule);
        } else if (t instanceof ThingStateUpdateEventTrigger) {
            ThingStateUpdateEventTrigger tsuTrigger = (ThingStateUpdateEventTrigger) t;
            Set<Rule> rules = thingUpdateEventTriggeredRules.get(tsuTrigger.getThing());
            if (rules == null) {
                rules = new HashSet<Rule>();
                thingUpdateEventTriggeredRules.put(tsuTrigger.getThing(), rules);
            }
            rules.add(rule);
        } else if (t instanceof ThingStateChangedEventTrigger) {
            ThingStateChangedEventTrigger tscTrigger = (ThingStateChangedEventTrigger) t;
            Set<Rule> rules = thingChangedEventTriggeredRules.get(tscTrigger.getThing());
            if (rules == null) {
                rules = new HashSet<Rule>();
                thingChangedEventTriggeredRules.put(tscTrigger.getThing(), rules);
            }
            rules.add(rule);
        }
    }
}
Also used : SystemOnStartupTrigger(org.eclipse.smarthome.model.rule.rules.SystemOnStartupTrigger) ThingStateUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger) HashSet(java.util.HashSet) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) SchedulerException(org.quartz.SchedulerException) GroupMemberUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) ChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) CommandEventTrigger(org.eclipse.smarthome.model.rule.rules.CommandEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) EventEmittedTrigger(org.eclipse.smarthome.model.rule.rules.EventEmittedTrigger) UpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger) ThingStateUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger) GroupMemberUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger) SystemOnShutdownTrigger(org.eclipse.smarthome.model.rule.rules.SystemOnShutdownTrigger) Rule(org.eclipse.smarthome.model.rule.rules.Rule) TimerTrigger(org.eclipse.smarthome.model.rule.rules.TimerTrigger) UpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) ThingStateUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger) EventTrigger(org.eclipse.smarthome.model.rule.rules.EventTrigger) ChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger) GroupMemberUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) CommandEventTrigger(org.eclipse.smarthome.model.rule.rules.CommandEventTrigger) HashSet(java.util.HashSet)

Example 2 with TimerTrigger

use of org.eclipse.smarthome.model.rule.rules.TimerTrigger in project smarthome by eclipse.

the class RuleTriggerManager method createTimer.

/**
 * Creates and schedules a new quartz-job and trigger with model and rule name as jobData.
 *
 * @param rule the rule to schedule
 * @param trigger the defined trigger
 *
 * @throws SchedulerException if there is an internal Scheduler error.
 */
private void createTimer(Rule rule, TimerTrigger trigger) throws SchedulerException {
    String cronExpression = trigger.getCron();
    if (trigger.getTime() != null) {
        if (trigger.getTime().equals("noon")) {
            cronExpression = "0 0 12 * * ?";
        } else if (trigger.getTime().equals("midnight")) {
            cronExpression = "0 0 0 * * ?";
        } else {
            logger.warn("Unrecognized time expression '{}' in rule '{}'", trigger.getTime(), rule.getName());
            return;
        }
    }
    String jobIdentity = getJobIdentityString(rule, trigger);
    try {
        JobDetail job = newJob(ExecuteRuleJob.class).usingJobData(ExecuteRuleJob.JOB_DATA_RULEMODEL, rule.eResource().getURI().path()).usingJobData(ExecuteRuleJob.JOB_DATA_RULENAME, rule.getName()).withIdentity(jobIdentity).build();
        Trigger quartzTrigger = newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
        scheduler.scheduleJob(job, Collections.singleton(quartzTrigger), true);
        logger.debug("Scheduled rule '{}' with cron expression '{}'", rule.getName(), cronExpression);
    } catch (RuntimeException e) {
        throw new SchedulerException(e.getMessage());
    }
}
Also used : JobDetail(org.quartz.JobDetail) EventEmittedTrigger(org.eclipse.smarthome.model.rule.rules.EventEmittedTrigger) UpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger) TimerTrigger(org.eclipse.smarthome.model.rule.rules.TimerTrigger) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) ThingStateUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger) EventTrigger(org.eclipse.smarthome.model.rule.rules.EventTrigger) Trigger(org.quartz.Trigger) ChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger) SystemOnShutdownTrigger(org.eclipse.smarthome.model.rule.rules.SystemOnShutdownTrigger) GroupMemberUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) CommandEventTrigger(org.eclipse.smarthome.model.rule.rules.CommandEventTrigger) SystemOnStartupTrigger(org.eclipse.smarthome.model.rule.rules.SystemOnStartupTrigger) SchedulerException(org.quartz.SchedulerException)

Aggregations

ChangedEventTrigger (org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger)2 CommandEventTrigger (org.eclipse.smarthome.model.rule.rules.CommandEventTrigger)2 EventEmittedTrigger (org.eclipse.smarthome.model.rule.rules.EventEmittedTrigger)2 EventTrigger (org.eclipse.smarthome.model.rule.rules.EventTrigger)2 GroupMemberChangedEventTrigger (org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger)2 GroupMemberCommandEventTrigger (org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger)2 GroupMemberUpdateEventTrigger (org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger)2 SystemOnShutdownTrigger (org.eclipse.smarthome.model.rule.rules.SystemOnShutdownTrigger)2 SystemOnStartupTrigger (org.eclipse.smarthome.model.rule.rules.SystemOnStartupTrigger)2 ThingStateChangedEventTrigger (org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger)2 ThingStateUpdateEventTrigger (org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger)2 TimerTrigger (org.eclipse.smarthome.model.rule.rules.TimerTrigger)2 UpdateEventTrigger (org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger)2 SchedulerException (org.quartz.SchedulerException)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 Rule (org.eclipse.smarthome.model.rule.rules.Rule)1 JobDetail (org.quartz.JobDetail)1 Trigger (org.quartz.Trigger)1