Binding vDev changes

Discussions about RaZberry - Z-Wave board for Raspberry computer
Post Reply
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Binding vDev changes

Post by pz1 »

I have got the binding on Z-Wave sensor changes working:

Code: Select all

      devices[N].SwitchBinary.data.level.bind(function()
      {
          some code
      });
This is used for sending sensor updates over UDP. I would like to do the same now with metric changes in Virtual Devices. The latest Developers Manual writes about Binding to metric changes on page 67, but that is rather cryptic for me:

Code: Select all

vDev.on(’change:metrics:... ”, function (vDev) ... ); 
Someone around who can give a little bit more explanation, or some example?
Since 29-12-2016 I am no longer a moderator for this forum
dolpheen
Posts: 119
Joined: 10 Feb 2015 00:38

Re: Binding vDev changes

Post by dolpheen »

Like this?

Code: Select all

//Get vDev reference by deviceId
binarySensor = controller.devices.get('RemoteHA_3_ZWayVDev_zway_29-0-37');

// Subscribe to metrics:level change of the device
binarySensor.on('change:metrics:level', function (vDev) {
  // The code is called when metrics:level of vDev is changed
  // vDev is the reference of device
  // So you can use it to retieve the level value
  var level = binarySensor.get("metrics:level");
}); 

// Produce the same result as the example above for binarySensor.on
controller.devices.on(binarySensor, 'change:metrics:level', handler)

// Unsubscribe from receiving events
binarySensor.off('change:metrics:level', handler)
Razberry B+ 2.0.1-rc25 on ZW500 + 15 devices / Razberry B 2.0.1-rc25 on ZW300 for test
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: Binding vDev changes

Post by pz1 »

dolpheen wrote:Like this?

Code: Select all

//Get vDev reference by deviceId
binarySensor = controller.devices.get('RemoteHA_3_ZWayVDev_zway_29-0-37');

// Subscribe to metrics:level change of the device
binarySensor.on('change:metrics:level', function (vDev) {
  // The code is called when metrics:level of vDev is changed
  // vDev is the reference of device
  // So you can use it to retieve the level value
  var level = binarySensor.get("metrics:level");
}); 

// Produce the same result as the example above for binarySensor.on
controller.devices.on(binarySensor, 'change:metrics:level', handler)

// Unsubscribe from receiving events
binarySensor.off('change:metrics:level', handler)
Thanks for the reply. I do get it partially. Don't understand what "handler" means.
Below is a more complete listing for the UPD status updater that I use for z-wave devices. My aim is to get similar functionality for virtual devices I create with a number of userModules I made.

Code: Select all

	var sock = new sockets.udp();
	sock.connect("192.168.1.33", 9091);

	this.bindFunc1 = function (zwayName) {
		if (zwayName != "zway")
			return; // you want to bind to default zway instance

		var devices = global.ZWave[zwayName].zway.devices;

		//from here insert your devices
		devices[2].SwitchBinary.data.level.bind(function () {
			var status = (this.value) ? "on" : "off";
			sock.send("ZWay_2," + status);
		});

		devices[3].SwitchBinary.data.level.bind(function () {
			var status = (this.value) ? "on" : "off";
			sock.send("ZWay_3," + status);
		});
So how does your "handler" relate to my "sock.send("ZWay_2," + status)"
dolpheen
Posts: 119
Joined: 10 Feb 2015 00:38

Re: Binding vDev changes

Post by dolpheen »

pz1 wrote: Don't understand what "handler" means.
Handler means function to handle you event.

You can write the function in line of your event.
For ex. in your case

Code: Select all

binarySensor.on('change:metrics:level', function (vDev) {
  var level = binarySensor.get("metrics:level");
  sock.send(vDev.id + ',' + level); //Will send the id of vDev and status - on/off (for binary switch)
}); 
or use function expression assigned to variable

Code: Select all

var handler =  function (vDev) {
  var level = binarySensor.get("metrics:level");
  sock.send(vDev.id + ',' + level); //Will send the id of vDev and status - on/off (for binary switch)
});

//Here we use handler variable that contains function to handle our event
binarySensor.on('change:metrics:level', handler)  
Both codes produce same result in your case, the using of each pattern depends on your program structure.
Razberry B+ 2.0.1-rc25 on ZW500 + 15 devices / Razberry B 2.0.1-rc25 on ZW300 for test
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: Binding vDev changes

Post by pz1 »

Thanks, I'll give it a try the coming days
Post Reply