openhab-scripters / openhab-helper-libraries

Scripts and modules for use with openHAB
Eclipse Public License 1.0
88 stars 69 forks source link

ReferenceError: "me" is not defined in triggers.js #265

Open Martin-Stangl opened 4 years ago

Martin-Stangl commented 4 years ago

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 far getTrName in triggers.js seems to be the only place demanding it. Currently I found 2 workarounds:

  1. Globally define me, for e.g.: var me = __FILE__.split(__DIR__).pop();

  2. Provide a triggerName with each trigger.

My solution suggestions:

  1. 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.

  2. Use the rule name instead of me. Probably needs to be done in JSRulethen instead of getTrName: Go through all triggers in the JSRule function and if a trigger has no triggerName, generatge it there with uuid and name.

Martin-Stangl commented 4 years ago

Not directly related to the issue, but two corrections to my workaround 1:

  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.

  2. __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.

5iver commented 4 years ago

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.

Martin-Stangl commented 4 years ago

Time is also an issue for me, but I feel motivated to find some currently and give it a try.

5iver commented 4 years ago

Just a heads up... you might want to look into #267. @jpg0 is proposing something that would likely cleanup this issue.

Martin-Stangl commented 4 years ago

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.

jpg0 commented 4 years ago

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.