forked from alexbain/lirc_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
252 lines (210 loc) · 6.52 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
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
#! /usr/bin/env node
// Requirements
var express = require('express');
var logger = require('morgan');
var compress = require('compression');
var lircNode = require('lirc_node');
var consolidate = require('consolidate');
var swig = require('swig');
var labels = require('./lib/labels');
var https = require('https');
var fs = require('fs');
var gpio = require('./lib/gpio');
var macros = require('./lib/macros');
// Precompile templates
var JST = {
index: swig.compileFile(__dirname + '/templates/index.swig'),
};
// Create app
var app = module.exports = express();
// lirc_web configuration
var config = {};
var hasServerPortConfig = false;
var hasSSLConfig = false;
// Server & SSL options
var port = 3000;
var sslOptions = {
key: null,
cert: null,
};
var labelFor = {};
// App configuration
app.engine('.html', consolidate.swig);
app.use(logger('combined'));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(compress());
app.use(express.static(__dirname + '/static'));
function _init() {
var searchPaths = [];
function configure(configFileName) {
searchPaths.push(configFileName);
config = require(configFileName);
console.log('Open Source Universal Remote is configured by ' + configFileName);
}
lircNode.init();
// Config file is optional
try {
try {
configure(__dirname + '/config.json');
} catch (e) {
configure(process.env.HOME + '/.lirc_web_config.json');
}
} catch (e) {
console.log('DEBUG:', e);
console.log('WARNING: Cannot find config.json!');
console.log('DEBUG: tried: ' + JSON.stringify(searchPaths));
}
hasServerPortConfig = config.server && config.server.port;
hasSSLConfig =
config.server && config.server.ssl && config.server.ssl_cert
&& config.server.ssl_key && config.server.ssl_port;
}
function refineRemotes(myRemotes) {
var newRemotes = {};
var newRemoteCommands = null;
var remote = null;
function isBlacklistExisting(remoteName) {
return config.blacklists && config.blacklists[remoteName];
}
function getCommandsForRemote(remoteName) {
var remoteCommands = myRemotes[remoteName];
var blacklist = null;
if (isBlacklistExisting(remoteName)) {
blacklist = config.blacklists[remoteName];
remoteCommands = remoteCommands.filter(function (command) {
return blacklist.indexOf(command) < 0;
});
}
return remoteCommands;
}
for (remote in myRemotes) {
newRemoteCommands = getCommandsForRemote(remote);
newRemotes[remote] = newRemoteCommands;
}
return newRemotes;
}
// Based on node environment, initialize connection to lircNode or use test data
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
lircNode.remotes = require(__dirname + '/test/fixtures/remotes.json');
config = require(__dirname + '/test/fixtures/config.json');
gpio.overrideWiringPi(require('./test/lib/wiring-pi-mock'));
} else {
_init();
}
// initialize Labels for remotes / commands
labelFor = labels(config.remoteLabels, config.commandLabels);
// Routes
// Index
app.get('/', function (req, res) {
var refinedRemotes = refineRemotes(lircNode.remotes);
res.send(JST.index({
remotes: refinedRemotes,
macros: config.macros,
repeaters: config.repeaters,
gpios: config.gpios,
labelForRemote: labelFor.remote,
labelForCommand: labelFor.command,
}));
});
// Refresh
app.get('/refresh', function (req, res) {
_init();
res.redirect('/');
});
// List all remotes in JSON format
app.get('/remotes.json', function (req, res) {
res.json(refineRemotes(lircNode.remotes));
});
// List all commands for :remote in JSON format
app.get('/remotes/:remote.json', function (req, res) {
if (lircNode.remotes[req.params.remote]) {
res.json(refineRemotes(lircNode.remotes)[req.params.remote]);
} else {
res.sendStatus(404);
}
});
function respondWithGpioState(res) {
if (config.gpios) {
gpio.updatePinStates();
res.json(config.gpios);
} else {
res.send(404);
}
}
// List all gpio switches in JSON format
app.get('/gpios.json', function (req, res) {
respondWithGpioState(res);
});
// List all macros in JSON format
app.get('/macros.json', function (req, res) {
res.json(config.macros);
});
// List all commands for :macro in JSON format
app.get('/macros/:macro.json', function (req, res) {
if (config.macros && config.macros[req.params.macro]) {
res.json(config.macros[req.params.macro]);
} else {
res.sendStatus(404);
}
});
// Send :remote/:command one time
app.post('/remotes/:remote/:command', function (req, res) {
lircNode.irsend.send_once(req.params.remote, req.params.command, function () {});
res.setHeader('Cache-Control', 'no-cache');
res.sendStatus(200);
});
// Start sending :remote/:command repeatedly
app.post('/remotes/:remote/:command/send_start', function (req, res) {
lircNode.irsend.send_start(req.params.remote, req.params.command, function () {});
res.setHeader('Cache-Control', 'no-cache');
res.sendStatus(200);
});
// Stop sending :remote/:command repeatedly
app.post('/remotes/:remote/:command/send_stop', function (req, res) {
lircNode.irsend.send_stop(req.params.remote, req.params.command, function () {});
res.setHeader('Cache-Control', 'no-cache');
res.sendStatus(200);
});
// toggle /gpios/:gpio_pin
app.post('/gpios/:gpio_pin', function (req, res) {
var newValue = gpio.togglePin(req.params.gpio_pin);
res.setHeader('Cache-Control', 'no-cache');
res.json(newValue);
res.end();
});
// Execute a macro (a collection of commands to one or more remotes)
app.post('/macros/:macro', function (req, res) {
if (config.macros && config.macros[req.params.macro]) {
macros.exec(config.macros[req.params.macro], lircNode);
res.setHeader('Cache-Control', 'no-cache');
if (config.gpios) {
respondWithGpioState(res);
} else {
res.sendStatus(200);
}
} else {
res.setHeader('Cache-Control', 'no-cache');
res.sendStatus(404);
}
});
gpio.init(config.gpios);
// Listen (http)
if (hasServerPortConfig) {
port = config.server.port;
}
// only start server, when called as application
if (!module.parent) {
app.listen(port);
console.log('Open Source Universal Remote UI + API has started on port ' + port + ' (http).');
}
// Listen (https)
if (hasSSLConfig) {
sslOptions = {
key: fs.readFileSync(config.server.ssl_key),
cert: fs.readFileSync(config.server.ssl_cert),
};
https.createServer(sslOptions, app).listen(config.server.ssl_port);
console.log('Open Source Universal Remote UI + API has started on port '
+ config.server.ssl_port + ' (https).');
}