Open Martin-Stangl opened 4 years ago
Not directly related to the issue, but two corrections to my workaround 1:
__FILE__.split(__DIR__).pop()
works on Linux, but not on Windows, as FILE always uses /
as path separator, but DIR uses \
on Windows. Therefore __FILE__.split('/').pop()
needs to be used.
__FILE__.split('/').pop()
works only for files loaded in with load()
. The initial .js seems to be loaded using a reader. therefore FILE contains <eval>
and not the file name. So it seems to be no clever way around to defining something like me = "myrule.js"
for logging purposes. Still, the usage of me
in getTrName
is not clean and should be fixed.
Actually, I consider to give it a go myself to implement my solution suggestion 2, but I am a little unsure on how JSRuleNew fits into the picture. Is this just a work in progress of a new implementation of JSRule? Then I would need to implement my fix in both.
Thank you for reporting the problem and the analysis. The JS libraries will be completely reworked to match the Jython libraries once ES6 is available (OH JDK9 compatibility). If you want to clean them up, go ahead, otherwise I'll get in there at some point, but my time is very limited ATM.
Time is also an issue for me, but I feel motivated to find some currently and give it a try.
Just a heads up... you might want to look into #267. @jpg0 is proposing something that would likely cleanup this issue.
I am no JS expert, but if I understand it right, it would force us to fix this issue, as there would be no way to access me
from within getTrName
anymore.
FYI this did indeed force it to be fixed. (Well, you could probably explicitly assign the name to global.triggerName
, but, let's not.) I just used the uuid if there was no name.
Full error message:
Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/javascript/personal/Papierschredder.js': ReferenceError: "me" is not defined in /etc/openhab2/automation/lib/javascript/core/triggers.js at line number 158
I did not define
me
in Papierschredder.js, as I don't use it anywhere. So fargetTrName
in triggers.js seems to be the only place demanding it. Currently I found 2 workarounds:Globally define me, for e.g.:
var me = __FILE__.split(__DIR__).pop();
Provide a triggerName with each trigger.
My solution suggestions:
Either just use the uuid if no triggerName was provided. If someone wants to be able to identify the source of their triggers, they have to do the works and provide triggerNames.
Use the rule name instead of
me
. Probably needs to be done inJSRule
then instead ofgetTrName
: Go through all triggers in the JSRule function and if a trigger has no triggerName, generatge it there with uuid and name.