Page 2 of 3

Re: Javascript sample for a newbie

Posted: 30 Nov 2016 15:05
by Provo
You are creating a function expression and assigning it to this.bindFunc1, but you are never actually running the function. If you look at the recipe, you will see that after the function expression is defined, there is also the following code:

Code: Select all

// process all active bindings
if (global.ZWave) {
   global.ZWave().forEach(this.bindFunc1);
}
This runs the function expression with every element in the result from global.ZWave() as the argument to the function. So if gobal.ZWave() returns an array of the three strings "blah", "blahblah" and "zway", your function will be run three times: one with "blah" passed as the argument, one with "blahblah" passed as the argument, and one with "zway" as the argument.

The name "bindFunc1" is pretty arbitrary and could have been anything, but the author presumably called it "bindFunc1" because what it does is creating new small functions that it is binding to certain changes in certain devices (meaning these small functions will be run when these kind of changes occur). Unless you're doing anything like that, I suggest you pick a more descriptive name. :)

Re: Javascript sample for a newbie

Posted: 01 Dec 2016 11:01
by AMal
Provo wrote:You are creating a function expression and assigning it to this.bindFunc1, but you are never actually running the function. If you look at the recipe, you will see that after the function expression is defined, there is also the following code:

Code: Select all

// process all active bindings
if (global.ZWave) {
   global.ZWave().forEach(this.bindFunc1);
}
This runs the function expression with every element in the result from global.ZWave() as the argument to the function. So if gobal.ZWave() returns an array of the three strings "blah", "blahblah" and "zway", your function will be run three times: one with "blah" passed as the argument, one with "blahblah" passed as the argument, and one with "zway" as the argument.

The name "bindFunc1" is pretty arbitrary and could have been anything, but the author presumably called it "bindFunc1" because what it does is creating new small functions that it is binding to certain changes in certain devices (meaning these small functions will be run when these kind of changes occur). Unless you're doing anything like that, I suggest you pick a more descriptive name. :)
Thanks, provo.
But I'm still confused.
I have changed my code to add your suggestion as follows:

Code: Select all

console.log("Starting");
this.bindFunc1=function(zwayName)
 {
  if (zwayName!="zway"){console.log("name failure");return;}
  var devices=global.ZWave[zwayName].zway.devices;
  console.log("name found and devices array set");
 };
// process all active bindings
if (global.ZWave) {
   console.log("global.ZWave=true");
   global.ZWave().forEach(this.bindFunc1);
}else{console.log("global.ZWave=false");}
The result is the boolean global.ZWave is false.

Clearly I am missing something fundamental.
I have searched this forum, but without success.
I did notice in one post the comment:
"Note that global zway object may change or disappear, so your module should be ready to handle that."
This seems rather worrying!

It might be worth restating that I am trying to create (at first) a minimal server-side JS program to list the elements in the network, and the methods and properties of each object.
Thanks for your patients.

Re: Javascript sample for a newbie

Posted: 01 Dec 2016 14:45
by Provo
Ok, I just noticed that you said that you're running the script from /opt/z-way-server with node. That will not work. Then you're just running your file alone, and there is no global.ZWave() defined there. As the recipe says, put your script in /opt/z-way-server/automation/storage, and I assume it will be run by the Z-Way system when you restart it. (I'm not exactly sure where console.log() will output, but I guess it's in the z-way log.)

(By the way, it's much easier to read your code if you use code-tags around it. That way indentation is preserved. Use it like this: [ code ]your code here[ /code ]. Just remove the spaces within the square brackets to make it work.)

Re: Javascript sample for a newbie

Posted: 01 Dec 2016 15:41
by pz1
Provo wrote:Ok, I just noticed that you said that you're running the script from /opt/z-way-server with node. That will not work. Then you're just running your file alone, and there is no global.ZWave() defined there. As the recipe says, put your script in /opt/z-way-server/automation/storage, and I assume it will be run by the Z-Way system when you restart it.
Placing the JS code in that directory does not make it start. According to the recipe you have to use the JS App to get it started:
JSMJodule.PNG
JSMJodule.PNG (3.94 KiB) Viewed 13972 times
IIRC if you make changes to your script you can just stop the JS script by pressing on the little flame as shown at the very right of the image above. (and start it again there)
(I'm not exactly sure where console.log() will output, but I guess it's in the z-way log.)
It does indeed go to /var/log/z-way-server.log
(By the way, it's much easier to read your code if you use code-tags around it. That way indentation is preserved. Use it like this: [ code ]your code here[ /code ]. Just remove the spaces within the square brackets to make it work.)
In my role as moderator I had taken the liberty of adding those quotes earlier today.

Re: Javascript sample for a newbie

Posted: 02 Dec 2016 20:54
by AMal
pz1 wrote:
Provo wrote:Ok, I just noticed that you said that you're running the script from /opt/z-way-server with node. That will not work. Then you're just running your file alone, and there is no global.ZWave() defined there. As the recipe says, put your script in /opt/z-way-server/automation/storage, and I assume it will be run by the Z-Way system when you restart it.
Placing the JS code in that directory does not make it start. According to the recipe you have to use the JS App to get it started:
JSMJodule.PNG
IIRC if you make changes to your script you can just stop the JS script by pressing on the little flame as shown at the very right of the image above. (and start it again there)
(I'm not exactly sure where console.log() will output, but I guess it's in the z-way log.)
It does indeed go to /var/log/z-way-server.log
(By the way, it's much easier to read your code if you use code-tags around it. That way indentation is preserved. Use it like this: [ code ]your code here[ /code ]. Just remove the spaces within the square brackets to make it work.)
In my role as moderator I had taken the liberty of adding those quotes earlier today.
I seem to be getting nowhere with this.
I am running from a terminal session on the Pi server - no GUI.
As suggested, I have put my code into /opt/z-way-server/automation/storage, but if I execute it there with

Code: Select all

sudo node bind.js
I get the same result: The boolean global.ZWave is false.
If, rather than use nodeJS I try to execute the command JS as suggested, I get "JS: command not found".
I've tried restarting the Pi, and I've looked in z-way-server.log, but find no clues.
I have re-read the Z-Way Developers' Documentation, which indeed contains a lot of potentially useful information, but I am still clueless on how to get started.

Re: Javascript sample for a newbie

Posted: 05 Dec 2016 12:13
by Provo
You can't run your script from the terminal. You have to use the Z-Way module/app for executing JS scripts inside the Z-Way environment.

If you want something that can be run from the terminal, you need to change your strategy. Like making http requests to your Z-Way server and using one of the APIs described in the developers manual: https://razberry.z-wave.me/docs/zway.pdf

Re: Javascript sample for a newbie

Posted: 05 Dec 2016 12:34
by AMal
Provo wrote: Quote 1:Placing the JS code in that directory does not make it start. According to the recipe you have to use the JS App to get it started:
Quote 2: You can't run your script from the terminal. You have to use the Z-Way module/app for executing JS scripts inside the Z-Way environment.

If you want something that can be run from the terminal, you need to change your strategy. Like making http requests to your Z-Way server and using one of the APIs described in the developers manual: https://razberry.z-wave.me/docs/zway.pdf
OK, thanks.
Can you point me to a step by step process to add my script to the Z-Way environment and how to "use the Z-Way module/app for executing JS scripts inside the Z-Way environment."
Quote 1 refers to the JS App. Where is that, and how do I run it?
I want to avoid, if possible, running from a browser.
Cheers.

Re: Javascript sample for a newbie

Posted: 05 Dec 2016 12:44
by Provo
You log into your Z-Way web interface. Then you click the cog in the upper right corner, and go to "Apps". Then click the "Online Apps" button. There you have a button which probably says "Featured apps". Click it and change to "Developers Stuff". There you have an app called "Load custom JavaScript file", most likely abbreviated in the overview to "Load custom JavaScript …". Install, add app, and set it up with the path to your file.

Re: Javascript sample for a newbie

Posted: 06 Dec 2016 20:29
by AMal
Provo wrote:You log into your Z-Way web interface. Then you click the cog in the upper right corner, and go to "Apps". Then click the "Online Apps" button. There you have a button which probably says "Featured apps". Click it and change to "Developers Stuff". There you have an app called "Load custom JavaScript file", most likely abbreviated in the overview to "Load custom JavaScript …". Install, add app, and set it up with the path to your file.
Thank you, that was helpful.
However, I now find that I cannot use the javascript/node directive "require" as in

Code: Select all

var fs=require('fs');
.
I was hoping to use this to read and write files (in particular to write my own log file rather than write to console.log which goes into z-way-server.log.
I also want to require('socket.io') and require('net').
These are all typical server side functions.
Is there any way these can be implemented in z-way?
Also, It seems that JS code added by the "load custom Javascript" process gets executed only at system startup.
Is that correct?
And how/when is the log file emptied? It seem to contain rather a lot for my very simple network.
Thanks

Re: Javascript sample for a newbie

Posted: 06 Jan 2017 19:58
by PoltoS
Yes, Z-Way has fs. and socket. and some other functions. Please check https://github.com/Z-Wave-Me/Z-Way-Manu ... wayDev.pdf