-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
88 lines (76 loc) · 2.4 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
var express = require('express'),
app = express(),
http = require('http'),
path = require('path'),
os = require('os'),
server = require('http').Server(app),
io = require('socket.io')(server),
exec = require('child_process').exec,
config = require('./config.json');
server.listen(config.port,'localhost');
app.use(express.static(path.join(__dirname,'public')));
console.log('Listening on port ' + config.port);
var intervals = {};
function handleError(moduleName, error) {
console.error('Error in module ' + moduleName + ': ' + error);
clearInterval(intervals[moduleName]);
}
//Global declarations for easy configuration
global.song = function(){
exec('mpc current',function(err,stdout,stderr) {
if(err) {
handleError('song', err);
} else if(stdout) {
io.emit('song',stdout.toString('utf8'));
}
});
};
global.disk = function(){
exec("echo \"$(df "+ config.partition +" --output=used -h |sed '1d')/$(df "+ config.partition +" --output=size -h |sed '1d')\" used",function(err,stdout,stderr){
if(err) {
handleError('disk', err);
} else if(stdout) {
io.emit('disk',stdout.toString());
}
});
};
global.ram = function(){
exec("echo \"$(free -h | awk '/Mem:/ {print $3}')/$(free -h | awk '/Mem:/ {print $2}') used\"",function(err,stdout,stderr){
if(err) {
handleError('ram', err);
} else if(stdout) {
io.emit('ram',stdout.toString('utf8'));
}
});
};
global.weather = function(){
http.get("http://api.openweathermap.org/data/2.5/weather?q="+config.city,function(res){
var str = '';
res.on('data',function(chunk){
str += chunk;
});
res.on('end',function(){
io.emit('weather',str);
});
});
};
global.uname = function(){
io.emit('uname', os.type() + ' ' + os.release() + ' ' + os.arch());
};
//Initialize the modules
io.on('connection',function(socket){
config.modules.forEach(function(val){
if(typeof global[val.name] === "function"){
global[val.name]();
}
});
socket.emit('config',config);
socket.on('command',function(command){
exec(command);
});
});
config.modules.forEach(function(val){
if(typeof global[val.name] === "function"){
intervals[val.name] = setInterval(global[val.name],val.interval);
}
});