Unbind everything?

Discussions about RaZberry - Z-Wave board for Raspberry computer
Post Reply
Tiptop
Posts: 56
Joined: 01 Mar 2013 13:50

Unbind everything?

Post by Tiptop »

Right at the moment, I am using the following technique to bind my python based home server system to the z-way server:

Code: Select all

setTimeout( function() {

function NodeNotifier(nodeID, value)
{
    var pushstring = 'NID=' + nodeID + '&VAL=' + value;
    debugPrint("NodeNotifier: " + pushstring);
    http.request({ method:'GET', url:'127.0.0.1:8888/push&' + pushstring });
}

// ------------------------------------

debugPrint('Homeserver: Binding devices devices to NodeNotifier...');

// Demo lamp
try { zway.devices[4].instances[0].commandClasses[37].data.level.bind( function() { NodeNotifier('4', this.value); } ); } catch (e) { debugPrint('\nERROR: Failed to bind to zway.devices[4].instances[0].commandClasses[37].data.level!\n'); }

// ... other device bindings ...

}, 60*1000);
This java script source is loaded through the "CustomUserCode" plugin.

The first issue is the ugly hack using the setTimeout() function. If I don't use this, the zway instance including the devices may not yet be available during system startup.
I have not found a better solution yet to synchronize that. Does anyone have an idea for a better solution?

Coming to the topic of this message: Is it possible to "unbind" everything? If I update the custom binding file, I have to completely restart the z-way server as otherwise I would get redundant multiple bindings when reloading the CustomUserCode plugin.
In my eyes, the best thing would be being able to unbind anything I've previously bound to any event.
Is there a good way to do so?

Thanks in advance for your help!
pofs
Posts: 688
Joined: 25 Mar 2011 19:03

Re: Unbind everything?

Post by pofs »

The correct way of handling future loading of zway is discussed here: viewtopic.php?f=3422&t=20816&start=10#p53478

As of unbinding, CustomUserCode is not a good solution here, because it doesn't have a concept of unloading. You'd better write a custom module for that.

ZWave object listens to dataBind and dataUnbind events, which help to maintain a list of bindings:

Code: Select all

YourModule.prototype.init = function(config) {
    YourModule.super_.prototype.init.call(this, config);
    
    this.bindings = [];

    var self = this;
    this.registeredHandler = function(name) {
        if (name !== "zway") return;
        self.controller.emit("ZWave.dataBind", self.bindings, name, 4, 0, 37, "level", function() { ... });
        // add other bindings
    };

    this.unregisterBinding = function(name) {
        if (name !== "zway") return;
        // no need to actually unbind, because the instance is already destroyed
        // just reset bindings array
        self.bindings = [];
    }

    // process all active bindings
    if (global.ZWave) {
        global.ZWave().forEach(this.registerBinding);
    }
    // and listen for future ones
    this.controller.on("ZWave.register", this.registerBinding);
    this.controller.on("ZWave.unregister", this.unregisterBinding);
}

YourModule.prototype.stop = function() {
    YourModule.super_.prototype.stop.call(this);
    
    // unbind all bound handlers
    this.controller.emit("ZWave.dataUnbind", this.bindings);
 
    if (this.registerBinding) {
        this.controller.off("ZWave.register", this.registerBinding);
    }
    if (this.unregisterBinding) {
        this.controller.off("ZWave.unregister", this.unregisterBinding);
    }
}
The above code might have errors though, as I wrote it in browser and have not actually tested it :)
Post Reply