forked from alexbain/lirc_web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
150 lines (121 loc) · 4.2 KB
/
app.js
File metadata and controls
150 lines (121 loc) · 4.2 KB
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
// lirc_web - v0.0.8
// Alex Bain <alex@alexba.in>
// Requirements
var express = require('express'),
lirc_node = require('lirc_node'),
consolidate = require('consolidate'),
path = require('path'),
swig = require('swig'),
labels = require('./lib/labels');
// Precompile templates
var JST = {
index: swig.compileFile(__dirname + '/templates/index.swig')
};
// Create app
var app = module.exports = express();
// App configuration
app.engine('.html', consolidate.swig);
app.configure(function() {
app.use(express.logger());
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.compress());
app.use(express.static(__dirname + '/static'));
});
// lirc_web configuration
var config = {};
// Based on node environment, initialize connection to lirc_node or use test data
if (process.env.NODE_ENV == 'test' || process.env.NODE_ENV == 'development') {
lirc_node.remotes = require(__dirname + '/test/fixtures/remotes.json');
config = require(__dirname + '/test/fixtures/config.json');
} else {
lirc_node.init();
// Config file is optional
try {
config = require(__dirname + '/config.json');
} catch(e) {
console.log("DEBUG:", e);
console.log("WARNING: Cannot find config.json!");
}
}
// Routes
var labelFor = labels(config.remoteLabels, config.commandLabels)
// Web UI
app.get('/', function(req, res) {
res.send(JST['index'].render({
remotes: lirc_node.remotes,
macros: config.macros,
repeaters: config.repeaters,
labelForRemote: labelFor.remote,
labelForCommand: labelFor.command
}));
});
// List all remotes in JSON format
app.get('/remotes.json', function(req, res) {
res.json(lirc_node.remotes);
});
// List all commands for :remote in JSON format
app.get('/remotes/:remote.json', function(req, res) {
if (lirc_node.remotes[req.params.remote]) {
res.json(lirc_node.remotes[req.params.remote]);
} else {
res.send(404);
}
});
// 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.send(404);
}
});
// Send :remote/:command one time
app.post('/remotes/:remote/:command', function(req, res) {
lirc_node.irsend.send_once(req.params.remote, req.params.command, function() {});
res.setHeader('Cache-Control', 'no-cache');
res.send(200);
});
// Start sending :remote/:command repeatedly
app.post('/remotes/:remote/:command/send_start', function(req, res) {
lirc_node.irsend.send_start(req.params.remote, req.params.command, function() {});
res.setHeader('Cache-Control', 'no-cache');
res.send(200);
});
// Stop sending :remote/:command repeatedly
app.post('/remotes/:remote/:command/send_stop', function(req, res) {
lirc_node.irsend.send_stop(req.params.remote, req.params.command, function() {});
res.setHeader('Cache-Control', 'no-cache');
res.send(200);
});
// Execute a macro (a collection of commands to one or more remotes)
app.post('/macros/:macro', function(req, res) {
// If the macro exists, execute each command in the macro with 100msec
// delay between each command.
if (config.macros && config.macros[req.params.macro]) {
var i = 0;
var nextCommand = function() {
var command = config.macros[req.params.macro][i];
if (!command) { return true; }
// increment
i = i + 1;
if (command[0] == "delay") {
setTimeout(nextCommand, command[1]);
} else {
// By default, wait 100msec before calling next command
lirc_node.irsend.send_once(command[0], command[1], function() { setTimeout(nextCommand, 100); });
}
};
// kick off macro w/ first command
nextCommand();
}
res.setHeader('Cache-Control', 'no-cache');
res.send(200);
});
// Default port is 3000
app.listen(3000);
console.log("Open Source Universal Remote UI + API has started on port 3000.");