From d2db49ff67adb76bd75a6f4e03efb6b48f5b90e4 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Sun, 12 Feb 2023 11:53:51 +0100 Subject: [PATCH] [jsscripting] Extend mapping of openhab-js classes to native openHAB counterparts (#14335) Signed-off-by: Florian Hotze --- .../bnd.bnd | 4 ++-- .../internal/OpenhabGraalJSScriptEngine.java | 21 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.automation.jsscripting/bnd.bnd b/bundles/org.openhab.automation.jsscripting/bnd.bnd index e54714acb..b10e569e6 100644 --- a/bundles/org.openhab.automation.jsscripting/bnd.bnd +++ b/bundles/org.openhab.automation.jsscripting/bnd.bnd @@ -1,7 +1,7 @@ Bundle-SymbolicName: ${project.artifactId} DynamicImport-Package: * -Import-Package: org.openhab.core.automation.module.script,javax.management,javax.script,javax.xml.datatype,javax.xml.stream;version="[1.0,2)",org.osgi.framework;version="[1.8,2)",org.slf4j;version="[1.7,2)" -Require-Capability: +Import-Package: org.openhab.core.automation.module.script,org.openhab.core.items,org.openhab.core.library.types,javax.management,javax.script,javax.xml.datatype,javax.xml.stream;version="[1.0,2)",org.osgi.framework;version="[1.8,2)",org.slf4j;version="[1.7,2)" +Require-Capability: osgi.extender:= filter:="(osgi.extender=osgi.serviceloader.processor)", osgi.serviceloader:= diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java index 8bc1466df..f16272e47 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java @@ -52,6 +52,8 @@ import org.openhab.automation.jsscripting.internal.fs.ReadOnlySeekableByteArrayC import org.openhab.automation.jsscripting.internal.fs.watch.JSDependencyTracker; import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable; import org.openhab.core.automation.module.script.ScriptExtensionAccessor; +import org.openhab.core.items.Item; +import org.openhab.core.library.types.QuantityType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -101,16 +103,23 @@ public class OpenhabGraalJSScriptEngine /** Provides unlimited host access as well as custom translations from JS to Java Objects */ private static final HostAccess HOST_ACCESS = HostAccess.newBuilder(HostAccess.ALL) // Translate JS-Joda ZonedDateTime to java.time.ZonedDateTime - .targetTypeMapping(Value.class, ZonedDateTime.class, (v) -> v.hasMember("withFixedOffsetZone"), v -> { - return ZonedDateTime.parse(v.invokeMember("withFixedOffsetZone").invokeMember("toString").asString()); - }, HostAccess.TargetMappingPrecedence.LOW) + .targetTypeMapping(Value.class, ZonedDateTime.class, v -> v.hasMember("withFixedOffsetZone"), + v -> ZonedDateTime.parse(v.invokeMember("withFixedOffsetZone").invokeMember("toString").asString()), + HostAccess.TargetMappingPrecedence.LOW) // Translate JS-Joda Duration to java.time.Duration .targetTypeMapping(Value.class, Duration.class, // picking two members to check as Duration has many common function names - (v) -> v.hasMember("minusDuration") && v.hasMember("toNanos"), v -> { - return Duration.ofNanos(v.invokeMember("toNanos").asLong()); - }, HostAccess.TargetMappingPrecedence.LOW) + v -> v.hasMember("minusDuration") && v.hasMember("toNanos"), + v -> Duration.ofNanos(v.invokeMember("toNanos").asLong()), HostAccess.TargetMappingPrecedence.LOW) + + // Translate openhab-js Item to org.openhab.core.items.Item + .targetTypeMapping(Value.class, Item.class, v -> v.hasMember("rawItem"), + v -> v.getMember("rawItem").as(Item.class), HostAccess.TargetMappingPrecedence.LOW) + + // Translate openhab-js Quantity to org.openhab.core.library.types.QuantityType + .targetTypeMapping(Value.class, QuantityType.class, v -> v.hasMember("raw") && v.hasMember("toUnit"), + v -> v.getMember("raw").as(QuantityType.class), HostAccess.TargetMappingPrecedence.LOW) .build(); /** {@link Lock} synchronization of multi-thread access */