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);
}
}
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
}
}
}
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);
}
}
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;
}
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;
}
Aggregations