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';
console.log('Callback URL = %s',callback_url);
// console.log('Callback URL = %s',callback_url);
var subscribeOptions = {
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();
}
}
@ -221,12 +229,21 @@ module.exports = function(RED) {
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);
send = send || function() { node.send.apply(node,arguments) }
if (!dev) {
//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;
}
@ -398,12 +415,19 @@ module.exports = function(RED) {
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);
send = send || function() { node.send.apply(node,arguments) }
if (!dev) {
//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;
}
@ -427,7 +451,16 @@ module.exports = function(RED) {
delete status.capabilities;
// }
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') {
console.log("socket_insight");
@ -437,17 +470,36 @@ module.exports = function(RED) {
msg.payload = insightParameters;
// 'state' should be a number for backwards compatibility
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 {
console.log("socket");
//console.log("socket");
// classic socket: no power measurement, so only request current switch status
wemo.getSocketStatus(dev)
.then(function(status) {
msg.payload = {
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
};
//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;
wemo.emit('event',evt);
})
.catch(err => {
console.log(err)
});
res.send('');
});

View File

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

View File

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