Skip to content

Commit

Permalink
[jsscripting] Extend mapping of openhab-js classes to native openHAB …
Browse files Browse the repository at this point in the history
…counterparts (openhab#14335)

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 authored and SMHRambo committed Feb 14, 2023
1 parent d2238f6 commit 881e830
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions bundles/org.openhab.automation.jsscripting/bnd.bnd
Original file line number Diff line number Diff line change
@@ -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:=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
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;

Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 881e830

Please sign in to comment.