Friday, 26 May 2017

NodeJs - correct class extension

I try to make code for my home automation on Lambda with NodeJS using alexa. I have classes for each switch type, extended from one switch type.

function Switch(name, idx, param) {
    this.name = name;
    this.param = param;
    this.idx = idx;
}
Switch.prototype.onCommand = function() { 
    return url+'/json.htm?type=command&param='+this.param+'&idx='+this.idx+'&switchcmd=On';
};
Switch.prototype.offCommand = function() { 
    return url+'/json.htm?type=command&param='+this.param+'&idx='+this.idx+'&switchcmd=Off'; 
};

function Blinds(name, idx) {
    Switch.call(this,name,idx,"switchlight");
}

function Light(name, idx) {
    Switch.call(this,name,idx,"switchlight");
}

function Scene(name, idx) {
    Switch.call(this,name,idx,"switchscene");
}

Blinds.prototype = Object.create(Switch.prototype);
Light.prototype = Object.create(Switch.prototype);
Scene.prototype = Object.create(Switch.prototype);

var blinds_list = {
    kitchen: new Blinds("kitchen", 182),
    livingroom: new Blinds("livingroom", 250),
    bedroom: new Blinds("bedroom", 278),
    office: new Blinds("office", 128)
};

var light_list = {
    'corridor one': new Light("corridor one", 89),
    'corridor two': new Light("corridor two", 92),
    'bedroom': new Light("bedroom", 210),
    'bedroom wardrobe': new Light("bedroom wardrobe", 213),
    'office': new Light("office", 258),
    'bathroom': new Light("bathroom", 190),
    'bath': new Light("bath", 210),
    'bathroom locker': new Light("bathroom locker", 193),
    'livingroom one': new Light("livingroom one", 29),
    'livingroom two': new Light("livingroom two", 32),
    'kitchen': new Light("kitchen", 325),
    'kitchen locker lower': new Light("kitchen locker lower", 233),
    'kitchen locker upper': new Light("kitchen locker upper", 230),
    'table': new Light("table", 306),
    'island': new Light("island", 52)
};

When i run "Lights" - everything work great, but using "Blinds" - i got:

Exception: TypeError: handle.offCommand is not a function

Right before it i log object handle:

Switch { name: 'kitchen', param: 'switchlight', idx: 182 }

I've tried many methods for classes/extentions - but nothing worked. It is really strange, even when i change plases of functions, prototypes, or lists - always Blinds don't work - but Light does.

Can anyone explain me why?



via wacki4

No comments:

Post a Comment