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

[actions] Add wrappers around actions to avoid type problems #98

Closed
rkoshak opened this issue Feb 14, 2022 · 8 comments
Closed

[actions] Add wrappers around actions to avoid type problems #98

rkoshak opened this issue Feb 14, 2022 · 8 comments
Assignees
Labels
enhancement New feature or request ohc / addon Needs to be fixed in openHAB Core or JS Scripting addon

Comments

@rkoshak
Copy link
Contributor

rkoshak commented Feb 14, 2022

I've encountered this issue on the forum a couple of times already and suspect it's only going to become more of a problem as more people adopt JS Scripting.

Take actions.Semantic for example. Each and every method on this Action requires a an OH Java Item Object, which we've gone out of our way to try to hide from the end users. It's jarring to the end user and we've not even documented that .rawItem exists so the users are left scratching their heads.

Obviously the simple thing to do would be to document that rawItem exists and that it's needed when calling the core actions. But it seems like we could create a wrapper or monkey patch those core actions that present this sort of type problem so they will accept our JS Item Object or even the Item name and accept other similar pure JS types in places that require openHAB Java Types (e.g. PercentType is required for actions that adjust the sound volume).

I think that time.ZonedDateTime already works just fine for those Actions that require a java.time.ZonedDateTime like createTimer() and getBankHolidayName(). Binding provided actions can fend for themselves. So the only troublesome actions are:

  • Audio actions that take a PercentType for the volume
  • maybe the BusEvent storeStates and restoreStates actions, though those may be OK as is
  • executeCommandLine that takes a Duration, need to verify that time.Duration works instead of java.time.Duration
  • the Semantic actions
  • Voice.say actions that take a PercentType to change the volume

This seems to fall within the scope of what this library should do but want to raise the discussion before I start writing a PR for it. I personally don't use any of these Actions so I'll have to set up a testbed to verify them all.

@rkoshak rkoshak added the enhancement New feature or request label Feb 14, 2022
@digitaldan
Copy link
Contributor

Right, this is problematic. We could try and wrap all actions, but i fear this is going to be a maintenance issue as additional actions are added.

In the binding as you know we translate both js-joda ZonedDateTime and Duration parameters to Java native versions like:

    .targetTypeMapping(Value.class, ZonedDateTime.class, (v) -> v.hasMember("withFixedOffsetZone"), v -> {
        return 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)

we could add something like this to match Items

.targetTypeMapping(Value.class, Item.class,
    (v) -> v.hasMember("rawItem"), v -> {
        return v.getMember("rawItem").as(Item.class);
    }, HostAccess.TargetMappingPrecedence.LOW)

@rkoshak
Copy link
Contributor Author

rkoshak commented Feb 18, 2022

I didn't know that that mapping happened in the binding. That's useful to know. I thought maybe GraalVM handled it somehow.

Should I move this issue to the add-on then? I think implementing it there as you suggest would be more consistent with what's already been done for these other types.

@digitaldan
Copy link
Contributor

Should I move this issue to the add-on then? I think implementing it there as you suggest would be more consistent with what's already been done for these other types.

Sounds good! I'll take a stab at this tonight and get a PR and jar posted soon.

@digitaldan
Copy link
Contributor

updated binding at https://github.com/digitaldan/openhab-addons/releases/tag/jsscripting-item-proxy

I don't use these actions either , but this seems to fix the Semantic compatibility.

Voice.say actions that take a PercentType to change the volume

Hmm, i wonder how most people use that? Is volume something that is pulled from an item, or do most people hardcode this in their script ? I can probably add something for this as well like:

.targetTypeMapping(Value.class, PercentType.class, (v) -> v.fitsInInt(), v -> {
      return new PercentType(v.asInt());
  }, HostAccess.TargetMappingPrecedence.LOW)

It would be nice to have a standard mapping of native OH types instead of doing them one by one as we need them, but i'm not clear what that would be atm.

@rkoshak
Copy link
Contributor Author

rkoshak commented Feb 21, 2022

I don't know if people often pull this from an Item. I get the impression that most people want to set the volume for the say followed by restoring the volume back to what it was. PercentType is used because that's what the action requires. It makes some sense since the volume is usually represented as a percent.

It would be nice to have a standard mapping of native OH types instead of doing them one by one as we need them, but i'm not clear what that would be atm.

I agree. The troublesome types will be the compound types like HSBType and QuantityType. Do we have other places besides the Actions where this type conversion is required?

@digitaldan
Copy link
Contributor

I agree. The troublesome types will be the compound types like HSBType and QuantityType. Do we have other places besides the Actions where this type conversion is required?

I'm not sure, I don't do much with those in my own rules.

Let me know if the percent fix works for you.

@florian-h05 florian-h05 changed the title Add wrappers around actions to avoid type problems [actions] Add wrappers around actions to avoid type problems May 27, 2022
@florian-h05
Copy link
Contributor

florian-h05 commented Dec 29, 2022

So the only troublesome actions are:

  • Audio actions that take a PercentType for the volume
  • maybe the BusEvent storeStates and restoreStates actions, though those may be OK as is
  • executeCommandLine that takes a Duration, need to verify that time.Duration works instead of java.time.Duration
  • the Semantic actions
    -Voice.say actions that take a PercentType to change the volume

We should also convert the new JS Quantity to QuantityType inside the addon.

FYI: We are already wrapping a few actions to do Argument type checks and avoid wrong arguments going to the Java layer, because the exception from there is difficult to trace back; and to get type definitions for actions.

@florian-h05 florian-h05 self-assigned this Dec 29, 2022
florian-h05 added a commit to florian-h05/openhab-addons that referenced this issue Feb 4, 2023
…arts

Related to openhab/openhab-js#98.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@florian-h05 florian-h05 added the ohc / addon Needs to be fixed in openHAB Core or JS Scripting addon label Feb 5, 2023
florian-h05 added a commit to florian-h05/openhab-js that referenced this issue Feb 5, 2023
Related to
openhab#98 (comment)
.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
florian-h05 added a commit to florian-h05/openhab-js that referenced this issue Feb 5, 2023
Related to
openhab#98 (comment)
.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
florian-h05 added a commit that referenced this issue Feb 5, 2023
Related to #98 (comment).

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@florian-h05
Copy link
Contributor

Closing as both the core PR to accept floats for Audio and Voice actions volume (openhab/openhab-core#3352) and the addon PR to convert JS Item & Quantity to the Java counterparts (openhab/openhab-addons#14335) are merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ohc / addon Needs to be fixed in openHAB Core or JS Scripting addon
Projects
None yet
Development

No branches or pull requests

3 participants