Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ScriptExecution to plain Java ScriptExtension #3155

Merged
merged 3 commits into from
Nov 19, 2022

Conversation

J-N-K
Copy link
Member

@J-N-K J-N-K commented Nov 9, 2022

JSR-223 automation add-ons often use the script actions from the org.openhab.core.model.script bundle. The downside is that the actions are tied to XText, making it impossible for the automation add-ons to modify/wrap these actions without explicit dependency on XText (see #3128).

This is the first step to refactor these actions to plain Java, making them available as ScriptExtension via an import-preset ScriptAction which is not imported by default to prevent unwanted shadowing of other components. They are wrapped for DSL rules, to stay backward compatible.

Signed-off-by: Jan N. Klug github@klug.nrw

Signed-off-by: Jan N. Klug <github@klug.nrw>
@J-N-K J-N-K requested a review from a team as a code owner November 9, 2022 20:18
@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/js-scripting-why-are-we-deprecated-createtimer/140748/15

@J-N-K
Copy link
Member Author

J-N-K commented Nov 9, 2022

@florian-h05 Is that what you would expect?

@florian-h05
Copy link
Contributor

Wow, that was fast Jan 👍
Looks good on the first view, but I have to checkout to have a closer look.

Copy link
Contributor

@ccutrer ccutrer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much nicer. I was just working with createTimer from the jrubyscripting library yesterday, and was wondering why it wasn't just using a Runnable.

Signed-off-by: Jan N. Klug <github@klug.nrw>
* @param closure the code block to execute
* @return a handle to the created timer, so that it can be canceled or rescheduled
*/
Timer createTimer(ZonedDateTime instant, Runnable closure);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of the ZonedDateTime as instant triggers me to ask the question: Would an Instant be sufficient here, since the the point in time doesn't care about time-zone? If ZonedDateTime is more convenient, consider if parameter should be renamed for clarity, and/or if a variant taking Instant would be relevant.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs actually say it should be an AbstractInstant.

Copy link
Member Author

@J-N-K J-N-K Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AbstractInstant is surely wrong because that's a class from joda-time which we don't use anymore. So the docs need to be updated.

IMO an Instant would also be fine (also see my comment on #2898) but I re-implemented what we have in DSL and that is a ZonedDateTime. If we try to solve that now, we won't get a solution before next release.

Copy link
Contributor

@florian-h05 florian-h05 Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest: Why can't we get a solution if we try that before next release?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always felt that the docs references to abstract classes like this was confusing to new users anyway. I'm not sure Instant is any better but it's at least consistent. When/if this ever changes here to Instant, we should also change the persistence actions which are documented as using ZonedDateTime to be consistent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be my proposal. IMO user-facing ZDT and Instant don‘t make much difference, so using Instant internally in DateTimeType and ZDT in rules is fine. Does it make a difference in JS?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make a difference in JS?

Regarding the compatibility, no.
We are using JS-Joda as date and time library, which provides the same functionality that the Joda time library had (ZonedDateTime, Instant and so on).
When core moves from ZonedDateTime to Instant, we only need to use Instant from JS-Joda instead of ZonedDateTime from JS-Joda.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no real benefit (I don't see one for DSL or JavaRule), we should probably stay where we are.

Copy link
Contributor

@florian-h05 florian-h05 Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference I can see is, which argument and which string the parse function takes and that instance has function like ofEpochMillis. But we have a nice utility function that parses many different date and/or time representations to a ZonedDateTime, so I can’t see any benefit in using an Instant.

Given the large number of scripts that have to be updated when that’s changed to an Instant, I agree that we should probably stay on ZonedDateTime.
The question for now is: should we rename the parameter from instant to zdt or zonedDateTime or something like that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I‘ll update that.

closure.apply();
}, identifier);
@ActionDoc(text = "create an identifiable timer ")
public static Timer createTimer(@Nullable String identifier, ZonedDateTime zonedDateTime, Procedures.Procedure0 closure) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static Timer createTimer(@Nullable String identifier, ZonedDateTime zonedDateTime, Procedures.Procedure0 closure) {
public static Timer createTimer(@Nullable String identifier, ZonedDateTime zonedDateTime, Procedures.Procedure0 closure) {

closure.apply(arg1);
}, identifier);
@ActionDoc(text = "create an identifiable timer with argument")
public static Timer createTimerWithArgument(@Nullable String identifier, ZonedDateTime zonedDateTime, Object arg1, Procedures.Procedure1 closure) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static Timer createTimerWithArgument(@Nullable String identifier, ZonedDateTime zonedDateTime, Object arg1, Procedures.Procedure1 closure) {
public static Timer createTimerWithArgument(@Nullable String identifier, ZonedDateTime zonedDateTime, Object arg1, Procedures.Procedure1 closure) {

@florian-h05
Copy link
Contributor

florian-h05 commented Nov 10, 2022

@J-N-K
I had the time to check this out, and this is exactly what I expected.
Thanks 👍
JS Scripting PR is now open: openhab/openhab-addons#13695

Just one thing I am wondering: I've seen that you implemented a ScriptExtensionProvider for the actions, but I am wondering how I could access this script extension from JS Scripting (the JS code). I have dumped the object returned by require('@runtime') (this is the way of accessing itemRegistry, events and so on from JS code), and I can neither see ScriptAction nor scriptExtension.
FYI this is technically not required for the timers, but could you maybe point me in the right direction?

@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/js-scripting-why-are-we-deprecated-createtimer/140748/17

@florian-h05
Copy link
Contributor

@J-N-K What do you think, when does this PR get merged?

@J-N-K
Copy link
Member Author

J-N-K commented Nov 12, 2022

I'm probably the last person with an answer to this: I can't review and merge my own PR.

@florian-h05
Copy link
Contributor

@kaikreuzer @cweitkamp @wborn Just asking, WDYT when can you review and merge this PR? openhab/openhab-addons#13695 depends on this.

@J-N-K J-N-K added the enhancement An enhancement or new feature of the Core label Nov 13, 2022
Signed-off-by: Jan N. Klug <github@klug.nrw>
Copy link
Member

@kaikreuzer kaikreuzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@kaikreuzer kaikreuzer merged commit c340e8d into openhab:main Nov 19, 2022
@kaikreuzer kaikreuzer added this to the 3.4 milestone Nov 19, 2022
@J-N-K J-N-K deleted the feature-javascriptexecution branch November 19, 2022 19:58
splatch pushed a commit to ConnectorIO/copybara-hab-core that referenced this pull request Jul 12, 2023
* Refactor ScriptExecution to plain Java ScriptExtension
* keep callScript backward compatible

Signed-off-by: Jan N. Klug <github@klug.nrw>
GitOrigin-RevId: c340e8d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automation enhancement An enhancement or new feature of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants