Debugger for Z-Way JavaScript

Discussions about RaZberry - Z-Wave board for Raspberry computer
billyboy
Posts: 61
Joined: 16 Mar 2013 21:33

Re: Debugger for Z-Way JavaScript

Post by billyboy »

who shit, I have to think. I did something like this

sudo mkdir /opt/node
sudo npm install -g node-inspector
node -v returns: v0.10.4

hope this helps you.
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: Debugger for Z-Way JavaScript

Post by PoltoS »

Hm, may be we need to upgrade our Raspbian ;)
User avatar
kambis
Posts: 36
Joined: 23 Jun 2014 08:06
Location: Germany
Contact:

How I installed the node, npm & node-inspector on RPi

Post by kambis »

I had to spend some hours to find out how to install the node-inspector on my RPi.
For others to have it faster done here how I did it:
Note:
To prevent typing 'sudo' all the time I logged to my RPi as root and updated my RPi using:

Code: Select all

apt-get upgrade
apt-get update
Then I searched for the latest distribution of NODE on http://nodejs.org/dist/ by looking for a file
ending with *.-linux-arm-pi.tar.gz. As per 23.06.2014 the latest build was found in the v0.10.26 folder. So following commands was executed on RPi:

Code: Select all

cd /opt
wget http://nodejs.org/dist/v0.10.26/node-v0.10.26-linux-arm-pi.tar.gz
tar -xvzf node-v0.10.26-linux-arm-pi.tar.gz
mv node-v0.10.26-linux-arm-pi node
Then I had to set the NODE_JS_HOME variable to the directory where I un-tarred NODE, and added the bin dir to my PATH; In my case I have modified /etc/profile file and added following two lines before the line export PATH:

Code: Select all

NODE_JS_HOME=/opt/node
PATH=$PATH:$NODE_JS_HOME/bin
export PATH
I have rebooted my RPi just to be sure updates and new PATH is applied correctly and there is no problem. After logging again as root into my RPi, I checked if the installation was okay or not by typing:

Code: Select all

node --version
which returned in my case: v0.10.26, or typing:

Code: Select all

npm --version
which returned in my case: 1.4.3
Then I installed node-inspector using following command:

Code: Select all

npm install -g node-inspector
This takes a while till it gets done. At the end I've started the node-inspector by typing:

Code: Select all

node-inspector --debug-port 8183
As a result I got:
Node Inspector v0.7.4
Visit http://127.0.0.1:8080/debug?port=8183 to start debugging.
Now I could start debugging on my PC using Google Chrome and opening following link:

http://<IP_Address_of_RPi>:8080/debug?port=8183

That's it ;)
Last edited by kambis on 25 Jun 2014 14:59, edited 2 times in total.
raZcherry
Posts: 11
Joined: 24 Jun 2014 01:14

Re: Debugger for Z-Way JavaScript

Post by raZcherry »

kambis, thanks for the guide!
I installed node with the version v0.10.26 (latest for pi) and node-inspector ( v.0.7.4).

Starting node-inspector with --debug-port=8183 , I see the node-inspector gui with all .js files of Z-Way.
But is it possible to set breakpoints and read the objects or values of variables?
I am starting first z-way-server and second node-inspector but I have no control of what .js file is executed and breakpoints on the node-insepctor gui will be ignored.

Thanks in advance for your responses ;)
Using: Z-Way v2.0.1-rc11 on Raspberry Pi
User avatar
kambis
Posts: 36
Joined: 23 Jun 2014 08:06
Location: Germany
Contact:

How to instruct the debugger to stop at first HA code line

Post by kambis »

Hi raZcherry,
let me try to give you a starting point:
As you mentioned you can see the .js files. That is a good starting point.
Open two SSH sessions in parallel from your PC to the RPi and log into them at the same time. In the first SSH session start the node-inspector and check on your PC if you find again the list of .js files. Don't touch this SSH session anymore and keep it in behind running. Any further actions with RPi must be done from now on over the second SSH session. On your PC in the browser find the file automation/main.js . Open it go to the
line 65: config = loadObject("config.json") || {and set a break point by clicking with the mouse on the number 65. You see a blue marking behind the number 65.
main.js breakpoint
main.js breakpoint
20140625#1.PNG (70.69 KiB) Viewed 19396 times
Leave the browser as is. We come later back to this position.

In the second SSH session check please if following configuration line is present in your main configuration file (/opt/z-way-server/config.xml):

Code: Select all

<automation-file>automation/main.js</automation-file>
if not add this config line and save it. For more details follow please this link: https://github.com/Z-Wave-Me/home-autom ... figuration. Before you jump into setting up config.json you can already check if the debugger is working as expected, or not. For this you need first to stop the z-way-server by typing in the second SSH terminal:

Code: Select all

/etc/init.d/z-way-server stop
You will see that all the .js files on your PC browser will disappear after a while. Now start the z-way-server by typing:

Code: Select all

/etc/init.d/z-way-server start
As soon as you see in terminal the message "Starting z-way-server: done.", go to your browser and reload the debugging page once again. Don't delay too much in this action!
After a while you see only one .js file appears in the browser and code execution is stopped at line 65 8-)
main.js line 65 breakpoint
main.js line 65 breakpoint
20140625#2.PNG (45.6 KiB) Viewed 19396 times
Now you can play around or let the execution run free and you see how all the other .js files appear again.
I hope this is a starting point for you.
raZcherry
Posts: 11
Joined: 24 Jun 2014 01:14

Re: Debugger for Z-Way JavaScript

Post by raZcherry »

Hi kambis,

thanks for your answer!

As you wrote I set the breakpoint at line 65 (loading the config.json).
It seems to be that in the new version (I am using v.1.7.1) there is no more config.json under the folder z-way-server/automation. Just under automation/storage is an autogenerated config file.
Maybe you migrate an older version of z-way and therefore you have a config.json file. I installed a completey fresh v.1.7.1.

I added the line

Code: Select all

<automation-file>automation/main.js</automation-file>
And my config.xml looks like:

Code: Select all

<config>
    <device>/dev/ttyAMA0</device>
    <config-dir></config-dir>
    <translations-dir></translations-dir>
    <zddx-dir></zddx-dir>
    <port>8083</port>
    <http-root-dir>./htdocs</http-root-dir>
    <automation-dir>automation</automation-dir>
    <automation-file>automation/main.js</automation-file>
    <shell-script-on-save-xml>sync</shell-script-on-save-xml>
    <shell-script-on-save-http>sync</shell-script-on-save-http>
    <log-file>/var/log/z-way-server.log</log-file>
    <log-level>0</log-level>
    <http-log-file>/var/log/z-way-server-http.log</http-log-file>
    <http-log-level>0</http-log-level>
</config>
Unfortunately, there is nothing changed after adding automation-file tag and restarting z-way. I also tried only automation-file without using the automation-dir tag in the config.xml, but this doesn´t work.
The breakpoints (in any .js file) will be ignored.
In the error log of node-inspector gui I also get this error message:

Code: Select all

Page.getResourceTree failed.
ReferenceError: process is not defined
Z-Way Debugging with Node-Inspector
Z-Way Debugging with Node-Inspector
z-way debugging.JPG (121.7 KiB) Viewed 19347 times
Maybe you can write your config.xml or I have overlooked something.
It looks like a mystery ;)
Using: Z-Way v2.0.1-rc11 on Raspberry Pi
User avatar
kambis
Posts: 36
Joined: 23 Jun 2014 08:06
Location: Germany
Contact:

Re: Debugger for Z-Way JavaScript

Post by kambis »

Hi raZcherry,

yes, in the new version .json file is located in the storage and it gets generated automatically. You don't need to take care of it. Also there is no need to have the automation-file entry in the config.xml file. Here is mine:

Code: Select all

<config>
    <device>/dev/ttyAMA0</device>
    <config-dir></config-dir>
    <translations-dir></translations-dir>
    <zddx-dir></zddx-dir>
    <port>8083</port>
    <http-root-dir>./htdocs</http-root-dir>
    <automation-dir>automation</automation-dir>
    <shell-script-on-save-xml>sync</shell-script-on-save-xml>
    <shell-script-on-save-http>sync</shell-script-on-save-http>
    <log-file>/var/log/z-way-server.log</log-file>
    <log-level>0</log-level>
    <http-log-file>/var/log/sm/z-way-http.log</http-log-file>
    <http-log-level>0</http-log-level>
</config>
...and yes, I have also the same error message in the node-inspector gui telling:
  • Page.getResourceTree failed.
    ReferenceError: process is not defined
No problem, just ignore it.
The point is you are missing the moment when the z-way-server starts and you click on 'reload' in your browser. It seems that the time your browser refreshes and gets ready is after the time when the server executes the code on line 65, where your break point is set. So try to set another breakpoint at line number 127 and restart the z-way-server once again, without stopping the debugger, or closing your browser. As soon as you see the message in your second terminal, that the server is started, wait about 2 seconds and then reload your browser page only once and wait for the breakpoints. If you see that the file automation/z-way-utils/CreateZDDX.js is listed in your browser, then you have missed again the breakpoints.

If still you don't have success, we can try something else:
In your automation panel create a "Auto Off" rule for a switch, which you hopefully have in your network. Then set a breakpoint in the file automation/modules/AutoOff/index.js on code line 47. Now turn on the switch. I expect that your code execution stops at the breakpoint on line 47.
good luck.
raZcherry
Posts: 11
Joined: 24 Jun 2014 01:14

Re: Debugger for Z-Way JavaScript

Post by raZcherry »

Hi kambis,

Thank you! It was only a timing problem. So you must refresh the node-inspector gui as soon as possible when you restart z-way.
I want to use node-inspector gui for editing and saving source code on .js files.
On node-inspector you can run the tool https://github.com/node-inspector/node- ... figuration with the command --save-live-edit true or configure the config.json of node-inspector.

Code: Select all

node-inspector --debug-port 8183 --save-live-edit true
otherwise you get an error like this:
error when saving file with node-inspector
error when saving file with node-inspector
saving file.png (22.41 KiB) Viewed 19243 times
After setting --save-live-edit true I got a warning

Code: Select all

Cannot save changes to disk. Error: The new content is not a valid node.js script.
Maybe someone has a workaround for this or a completely other way for editing/saving source code files ;)
Don´t be suprised: After restarting node-inspector, deleting and refreshing browser cache, the changes persists on the gui, but not on the real .js files of zway.

Another interesting point would be bypassing the error messages of z-way (webserver) in the console log of node-inspector gui like:

Code: Select all

JavaScript compilation error: SyntaxError: Unexpected token ;
    at AutomationController.loadModulesFromFolder (automation/classes/AutomationController.js:173:9)
    at automation/classes/AutomationController.js:116:14
    at Array.forEach (native)
    at AutomationController.loadModules (automation/classes/AutomationController.js:115:25)
    at AutomationController.init (automation/classes/AutomationController.js:44:10)
    at automation/main.js:126:15
Using: Z-Way v2.0.1-rc11 on Raspberry Pi
dylancaponi
Posts: 19
Joined: 25 Jun 2014 12:47

Re: Debugger for Z-Way JavaScript

Post by dylancaponi »

Followed the kambis guide. My npm, node, and node-inspector versions match.

I run node-inspector and get proper output:
node-inspector --debug-port 8183
Node Inspector v0.7.4
Visit http://127.0.0.1:8080/debug?port=8183 to start debugging.

Then I create a tunnel and am prompted for a password:
ssh -N pi@raspberryIP -L 8183:127.0.0.1:8183

but when I go to http://127.0.0.1:8080/debug?port=8183
I get an "Oops! Google Chrome could not connect to 127.0.0.1:8080"
and when I go to: http://127.0.0.1:8183/debug?port=8183
I get "No data received" and my tunnel terminal shows
"channel 2: open failed: connect failed: Connection refused"

Any suggestions /ideas as to where I'm going wrong?
User avatar
kambis
Posts: 36
Joined: 23 Jun 2014 08:06
Location: Germany
Contact:

Re: Debugger for Z-Way JavaScript

Post by kambis »

Hi dylancaponi,
You don't need any tunnel! Even though the message on raspberry tells to visit
http://127.0.0.1:8080/debug?port=8183 to start debugging, don't use the ip 127.0.0.1!
Just open in your browser following link:
[url]http://<IP_of_your_Raspberry>:8080/debug?port=8183[/url]
by replacing the string <IP_of_your_Raspberry> with the IP of your RPi.
Then I expect you can start to debug.
Post Reply