forked from jnewland/mpr-6zhmaut-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
198 lines (183 loc) · 5.16 KB
/
app.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
var express = require("express");
var morgan = require("morgan");
var bodyParser = require("body-parser");
var serialport = require("serialport");
var async = require("async");
var app = express();
var logFormat = "'[:date[iso]] - :remote-addr - :method :url :status :response-time ms - :res[content-length]b'";
app.use(morgan(logFormat));
app.use(bodyParser.text({type: '*/*'}));
const ReQuery = /^true$/i.test(process.env.REQUERY);
const UseCORS = /^true$/i.test(process.env.CORS);
const AmpCount = process.env.AMPCOUNT || 1;
var SerialPort = serialport.SerialPort;
var device = process.env.DEVICE || "/dev/ttyUSB0";
var connection = new SerialPort(device, {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
connection.on("open", function () {
var zones = {};
connection.write("?10\r");
AmpCount >= 2 && connection.write("?20\r");
AmpCount >= 3 && connection.write("?30\r");
UseCORS && app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
connection.on('data', function(data) {
console.log(data);
var zone = data.match(/#>(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/);
if (zone != null) {
zones[zone[1]] = {
"zone": zone[1],
"pa": zone[2],
"pr": zone[3],
"mu": zone[4],
"dt": zone[5],
"vo": zone[6],
"tr": zone[7],
"bs": zone[8],
"bl": zone[9],
"ch": zone[10],
"ls": zone[11]
};
}
});
app.get('/zones', function(req, res) {
var zoneCount = Object.keys(zones).length;
if (ReQuery) {
zones = {};
connection.write("?10\r");
AmpCount >= 2 && connection.write("?20\r");
AmpCount >= 3 && connection.write("?30\r");
}
async.until(
function () {
return (typeof zones !== "undefined" && Object.keys(zones).length === zoneCount);
},
function (callback) {
setTimeout(callback, 10);
},
function () {
var zoneArray = [];
for(var o in zones) {
zoneArray.push(zones[o]);
}
res.json(zoneArray);
}
);
});
// Only allow query and control of single zones
app.param('zone', function(req, res, next, zone) {
if (zone % 10 > 0 && Number(zone) != "NaN") {
req.zone = zone;
next();
} else {
res.status(500).send({ error: zone + ' is not a valid zone'});
}
});
app.get('/zones/:zone', function(req, res) {
async.until(
function () { return typeof zones[req.zone] !== "undefined"; },
function (callback) {
setTimeout(callback, 10);
},
function () {
res.json(zones[req.zone]);
}
);
});
// Validate and standarize control attributes
app.param('attribute', function(req, res, next, attribute) {
if (typeof attribute !== 'string') {
res.status(500).send({ error: attribute + ' is not a valid zone control attribute'});
}
switch(attribute.toLowerCase()) {
case "pa":
req.attribute = "pa";
next();
break;
case "pr":
case "power":
req.attribute = "pr";
next();
break;
case "mu":
case "mute":
req.attribute = "mu";
next();
break;
case "dt":
req.attribute = "dt";
next();
break;
case "vo":
case "volume":
req.attribute = "vo";
next();
break;
case "tr":
case "treble":
req.attribute = "tr";
next();
break;
case "bs":
case "bass":
req.attribute = "bs";
next();
break;
case "bl":
case "balance":
req.attribute = "bl";
next();
break;
case "ch":
case "channel":
case "source":
req.attribute = "ch";
next();
break;
case "ls":
case "keypad":
req.attribute = "ls";
next();
break;
default:
res.status(500).send({ error: attribute + ' is not a valid zone control attribute'});
}
});
app.post('/zones/:zone/:attribute', function(req, res) {
zones[req.zone] = undefined;
connection.write("<"+req.zone+req.attribute+req.body+"\r");
connection.write("?10\r");
AmpCount >= 2 && connection.write("?20\r");
AmpCount >= 3 && connection.write("?30\r");
async.until(
function () { return typeof zones[req.zone] !== "undefined"; },
function (callback) {
setTimeout(callback, 10);
},
function () {
res.json(zones[req.zone]);
}
);
});
app.get('/zones/:zone/:attribute', function(req, res) {
zones[req.zone] = undefined;
connection.write("?10\r");
AmpCount >= 2 && connection.write("?20\r");
AmpCount >= 3 && connection.write("?30\r");
async.until(
function () { return typeof zones[req.zone] !== "undefined"; },
function (callback) {
setTimeout(callback, 10);
},
function () {
res.send(zones[req.zone][req.attribute]);
}
);
});
app.listen(process.env.PORT || 8181);
});