Search in sources :

Example 11 with Rule

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

the class RuleEngineImpl method executeRules.

protected synchronized void executeRules(Iterable<Rule> rules, ThingStatus oldThingStatus) {
    for (Rule rule : rules) {
        RuleEvaluationContext context = new RuleEvaluationContext();
        context.newValue(QualifiedName.create(RulesJvmModelInferrer.VAR_PREVIOUS_STATE), oldThingStatus.toString());
        executeRule(rule, context);
    }
}
Also used : Rule(org.eclipse.smarthome.model.rule.rules.Rule)

Example 12 with Rule

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

the class RuleEngineImpl method receiveCommand.

private void receiveCommand(ItemCommandEvent commandEvent) {
    if (!starting && triggerManager != null && itemRegistry != null) {
        String itemName = commandEvent.getItemName();
        Command command = commandEvent.getItemCommand();
        try {
            Item item = itemRegistry.getItem(itemName);
            Iterable<Rule> rules = triggerManager.getRules(COMMAND, item, command);
            executeRules(rules, item, command);
        } catch (ItemNotFoundException e) {
        // ignore commands for non-existent items
        }
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) Command(org.eclipse.smarthome.core.types.Command) Rule(org.eclipse.smarthome.model.rule.rules.Rule) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 13 with Rule

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

the class RuleEngineImpl method receiveThingStatus.

private void receiveThingStatus(ThingStatusInfoChangedEvent event) {
    String thingUid = event.getThingUID().getAsString();
    ThingStatus oldStatus = event.getOldStatusInfo().getStatus();
    ThingStatus newStatus = event.getStatusInfo().getStatus();
    Iterable<Rule> rules = triggerManager.getRules(THINGUPDATE, thingUid, newStatus);
    executeRules(rules);
    if (oldStatus != newStatus) {
        rules = triggerManager.getRules(THINGCHANGE, thingUid, oldStatus, newStatus);
        executeRules(rules, oldStatus);
    }
}
Also used : ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) Rule(org.eclipse.smarthome.model.rule.rules.Rule)

Example 14 with Rule

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

the class RuleTriggerManager method getRules.

/**
 * Returns all rules which have a trigger of a given type
 *
 * @param type the trigger type of the rules to return
 * @return rules with triggers of the given type
 */
public Iterable<Rule> getRules(TriggerTypes type) {
    Iterable<Rule> result;
    switch(type) {
        case STARTUP:
            result = systemStartupTriggeredRules;
            break;
        case SHUTDOWN:
            result = systemShutdownTriggeredRules;
            break;
        case TIMER:
            result = timerEventTriggeredRules;
            break;
        case UPDATE:
            result = updateEventTriggeredRules.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
            break;
        case CHANGE:
            result = changedEventTriggeredRules.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
            break;
        case COMMAND:
            result = commandEventTriggeredRules.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
            break;
        case TRIGGER:
            result = triggerEventTriggeredRules.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
            break;
        case THINGUPDATE:
            result = thingUpdateEventTriggeredRules.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
            break;
        case THINGCHANGE:
            result = thingChangedEventTriggeredRules.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
            break;
        default:
            result = new HashSet<>();
    }
    List<Rule> filteredList = new ArrayList<>();
    for (Rule rule : result) {
        // we really only want to return rules that are still loaded
        if (rule.eResource() != null && !rule.eIsProxy()) {
            filteredList.add(rule);
        }
    }
    return filteredList;
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) Rule(org.eclipse.smarthome.model.rule.rules.Rule)

Example 15 with Rule

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

the class RuleTriggerManager method internalGetRules.

private Iterable<Rule> internalGetRules(TriggerTypes triggerType, Item item, Type oldType, Type newType) {
    List<Rule> result = new ArrayList<>();
    switch(triggerType) {
        case STARTUP:
            return systemStartupTriggeredRules;
        case SHUTDOWN:
            return systemShutdownTriggeredRules;
        case TIMER:
            return timerEventTriggeredRules;
        case UPDATE:
            if (newType instanceof State) {
                List<Class<? extends State>> acceptedDataTypes = item.getAcceptedDataTypes();
                final State state = (State) newType;
                internalGetUpdateRules(item.getName(), false, acceptedDataTypes, state, result);
                for (String groupName : item.getGroupNames()) {
                    internalGetUpdateRules(groupName, true, acceptedDataTypes, state, result);
                }
            }
            break;
        case CHANGE:
            if (newType instanceof State && oldType instanceof State) {
                List<Class<? extends State>> acceptedDataTypes = item.getAcceptedDataTypes();
                final State newState = (State) newType;
                final State oldState = (State) oldType;
                internalGetChangeRules(item.getName(), false, acceptedDataTypes, newState, oldState, result);
                for (String groupName : item.getGroupNames()) {
                    internalGetChangeRules(groupName, true, acceptedDataTypes, newState, oldState, result);
                }
            }
            break;
        case COMMAND:
            if (newType instanceof Command) {
                List<Class<? extends Command>> acceptedCommandTypes = item.getAcceptedCommandTypes();
                final Command command = (Command) newType;
                internalGetCommandRules(item.getName(), false, acceptedCommandTypes, command, result);
                for (String groupName : item.getGroupNames()) {
                    internalGetCommandRules(groupName, true, acceptedCommandTypes, command, result);
                }
            }
            break;
        default:
            break;
    }
    return result;
}
Also used : Command(org.eclipse.smarthome.core.types.Command) State(org.eclipse.smarthome.core.types.State) ArrayList(java.util.ArrayList) Rule(org.eclipse.smarthome.model.rule.rules.Rule)

Aggregations

Rule (org.eclipse.smarthome.model.rule.rules.Rule)18 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 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 ArrayList (java.util.ArrayList)3 Command (org.eclipse.smarthome.core.types.Command)3 State (org.eclipse.smarthome.core.types.State)3 HashSet (java.util.HashSet)2 ThingStatus (org.eclipse.smarthome.core.thing.ThingStatus)2 Script (org.eclipse.smarthome.model.script.engine.Script)2 ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)2 Collection (java.util.Collection)1 Set (java.util.Set)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1