Skip to content

Commit c881b10

Browse files
committed
switched to bluebird promises, added number for zone selection, fixed zone parameters for some methods, added zone tests, switched to using promises in tests
1 parent 9d373ee commit c881b10

File tree

5 files changed

+185
-228
lines changed

5 files changed

+185
-228
lines changed

chainedCommands.js

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// var simpleCommands = require('./simpleCommands');
2-
var deferred = require('deferred');
1+
var Promise = require("bluebird");
2+
var xml2js = Promise.promisifyAll(require("xml2js"));
33

44
function Yamaha()
55
{
@@ -8,32 +8,26 @@ function Yamaha()
88
// Navigates and selects the #number of the webradio favorites
99
Yamaha.prototype.switchToFavoriteNumber = function(favoritelistname, number){
1010
var self = this;
11-
var d = deferred();
12-
self.powerOn().done(function(){
13-
self.setMainInputTo("NET RADIO").done( function(){
14-
self.selectWebRadioListItem(1).done(function(){
15-
self.whenMenuReady("NET_RADIO").done(function(){
16-
self.selectWebRadioListItem(number).done(d.resolve);
11+
return self.powerOn().then(function(){
12+
self.setMainInputTo("NET RADIO").then( function(){
13+
self.selectWebRadioListItem(1).then(function(){
14+
self.whenMenuReady("NET_RADIO").then(function(){
15+
return self.selectWebRadioListItem(number);
1716
});
1817
});
1918
});
2019
});
21-
return d.promise;
2220
};
2321

24-
22+
// unfinished - not working
2523
Yamaha.prototype.switchToWebRadioWithName = function(name){
2624
var self = this;
27-
self.setMainInputTo("NET RADIO").done(function(){
25+
self.setMainInputTo("NET RADIO").then(function(){
2826

29-
self.getWebRadioList().done(function(result){
27+
self.getWebRadioList().then(xml2js.parseStringAsync).then(function(result){
3028
console.log(result);
31-
parseString(result, function (err, result) {
32-
console.dir(result);
33-
});
34-
3529
}, function (err) {
36-
console.log("err "+err);
30+
console.log("err "+err);
3731
});
3832

3933
});

mochatest.js

+100-103
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
var assert = require("assert");
1+
var chai = require('chai');
2+
var chaiAsPromised = require("chai-as-promised");
3+
chai.use(chaiAsPromised);
24
var expect = require('chai').expect;
35
var Yamaha = require("./yamaha.js");
46

7+
8+
59
var yamaha_ip= process.argv[4] || "192.168.0.25";
610

711
// Tests For Yamaha RV-775
@@ -12,168 +16,161 @@ describe('Yamaha-API', function() {
1216
var yamaha = new Yamaha(yamaha_ip);
1317
});
1418

15-
16-
it('should be turned on', function(done) {
19+
it('should be turned on', function() {
1720
var yamaha = new Yamaha(yamaha_ip, 0.5);
18-
yamaha.powerOn().done(function(on){
19-
yamaha.isOn().done(function(result){
20-
expect(result).to.be.true;
21-
done();
22-
});
23-
});
21+
return expect(yamaha.powerOn().then(function(){
22+
return yamaha.isOn();
23+
})).to.eventually.be.true;
24+
2425
});
2526

26-
it('should be double turned on', function(done) {
27+
it('should be double turned on', function() {
2728
var yamaha = new Yamaha(yamaha_ip, 0.5);
28-
yamaha.powerOn().done(function(on){
29-
yamaha.isOn().done(function(result){
30-
expect(result).to.be.true;
31-
done();
32-
});
33-
});
29+
return expect(yamaha.powerOn().then(function(){
30+
return yamaha.isOn();
31+
})).to.eventually.be.true;
3432
});
3533

3634

37-
it('should return 16 Inputs', function(done) {
35+
it('should return 16 Inputs', function() {
3836
var yamaha = new Yamaha(yamaha_ip);
39-
yamaha.getAvailableInputs().done(function(inputs){
40-
expect(inputs).to.have.length(16);
41-
done();
42-
});
37+
return expect(yamaha.getAvailableInputs()).to.eventually.have.length(16);
4338
});
4439

45-
it('should set volume to -600', function(done) {
40+
it('should set volume to -600', function() {
4641
var yamaha = new Yamaha(yamaha_ip, 0.5);
47-
yamaha.setVolume(-600).done(function(on){
48-
yamaha.getVolume().done(function(volume){
49-
expect(volume).to.equal(-600);
50-
done();
51-
});
52-
});
42+
return expect(yamaha.setVolume(-600).then(function(on){
43+
return yamaha.getVolume();
44+
})).to.eventually.equal(-600);
5345
});
5446

55-
it('should increase volume by 100', function(done) {
47+
it('should increase volume by 100', function() {
5648
var yamaha = new Yamaha(yamaha_ip, 0.5);
57-
yamaha.volumeUp(100).done(function(on){
58-
yamaha.getVolume().done(function(volume){
59-
expect(volume).to.equal(-500);
60-
done();
61-
});
62-
});
49+
return expect(yamaha.volumeUp(100).then(function(on){
50+
return yamaha.getVolume();
51+
})).to.eventually.equal(-500);;
6352
});
6453

6554

66-
// it('should switch to the webradio favorites using the chained command', function(done) {
55+
// it('should switch to the webradio favorites using the chained command', function() {
6756
// var yamaha = new Yamaha(yamaha_ip, 0.5);
6857

69-
// yamaha.switchToFavoriteNumber(1).done(function(result){
70-
// yamaha.getCurrentInput().done(function(result){
58+
// yamaha.switchToFavoriteNumber(1).then(function(result){
59+
// yamaha.getCurrentInput().then(function(result){
7160
// expect(result).to.equal("NET RADIO");
72-
// done();
7361
// });
74-
// });
62+
// }.to.eventually.be.false);
7563

7664
// });
7765

7866

79-
it('should switch to HDMI2', function(done) {
67+
it('should switch to HDMI2', function() {
8068
var yamaha = new Yamaha(yamaha_ip, 0.5);
81-
yamaha.setMainInputTo("HDMI2").done(function() {
82-
yamaha.getCurrentInput().done(function(result){
83-
expect(result).to.equal("HDMI2");
84-
done();
85-
});
86-
});
69+
return expect(yamaha.setMainInputTo("HDMI2").then(function() {
70+
return yamaha.getCurrentInput();
71+
})).to.eventually.equal("HDMI2");
8772

8873
});
8974

90-
it('should switch to NET RADIO', function(done) {
75+
it('should switch to NET RADIO', function() {
9176
var yamaha = new Yamaha(yamaha_ip, 0.5);
92-
yamaha.setMainInputTo("NET RADIO").done(function() {
93-
yamaha.getCurrentInput().done(function(result){
94-
expect(result).to.equal("NET RADIO");
95-
done();
96-
});
97-
});
77+
return expect(yamaha.setMainInputTo("NET RADIO").then(function() {
78+
return yamaha.getCurrentInput();
79+
})).to.eventually.equal("NET RADIO");
9880

9981
});
10082

101-
it('should switch to the webradio favorites and wait to be ready', function(done) {
83+
it('should switch to the webradio favorites and wait to be ready', function() {
10284
var yamaha = new Yamaha(yamaha_ip, 0.5);
10385

104-
yamaha.whenMenuReady("NET_RADIO").done(function(result){
105-
yamaha.selectWebRadioListItem(1).done(function(inputs){
106-
yamaha.whenMenuReady("NET_RADIO").done(function(result){
107-
expect(result).to.be.true;
108-
done();
109-
});
86+
return expect(yamaha.whenMenuReady("NET_RADIO").then(function(result){
87+
return yamaha.selectWebRadioListItem(1).then(function(inputs){
88+
return yamaha.whenMenuReady("NET_RADIO");
11089
});
90+
})).to.eventually.be.true;
11191

112-
});
92+
});
11393

94+
it('should switch to partey mode on', function() {
95+
var yamaha = new Yamaha(yamaha_ip, 0.5);
96+
return expect(yamaha.partyModeOn().then(function(on){
97+
return yamaha.isPartyModeEnabled();
98+
})).to.eventually.be.true;
11499
});
115100

116-
it('should switch to partey mode on', function(done) {
101+
it('should switch to partey mode off', function() {
117102
var yamaha = new Yamaha(yamaha_ip, 0.5);
118-
yamaha.partyModeOn().done(function(on){
119-
yamaha.isPartyModeEnabled().done(function(result){
120-
expect(result).to.be.true;
121-
done();
122-
});
123-
});
103+
return expect(yamaha.partyModeOff().then(function(on){
104+
return yamaha.isPartyModeEnabled();
105+
})).to.eventually.be.false;
124106
});
125107

126-
it('should switch to partey mode off', function(done) {
108+
it('should mute main_zone', function() {
127109
var yamaha = new Yamaha(yamaha_ip, 0.5);
128-
yamaha.partyModeOff().done(function(on){
129-
yamaha.isPartyModeEnabled().done(function(result){
130-
expect(result).to.be.false;
131-
done();
132-
});
133-
});
110+
return expect(yamaha.muteOn().then(function(on){
111+
return yamaha.isMuted();
112+
})).to.eventually.be.true;
134113
});
135114

136-
it('should mute', function(done) {
115+
it('should unmute main_zone', function() {
137116
var yamaha = new Yamaha(yamaha_ip, 0.5);
138-
yamaha.muteOn().done(function(on){
139-
yamaha.isMuted().done(function(result){
140-
expect(result).to.be.true;
141-
done();
142-
});
143-
});
117+
return expect(yamaha.muteOff().then(function(on){
118+
return yamaha.isMuted();
119+
})).to.eventually.be.false;
144120
});
145121

146-
it('should unmute', function(done) {
122+
123+
it('should turn on zone 2', function() {
147124
var yamaha = new Yamaha(yamaha_ip, 0.5);
148-
yamaha.muteOff().done(function(on){
149-
yamaha.isMuted().done(function(result){
150-
expect(result).to.be.false;
151-
done();
152-
});
153-
});
125+
return expect(yamaha.powerOn(2).then(function(){
126+
return yamaha.isOn(2);
127+
})).to.eventually.be.true;
128+
154129
});
155130

156-
it('should be turned off', function(done) {
131+
it('should mute zone 2', function() {
157132
var yamaha = new Yamaha(yamaha_ip, 0.5);
158-
yamaha.powerOff().done(function(on){
159-
yamaha.isOn().done(function(result){
160-
expect(result).to.be.false;
161-
done();
162-
});
163-
});
133+
return expect(yamaha.muteOn(2).then(function(on){
134+
return yamaha.isMuted(2);
135+
})).to.eventually.be.true;
136+
});
137+
138+
it('should still have main zone unmuted', function() {
139+
var yamaha = new Yamaha(yamaha_ip, 0.5);
140+
return expect(yamaha.isMuted()).to.eventually.be.false;
164141
});
165142

143+
it('should unmute zone 2', function() {
144+
var yamaha = new Yamaha(yamaha_ip, 0.5);
145+
return expect(yamaha.muteOff(2).then(function(on){
146+
return yamaha.isMuted(2);
147+
})).to.eventually.be.false;
148+
});
166149

167-
// it('should list the webradio favorites list info', function(done) {
150+
it('should be turn off zone 2', function() {
151+
var yamaha = new Yamaha(yamaha_ip, 0.5);
152+
return expect(yamaha.powerOff(2).then(function(on){
153+
return yamaha.isOn(2);
154+
})).to.eventually.be.false;
155+
});
156+
157+
it('should be turned off', function() {
158+
var yamaha = new Yamaha(yamaha_ip, 0.5);
159+
return expect(yamaha.powerOff().then(function(on){
160+
return yamaha.isOn();
161+
})).to.eventually.be.false;
162+
});
163+
164+
165+
// it('should list the webradio favorites list info', function() {
168166
// var yamaha = new Yamaha(yamaha_ip, 0.5);
169167

170-
// yamaha.whenMenuReady("NET_RADIO").done(function(result){
171-
// yamaha.selectWebRadioListItem(1).done(function(inputs){
172-
// yamaha.whenMenuReady("NET_RADIO").done(function(result){
168+
// return yamaha.whenMenuReady("NET_RADIO").then(function(result){
169+
// return yamaha.selectWebRadioListItem(1).then(function(inputs){
170+
// return yamaha.whenMenuReady("NET_RADIO").then(function(result){
173171
// expect(result).to.be.true;
174-
// done();
175172
// });
176-
// });
173+
// }.to.eventually.be.false);
177174

178175
// });
179176

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"description": "An API to control your YAMAHA Receiver written in nodejs",
55
"main": "yamaha.js",
66
"dependencies": {
7-
"deferred": "~0.7.1",
8-
"request": "~2.40.0",
9-
"xml2js": "~0.4.4",
10-
"async": "~0.9.0"
7+
"bluebird": "^3.3.4",
8+
"request": "^2.69.0",
9+
"xml2js": "^0.4.16"
1110
},
1211
"devDependencies": {
13-
"chai": "~1.9.1"
12+
"chai": "^3.5.0",
13+
"chai-as-promised": "^5.2.0"
1414
},
1515
"scripts": {
1616
"test": "mocha mochatest"

0 commit comments

Comments
 (0)