[jsscripting] Extend mapping of openhab-js classes to native openHAB counterparts (#14335)

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze 2023-02-12 11:53:51 +01:00 committed by GitHub
parent a7b30ae3e5
commit d2db49ff67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View File

@ -1,6 +1,6 @@
Bundle-SymbolicName: ${project.artifactId} Bundle-SymbolicName: ${project.artifactId}
DynamicImport-Package: * 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)" 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: Require-Capability:
osgi.extender:= osgi.extender:=
filter:="(osgi.extender=osgi.serviceloader.processor)", filter:="(osgi.extender=osgi.serviceloader.processor)",

View File

@ -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.fs.watch.JSDependencyTracker;
import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable; import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable;
import org.openhab.core.automation.module.script.ScriptExtensionAccessor; 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.Logger;
import org.slf4j.LoggerFactory; 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 */ /** Provides unlimited host access as well as custom translations from JS to Java Objects */
private static final HostAccess HOST_ACCESS = HostAccess.newBuilder(HostAccess.ALL) private static final HostAccess HOST_ACCESS = HostAccess.newBuilder(HostAccess.ALL)
// Translate JS-Joda ZonedDateTime to java.time.ZonedDateTime // Translate JS-Joda ZonedDateTime to java.time.ZonedDateTime
.targetTypeMapping(Value.class, ZonedDateTime.class, (v) -> v.hasMember("withFixedOffsetZone"), v -> { .targetTypeMapping(Value.class, ZonedDateTime.class, v -> v.hasMember("withFixedOffsetZone"),
return ZonedDateTime.parse(v.invokeMember("withFixedOffsetZone").invokeMember("toString").asString()); v -> ZonedDateTime.parse(v.invokeMember("withFixedOffsetZone").invokeMember("toString").asString()),
}, HostAccess.TargetMappingPrecedence.LOW) HostAccess.TargetMappingPrecedence.LOW)
// Translate JS-Joda Duration to java.time.Duration // Translate JS-Joda Duration to java.time.Duration
.targetTypeMapping(Value.class, Duration.class, .targetTypeMapping(Value.class, Duration.class,
// picking two members to check as Duration has many common function names // picking two members to check as Duration has many common function names
(v) -> v.hasMember("minusDuration") && v.hasMember("toNanos"), v -> { v -> v.hasMember("minusDuration") && v.hasMember("toNanos"),
return Duration.ofNanos(v.invokeMember("toNanos").asLong()); v -> Duration.ofNanos(v.invokeMember("toNanos").asLong()), HostAccess.TargetMappingPrecedence.LOW)
}, 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(); .build();
/** {@link Lock} synchronization of multi-thread access */ /** {@link Lock} synchronization of multi-thread access */