mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
[automation] AbstractScriptModuleHandler: Inject module type ID into context (#5054)
* [automation] AbstractScriptModuleHandler: Inject module type ID into context Signed-off-by: Florian Hotze <dev@florianhotze.com>
This commit is contained in:
+17
-4
@@ -51,10 +51,15 @@ public abstract class AbstractScriptModuleHandler<T extends Module> extends Base
|
||||
private final Logger logger = LoggerFactory.getLogger(AbstractScriptModuleHandler.class);
|
||||
|
||||
/** Constant defining the configuration parameter of modules that specifies the mime type of a script */
|
||||
public static final String SCRIPT_TYPE = "type";
|
||||
public static final String CONFIG_SCRIPT_TYPE = "type";
|
||||
|
||||
/** Constant defining the configuration parameter of modules that specifies the script itself */
|
||||
public static final String SCRIPT = "script";
|
||||
public static final String CONFIG_SCRIPT = "script";
|
||||
|
||||
/**
|
||||
* Constant defining the context key of the module type id.
|
||||
*/
|
||||
public static final String CONTEXT_KEY_MODULE_TYPE_ID = "oh.module-type-id";
|
||||
|
||||
protected final ScriptEngineManager scriptEngineManager;
|
||||
|
||||
@@ -73,8 +78,8 @@ public abstract class AbstractScriptModuleHandler<T extends Module> extends Base
|
||||
this.ruleUID = ruleUID;
|
||||
this.engineIdentifier = UUID.randomUUID().toString();
|
||||
|
||||
this.type = getValidConfigParameter(SCRIPT_TYPE, module.getConfiguration(), module.getId(), false);
|
||||
this.script = getValidConfigParameter(SCRIPT, module.getConfiguration(), module.getId(), true);
|
||||
this.type = getValidConfigParameter(CONFIG_SCRIPT_TYPE, module.getConfiguration(), module.getId(), false);
|
||||
this.script = getValidConfigParameter(CONFIG_SCRIPT, module.getConfiguration(), module.getId(), true);
|
||||
}
|
||||
|
||||
private static String getValidConfigParameter(String parameter, Configuration config, String moduleId,
|
||||
@@ -135,6 +140,13 @@ public abstract class AbstractScriptModuleHandler<T extends Module> extends Base
|
||||
return ruleUID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the type identifier of this module handler
|
||||
*
|
||||
* @return the type identifier
|
||||
*/
|
||||
abstract public String getTypeId();
|
||||
|
||||
/**
|
||||
* Gets the script engine identifier for this module
|
||||
*
|
||||
@@ -194,6 +206,7 @@ public abstract class AbstractScriptModuleHandler<T extends Module> extends Base
|
||||
contextNew.put(key, value);
|
||||
}
|
||||
contextNew.put("ruleUID", this.ruleUID);
|
||||
contextNew.put(CONTEXT_KEY_MODULE_TYPE_ID, this.getTypeId());
|
||||
executionContext.setAttribute("ctx", contextNew, ScriptContext.ENGINE_SCOPE);
|
||||
|
||||
// add the single contextNew entries to the scope
|
||||
|
||||
+5
@@ -65,6 +65,11 @@ public class ScriptActionHandler extends AbstractScriptModuleHandler<Action> imp
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeId() {
|
||||
return TYPE_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile() throws ScriptException {
|
||||
super.compileScript();
|
||||
|
||||
+5
@@ -46,6 +46,11 @@ public class ScriptConditionHandler extends AbstractScriptModuleHandler<Conditio
|
||||
super(module, ruleUID, scriptEngineManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeId() {
|
||||
return TYPE_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compile() throws ScriptException {
|
||||
super.compileScript();
|
||||
|
||||
+4
-4
@@ -119,8 +119,8 @@ public class DSLRuleProviderTest extends JavaOSGiTest {
|
||||
assertThat(firstRule.getName(), is("RuleNumberOne"));
|
||||
assertThat(firstRule.getTriggers().getFirst().getTypeUID(), is(SystemTriggerHandler.STARTLEVEL_MODULE_TYPE_ID));
|
||||
assertThat(firstRule.getActions().getFirst().getTypeUID(), is(ScriptActionHandler.TYPE_ID));
|
||||
assertThat(firstRule.getActions().getFirst().getConfiguration().get(AbstractScriptModuleHandler.SCRIPT_TYPE),
|
||||
is(DSLScriptEngine.MIMETYPE_OPENHAB_DSL_RULE));
|
||||
assertThat(firstRule.getActions().getFirst().getConfiguration()
|
||||
.get(AbstractScriptModuleHandler.CONFIG_SCRIPT_TYPE), is(DSLScriptEngine.MIMETYPE_OPENHAB_DSL_RULE));
|
||||
|
||||
Rule secondRule = it.next();
|
||||
|
||||
@@ -130,8 +130,8 @@ public class DSLRuleProviderTest extends JavaOSGiTest {
|
||||
assertThat(secondRule.getTriggers().getFirst().getConfiguration().get(ItemStateTriggerHandler.CFG_ITEMNAME),
|
||||
is("X"));
|
||||
assertThat(secondRule.getActions().getFirst().getTypeUID(), is(ScriptActionHandler.TYPE_ID));
|
||||
assertThat(secondRule.getActions().getFirst().getConfiguration().get(AbstractScriptModuleHandler.SCRIPT_TYPE),
|
||||
is(DSLScriptEngine.MIMETYPE_OPENHAB_DSL_RULE));
|
||||
assertThat(secondRule.getActions().getFirst().getConfiguration()
|
||||
.get(AbstractScriptModuleHandler.CONFIG_SCRIPT_TYPE), is(DSLScriptEngine.MIMETYPE_OPENHAB_DSL_RULE));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user