forked from Alwinfy/denon-avr-telnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch.js
60 lines (55 loc) · 1.68 KB
/
dispatch.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
"use strict";
const makeBoolean = (truthy, falsey) => ({
into(bool) {
return bool ? truthy : falsey;
},
from(string) {
switch (string) {
case truthy: return true;
case falsey: return false;
default: throw new Error(`Expected ${truthy} or ${falsey} from PW, got ${string}`);
}
}
});
const SI_TYPES = "PHONO CD TUNER DVD BD TV SAT/CBL CBL/SAT DVR GAME GAME2 V.AUX AUX1 MPLAY DOCK HDRADIO IPOD NET/USB RHAPSODY NAPSTER PANDORA LASTFM FLICKR FAVORITES IRADIO SERVER USB/IPOD USB IPD FVP IRP NETWORK SPOTIFY".split(" ");
const dispatchTable = {
"PW": makeBoolean("ON", "STANDBY"),
"MU": makeBoolean("ON", "OFF"),
"SI": {
into(value) {
if (~SI_TYPES.indexOf(value)) {
return value;
}
throw new Error(`Tried to set unsupported input source: ${value}`);
},
from(string) {
return string;
}
},
"MV": {
into(value) {
// HACK: we take a boolean-or-string here. Find a dang either type?
if (typeof value === "boolean") {
return value ? "UP" : "DOWN";
}
const norm = 280 + value;
if (norm > 199 && norm <= 279 && (norm * 2 | 0) == norm * 2) {
return norm.toString().substring(1).replace(".", "");
}
throw new Error(`Expected to get boolean or integer or half-integer in range -80.5 to +1.0, got ${value}`);
},
from(string) {
if (string.startsWith("MAX ") || string.startsWith("MIN ")) {
return null;
}
const overflow = string.startsWith("99");
const value = +(string.length == 2 ? string + "0" : string);
if (isNaN(value)) {
throw new Error(`Expected 2-3 digit number, got ${string}`);
}
// TODO: Add a better validator here
return value / 10 - 80 - overflow * 100;
}
},
};
module.exports = {SI_TYPES, dispatchTable};