This repository has been archived by the owner on Mar 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
onkyo.js
executable file
·294 lines (258 loc) · 7.17 KB
/
onkyo.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// SETUP
//
var _ = require('lodash');
var fs = require('fs');
var sources = JSON.parse(fs.readFileSync('sources.json'));
var config = JSON.parse(fs.readFileSync('config.json'));
var Promise = require('bluebird');
var events = require('events');
var emitter = new events.EventEmitter();
var SerialPort = require('serialport');
var port = new SerialPort('/dev/ttyUSB0', {
parser: SerialPort.parsers.byteDelimiter([26]),
baudRate: 9600,
autoOpen: false
});
var app = require('express')();
//
// CONSTANTS
//
const CMD_POWER = 'PWR';
const CMD_MASTERVOL = 'MVL';
const CMD_INPUT = 'SLI';
const CMD_Z2_POWER = 'ZPW';
const CMD_Z2_VOLUME = 'ZVL';
const CMD_Z2_INPUT = 'SLZ';
function zero_pad_hex(value) {
var val = parseInt(value);
return val < 10 ? '0' + val.toString(16).toUpperCase() : val.toString(16).toUpperCase();
}
Object.prototype.getKeyIn = function( value ) {
for ( var prop in this ) {
if( this.hasOwnProperty( prop ) ) {
if (typeof this[ prop ] === 'string' || this[ prop ] instanceof String) {
var propval = this[ prop ];
var propvals = propval.split(',');
if (_.includes(propvals, value))
return prop;
}
}
}
}
//
// REST API
//
app.get('/', (req, res) => {
res.json({ message: 'Server is loaded and running.' });
});
// wait for responses for all of the following queries
var queries = [
new Promise((resolve) => { emitter.on(CMD_POWER, resolve); }),
new Promise((resolve) => { emitter.on(CMD_MASTERVOL, resolve); }),
new Promise((resolve) => { emitter.on(CMD_INPUT, resolve); }),
new Promise((resolve) => { emitter.on(CMD_Z2_POWER, resolve); }),
new Promise((resolve) => { emitter.on(CMD_Z2_VOLUME, resolve); }),
new Promise((resolve) => { emitter.on(CMD_Z2_INPUT, resolve); })
];
app.get('/status', (req, res) => {
query_status();
Promise.all(queries)
.then( () => {
res.json(onkyo_status);
});
});
// MASTER
app.get('/master/volume/:level?', (req, res) => {
if (typeof req.params.level !== undefined && req.params.level) {
write_cmd(CMD_MASTERVOL + zero_pad_hex(req.params.level));
write_cmd('MVLQSTN'),
new Promise((resolve) => { emitter.on(CMD_MASTERVOL, resolve); }).then(() => res.json(onkyo_status));
} else {
res.json(onkyo_status);
}
});
app.get('/master/power/:state?', (req, res) => {
if (typeof req.params.state !== undefined && req.params.state) {
if (req.params.state != 'on' && req.params.state != 'off') {
return;
}
write_cmd(CMD_POWER + (req.params.state == 'on' ? '01' : '00'));
write_cmd('PWRQSTN');
new Promise((resolve) => { emitter.on(CMD_POWER, resolve); }).then(() => res.json(onkyo_status));
} else {
res.json(onkyo_status);
}
});
app.get('/master/input/:source?', (req, res) => {
if (typeof req.params.source !== undefined && req.params.source) {
var source = sources.getKeyIn(req.params.source);
if (source !== undefined && source) {
write_cmd(CMD_INPUT + source);
write_cmd('SLIQSTN');
new Promise((resolve) => { emitter.on(CMD_INPUT, resolve); }).then(() => res.json(onkyo_status));
} else {
res.json({ 'error': 'source ' + req.params.source + ' not found.' });
}
} else {
res.json(onkyo_status);
}
});
// ZONE 2
app.get('/zone2/volume/:level?', (req, res) => {
if (typeof req.params.level !== undefined && req.params.level) {
write_cmd(CMD_Z2_VOLUME + zero_pad_hex(req.params.level));
write_cmd('ZVLQSTN'),
new Promise((resolve) => { emitter.on(CMD_Z2_VOLUME, resolve); }).then(() => res.json(onkyo_status));
} else {
res.json(onkyo_status);
}
});
app.get('/zone2/power/:state?', (req, res) => {
if (typeof req.params.state !== undefined && req.params.state) {
if (req.params.state != 'on' && req.params.state != 'off') {
return;
}
write_cmd(CMD_Z2_POWER + (req.params.state == 'on' ? '01' : '00'));
write_cmd('ZPWQSTN');
new Promise((resolve) => { emitter.on(CMD_Z2_POWER, resolve); }).then(() => res.json(onkyo_status));
} else {
res.json(onkyo_status);
}
});
app.get('/zone2/input/:source?', (req, res) => {
if (typeof req.params.source !== undefined && req.params.source) {
var source = sources.getKeyIn(req.params.source);
if (source !== undefined && source) {
write_cmd(CMD_Z2_INPUT + source);
write_cmd('SLZQSTN');
new Promise((resolve) => { emitter.on(CMD_Z2_INPUT, resolve); }).then(() => res.json(onkyo_status));
} else {
res.json({ 'error': 'source ' + req.params.source + ' not found.' });
}
} else {
res.json(onkyo_status);
}
});
//
// SERIAL PORT COMMUNICATION
//
var onkyo_status = {
master: {
power: false,
volume: 0,
source: ''
},
zone2: {
power: false,
volume: 0,
source: ''
}
};
function write_cmd(cmd) {
if (port.isOpen()) {
//console.log("SENDING: " + cmd);
port.write('!1' + cmd + '\r');
}
};
function connect() {
if (!port.isOpen()) {
port.open();
}
};
function query_status() {
write_cmd('PWRQSTN'),
write_cmd('MVLQSTN'),
write_cmd('SLIQSTN'),
write_cmd('ZPWQSTN'),
write_cmd('ZVLQSTN'),
write_cmd('SLZQSTN'),
write_cmd('LMDQSTN')
};
function process_message(message) {
format_str = /\!1([A-Z]{3})(.{2})?/g;
if (match = format_str.exec(message)) {
cmd = match[1];
value = match[2];
switch (cmd) {
case CMD_POWER:
onkyo_status.master.power = (value == '01');
emitter.emit(CMD_POWER);
break;
case CMD_MASTERVOL:
onkyo_status.master.volume = parseInt('0x' + value);
emitter.emit(CMD_MASTERVOL);
break;
case CMD_INPUT:
onkyo_status.master.source = sources[value];
emitter.emit(CMD_INPUT);
break;
case CMD_Z2_POWER:
onkyo_status.zone2.power = (value == '01');
emitter.emit(CMD_Z2_POWER);
break;
case CMD_Z2_VOLUME:
onkyo_status.zone2.volume = parseInt('0x' + value);
emitter.emit(CMD_Z2_VOLUME);
break;
case CMD_Z2_INPUT:
onkyo_status.zone2.source = sources[value];
emitter.emit(CMD_Z2_INPUT);
break;
default:
break;
}
}
};
function print_status() {
console.log(onkyo_status);
};
function receive() {
return new Promise( (resolve, reject) => {
port.on('data', (data) => {
var buffer = Buffer.from(data);
var message = buffer.toString('utf8');
//console.log(message);
process_message(message);
resolve(port);
});
port.on('error', (err) => {
reject(err);
});
});
}
port.on('close', () => {
setTimeout(connect, 100);
});
port.on('open', (err) => {
if (err) {
console.log('error on open ' + err);
} else {
query_status();
}
});
port.on('error', (err) => {
console.log('error: ', err.message);
setTimeout(connect, 100);
});
port.on('data', (data) => {
var buffer = Buffer.from(data);
var message = buffer.toString('utf8');
//console.log(message);
process_message(message);
});
//
// MAIN
//
connect();
app.listen(config.rest_port);
//
// query the status on a regular basis
//
setInterval(query_status, config.refresh_frequency);
//
// KEYPRESS TO QUIT
//
//process.stdin.setRawMode(true);
//process.stdin.resume();
//process.stdin.on('data', process.exit.bind(process, 0));