Add triggeringThing name to DSL rules (#2756)

* Add triggeringThing name to DSL rules
* Include previous and new statuses

Signed-off-by: James Melville <jamesmelville@gmail.com>
pull/2759/head
James Melville 2022-02-13 09:34:08 +00:00 committed by GitHub
parent b594d0a8b7
commit 805f223fa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 3 deletions

View File

@ -150,9 +150,13 @@ class RulesJvmModelInferrer extends ScriptJvmModelInferrer {
val channelRef = ruleModel.newTypeRef(String)
parameters += rule.toParameter(VAR_TRIGGERING_CHANNEL, channelRef)
}
if (containsThingStateChangedEventTrigger(rule) && !containsParam(parameters, VAR_PREVIOUS_STATE)) {
val stateTypeRef = ruleModel.newTypeRef(State)
parameters += rule.toParameter(VAR_PREVIOUS_STATE, stateTypeRef)
if (containsThingStateChangedEventTrigger(rule)) {
val thingRef = ruleModel.newTypeRef(String)
parameters += rule.toParameter(VAR_TRIGGERING_THING, thingRef)
val oldStatusRef = ruleModel.newTypeRef(String)
parameters += rule.toParameter(VAR_PREVIOUS_STATUS, oldStatusRef)
val newStatusRef = ruleModel.newTypeRef(String)
parameters += rule.toParameter(VAR_NEW_STATUS, newStatusRef)
}
if ((containsStateChangeTrigger(rule) || containsStateUpdateTrigger(rule)) && !containsParam(parameters, VAR_NEW_STATE)) {
val stateTypeRef = ruleModel.newTypeRef(State)

View File

@ -38,6 +38,7 @@ import org.openhab.core.model.script.engine.ScriptParsingException;
import org.openhab.core.model.script.jvmmodel.ScriptJvmModelInferrer;
import org.openhab.core.model.script.runtime.DSLScriptContextProvider;
import org.openhab.core.thing.events.ChannelTriggeredEvent;
import org.openhab.core.thing.events.ThingStatusInfoChangedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -173,6 +174,15 @@ public class DSLScriptEngine implements javax.script.ScriptEngine {
evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_TRIGGERING_ITEM_NAME),
event.getItemName());
}
if (value instanceof ThingStatusInfoChangedEvent) {
ThingStatusInfoChangedEvent event = (ThingStatusInfoChangedEvent) value;
evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_TRIGGERING_THING),
event.getThingUID().toString());
evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_PREVIOUS_STATUS),
event.getOldStatusInfo().getStatus().toString());
evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_NEW_STATUS),
event.getStatusInfo().getStatus().toString());
}
return evalContext;
}

View File

@ -61,6 +61,15 @@ class ScriptJvmModelInferrer extends AbstractModelInferrer {
/** Variable name for the triggering channel in a "trigger event" rule */
public static final String VAR_TRIGGERING_CHANNEL = "triggeringChannel";
/** Variable name for the triggering thing in a "thing status trigger" rule */
public static final String VAR_TRIGGERING_THING = "triggeringThing";
/** Variable name for the previous status of the triggering thing in a "thing status trigger" rule */
public static final String VAR_PREVIOUS_STATUS = "previousStatus";
/** Variable name for the new status of the triggering thing in a "thing status trigger" rule */
public static final String VAR_NEW_STATUS = "newStatus";
/**
* conveninence API to build and initialize JvmTypes and their members.
*/
@ -124,6 +133,12 @@ class ScriptJvmModelInferrer extends AbstractModelInferrer {
parameters += script.toParameter(VAR_RECEIVED_EVENT, eventTypeRef)
val channelRef = script.newTypeRef(String)
parameters += script.toParameter(VAR_TRIGGERING_CHANNEL, channelRef)
val thingRef = script.newTypeRef(String)
parameters += script.toParameter(VAR_TRIGGERING_THING, thingRef)
val oldThingStatusRef = script.newTypeRef(String)
parameters += script.toParameter(VAR_PREVIOUS_STATUS, oldThingStatusRef)
val newThingStatusRef = script.newTypeRef(String)
parameters += script.toParameter(VAR_NEW_STATUS, newThingStatusRef)
val stateTypeRef2 = script.newTypeRef(State)
parameters += script.toParameter(VAR_NEW_STATE, stateTypeRef2)
body = script