Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add triggeringThing name to DSL rules #2756

Merged
merged 2 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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;

Expand Down Expand Up @@ -173,6 +174,15 @@ private DefaultEvaluationContext createEvaluationContext(Script script, IEvaluat
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down