[automation] Make quantity types available in scripts (#1940)

Fixes #1763

Signed-off-by: Kai Kreuzer <kai@openhab.org>
pull/1947/head
Kai Kreuzer 2020-12-16 15:11:46 +01:00 committed by GitHub
parent 2b9f890831
commit ad189bd4f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 2 deletions

View File

@ -33,6 +33,7 @@ Import-Package: \
org.openhab.core.model.script.engine.action,\
com.google.common.base;version="14",\
javax.measure,\
javax.measure.quantity,\
org.eclipse.jdt.annotation;resolution:=optional,\
org.eclipse.xtext.xbase.lib,\
org.osgi.*,\

View File

@ -43,7 +43,8 @@ Import-Package: \
org.openhab.core.io.net.http,\
org.openhab.core.model.core,\
com.google.common.*;version="14",\
javax.measure.*,\
javax.measure,\
javax.measure.quantity,\
org.apache.*,\
org.eclipse.jdt.annotation;resolution:=optional,\
org.eclipse.jetty.http.*,\

View File

@ -35,6 +35,7 @@ public class ScriptImportSectionNamespaceScopeProvider extends XImportSectionNam
public static final QualifiedName MODEL_SCRIPT_ACTIONS_PACKAGE = QualifiedName.create("org", "openhab", "core",
"model", "script", "actions");
public static final QualifiedName TIME_PACKAGE = QualifiedName.create("java", "time");
public static final QualifiedName QUANTITY_PACKAGE = QualifiedName.create("javax", "measure", "quantity");
@Override
protected List<ImportNormalizer> getImplicitImports(boolean ignoreCase) {
@ -46,6 +47,7 @@ public class ScriptImportSectionNamespaceScopeProvider extends XImportSectionNam
implicitImports.add(doCreateImportNormalizer(CORE_PERSISTENCE_PACKAGE, true, false));
implicitImports.add(doCreateImportNormalizer(MODEL_SCRIPT_ACTIONS_PACKAGE, true, false));
implicitImports.add(doCreateImportNormalizer(TIME_PACKAGE, true, false));
implicitImports.add(doCreateImportNormalizer(QUANTITY_PACKAGE, true, false));
return implicitImports;
}

View File

@ -262,7 +262,9 @@ public class ScriptEngineOSGiTest extends JavaOSGiTest {
Item numberItem = itemRegistry.get(NUMBER_ITEM_LENGTH);
((NumberItem) numberItem).setState(new QuantityType<>("1 m"));
assertThat((QuantityType<?>) runScript("val length = NumberC.state as QuantityType; return length / 2|cm;"),
assertThat(
(QuantityType<?>) runScript(
"val length = NumberC.state as QuantityType<Length>; return length / 2|cm;"),
is(QuantityType.valueOf("50")));
}