Programatically generate elements for field in module.json?

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Programatically generate elements for field in module.json?

Post by Provo »

I'm working on a user module which needs to programatically generate a list of elements in a select field in the module's config. According to the Alpaca js documentation, this is perfectly possible, but when I try, the z-wave-server fails to load the module with the following error message:

[core] Notification: error (core): Cannot load modules.json from userModules/SomeModule/module.json: SyntaxError: Unexpected token u

If I just write:

Code: Select all

"dataSource" : [{"value": "test1", "text": "Test 1"},{"value": "test2", "text": "Test 2"}]
... it works. But if I do, like the documentation says:

Code: Select all

"dataSource" : function(callback){
        value_object = [{"value": "test1", "text": "Test 1"},{"value": "test2", "text": "Test 2"}];
        callback(value_object);
}
... it fails to load, with the error above. I've also tried this, but with the same result:

Code: Select all

"dataSource" : function(){
        value_object = [{"value": "test1", "text": "Test 1"},{"value": "test2", "text": "Test 2"}];
        return value_object;
}()
So, is there any way of achieving this at all? If so, how?
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Programatically generate elements for field in module.json?

Post by PoltoS »

module.json is a pure JSON. It can not contain JS code
Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: Programatically generate elements for field in module.json?

Post by Provo »

Ok. I tried to do this the other way around, by creating a postrender script that will hopefully be able to populate the select field when the form has rendered, but I'm having trouble getting the postrender script to execute. For now, I'm keeping it as simple as possible, and have basically copied the basics of what's present in e.g. the SensorsPolling module.

I have added the following in my module.json: "postRender" : "loadFunction:postRender.js"

I have also created the file postRender.js in htdocs/js, which has the following content:

Code: Select all

function modulePostRender(){
    console.log("123123123 postrender run");
};
The module itself loads fine. And I would expect the system to log the output "123123123 postrender run" when the form has rendered on screen, but grepping the log shows that it doesn't. What am I not getting right?
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Programatically generate elements for field in module.json?

Post by PoltoS »

Who is supposed to call your modulePostRender? may be you need to call it at the end of this file?

I'm not sure how this postRender works, never looked into, so excuse for a false assumption
Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: Programatically generate elements for field in module.json?

Post by Provo »

According to the Alpaca documentation, the postRender function is called when the form has rendered in the client. So I searched the home-automation repo and found a couple of instances where it has been used. Except instead of feeding the function definition directly in the module.json, like the Alpaca examples do, they use "loadfunction:postRender.js" instead, with a postRender.js located in the htdocs/js folder. I found that the MailNotifier module does almost exactly what I want.

Its module.json has the key and value "postRender": "loadFunction:postRender.js". Its index.js makes no reference to this, but there exists a file postRender.js in htdocs/js with the function modulePostRender(), which seems to be automatically called when the config form is rendered in the browser. I've tried to copy this approach in a very bare boned manner. My module.json contains the same key:value pair, the htdocs/js folder contains a file postRender.js with a function modulePostRender() which only writes to the console. Except it never does. The module initializes correctly, but nothing is output to the log when the config form has rendered.

I guess I'll try to copy the MailNotifier module and start stripping it down step by step to see what I could be missing, but as this process is very tedious, I'd appreciate any input on what could be wrong in my approach in the meantime.
Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: Programatically generate elements for field in module.json?

Post by Provo »

In case someone else sees this and has the same question: The function does actually run, but it's run by the browser, and thus console.log() outputs to the browser console and not the z-way log. It also means that it runs with the browser scope.

I really wish there was a way to fill or generate elements programmatically from within the z-way scope at form generation, as not everything useful is accessible from outside the z-way scope, and it's very hard if not impossible to do udp sockets for instance, and one also has to have cross browser compatibility in mind, which can be a major hassle.
jet11x
Posts: 53
Joined: 29 Dec 2014 21:15

Re: Programatically generate elements for field in module.json?

Post by jet11x »

I'd also find the ability to use the js capabilities of Alpaca useful, preferably by adding JavaScript in the index.json file, or if not in there then an associated file that's part of the module.
Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: Programatically generate elements for field in module.json?

Post by Provo »

I just made this pull request, which ads exactly this functionality. It's a copy of the approach used for passing functions to click and onFieldChange events for execution client side in alpaca, only it evaluates server side for dataSource fields instead.
Post Reply