我正在尝试使用 JSR223 ( https://github.com/openhab/openhab/wiki/Jsr223-Script-Engine )在 Javascript 中为 openHAB ( http://www.openhab.org/ ) 实施规则。
有人对以下异常的根本原因有建议吗?请注意,作为参数传递的两个实例都实现了在方法声明中用作参数的接口。
java.lang.RuntimeException: java.lang.NoSuchMethodException: None of the fixed arity signatures [(org.openhab.core.items.Item, org.joda.time.base.AbstractInstant)] of method org.openhab.core.persistence.extensions.PersistenceExtensions.changedSince match the argument types [org.openhab.core.items.GroupItem, org.joda.time.DateTime]
at jdk.nashorn.javaadapters.java.util.function.Consumer.accept(Unknown Source) ~[na:na]
at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_31]
at jdk.nashorn.internal.scripts.Script$\^eval\_.L:13(<eval>:14) ~[na:na]
at org.openhab.core.jsr223.internal.shared.Rule$$NashornJavaAdapter.execute(Unknown Source) ~[na:na]
at org.openhab.core.jsr223.internal.engine.RuleExecutionRunnable.run(RuleExecutionRunnable.java:36) ~[na:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_31]
以下是实现的脚本:
'use strict';
load("nashorn:mozilla_compat.js");
importPackage(org.openhab.core.jsr223.internal.shared);
importPackage(org.joda.time);
importPackage(org.joda.time.base);
var autoOffRule = new org.openhab.core.jsr223.internal.shared.Rule() {
getEventTrigger: function() {
return [
new org.openhab.core.jsr223.internal.shared.TimerTrigger("* * * * * ?")
];
},
execute: function(event) {
for each(var item in ItemRegistry.getItems()) {
if (item.getState() == org.openhab.core.library.types.OnOffType.ON) {
var dateTime = org.joda.time.DateTime.now().withFieldAdded(DurationFieldType.seconds(), -5);
if (!(org.openhab.core.persistence.extensions.PersistenceExtensions.class.static.changedSince(item, var dateTime))) {
print("Auto-off for " + item.getName())
}
}
}
}
};
function getRules() {
return new org.openhab.core.jsr223.internal.shared.RuleSet([ autoOffRule ]);
}
被调用的方法被重载并具有以下签名:
org.openhab.core.persistence.extensions.PersistenceExtensions#changedSince(org.openhab.core.items.Item, org.joda.time.base.AbstractInstant)
org.openhab.core.persistence.extensions.PersistenceExtensions#changedSince(org.openhab.core.items.Item, org.joda.time.base.AbstractInstant, java.lang.String)
在 jdk1.8.0_31 和 jdk1.8.0_65 上测试并失败。碰到一个或多或少类似的异常,在 Groovy 中实现了规则。