-
Notifications
You must be signed in to change notification settings - Fork 2
/
yamaha.js
144 lines (121 loc) · 3.75 KB
/
yamaha.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var Request = require('request');
var Q = require('q');
var StringFormat = require('string-format');
var parseString = require('xml2js').parseString;
var YamahaCommands = require('./yamahaCommands');
var YamahaDiscovery = require('./yamahaDiscovery');
function Yamaha(ip){
this.commands = new YamahaCommands();
this.discovery = new YamahaDiscovery(ip);
}
Yamaha.prototype.discover = function(){
return this.discovery.getIp();
};
Yamaha.prototype.isOnline = function(){
var xml = this.commands.basicStatusCommand();
return deferredAction(this.discovery, xml, function(result){
// Check that returned xml is in valid Yamaha format
return result.YAMAHA_AV !== undefined;
});
};
Yamaha.prototype.getStatus = function(){
var xml = this.commands.basicStatusCommand();
return deferredAction(this.discovery, xml, function(result){
return result;
});
};
Yamaha.prototype.getSystemConfig = function(){
var xml = this.commands.systemConfigCommand();
return deferredAction(this.discovery, xml, function(result){
return result;
});
};
Yamaha.prototype.isOn = function(){
var xml = this.commands.basicStatusCommand();
return deferredAction(this.discovery, xml, function(result){
return result.YAMAHA_AV.Main_Zone[0].Basic_Status[0].Power_Control[0].Power[0] === "On";
});
};
Yamaha.prototype.setPower = function(state){
var xml;
if (state == "on"){
xml = this.commands.powerOnCommand();
}else{
xml = this.commands.powerOffCommand();
}
return deferredAction(this.discovery, xml, function(result){
return result.YAMAHA_AV.Main_Zone[0].Power_Control[0].Power[0];
});
};
Yamaha.prototype.setMute = function(state){
var xml;
if (state == "on"){
xml = this.commands.muteOnCommand();
}else{
xml = this.commands.muteOffCommand();
}
return deferredAction(this.discovery, xml, function(result){
return result.YAMAHA_AV.Main_Zone !== undefined;
});
};
Yamaha.prototype.getVolume = function(){
var xml = this.commands.getVolumeCommand();
return deferredAction(this.discovery, xml, function(result){
return result.YAMAHA_AV.Main_Zone[0].Volume[0].Lvl[0].Val[0];
});
};
Yamaha.prototype.setVolume = function(volume){
var xml = this.commands.setVolumeCommand(volume);
return deferredAction(this.discovery, xml, function(result){
return result.YAMAHA_AV.Main_Zone[0].Volume[0].Lvl[0].Val[0];
});
};
Yamaha.prototype.setInput = function(input_name){
var xml = this.commands.setInputCommand(input_name);
return deferredAction(this.discovery, xml, function(result){
// Responses Input_Sel is ''
return result.YAMAHA_AV.Main_Zone[0].Input[0].Input_Sel[0] !== undefined;
});
};
Yamaha.prototype.setScene = function(scene_num){
var xml = this.commands.setSceneCommand(scene_num);
return deferredAction(this.discovery, xml, function(result){
// Responses Scene_Sel is ''
return result.YAMAHA_AV.Main_Zone[0].Scene[0].Scene_Sel[0] !== undefined;
});
};
function deferredAction(discovery, commandXml, parseAction){
var deferred = Q.defer();
discovery.getUrl()
.then(function (url){
return getCommandReply(url, commandXml);
})
.then(function (response){
parseString(response, function (err, result){
var res = parseAction(result);
deferred.resolve(res);
});
})
.catch(function (error){
deferred.reject("No connection");
});
return deferred.promise;
}
function getCommandReply(url, commandXml){
var deferred = Q.defer();
var request = Request.post({
method: 'POST',
uri: url,
body: commandXml
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
deferred.resolve(body);
}
else{
deferred.reject(body);
}
});
return deferred.promise;
}
module.exports = Yamaha;