//Searching for reactions using this trigger
boolean found = false;
while (it.hasNext()) {
Reaction reaction = it.next();
Trigger reactionTrigger = reaction.getTrigger();
//found a related reaction. This must be executed
if (trigger.equals(reactionTrigger) && !reaction.getCommands().isEmpty()) {
if (!checkAdditionalConditions(reaction)) {
LOG.log(Level.INFO,
"Additional conditions test failed in reaction {0}", reaction.toString());
return;
}
reactionTrigger.setExecuted();
found = true;
LOG.log(Level.FINE, "Try to execute reaction {0}", reaction.toString());
try {
//executes the commands in sequence (only the first sequence is used)
//if more then one sequence is needed it can be done with two reactions with the same trigger
Resolver commandResolver = new Resolver();
event.getPayload().addStatement("description",
trigger.getDescription()); //embedd the trigger description to the event payload
commandResolver.addContext("event.",
event.getPayload());
for (final Command command : reaction.getCommands()) {
if (command == null) {
continue; //skip this loop
}
if (command.getReceiver()
.equalsIgnoreCase(BehaviorManager.getMessagingChannel())) {
//this command is for an object so it needs only to know only about event parameters
Command resolvedCommand = commandResolver.resolve(command);
//doing so we bypass messaging system gaining better performances
BehaviorManager.parseCommand(resolvedCommand);
} else {
//if the event has a target object we include also object info
EnvObjectLogic targetObject =
EnvObjectPersistence.getObjectByName(event.getProperty("object.name"));
if (targetObject != null) {
commandResolver.addContext("current.",
targetObject.getExposedProperties());
commandResolver.addContext("current.",
targetObject.getExposedBehaviors());
}
final Command resolvedCommand = commandResolver.resolve(command);
//it's not a user level command for objects (eg: turn it on), it is for another kind of actuator
Command reply = busService.send(resolvedCommand); //blocking wait until executed
if (reply == null) {
LOG.log(Level.WARNING,
"Unreceived reply within given time ({0}ms) for command {1}",
new Object[]{command.getReplyTimeout(), command.getName()});
} else {
if (reply.isExecuted()) {
LOG.log(Level.FINE, "Executed succesfully {0}", command.getName());
} else {
LOG.log(Level.WARNING, "Unable to execute command{0}", command.getName());
}
}
}
}
} catch (Exception e) {
LOG.severe("Exception while merging event parameters into reaction.\n");
LOG.severe(Freedomotic.getStackTraceInfo(e));
return;
}
String info =
"Executing automation '" + reaction.toString() + "' takes "
+ (System.currentTimeMillis() - event.getCreation()) + "ms.";
LOG.info(info);
MessageEvent message = new MessageEvent(null, info);
message.setType("callout"); //display as callout on frontends