mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
[automation] AbstractScriptModuleHandler: Remove prefixes from context entries before injecting ctx into execution context (#4919)
This aligns the keys of the injected `ctx` HashMap with the keys used when injecting the single entries of that HashMap. It will allow JS Scripting to provide event information in a native JS object in UI-based scripts similarly to how it's done for file-based scripts. Signed-off-by: Florian Hotze <dev@florianhotze.com>
This commit is contained in:
+11
-8
@@ -177,14 +177,8 @@ public abstract class AbstractScriptModuleHandler<T extends Module> extends Base
|
||||
|
||||
// Add the rule's UID to the context and make it available as "ctx".
|
||||
// Note: We don't use "context" here as it doesn't work on all JVM versions!
|
||||
final Map<String, Object> contextNew = new HashMap<>(context);
|
||||
contextNew.put("ruleUID", this.ruleUID);
|
||||
executionContext.setAttribute("ctx", contextNew, ScriptContext.ENGINE_SCOPE);
|
||||
|
||||
// Add the rule's UID to the global namespace.
|
||||
executionContext.setAttribute("ruleUID", this.ruleUID, ScriptContext.ENGINE_SCOPE);
|
||||
|
||||
// add the single context entries without their prefix to the scope
|
||||
final Map<String, Object> contextNew = new HashMap<>();
|
||||
// add the single context entries without their prefix to contextNew
|
||||
for (Entry<String, ?> entry : context.entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
String key = entry.getKey();
|
||||
@@ -192,6 +186,15 @@ public abstract class AbstractScriptModuleHandler<T extends Module> extends Base
|
||||
if (dotIndex != -1) {
|
||||
key = key.substring(dotIndex + 1);
|
||||
}
|
||||
contextNew.put(key, value);
|
||||
}
|
||||
contextNew.put("ruleUID", this.ruleUID);
|
||||
executionContext.setAttribute("ctx", contextNew, ScriptContext.ENGINE_SCOPE);
|
||||
|
||||
// add the single contextNew entries to the scope
|
||||
for (Entry<String, ?> entry : contextNew.entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
String key = entry.getKey();
|
||||
executionContext.setAttribute(key, value, ScriptContext.ENGINE_SCOPE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user