Fix crash when WeMo devices are powered off (#794)

* Big Update

Includes:
 - New lookup node to check state of a device
 - Fix dimming control for lights
 - Fix light group control
 - Set the node label to match the device name
 - The event now includes the text description of the light capability

* Add extra check for empty results in discovery

* Attempt to catch errors from wemo-lookup

This is to catch errors when device is offline

* Put request back

Will look at making it all http.request later

* Add more error handling and reporting

* Try and not crash with subscriptions

Failed subscriptions will try agian in 1 min

* Fix some messages

* Remove most of the console.logs

* Last of console.logs
This commit is contained in:
Ben Hardill 2021-04-10 20:02:36 +01:00 committed by GitHub
parent 2c5cc5f70c
commit 874708dc0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 42 deletions

View File

@ -125,7 +125,7 @@ module.exports = function(RED) {
callback_url += 'wemoNG/notification'; callback_url += 'wemoNG/notification';
console.log('Callback URL = %s',callback_url); // console.log('Callback URL = %s',callback_url);
var subscribeOptions = { var subscribeOptions = {
host: device.ip, host: device.ip,
@ -152,6 +152,14 @@ module.exports = function(RED) {
} }
}); });
sub_request.on('error', function(){
// devie probably offline
// try again after a minute
setTimeout( function() {
subscribe(node)
}, 60000)
})
sub_request.end(); sub_request.end();
} }
} }
@ -221,12 +229,21 @@ module.exports = function(RED) {
node.status({fill: 'green',shape: 'dot',text: 'found'}); node.status({fill: 'green',shape: 'dot',text: 'found'});
} }
node.on('input', function(msg) { node.on('input', function(msg, send, done){
var dev = wemo.get(node.dev); var dev = wemo.get(node.dev);
send = send || function() { node.send.apply(node,arguments) }
if (!dev) { if (!dev) {
//need to show that dev not currently found //need to show that dev not currently found
console.log('no device found'); // console.log('no device found');
if (done) {
done("Device not discovered yet")
} else {
node.error("Device not discovered yet", msg)
}
return; return;
} }
@ -398,12 +415,19 @@ module.exports = function(RED) {
node.status({fill: 'green',shape: 'dot',text: 'found'}); node.status({fill: 'green',shape: 'dot',text: 'found'});
} }
node.on('input', function(msg) { node.on('input', function(msg, send, done) {
var dev = wemo.get(node.dev); var dev = wemo.get(node.dev);
send = send || function() { node.send.apply(node,arguments) }
if (!dev) { if (!dev) {
//need to show that dev not currently found //need to show that dev not currently found
console.log('no device found'); console.log('Device not discovered yet');
if (done) {
done("Device not discovered yet")
} else {
node.error("Device not discovered yet",msg)
}
return; return;
} }
@ -427,7 +451,16 @@ module.exports = function(RED) {
delete status.capabilities; delete status.capabilities;
// } // }
msg.payload = status; msg.payload = status;
node.send(msg); send(msg);
if (done) {
done()
}
}).catch(function(err){
if (done) {
done(err)
} else {
node.error(err,msg)
}
}); });
} else if (dev.type === 'socket_insight') { } else if (dev.type === 'socket_insight') {
console.log("socket_insight"); console.log("socket_insight");
@ -437,17 +470,36 @@ module.exports = function(RED) {
msg.payload = insightParameters; msg.payload = insightParameters;
// 'state' should be a number for backwards compatibility // 'state' should be a number for backwards compatibility
msg.payload.state = parseInt(msg.payload.state); msg.payload.state = parseInt(msg.payload.state);
node.send(msg); send(msg);
if (done) {
done()
}
}).catch(function(err){
if (done) {
done(err)
} else {
node.error(err,msg)
}
}); });
} else { } else {
console.log("socket"); //console.log("socket");
// classic socket: no power measurement, so only request current switch status // classic socket: no power measurement, so only request current switch status
wemo.getSocketStatus(dev) wemo.getSocketStatus(dev)
.then(function(status) { .then(function(status) {
msg.payload = { msg.payload = {
state: status state: status
}; };
node.send(msg); send(msg);
if (done) {
done()
}
})
.catch(function(err){
if (done) {
done(err)
} else {
node.error(err,msg)
}
}); });
} }
}); });
@ -470,9 +522,13 @@ module.exports = function(RED) {
'sid': req.headers.sid 'sid': req.headers.sid
}; };
//console.log("Incoming Event %s", req.body.toString()); //console.log("Incoming Event %s", req.body.toString());
wemo.parseEvent(req.body.toString()).then(function(evt) { wemo.parseEvent(req.body.toString())
.then(function(evt) {
evt.sid = notification.sid; evt.sid = notification.sid;
wemo.emit('event',evt); wemo.emit('event',evt);
})
.catch(err => {
console.log(err)
}); });
res.send(''); res.send('');
}); });

View File

@ -271,6 +271,11 @@ WeMoNG.prototype.start = function start() {
post_request.abort(); post_request.abort();
}); });
post_request.on('error', function(err){
// should log err
//console.log(err);
})
post_request.write(util.format(getenddevs.body, udn)); post_request.write(util.format(getenddevs.body, udn));
post_request.end(); post_request.end();
@ -358,12 +363,12 @@ WeMoNG.prototype.toggleSocket = function toggleSocket(socket, on) {
}); });
post_request.on('error', function (e) { post_request.on('error', function (e) {
console.log(e); // console.log(e);
console.log("%j", postoptions); // console.log("%j", postoptions);
}); });
post_request.on('timeout', function () { post_request.on('timeout', function () {
console.log("Timeout"); // console.log("Timeout");
post_request.abort(); post_request.abort();
}); });
@ -408,21 +413,21 @@ WeMoNG.prototype.getSocketStatus = function getSocketStatus(socket) {
status = parseInt(status); status = parseInt(status);
def.resolve(status); def.resolve(status);
} else { } else {
def.reject(); def.reject(err);
} }
}); });
}); });
}); });
post_request.on('error', function (e) { post_request.on('error', function (e) {
console.log(e); // console.log(e);
console.log("%j", postoptions); // console.log("%j", postoptions);
def.reject(); def.reject(e);
}); });
post_request.on('timeout', function(){ post_request.on('timeout', function(){
post_request.abort(); post_request.abort();
def.reject(); def.reject("timeout");
}); });
post_request.write(getSocketState.body); post_request.write(getSocketState.body);
@ -466,14 +471,14 @@ WeMoNG.prototype.getInsightParams = function getInsightParams(socket) {
}); });
post_request.on('error', function (e) { post_request.on('error', function (e) {
console.log(e); // console.log(e);
console.log("%j", postoptions); // console.log("%j", postoptions);
def.reject(); def.reject(e);
}); });
post_request.on('timeout', function(){ post_request.on('timeout', function(){
post_request.abort(); post_request.abort();
def.reject(); def.reject("timeout");
}); });
post_request.write(getInsightParameters.body); post_request.write(getInsightParameters.body);
@ -521,28 +526,28 @@ WeMoNG.prototype.getLightStatus = function getLightStatus(light) {
}; };
def.resolve(obj); def.resolve(obj);
} else { } else {
def.reject(); def.reject(err);
console.log("err"); // console.log("err");
} }
}); });
} }
} else { } else {
console.log("err"); // console.log("err");
def.reject(); def.reject(err);
} }
}); });
}); });
}); });
post_request.on('error', function (e) { post_request.on('error', function (e) {
console.log(e); // console.log(e);
console.log("%j", postoptions); // console.log("%j", postoptions);
def.reject(); def.reject(e);
}); });
post_request.on('timeout', function () { post_request.on('timeout', function () {
console.log("Timeout"); // console.log("Timeout");
post_request.abort(); post_request.abort("timeout");
def.reject(); def.reject();
}); });
@ -578,12 +583,12 @@ WeMoNG.prototype.setStatus = function setStatus(light, capability, value) {
}); });
post_request.on('error', function (e) { post_request.on('error', function (e) {
console.log(e); // console.log(e);
console.log("%j", postoptions); // console.log("%j", postoptions);
}); });
post_request.on('timeout', function () { post_request.on('timeout', function () {
console.log("Timeout"); // console.log("Timeout");
post_request.abort(); post_request.abort();
}); });
@ -612,7 +617,7 @@ WeMoNG.prototype.parseEvent = function parseEvent(evt) {
msg.value = res['StateEvent']['Value'][0]; msg.value = res['StateEvent']['Value'][0];
def.resolve(msg); def.resolve(msg);
} else { } else {
def.reject(); def.reject(err);
} }
}); });
} else if (prop.hasOwnProperty('BinaryState')) { } else if (prop.hasOwnProperty('BinaryState')) {
@ -629,7 +634,7 @@ WeMoNG.prototype.parseEvent = function parseEvent(evt) {
} }
} else { } else {
//error //error
def.reject(); def.reject(err);
} }
}); });

View File

@ -1,6 +1,6 @@
{ {
"name": "node-red-node-wemo", "name": "node-red-node-wemo",
"version": "0.2.0", "version": "0.2.1",
"description": "Input and Output nodes for Belkin WeMo devices", "description": "Input and Output nodes for Belkin WeMo devices",
"repository": "https://github.com/node-red/node-red-nodes/tree/master/hardware", "repository": "https://github.com/node-red/node-red-nodes/tree/master/hardware",
"main": "WeMoNG.js", "main": "WeMoNG.js",
@ -12,11 +12,16 @@
"wemo", "wemo",
"belkin" "belkin"
], ],
"author": { "contributors": [
"email": "hardillb@gmail.com", {
"name": "Benjamin Hardill", "email": "hardillb@gmail.com",
"url": "http://www.hardill.me.uk/wordpress/" "name": "Benjamin Hardill",
}, "url": "http://www.hardill.me.uk/wordpress/"
},
{
"name": "Bart Butenaers"
}
],
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"node-ssdp": "~3.2.5", "node-ssdp": "~3.2.5",