Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle request timeout error #18

Open
bogdanim36 opened this issue Dec 19, 2017 · 5 comments
Open

Handle request timeout error #18

bogdanim36 opened this issue Dec 19, 2017 · 5 comments

Comments

@bogdanim36
Copy link

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.

@PSeitz
Copy link
Owner

PSeitz commented Dec 19, 2017

Probably same as #7

Did you try catching and handling the errors from the promise?

@bogdanim36
Copy link
Author

bogdanim36 commented Dec 19, 2017

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.

@PSeitz
Copy link
Owner

PSeitz commented Dec 19, 2017

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.

@bogdanim36
Copy link
Author

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.

@bogdanim36
Copy link
Author

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>GetParam</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>Off</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>All</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>One</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>GetParam</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>On</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>Off</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>' + move + '</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>' + move + '</List_Control></' + listname + '></YAMAHA_AV>';
return this.SendXMLToReceiver(command);
};

Yamaha.prototype.getSERVERList = function () {
return this.getList("SERVER");
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants