-
Notifications
You must be signed in to change notification settings - Fork 2
/
PioneerAvrOut.js
226 lines (199 loc) · 5.89 KB
/
PioneerAvrOut.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
import { isJson, parse, random } from './utils';
const MSG_INTERVAL_MS = 180;
const channelToInput = {
PHONO: '00',
CD: '01',
TUNER: '02',
TAPE: '03',
DVD: '04',
TV: '05',
'SAT/CBL': '06',
SAT: '06',
CBL: '06',
CABLE: '06',
VIDEO: '10',
'MULTI CH IN': '12',
'USB-DAC': '13',
VIDEO2: '14',
'DVR/BDR': '15',
DVR: '15',
BDR: '15',
'IPOD/USB': '17',
USB: '17',
IPOD: '17',
'XM RADIO': '18',
HDMI1: '19',
HDMI2: '20',
HDMI3: '21',
HDMI4: '22',
HDMI5: '23',
HDMI6: '24',
BD: '25',
'MEDIA GALLERY': '26',
SIRIUS: '27',
'HDMI CYCLE': '31',
ADAPTER: '33',
HDMI7: '34',
HDMI8: '35',
NETRADIO: '38',
SIRIUSXM: '40',
PANDORA: '41',
'MEDIA SERVER': '44',
MEDIA: '44',
FAVORITE: '45',
MHL: '48',
GAME: '49',
SPOTIFY: '57',
};
const inputToChannel = {
'00': 'PHONO',
'01': 'CD',
'02': 'TUNER',
'03': 'TAPE',
'04': 'DVD',
'05': 'TV',
'06': 'SAT/CBL',
'10': 'VIDEO',
'12': 'MULTI CH IN',
'13': 'USB-DAC',
'14': 'VIDEO2',
'15': 'DVR/BDR',
'17': 'IPOD/USB',
'18': 'XM RADIO',
'19': 'HDMI1',
'20': 'HDMI2',
'21': 'HDMI3',
'22': 'HDMI4',
'23': 'HDMI5',
'24': 'HDMI6',
'25': 'BD',
'26': 'MEDIA GALLERY',
'27': 'SIRIUS',
'31': 'HDMI CYCLE',
'33': 'ADAPTER',
'34': 'HDMI7',
'35': 'HDMI8',
'38': 'NETRADIO',
'40': 'SIRIUSXM',
'41': 'PANDORA',
'44': 'MEDIA SERVER',
'45': 'FAVORITE',
'48': 'MHL',
'49': 'GAME',
'57': 'SPOTIFY',
};
export default function PioneerAvrOut(RED) {
return function(config) {
const node = this;
const queue = [];
let isBusy = false;
const avrState = {
volume: null,
muted: null,
on: null,
channel: null,
};
const updateFullState = () => {
onInput({ payload: '?V' });
onInput({ payload: '?P' });
onInput({ payload: '?M' });
onInput({ payload: '?F' });
};
const onData = d => {
const data = d
.toString()
.replace('\n', '')
.replace('\r', '');
node.debug(`Data from avr: ${data}`);
if (data.startsWith('PWR')) {
avrState.on = parse(data[3]) === 0;
}
if (data.startsWith('VOL')) {
avrState.volume = Math.round(
parse(data.substring(3, 6)) * node.serverConfig.volumeMultiplier
);
}
if (data.startsWith('MUT')) {
avrState.muted = parse(data[3]) === 0;
}
if (data.startsWith('FN')) {
avrState.channel = inputToChannel[data.substring(2, 4)];
}
if (data !== 'R') {
node.trace(`AVR state ${JSON.stringify(avrState)}`);
}
};
const sendData = data => {
node.debug(`Sending to AVR ${data}`);
node.serverConfig.client.write(`${data}\n\r`);
};
const sendNext = () => {
if (isBusy || !queue.length) {
return;
}
isBusy = true;
let msg = queue.shift();
if (isJson(msg)) {
msg = JSON.parse(msg);
}
if (typeof msg === 'object') {
if (
typeof msg.volume !== 'undefined' &&
(msg.volume < avrState.volume - 1 || msg.volume > avrState.volume + 1)
) {
// send the same message again in case there's more to do than just one VU or VD
queue.unshift(msg);
if (msg.volume < avrState.volume) {
sendData(`VD`);
} else {
sendData(`VU`);
}
} else {
if (
typeof msg.channel !== 'undefined' &&
msg.channel.toUpperCase() !== avrState.channel
) {
queue.unshift(`${channelToInput[msg.channel.toUpperCase()]}FN`);
}
if (typeof msg.muted !== 'undefined' && msg.muted !== avrState.muted) {
queue.unshift(`M${msg.muted ? 'O' : 'F'}`);
}
if (typeof msg.on !== 'undefined' && msg.on !== avrState.on) {
// send a second message to create enough time for receiver to turn on
queue.unshift(`?P`);
queue.unshift(`P${msg.on ? 'O' : 'F'}`);
}
}
} else {
sendData(msg);
}
setTimeout(() => {
isBusy = false;
sendNext();
}, MSG_INTERVAL_MS);
};
const onInput = msg => {
node.trace(`Node input: ${JSON.stringify(msg.payload)}`);
queue.push(msg.payload);
sendNext();
};
(function init() {
RED.nodes.createNode(node, config);
node.serverConfig = RED.nodes.getNode(config.server);
if (!node.serverConfig || !node.serverConfig.hostname) {
node.status({ fill: 'red', shape: 'ring', text: 'Hostname not set' });
return;
}
node.serverConfig.registerClientNode(node);
node.on('input', onInput);
node.on('close', function() {
if (node.serverConfig) {
node.serverConfig.client.off('data', onData);
node.serverConfig.deregisterClientNode(node);
}
});
node.serverConfig.client.on('data', onData);
setTimeout(updateFullState, random(10, 500));
})();
};
}