-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
215 lines (184 loc) · 5.91 KB
/
server.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
var app = require('http').createServer(handler),
io = require('socket.io').listen(app, { log: false }),
fs = require('fs'),
path = require('path'),
url = require('url'),
sh = require('execSync'),
config = require('./config.json');
// simple mimetypes...
var mimeTypes = {
'html' : 'text/html',
'js' : 'text/javascript',
'css' : 'text/css',
'svg' : 'image/svg+xml',
'ttf' : 'application/x-font-ttf',
'otf' : 'application/x-font-opentype',
'woff' : 'application/font-woff',
'eot' : 'application/vnd.ms-fontobject'
};
// http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format/4673436#4673436
// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
// https://stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string
if (typeof String.prototype.startsWith != 'function') {
// see below for better implementation!
String.prototype.startsWith = function (str){
return this.indexOf(str) == 0;
};
}
// sort config..
for (var i = 0; i < config.groups.length; i++) {
config.groups[i].items.sort(function(a, b) {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
});
}
app.listen(config.port);
// super simple server, maybe use connect or something like that?
function handler(req, res) {
var uri = url.parse(req.url).pathname;
if (uri === '/') {
uri = '/client.html';
}
// check uri
if (!uri.startsWith('/css') && !uri.startsWith('/js') && !uri.startsWith('/fonts') && !uri.startsWith('/favicon.ico') && !uri.startsWith('/client.html')) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.write('403 Forbidden\n');
res.end();
return;
}
// TODO maybe check for directory traversals?
var filename = path.join(process.cwd(), uri);
fs.exists(filename, function(exists) {
// 404 Not Found
if (!exists) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.write('404 Not Found\n');
res.end();
return;
}
var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
res.writeHead(200, { 'Content-Type': mimeType });
fs.createReadStream(filename).pipe(res);
});
}
// checks for timers in our items and triggers them if the time has passed...
function checkTimer() {
var now = new Date().getTime(),
switched = false;
for (var i = 0; i < config.groups.length; i++) {
for (var j = 0; j < config.groups[i].items.length; j++) {
var item = config.groups[i].items[j];
if (typeof(item.timer) != "undefined" && item.timer.length > 0) {
for (var k = item.timer.length -1; k >= 0 ; k--){
// check if the time has passed...
if (now > item.timer[k].time) {
var state = parseInt(item.timer[k].type == 2 ? (item.state == 1 ? 0 : 1) : item.timer[k].type);
switchInternal(item, state);
item.timer.splice(k, 1);
switched = true;
}
}
}
}
}
if (switched) {
updateAllClients();
}
setTimeout(checkTimer, config['timer-check-interval'] * 1000);
}
// check forever...
checkTimer();
// get the specific remote item
function getItem(system, remote) {
for (var i = 0; i < config.groups.length; i++) {
for (var j = 0; j < config.groups[i].items.length; j++) {
var item = config.groups[i].items[j];
if (item.system == system && item.remote == remote) {
return item;
}
}
}
console.log('invalid item: ' + system + ',' + remote);
return null;
}
// does the low level switching
function switchInternal(item, state) {
sh.run(config.command.format(item.system, item.remote, state));
item.state = state;
}
// updates all clients...
function updateAllClients() {
saveConfig();
io.sockets.emit('update', config.groups);
}
// save config
function saveConfig() {
fs.writeFile('config.json', JSON.stringify(config, null, 2), function(err) {
if (err) {
console.log('Failed to save config.json: ' + err);
}
});
}
io.sockets.on('connection', function(socket) {
// update client upon connection
socket.emit('initial', {
"ui-settings" : config['ui-settings'],
"groups" : config.groups
});
// register switch event
socket.on('switch', function(data) {
var item = getItem(data.system, data.remote);
if (item != null) {
switchInternal(item, data.state);
updateAllClients();
}
});
socket.on('timer', function(data) {
var item = getItem(data.system, data.remote);
if (item != null) {
// make sure our config is able to push timers
var timer;
if (typeof(item.timer) != "undefined") {
timer = item.timer;
} else {
timer = new Array();
item.timer = timer;
}
// calculate our trigger time
var time;
var v = data.time.split(':');
if (data.mode == 0) {
time = v[0] * 24 * 60 * 1000 + v[1] * 60 * 1000;
} else {
// TODO calculate time..
}
// add timer
timer.push({
mode: data.mode,
type: data.type,
time: new Date().getTime() + time
});
updateAllClients();
}
});
// register master off event. we just force ALL items to state 0!
socket.on('master-off', function(unused) {
for (var i = 0; i < config.groups.length; i++) {
for (var j = 0; j < config.groups[i].items.length; j++) {
var item = config.groups[i].items[j];
switchInternal(item, 0);
}
}
updateAllClients();
});
});