-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyamaha_controller.js
145 lines (138 loc) · 4.93 KB
/
yamaha_controller.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
145
var express = require('express');
var request = require('request');
var parseString = require('xml2js').parseString;
var logger = require('./log')
var app = express();
var yamaha_ip = null;
var yamaha_connected = false;
PowerState = {
OFF : 'Off',
ON : 'On',
STANDBY : 'Standby'
}
module.exports = {
connect: connect,
patioVolume: patioVolume,
command: command,
getStatus: getStatus
}
//<YAMAHA_AV cmd="PUT"><Pandora><Play_Control><Feedback>Thumb Up</Feedback></Play_Control></Pandora></YAMAHA_AV>
//<YAMAHA_AV cmd="PUT"><Pandora><Play_Control><Feedback>Thumb Down</Feedback></Play_Control></Pandora></YAMAHA_AV>
function connect(ip) {
yamaha_ip = ip;
console.log("Connecting to " + yamaha_ip);
var data = '<YAMAHA_AV cmd="GET"><System><Config>GetParam</Config></System></YAMAHA_AV>'
return request({
url: 'http://' + yamaha_ip + '/YamahaRemoteControl/ctrl',
timeout: 2000,
method: 'POST',
headers: {
'Content-Type': 'text/xml',
'Content-Length': data.length
},
body: data
}, function(error, response, body){
if(error) {
if (error.code == 'ETIMEDOUT') {
console.log('Timeout while connecting to ' + yamaha_ip);
yamaha_connected = false;
return 408;
}
else
{
console.log(error);
return 404;
}
} else {
parseString(body, function (err, result) {
if (result.YAMAHA_AV != null) {
console.log("Connected to " + yamaha_ip);
yamaha_connected = true;
return 200;
} else {
console.log('Failed to connect');
return 500;
}
})
}
})
}
function patioVolume(value) {
command('<YAMAHA_AV cmd="PUT"><Zone_2><Volume><Lvl><Val>' + value + '</Val><Exp>1</Exp><Unit>dB</Unit></Lvl></Volume></Zone_2></YAMAHA_AV>')
console.log(getStatus())
}
function getStatus() {
var data = '<YAMAHA_AV cmd="GET"><Zone_2><Basic_Status>GetParam</Basic_Status></Zone_2></YAMAHA_AV>'
request({
url: 'http://' + yamaha_ip + '/YamahaRemoteControl/ctrl',
timeout: 2000,
method: 'POST',
headers: {
'Content-Type': 'text/xml',
'Content-Length': data.length
},
body: data
}, function(error, response, body){
if(error) {
if (error.code == 'ETIMEDOUT') {
logger.error('Timeout while connecting to ' + yamaha_ip)
yamaha_connected = false
return null
}
else
{
logger.error(error)
yamaha_connected = false
return null
}
} else {
parseString(body, function (err, result) {
if (result.YAMAHA_AV != null) {
yamaha_connected = true
console.log(result.YAMAHA_AV)
return result.YAMAHA_AV
} else {
logger.error('Failed to connect');
yamaha_connected = false
return null
}
})
}
})
}
function command(data, callback) {
request({
url: 'http://' + yamaha_ip + '/YamahaRemoteControl/ctrl',
timeout: 2000,
method: 'POST',
headers: {
'Content-Type': 'text/xml',
'Content-Length': data.length
},
body: data
}, function(error, response, body){
if(error) {
if (error.code == 'ETIMEDOUT') {
console.log('Timeout while connecting to ' + yamaha_ip);
yamaha_connected = false;
callback(false)
}
else
{
console.log(error);
callback(false)
}
} else {
parseString(body, function (err, result) {
if (result.YAMAHA_AV != null) {
//console.log(body + " : success")
yamaha_connected = true;
callback(true)
} else {
console.log('Failed to connect');
callback(false)
}
});
}
});
}