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

[items] ItemPersistence: Add support for persisting TimeSeries #341

Closed florian-h05 closed 3 weeks ago

florian-h05 commented 3 weeks ago

This adds support for persisting a TimeSeries.

It adds a new JS TimeSeries class, which implements the same functionality as the Java TimeSeries, but in pure JS. When this JS TimeSeries is passed to ItemPersistence#persist, a Java TimeSeries is created from it by iterating over the timestamp -> state pairs and using org.openhab.core.types.TypeParser to parse Java States from the JS types.

The helpers have been extended with a isInstant and isTimeSeries method and for all isXXX methods, a check whether the passed in type is an object has been added.

florian-h05 commented 3 weeks ago

@jlaur Can you please test all six variantions of using the persist method? You can have a look at the updated README how to create a TimeSeries.

If you don’t want to mess with NPM, you can download the build artifact https://github.com/openhab/openhab-js/actions/runs/9427272367/artifacts/1581551041, unzip it and copy the openHAB.js file to the node_modules folder (delete the content before).

jlaur commented 3 weeks ago

@florian-h05 - sure, thanks! Actually I don't mind messing with NPM now that I have WSL running on my development system. However, I now tried the following:

But got the following:

TypeError: Cannot read property "constructor" from undefined: TypeError: Cannot read property "constructor" from undefined
    at _isInstant (C:\PROGRA~1\openHAB\conf\automation\js\node_modules\openhab.js:2)
    at _isTimeSeries (C:\PROGRA~1\openHAB\conf\automation\js\node_modules\openhab.js:2)
    at persist (C:\PROGRA~1\openHAB\conf\automation\js\node_modules\openhab.js:2)
    at <program> (test.js:37)
    at _run (C:\PROGRA~1\openHAB\conf\automation\js\node_modules\openhab.js:2)
    at execute (C:\PROGRA~1\openHAB\conf\automation\js\node_modules\openhab.js:2)
    at doExecute (C:\PROGRA~1\openHAB\conf\automation\js\node_modules\openhab.js:2)

This was before changing anything from the previous test run, i.e. line 37 (ZonedDateTime, Quantity):

items.Energi_Data_Service_Total_Price.persistence.persist(spotPrice.timestamp, totalPrice);
florian-h05 commented 3 weeks ago

@jlaur Can you please retry with https://github.com/openhab/openhab-js/actions/runs/9428132260/artifacts/1581671952?

jlaur commented 3 weeks ago

@jlaur Can you please retry with https://github.com/openhab/openhab-js/actions/runs/9428132260/artifacts/1581671952?

Much better, previous example now works again. I'll test the others.

jlaur commented 3 weeks ago

@florian-h05 - I can confirm that all six overloads work as expected. Good job! 👍

florian-h05 commented 3 weeks ago

Great :+1: I have also tested this on my dev system though I don't have a modifiable persistence service there. but what gets passed to the PersistenceExtensions looks good in the debugger.