Mix-up of JavaScript functions

I am encountering a major difficulty with mix-up of Javascript files.

On one side, I have the Sonos plugin with its D_Sonos1.json and D_Sonos1.js files.
On the other side, I have my DLNA plugin with its D_DLNAMediaController1.json and D_DLNAMediaController1.js files.

Here is what happen.

  • First I open my DLNA device: first flash tab is ok and additional JavaScript tabs are ok
  • Then I open one of my Sonos devices: first flash tab is ok and additional JavaScript tabs are ok
  • Finally I reopen my DLNA device: first flash tab is ok but additional JavaScript tabs are the ones from the Sonos plugin !!!

It is clearly not a problem of mix-up with JSON as the first flash tab is always the good one, but a mix-up with JS files leading to a loading of a wrong Javascript file.
It happens only in one direction.
It is systematic: as soon as I open one Sonos device, then the DLNA device is “corrupted”.
It happens even if I don’t go to a Javascript tab (on the Sonos device); just opening the Control tab is sufficient to “corrupt” the other device.
The behaviour is the same whatever the WEB browser.
The only workaround to restore the right tabs is to clear the cache of the browser (Ctrl+F5).

Any idea what could be wrong ?

I hope it is not another major MCV Vera bug !

Finally, I found the explanation just after posting. I was using the same function names in the two Javascript/JSON. If I use different function names in the 2 JSON files, the problem disappears.

It looks like a new bug in the Vera firmware.

Hopefully, we have a workaround.

The problem does not concern only the “entry” functions defined in the JSON files but all Javascript functions.
My current conclusion is that, as soon as you have two plugins using Javascript files, there is a problem as soon as the 2 plugins use the same name for a function !!!

I have not yet checked if the mix-up concerns Javascript variables having the same name in different Javascript files.

I have received a report of my ecobee thermostat plugin’s JSON information replacing what should appear for a Z-Wave thermostat in the dashboard, and my suspicion is that I use the UPnP standard device type for my thermostat device, and something in the system is assuming its use is unique per plugin. My understanding is that your plugin can use standard device types without creating conflicts with other plugins or the built-in Z-Wave support, but this report contradicts that.

Do you use standard device types in a way that could be exposing the same bug?

watou

Yes, Sonos is historically using a standard device type. But yesterday I tried to assign to it a new device type and it does not help.
I don’t think it is a mix-up relative to JSON or device types, but really a mix-up of Javascript files.

It seems that MCV indexes all Javascript functions (for all the plugins) by their name, not taling into account the JS file it belongs to. If you have 2 functions with the same name in two different Javascript files, you cannot know which one will be called. It depends on the order of JS loading.

It is a bug that has to be considered by all developers using JavaScript for the UI, because a new plugin can break another installed plugin !

I see two possible recommendations:
1 - name global JavaScript functions with a name that has a very low probability to be used by another plugin
2 - try to avoid when possible global JavaScript functions.

Regarding the second point, I will check it but I think I can probably move a lot of my global functions to sub-functions of the main functions (the functions defined in the JSON file). In this case, I could probably keep common names.

I don’t know if it is a known problem. If not, I am surprised anyone already discovered it.

When writing javascript in general you should always take into consideration of global variable clashing. This is not a bug really in vera, but how javascript works. I would try and have all functions as a sub-function of one main function if possible.

  • Garrett

You mean it is due to how a Web browser handles Javascript ?

I would try and have all functions as a sub-function of one main function if possible.

Yes, I will try to do it, at least for functions called from one unique function. I am not sure that it is possible for actions behind a button ?

Duplicating identical functions is not really a good practice…