Javascript sample for a newbie

Tips, Tricks and Scripts to enhance your home automation and workaround known device bugs, limitations and incompatibilities
AMal
Posts: 14
Joined: 09 Nov 2016 14:30

Javascript sample for a newbie

Post by AMal »

Can someone please help me start?
I have some familiarity with server-side javascript, and would like to create some initial server code for example to list devices in my network and to list their properties/capabilities.
I have a Razberry, Pi, Aeon Labs Multisersor 6, and can see the device and events through the supplied UI.
I want to learn how to create my own application.
A few lines of sample code to get me started would be much appreciated.
Thanks
golaonline
Posts: 19
Joined: 16 Dec 2015 21:59

Re: Javascript sample for a newbie

Post by golaonline »

You can use the APIs provided by ZWay read the docs if you are a developer at https://razberry.z-wave.me/docs/zway.pdf

e.g. http://YOUR_IP:8083/ZAutomation/api/v1/devices will give you a Json with all devices.
AMal
Posts: 14
Joined: 09 Nov 2016 14:30

Re: Javascript sample for a newbie

Post by AMal »

Thanks, golaonline.
I have read the reference documentation, and I think it will help when I get over the initial first steps.
If I use the URL http://MyIPAddress:8083/ZAutomation/api/v1/devices, I initially get a 401 error - not logged on.
If I open another browser window to log on then http://MyIPAddress:8083/ZAutomation/api/v1/devices does indeed give me the JSON string as you describe.
However, I particularly want to create a server javascript script to do the same thing using a command like
node myscript.js
I need to know what magic to put in myscript.

Thanks
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: Javascript sample for a newbie

Post by pz1 »

In this recipe there is some sample script that sends an UDP message on selected Z-Wave device events.
The first post is kept up to date. Some of the following post are old and may be outdated.

edit 20161128: Added link to recipe
AMal
Posts: 14
Joined: 09 Nov 2016 14:30

Re: Javascript sample for a newbie

Post by AMal »

pz1 wrote:In this recipe there is some sample script that sends an UDP message on selected Z-Wave device events.
The first post is kept up to date. Some of the following post are old and may be outdated.
Hi pz1, your post refers to a "recipe" but I can't see a reference to that recipe or any attachment.
The developer documentation refers to the z-way javascript core and the zway object.
What I cannot work out is how in a JS script I instantiate that zway object.
Thanks.
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: Javascript sample for a newbie

Post by pz1 »

:oops: Stupid me, forgot to post the link.
viewtopic.php?f=3424&t=20849
AMal
Posts: 14
Joined: 09 Nov 2016 14:30

Re: Javascript sample for a newbie

Post by AMal »

Thanks, pz1, I'll take a good look at that as soon as I get a chance.
AMal
Posts: 14
Joined: 09 Nov 2016 14:30

Re: Javascript sample for a newbie

Post by AMal »

pz1 wrote::oops: Stupid me, forgot to post the link.
viewtopic.php?f=3424&t=20849
No, it's me that's stupid (or blind) (or maybe blind stupid).
My code is in a file named test.js, I'm running the command node test.js in the directory /opt/z-way-server
When I try to run this code...

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");
};

... all I see on the console is "Starting"
Clearly I am missing something very basic.
I presume zwayName is undefined, but I would expect to get other console messages.
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: Javascript sample for a newbie

Post by pz1 »

Sorry I can't help you with this level. I am not a programmer. The code was provided by Alexei Skalozub from the Russian development team. Unfortunately it seems he is no longer around to help us.
AMal
Posts: 14
Joined: 09 Nov 2016 14:30

Re: Javascript sample for a newbie

Post by AMal »

Hi pz1, thanks for your thoughts anyway.
I hope someone might come along to the forum who understands some of this stuff.
Post Reply