yamaha-nodejs
yamaha-nodejs copied to clipboard
Handle request timeout error
Hi, I succeed to create my own web app using your api. It's my first nodejs application. I have one problem: from time to time my wireless network break, and in this case Yamaha.prototype.SendXMLToReceiver method throw and timeout error, that cause my nodejs app to crash. I tried a lot of method to avoid app exit, without success. Can you you help me? Thx.
Probably same as https://github.com/PSeitz/yamaha-nodejs/issues/7
Did you try catching and handling the errors from the promise?
I don't know how to do this :(. I find the line with prom.catch method:
Yamaha.prototype.SendXMLToReceiver = function(xml) {
var self = this;
return this.getOrDiscoverIP().then(ip => {
var isPutCommand = xml.indexOf("cmd=\"PUT\"" >= 0);
var delay = isPutCommand ? this.responseDelay * 1000 : 0;
var req = {
method: 'POST',
uri: 'http://' + ip + '/YamahaRemoteControl/ctrl',
body: xml
};
if (this.requestTimeout) req.timeout = this.requestTimeout;
var prom = request.postAsync(req).delay(delay).then(response => response.body)
if (self.catchRequestErrors === true) prom.catch(console.log.bind(console));
return prom
})
};
prom.catch method is fired on timeout error, but i don't know to do in callback , to stop exit app.listen.
It's not the same as #7. I receive ETIMEOUT error from nodejs server.
I meant same as in #7 as the errors are not handled by the user.
The lib returns bluebird promises: http://bluebirdjs.com/docs/api-reference.html Catch all errors:
yamaha.setMainInputTo("HDMI2").then(function() {
return yamaha.getCurrentInput();
}).catch(function(e) {
});
With the catchRequestErrors the lib swallows errors and prints them.
I did now like this:
var yamahaApi = require("yamaha.api")
var yamahaCtrl = new yamahaApi("192.168.1.2");
and the call
yamaha.getBasicInfo(zone).then(function (info) {
res.send(info);
}).catch(function (error) {
res.send(error);
});
and server still exit with error ETIMEDOUT.
I think I solved. with the solution above. I added a lot of commands on yamaha.simple.commands.api.js.:
Yamaha.prototype.getRepeatInfo = function (input) { var command = '<YAMAHA_AV cmd="GET"><' + input + '><Play_Control><Play_Mode><Repeat>GetParam</Repeat></Play_Mode></Play_Control></' + input + '></YAMAHA_AV>'; return this.SendXMLToReceiver(command).then(xml2js.parseStringAsync); };
Yamaha.prototype.repeatOff = function (input) { var command = '<YAMAHA_AV cmd="PUT"><' + input + '><Play_Control><Play_Mode><Repeat>Off</Repeat></Play_Mode></Play_Control></' + input + '></YAMAHA_AV>'; return this.SendXMLToReceiver(command); };
Yamaha.prototype.repeatAll = function (input) { var command = '<YAMAHA_AV cmd="PUT"><' + input + '><Play_Control><Play_Mode><Repeat>All</Repeat></Play_Mode></Play_Control></' + input + '></YAMAHA_AV>'; return this.SendXMLToReceiver(command); };
Yamaha.prototype.repeatOne = function (input) { var command = '<YAMAHA_AV cmd="PUT"><' + input + '><Play_Control><Play_Mode><Repeat>One</Repeat></Play_Mode></Play_Control></' + input + '></YAMAHA_AV>'; return this.SendXMLToReceiver(command); };
Yamaha.prototype.getShuffleInfo= function (input) { var command = '<YAMAHA_AV cmd="GET"><' + input + '><Play_Control><Play_Mode><Shuffle>GetParam</Shuffle></Play_Mode></Play_Control></' + input + '></YAMAHA_AV>'; return this.SendXMLToReceiver(command).then(xml2js.parseStringAsync); };
Yamaha.prototype.shuffleOn = function (input) { var command = '<YAMAHA_AV cmd="PUT"><' + input + '><Play_Control><Play_Mode><Shuffle>On</Shuffle></Play_Mode></Play_Control></' + input + '></YAMAHA_AV>'; return this.SendXMLToReceiver(command); };
Yamaha.prototype.shuffleOff = function (input) { var command = '<YAMAHA_AV cmd="PUT"><' + input + '><Play_Control><Play_Mode><Shuffle>Off</Shuffle></Play_Mode></Play_Control></' + input + '></YAMAHA_AV>'; return this.SendXMLToReceiver(command); };
Yamaha.prototype.menuCursorMove = function (listname, move) { if (["Return", "Up", "Down"].indexOf(move) === -1) throw "Cursor move can be only Retur, Up or Down"; var command = '<YAMAHA_AV cmd="PUT"><' + listname + '><List_Control><Cursor>' + move + '</Cursor></List_Control></' + listname + '></YAMAHA_AV>'; return this.SendXMLToReceiver(command); };
Yamaha.prototype.menuPageMove = function (listname, move) { if (["Up", "Down"].indexOf(move) === -1) throw "Cursor move can be only Retur, Up or Down"; var command = '<YAMAHA_AV cmd="PUT"><' + listname + '><List_Control><Page>' + move + '</Page></List_Control></' + listname + '></YAMAHA_AV>'; return this.SendXMLToReceiver(command); };
Yamaha.prototype.getSERVERList = function () { return this.getList("SERVER"); };