openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

Persisting TimeSeries fails for cached injection #346

Closed mherwege closed 1 week ago

mherwege commented 1 week ago

Based on https://github.com/openhab/openhab-js/pull/341, I am experimenting with persisting TimeSeries, the aim being to build this into Blockly.

In my tests, I am running build #4125, which I believe should include the updated js version (JavaScrip Scripting bundle addon version is 4.2.0.202406170328).

The following code fails on the last line (largely generated from my Blockly experiment, but running from a UI script). The first persist call with a ZonedDateTime and String serviceId works. The second call (last line) fails, and the log tells it cannot find an applicable overload function. As you can see, this code retrieves states from persistence from one item and tries to store the retrieved values to another item.

var tempList, persistedState, newTempList;

tempList = items.getItem('Local_Weather_and_Forecast_One_Call_API_Outdoor_Temperature').persistence.getAllStatesUntil(time.ZonedDateTime.now().plusWeeks(1), 'inmemory').map(v => ([v.timestamp, v.state]));
console.info('Original retrieved temperature list');
for (var persistedState_index in tempList) {
  persistedState = tempList[persistedState_index];
  console.info((['  state: ',persistedState[0],' -> ',persistedState[1]].join('')));
}
items.getItem('TemperatureCopy').persistence.persist(time.ZonedDateTime.now().minusHours(1), '0 °C', 'inmemory');
const timeSeries = new items.TimeSeries('REPLACE');
tempList.forEach(s => timeSeries.add(s[0], s[1]));
items.getItem('TemperatureCopy').persistence.persist(timeSeries, 'inmemory');

Here is the log:

16:40:37.671 [INFO ] [enhab.automation.script.ui.scratchpad] - Original retrieved temperature list
16:40:37.672 [INFO ] [enhab.automation.script.ui.scratchpad] -   state: 2024-06-18T17:00+02:00[Europe/Brussels] -> 17.99 °C
16:40:37.674 [INFO ] [enhab.automation.script.ui.scratchpad] -   state: 2024-06-18T18:00+02:00[Europe/Brussels] -> 17.59 °C
16:40:37.675 [INFO ] [enhab.automation.script.ui.scratchpad] -   state: 2024-06-18T19:00+02:00[Europe/Brussels] -> 16.83 °C
...
16:40:37.769 [INFO ] [enhab.automation.script.ui.scratchpad] -   state: 2024-06-20T13:00+02:00[Europe/Brussels] -> 20.14 °C
16:40:37.774 [INFO ] [enhab.automation.script.ui.scratchpad] -   state: 2024-06-20T14:00+02:00[Europe/Brussels] -> 21.35 °C
16:40:37.781 [INFO ] [enhab.automation.script.ui.scratchpad] -   state: 2024-06-20T15:00+02:00[Europe/Brussels] -> 21.31 °C
16:40:37.800 [ERROR] [tomation.script.javascript.scratchpad] - Failed to execute script: TypeError: invokeMember (persist) on org.openhab.core.persistence.extensions.PersistenceExtensions failed due to: no applicable overload found (overloads: [Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,org.openhab.core.types.State)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,org.openhab.core.types.TimeSeries)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,java.lang.String,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,org.openhab.core.types.TimeSeries,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,org.openhab.core.types.State,java.lang.String)]], arguments: [JavaObject[TemperatureCopy (Type=NumberItem, State=0 °C, Label=Outdoor Temperature Copy, Category=) (org.openhab.core.library.items.NumberItem)] (HostObject), DynamicObject<JSOrdinary>@50111392 (DefaultLayout), inmemory (String)])
        at <js>.#r(@openhab-globals.js:2)
        at <js>.persist(@openhab-globals.js:2)
        at <js>.:program(<eval>:13)
        at org.graalvm.polyglot.Context.eval(Context.java:399)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        ... 76 more
16:40:37.815 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'scratchpad' failed: org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (persist) on org.openhab.core.persistence.extensions.PersistenceExtensions failed due to: no applicable overload found (overloads: [Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,org.openhab.core.types.State)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,org.openhab.core.types.TimeSeries)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,java.lang.String,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,org.openhab.core.types.TimeSeries,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,org.openhab.core.types.State,java.lang.String)]], arguments: [JavaObject[TemperatureCopy (Type=NumberItem, State=0 °C, Label=Outdoor Temperature Copy, Category=) (org.openhab.core.library.items.NumberItem)] (HostObject), DynamicObject<JSOrdinary>@50111392 (DefaultLayout), inmemory (String)])

I may be doing something wrong here, but I was expecting it would be possible to create a TimeSeries to then persist it.

florian-h05 commented 1 week ago

When working on this PR, I tested all overloads and @jlaur tested as well, I wonder why it fails for you.

Have you configured the add-on to use the included library version?

jlaur commented 1 week ago

@florian-h05 - I just retested with one of my previous testcases, and it fails for me also now, but only when using library injection (I still have the manually installed openhab-js you provided previously).

The example is here: openhab/openhab-addons#16889

And the logs:

org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (persist) on org.openhab.core.persistence.extensions.PersistenceExtensions failed due to: no applicable overload found (overloads: [Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,org.openhab.core.types.TimeSeries,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,org.openhab.core.types.State)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,org.openhab.core.types.State,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,java.time.ZonedDateTime,java.lang.String,java.lang.String)], Method[public static void org.openhab.core.persistence.extensions.PersistenceExtensions.persist(org.openhab.core.items.Item,org.openhab.core.types.TimeSeries)]], arguments: [JavaObject[TotalPrice (Type=GroupItem, BaseType=NumberItem, Members=5, State=2.2856937562500000000000000000000008400000 DEF/kWh, Label=, Category=) (org.openhab.core.items.GroupItem)] (HostObject), DynamicObject<JSOrdinary>@16cf3a38 (DefaultLayout)])
    at <js>.#n(@openhab-globals.js:2) ~[?:?]
    at <js>.persist(@openhab-globals.js:2) ~[?:?]
    at <js>.:=>(test.js:35) ~[?:?]
    at <js>.:=>(@jsscripting-globals.js:167) ~[?:?]
    at com.oracle.truffle.polyglot.PolyglotFunctionProxyHandler.invoke(PolyglotFunctionProxyHandler.java:154) ~[?:?]
    at jdk.proxy1.$Proxy208.run(Unknown Source) ~[?:?]
    at org.openhab.automation.jsscripting.internal.threading.ThreadsafeTimers.lambda$1(ThreadsafeTimers.java:107) ~[?:?]
    at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$12(SchedulerImpl.java:189) ~[?:?]
    at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$1(SchedulerImpl.java:88) ~[?:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) [?:?]
    at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
    at java.lang.Thread.run(Thread.java:833) [?:?]

console.log(utils.OPENHAB_JS_VERSION); logs "5.1.0"

mherwege commented 1 week ago

I installed the addon in a fresh install and didn’t change any configuration. So I was expecting the included library version to be used. I didn’t install it externally either.

florian-h05 commented 1 week ago

and it fails for me also now, but only when using library injection

This made me find the error very quickly, I will provide a fix tomorrow.

florian-h05 commented 1 week ago

@mherwege @jlaur Have you been able to verify that the issue is fixed now?

jlaur commented 1 week ago

@mherwege @jlaur Have you been able to verify that the issue is fixed now?

I just tried with build 4131:

Unfortunately it still did not work. Does it work for you?

mherwege commented 1 week ago

I didn’t try yet, didn’t have a snapshot with the update installed so far.

florian-h05 commented 1 week ago

@jlaur Second attempt, please merge: https://github.com/openhab/openhab-addons/pull/16926

mherwege commented 6 days ago

@florian-h05 @jlaur The snapshot build still does not contain the newest openhab-js version, but I updated the bundle manually. I can confirm it now works for me with the development work done for Blocky and the test example listed here: https://github.com/openhab/openhab-webui/pull/2596#issuecomment-2186494902

florian-h05 commented 6 days ago

Great. Wrt to the snapshot: it seems Jenkins did not build a snapshot due to some issues, but I’ve read that it should work again now.

mherwege commented 6 days ago

Wrt to the snapshot: it seems Jenkins did not build a snapshot due to some issues, but I’ve read that it should work again now.

Yes, but it is still running. So it should be resolved in a bit.

florian-h05 commented 6 days ago

Build has just finished.

jlaur commented 6 days ago

For me it still doesn't work with build 4132.

florian-h05 commented 6 days ago

It worked for me when manually building the bundle while working on the openhab-js code.

jlaur commented 6 days ago

Is there anything it would help to check? Oh, now I noticed rule output of:

        console.log(utils.OPENHAB_JS_VERSION);

Result:

2024-06-24 17:52:10.497 [INFO ] [enhab.automation.script.file.test.js] - 5.1.1

This is unexpected. I had cleaned cache and tmp.

florian-h05 commented 6 days ago

This is indeed interesting. Please check what’s inside /etc/openhab/automation/js/node_modules and check the add-on settings.

jlaur commented 6 days ago

In automation/node_modules I have:

But I have configured the add-on to use injection. When flipping that setting to use above provided library I get:

2024-06-24 18:06:26.850 [INFO ] [enhab.automation.script.file.test.js] - 5.0.0
florian-h05 commented 6 days ago

Can you please remove them both?

jlaur commented 6 days ago

Sure, I just tried that, it made no difference. I also deleted cache+tmp before starting openHAB again.

florian-h05 commented 6 days ago

Seems that something went wrong during the addon build and no new bundle was uploaded. I have just checked which JS Scripting add-on version I get for the current snapshot, it is 4.2.0.202406230328 which obviously is too old to contain openhab-js 5.1.2.

jlaur commented 4 days ago

@florian-h05 - with build 4135 I can finally confirm that version 5.1.2 is installed and that the rule utilizing the new PersistenceExtensions works with this injected new version.