jpg0 / ohj

Openhab Javascript Library
Eclipse Public License 2.0
6 stars 2 forks source link

OHItem.postUpdate fails when passing DecimalType as value #32

Open LukasA83 opened 2 years ago

LukasA83 commented 2 years ago

I'm stuck with an issue related to GraalJS in general I guess. When I pass a DecimalType to postUpdate method of an OHItem, it fails:

TypeError: invokeMember (postUpdate) on org.openhab.core.automation.module.script.internal.defaultscope.ScriptBusEvent@30bd1bf7 failed due to: Multiple applicable overloads found for method name postUpdate (candidates: [Method[public java.lang.Object org.openhab.core.automation.module.script.internal.defaultscope.ScriptBusEvent.postUpdate(org.openhab.core.items.Item,java.lang.Number)], Method[public java.lang.Object org.openhab.core.automation.module.script.internal.defaultscope.ScriptBusEvent.postUpdate(org.openhab.core.items.Item,org.openhab.core.types.State)]], arguments: [JavaObject[RatedLuxLevel5MinAverage (Type=NumberItem, State=0 lx, Label=Rated Lux level average, Category=sun, Tags=[Measurement, Light], Groups=[gAussenHelligkeit, gLichtsensor, gVirtualSensor]) (org.openhab.core.library.items.NumberItem)] (HostObject), JavaObject[0 (org.openhab.core.library.types.DecimalType)] (HostObject)]): TypeError: invokeMember (postUpdate) on org.openhab.core.automation.module.script.internal.defaultscope.ScriptBusEvent@30bd1bf7 failed due to: Multiple applicable overloads found for method name postUpdate (candidates: [Method[public java.lang.Object org.openhab.core.automation.module.script.internal.defaultscope.ScriptBusEvent.postUpdate(org.openhab.core.items.Item,java.lang.Number)], Method[public java.lang.Object org.openhab.core.automation.module.script.internal.defaultscope.ScriptBusEvent.postUpdate(org.openhab.core.items.Item,org.openhab.core.types.State)]], arguments: [JavaObject[RatedLuxLevel5MinAverage (Type=NumberItem, State=0 lx, Label=Rated Lux level average, Category=sun, Tags=[Measurement, Light], Groups=[gAussenHelligkeit, gLichtsensor, gVirtualSensor]) (org.openhab.core.library.items.NumberItem)] (HostObject), JavaObject[0 (org.openhab.core.library.types.DecimalType)] (HostObject)])

I was running through GraalJs documentation and found the ability to specific the method signature, but that is also not helping: TypeError: events["postUpdate(org.openhab.core.items.Item,org.openhab.core.types.State)"] is not a function: TypeError: events["postUpdate(org.openhab.core.items.Item,org.openhab.core.types.State)"] is not a function

Any idea?

jpg0 commented 2 years ago

Personally in this case I just worked around it - either by coercing the value to a specific type (e.g. type.as(...)) or usually just converting to a string. Not great, but it has always worked for me.

LukasA83 commented 2 years ago

I'll go with string for now, type.as did not bring any improvement ;) Let's see how it works out on QuantityType with Unit.

LukasA83 commented 2 years ago

What is the version of Graaljs, that comes with openhab 3.2-snapshot?

My attempt shall work with anything > 21

jpg0 commented 2 years ago

I believe that the version is 20.1.0. It can probably be upgraded easily, most versions have been a drop-in replacement, but not all.

LukasA83 commented 2 years ago

ok. I will create a pull request into the openhab repo. I managed to upgrade to graal 21.3, but had to change few lines of code, e.g. removing ClassExtender (anyway unused!?) and adding few more options to the graal engine and context.