Skip to content

Commit

Permalink
add event trigger for channel
Browse files Browse the repository at this point in the history
  • Loading branch information
querdenker2k authored and seaside1 committed Oct 14, 2022
1 parent 3cb5c65 commit 3ed80ee
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jrule.internal.engine;

import java.lang.reflect.Method;

import org.openhab.automation.jrule.rules.JRule;
import org.openhab.automation.jrule.rules.JRulePrecondition;

import java.lang.reflect.Method;

/**
* The {@link JRuleChannelExecutionContext}
*
* @author Robert Delbrück - Initial contribution
*/
public class JRuleChannelExecutionContext extends JRuleExecutionContext {
private final String channel;
private final String event;

public JRuleChannelExecutionContext(JRule jRule, String logName, String[] loggingTags, String ruleName, Method method, boolean eventParameterPresent, JRulePrecondition[] preconditions, String channel, String event) {
public JRuleChannelExecutionContext(JRule jRule, String logName, String[] loggingTags, String ruleName,
Method method, boolean eventParameterPresent, JRulePrecondition[] preconditions, String channel,
String event) {
super(jRule, logName, loggingTags, ruleName, method, eventParameterPresent, preconditions);
this.channel = channel;
this.event = event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ private synchronized void clearTimers() {
timers.clear();
}

private void logInfo(String message, Object... paramteres) {
JRuleLog.info(logger, LOG_NAME_ENGINE, message, paramteres);
private void logInfo(String message, Object... parameters) {
JRuleLog.info(logger, LOG_NAME_ENGINE, message, parameters);
}

private void logDebug(String message, Object... parameters) {
Expand Down Expand Up @@ -228,10 +228,9 @@ public void add(JRule jRule) {
JRuleLog.info(logger, logName, "Validating JRule item: {} trigger: {} ", jRuleWhen.item(),
jRuleWhen.trigger());
addItemExecutionContext(jRule, logName, loggingTags, jRuleName.value(), jRuleWhen.trigger(),
jRuleWhen.update(), jRuleWhen.item(), method,
jRuleEventPresent,
getStringFromAnnotation(jRuleWhen.eq()),
getStringFromAnnotation(jRuleWhen.neq()), preconditions);
jRuleWhen.update(), jRuleWhen.item(), method, jRuleEventPresent,
getStringFromAnnotation(jRuleWhen.eq()), getStringFromAnnotation(jRuleWhen.neq()),
preconditions);
itemNames.add(jRuleWhen.item());

ruleLoadingStatistics.addItemStateTrigger();
Expand All @@ -248,9 +247,8 @@ public void add(JRule jRule) {
// JRuleWhen for a channel
JRuleLog.info(logger, logName, "Validating JRule channel: {} trigger: {} ", jRuleWhen.channel(),
jRuleWhen.trigger());
addChannelExecutionContext(jRule, logName, loggingTags, jRuleWhen.channel(), jRuleWhen.event(), jRuleName.value(),
method, jRuleEventPresent,
preconditions);
addChannelExecutionContext(jRule, logName, loggingTags, jRuleWhen.channel(), jRuleWhen.event(),
jRuleName.value(), method, jRuleEventPresent, preconditions);
ruleLoadingStatistics.addChannelTrigger();
}
}
Expand Down Expand Up @@ -322,8 +320,8 @@ private synchronized void addTimedExecution(JRule jRule, String logName, String[
timers.add(future);
JRuleLog.info(logger, logName, "Scheduling timer for rule: {} hours: {} minutes: {} seconds: {} cron: {}",
jRuleWhen.hours(), jRuleWhen.minutes(), jRuleWhen.seconds(), jRuleWhen.cron());
JRuleTimedExecutionContext executionContext = new JRuleTimedExecutionContext(jRule, logName, loggingTags, method,
jRuleName, jRuleEventPresent, preconditions);
JRuleTimedExecutionContext executionContext = new JRuleTimedExecutionContext(jRule, logName, loggingTags,
method, jRuleName, jRuleEventPresent, preconditions);
Consumer<Void> consumer = t -> {
try {
invokeRule(executionContext, jRuleEventPresent ? new JRuleEvent("") : null);
Expand All @@ -341,26 +339,25 @@ private synchronized void addTimedExecution(JRule jRule, String logName, String[
}
}

private void addItemExecutionContext(JRule jRule, String logName, String[] loggingTags,
String ruleName, String trigger, String update, String itemName, Method method,
boolean eventParameterPresent, String eq, String neq,
JRulePrecondition[] preconditions) {
private void addItemExecutionContext(JRule jRule, String logName, String[] loggingTags, String ruleName,
String trigger, String update, String itemName, Method method, boolean eventParameterPresent, String eq,
String neq, JRulePrecondition[] preconditions) {
List<JRuleItemExecutionContext> contextList = itemToExecutionContexts.computeIfAbsent(itemName,
k -> new ArrayList<>());
final JRuleItemExecutionContext context = new JRuleItemExecutionContext(jRule, logName, loggingTags, trigger,
null, null, update, ruleName, null, null, method, eventParameterPresent, null, null, null, null, eq, neq,
preconditions);
null, null, update, ruleName, null, null, method, eventParameterPresent, null, null, null, null, eq,
neq, preconditions);
JRuleLog.debug(logger, logName, "ItemContextList add context: {}", context);
contextList.add(context);
}

private void addChannelExecutionContext(JRule jRule, String logName, String[] loggingTags, String channel,
String event, String ruleName, Method method, boolean eventParameterPresent,
JRulePrecondition[] preconditions) {
String event, String ruleName, Method method, boolean eventParameterPresent,
JRulePrecondition[] preconditions) {
List<JRuleChannelExecutionContext> contextList = channelToExecutionContexts.computeIfAbsent(channel,
k -> new ArrayList<>());
final JRuleChannelExecutionContext context = new JRuleChannelExecutionContext(jRule, logName, loggingTags, ruleName, method, eventParameterPresent,
preconditions, channel, event);
final JRuleChannelExecutionContext context = new JRuleChannelExecutionContext(jRule, logName, loggingTags,
ruleName, method, eventParameterPresent, preconditions, channel, event);
JRuleLog.debug(logger, logName, "ChannelContextList add context: {}", context);
contextList.add(context);
}
Expand All @@ -384,13 +381,12 @@ private void handleChannelEvent(ChannelTriggeredEvent channelEvent) {
logDebug("No execution context for channelEvent: {}", channelEvent);
return;
}
executionContexts.stream()
.filter(context -> context.getChannel().equals(channelEvent.getChannel().toString()))
.filter(context -> context.getEvent().equals(channelEvent.getEvent()))
.forEach(context -> {
JRuleLog.debug(logger, context.getLogName(), "invoke when context matches");
invokeRule(context, new JRuleEvent(channelEvent.getEvent(), channelEvent.getChannel().toString(), channelEvent.getEvent()));
});
executionContexts.stream().filter(context -> context.getChannel().equals(channelEvent.getChannel().toString()))
.filter(context -> context.getEvent().equals(channelEvent.getEvent())).forEach(context -> {
JRuleLog.debug(logger, context.getLogName(), "invoke when context matches");
invokeRule(context, new JRuleEvent(channelEvent.getEvent(), channelEvent.getChannel().toString(),
channelEvent.getEvent()));
});
}

private void handleEventUpdate(Event event) {
Expand Down Expand Up @@ -552,12 +548,12 @@ private Object invokeRuleInternal(JRuleExecutionContext context, JRuleEvent even
MDC.put(MDC_KEY_RULE, context.getRuleName());
Arrays.stream(context.getLoggingTags()).forEach(s -> MDC.put(s, s));
return context.isEventParameterPresent() ? method.invoke(rule, event) : method.invoke(rule);
} catch (IllegalAccessException | IllegalArgumentException | SecurityException e) {
JRuleLog.error(logger, context.getRuleName(), "Error {}", e);
} catch (InvocationTargetException e) {
Throwable ex = e.getCause() != null ? e.getCause() : null;
JRuleLog.error(logger, context.getRuleName(), "Error message: {}", ex.getMessage());
JRuleLog.error(logger, context.getRuleName(), "Error Stacktrace: {}", getStackTraceAsString(ex));
} catch (Exception e) {
JRuleLog.error(logger, context.getRuleName(), "Error {}", e);
} finally {
Arrays.stream(context.getLoggingTags()).forEach(MDC::remove);
MDC.remove(MDC_KEY_RULE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
* <p>
*
* See the NOTICE file(s) distributed with this work for additional
* information.
* <p>
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
* <p>
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jrule.internal.engine;
Expand Down Expand Up @@ -36,10 +36,8 @@ public JRulePrecondition[] getPreconditions() {

private final JRulePrecondition[] preconditions;

public JRuleExecutionContext(JRule jRule, String logName, String[] loggingTags,
String ruleName, Method method,
boolean eventParameterPresent,
JRulePrecondition[] preconditions) {
public JRuleExecutionContext(JRule jRule, String logName, String[] loggingTags, String ruleName, Method method,
boolean eventParameterPresent, JRulePrecondition[] preconditions) {
this.logName = logName;
this.loggingTags = loggingTags;
this.jRule = jRule;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jrule.internal.engine;

import java.lang.reflect.Method;

import org.openhab.automation.jrule.rules.JRule;
import org.openhab.automation.jrule.rules.JRulePrecondition;

import java.lang.reflect.Method;

/**
* The {@link JRuleItemExecutionContext}
*
* @author Robert Delbrück - Initial contribution
*/
public class JRuleItemExecutionContext extends JRuleExecutionContext {
private static final String FROM_PREFIX = " from ";
private static final String TO_PREFIX = " to ";
Expand All @@ -23,7 +40,10 @@ public class JRuleItemExecutionContext extends JRuleExecutionContext {
protected final String eq;
protected final String neq;

public JRuleItemExecutionContext(JRule jRule, String logName, String[] loggingTags, String trigger, String from, String to, String update, String ruleName, String itemClass, String itemName, Method method, boolean eventParameterPresent, Double lt, Double lte, Double gt, Double gte, String eq, String neq, JRulePrecondition[] preconditions) {
public JRuleItemExecutionContext(JRule jRule, String logName, String[] loggingTags, String trigger, String from,
String to, String update, String ruleName, String itemClass, String itemName, Method method,
boolean eventParameterPresent, Double lt, Double lte, Double gt, Double gte, String eq, String neq,
JRulePrecondition[] preconditions) {
super(jRule, logName, loggingTags, ruleName, method, eventParameterPresent, preconditions);
this.itemClass = itemClass;
this.itemName = itemName;
Expand Down Expand Up @@ -89,11 +109,8 @@ private String buildFromToString(String trigger, String from, String to) {
return builder.toString();
}


private String buildUpdateString(String trigger, String update) {
return trigger +
SPACE +
update;
return trigger + SPACE + update;
}

public String getTriggerFullString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jrule.internal.engine;

import java.lang.reflect.Method;

import org.openhab.automation.jrule.rules.JRule;
import org.openhab.automation.jrule.rules.JRulePrecondition;

import java.lang.reflect.Method;

/**
* The {@link JRuleTimedExecutionContext}
*
* @author Robert Delbrück - Initial contribution
*/
public class JRuleTimedExecutionContext extends JRuleExecutionContext {
public JRuleTimedExecutionContext(JRule jRule, String logName, String[] loggingTags, Method method, String ruleName, boolean jRuleEventPresent, JRulePrecondition[] preconditions) {
public JRuleTimedExecutionContext(JRule jRule, String logName, String[] loggingTags, Method method, String ruleName,
boolean jRuleEventPresent, JRulePrecondition[] preconditions) {
super(jRule, logName, loggingTags, ruleName, method, jRuleEventPresent, preconditions);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
* <p>
*
* See the NOTICE file(s) distributed with this work for additional
* information.
* <p>
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
* <p>
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jrule.internal.handler;
Expand Down Expand Up @@ -588,5 +588,4 @@ private void logWarn(String message, Object... parameters) {
private void logError(String message, Object... parameters) {
JRuleLog.error(logger, LOG_NAME_EVENT, message, parameters);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public String getChannel() {

@Override
public String toString() {
return String.format("JRuleEvent [state=%s, oldState=%s, memberName=%s, itemName=%s, channel=%s, event=%s]", state,
oldState, memberName, itemName, channel);
return String.format("JRuleEvent [state=%s, oldState=%s, memberName=%s, itemName=%s, channel=%s, event=%s]",
state, oldState, memberName, itemName, channel);
}
}

0 comments on commit 3ed80ee

Please sign in to comment.