Search in sources :

Example 1 with EventTrigger

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

the class RuleTriggerManager method internalGetThingRules.

private Iterable<Rule> internalGetThingRules(TriggerTypes triggerType, String thingUid, ThingStatus oldStatus, ThingStatus newStatus) {
    List<Rule> result = new ArrayList<>();
    Iterable<Rule> rules = getAllRules(triggerType, thingUid);
    switch(triggerType) {
        case THINGUPDATE:
            for (Rule rule : rules) {
                for (EventTrigger t : rule.getEventtrigger()) {
                    if (t instanceof ThingStateUpdateEventTrigger) {
                        ThingStateUpdateEventTrigger tt = (ThingStateUpdateEventTrigger) t;
                        if (tt.getThing().equals(thingUid)) {
                            String stateString = tt.getState();
                            if (stateString != null) {
                                ThingStatus triggerState = ThingStatus.valueOf(stateString);
                                if (!newStatus.equals(triggerState)) {
                                    continue;
                                }
                            }
                            result.add(rule);
                        }
                    }
                }
            }
            break;
        case THINGCHANGE:
            for (Rule rule : rules) {
                for (EventTrigger t : rule.getEventtrigger()) {
                    if (t instanceof ThingStateChangedEventTrigger) {
                        ThingStateChangedEventTrigger ct = (ThingStateChangedEventTrigger) t;
                        if (ct.getThing().equals(thingUid)) {
                            String oldStatusString = ct.getOldState();
                            if (oldStatusString != null) {
                                ThingStatus triggerOldState = ThingStatus.valueOf(oldStatusString);
                                if (!oldStatus.equals(triggerOldState)) {
                                    continue;
                                }
                            }
                            String newStatusString = ct.getNewState();
                            if (newStatusString != null) {
                                ThingStatus triggerNewState = ThingStatus.valueOf(newStatusString);
                                if (!newStatus.equals(triggerNewState)) {
                                    continue;
                                }
                            }
                            result.add(rule);
                        }
                    }
                }
            }
            break;
        default:
            break;
    }
    return result;
}
Also used : ThingStateUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) ArrayList(java.util.ArrayList) Rule(org.eclipse.smarthome.model.rule.rules.Rule) 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) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger)

Example 2 with EventTrigger

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

the class RuleTriggerManager method internalGetCommandRules.

private void internalGetCommandRules(String name, Boolean isGroup, List<Class<? extends Command>> acceptedCommandTypes, Command command, List<Rule> result) {
    final String mapName = (isGroup) ? GROUP_NAME_PREFIX + name : name;
    for (Rule rule : getAllRules(COMMAND, mapName)) {
        for (final EventTrigger t : rule.getEventtrigger()) {
            String triggerCommandString = null;
            if ((!isGroup) && (t instanceof CommandEventTrigger)) {
                final CommandEventTrigger ct = (CommandEventTrigger) t;
                if (ct.getItem().equals(name)) {
                    triggerCommandString = ct.getCommand();
                } else {
                    continue;
                }
            } else if ((isGroup) && (t instanceof GroupMemberCommandEventTrigger)) {
                final GroupMemberCommandEventTrigger gmct = (GroupMemberCommandEventTrigger) t;
                if (gmct.getGroup().equals(name)) {
                    triggerCommandString = gmct.getCommand();
                } else {
                    continue;
                }
            } else {
                continue;
            }
            if (triggerCommandString != null) {
                final Command triggerCommand = TypeParser.parseCommand(acceptedCommandTypes, triggerCommandString);
                if (!command.equals(triggerCommand)) {
                    continue;
                }
            }
            result.add(rule);
        }
    }
}
Also used : GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) Command(org.eclipse.smarthome.core.types.Command) Rule(org.eclipse.smarthome.model.rule.rules.Rule) GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) CommandEventTrigger(org.eclipse.smarthome.model.rule.rules.CommandEventTrigger) 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)

Example 3 with EventTrigger

use of org.eclipse.smarthome.model.rule.rules.EventTrigger 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 4 with EventTrigger

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

the class RuleTriggerManager method internalGetUpdateRules.

private void internalGetUpdateRules(String name, Boolean isGroup, List<Class<? extends State>> acceptedDataTypes, State state, List<Rule> result) {
    final String mapName = (isGroup) ? GROUP_NAME_PREFIX + name : name;
    for (Rule rule : getAllRules(UPDATE, mapName)) {
        for (EventTrigger t : rule.getEventtrigger()) {
            String triggerStateString = null;
            if ((!isGroup) && (t instanceof UpdateEventTrigger)) {
                final UpdateEventTrigger ut = (UpdateEventTrigger) t;
                if (ut.getItem().equals(name)) {
                    triggerStateString = ut.getState();
                } else {
                    continue;
                }
            } else if ((isGroup) && (t instanceof GroupMemberUpdateEventTrigger)) {
                final GroupMemberUpdateEventTrigger gmut = (GroupMemberUpdateEventTrigger) t;
                if (gmut.getGroup().equals(name)) {
                    triggerStateString = gmut.getState();
                } else {
                    continue;
                }
            } else {
                continue;
            }
            if (triggerStateString != null) {
                final State triggerState = TypeParser.parseState(acceptedDataTypes, triggerStateString);
                if (!state.equals(triggerState)) {
                    continue;
                }
            }
            result.add(rule);
        }
    }
}
Also used : UpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger) ThingStateUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger) GroupMemberUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger) State(org.eclipse.smarthome.core.types.State) Rule(org.eclipse.smarthome.model.rule.rules.Rule) GroupMemberUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger) 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)

Example 5 with EventTrigger

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

the class RuleTriggerManager method internalGetChangeRules.

private void internalGetChangeRules(String name, Boolean isGroup, List<Class<? extends State>> acceptedDataTypes, State newState, State oldState, List<Rule> result) {
    final String mapName = (isGroup) ? GROUP_NAME_PREFIX + name : name;
    for (Rule rule : getAllRules(CHANGE, mapName)) {
        for (EventTrigger t : rule.getEventtrigger()) {
            String triggerOldStateString = null;
            String triggerNewStateString = null;
            if ((!isGroup) && (t instanceof ChangedEventTrigger)) {
                final ChangedEventTrigger ct = (ChangedEventTrigger) t;
                if (ct.getItem().equals(name)) {
                    triggerOldStateString = ct.getOldState();
                    triggerNewStateString = ct.getNewState();
                } else {
                    continue;
                }
            } else if ((isGroup) && (t instanceof GroupMemberChangedEventTrigger)) {
                final GroupMemberChangedEventTrigger gmct = (GroupMemberChangedEventTrigger) t;
                if (gmct.getGroup().equals(name)) {
                    triggerOldStateString = gmct.getOldState();
                    triggerNewStateString = gmct.getNewState();
                } else {
                    continue;
                }
            } else {
                continue;
            }
            if (triggerOldStateString != null) {
                final State triggerOldState = TypeParser.parseState(acceptedDataTypes, triggerOldStateString);
                if (!oldState.equals(triggerOldState)) {
                    continue;
                }
            }
            if (triggerNewStateString != null) {
                final State triggerNewState = TypeParser.parseState(acceptedDataTypes, triggerNewStateString);
                if (!newState.equals(triggerNewState)) {
                    continue;
                }
            }
            result.add(rule);
        }
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) Rule(org.eclipse.smarthome.model.rule.rules.Rule) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) ChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) 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)

Aggregations

ChangedEventTrigger (org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger)5 CommandEventTrigger (org.eclipse.smarthome.model.rule.rules.CommandEventTrigger)5 EventTrigger (org.eclipse.smarthome.model.rule.rules.EventTrigger)5 GroupMemberChangedEventTrigger (org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger)5 GroupMemberCommandEventTrigger (org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger)5 GroupMemberUpdateEventTrigger (org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger)5 Rule (org.eclipse.smarthome.model.rule.rules.Rule)5 ThingStateChangedEventTrigger (org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger)5 ThingStateUpdateEventTrigger (org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger)5 UpdateEventTrigger (org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger)5 State (org.eclipse.smarthome.core.types.State)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 ThingStatus (org.eclipse.smarthome.core.thing.ThingStatus)1 Command (org.eclipse.smarthome.core.types.Command)1 EventEmittedTrigger (org.eclipse.smarthome.model.rule.rules.EventEmittedTrigger)1 SystemOnShutdownTrigger (org.eclipse.smarthome.model.rule.rules.SystemOnShutdownTrigger)1 SystemOnStartupTrigger (org.eclipse.smarthome.model.rule.rules.SystemOnStartupTrigger)1