-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver-mac.js
107 lines (90 loc) · 2.96 KB
/
server-mac.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
var express = require('express'),
app = express(),
http = require('http'),
path = require('path'),
server = require('http').Server(app),
io = require('socket.io')(server),
exec = require('child_process').exec,
config = require('./config.json'),
osascript = require('node-osascript');
server.listen(config.port,'localhost');
app.use(express.static(path.join(__dirname,'public')));
// start with true to prevent from getting info when we haven't checked if iTunes is running
var paused = true;
// prevent from loading in each call to iTunesInfo
var infoCmd = 'tell application "iTunes" to ' +
'{ artist of current track as string, ' +
'name of current track as string }';
var iTunesInfo = function() {
// iTunes throws an error if it's paused and we try to obtain the current song
osascript.execute('tell app "iTunes" to get player state as string', function(err, stdout, stderr) {
var state = stdout.toString('utf8');
paused = state === 'paused' || state === 'stopped';
});
if(paused) {
return;
}
osascript.execute(infoCmd, function(err,stdout,stderr) {
if(err) {
throw err;
}
if(stdout) {
io.emit('song',stdout.join(' - ').toString('utf8'));
}
});
};
//Global declarations for easy configuration
global.song = function(){
osascript.execute('tell app "System Events" to count processes whose name is "iTunes"', function(err, stdout, stderr) {
if(stdout)
iTunesInfo();
});
};
global.disk = function(){
exec("echo \"$(df -h "+ config.partition + " | sed '1d' | awk '{print $3}')/$(df -h " + config.partition + " | sed '1d' | awk '{print $2}')\"",function(err,stdout,stderr){
if(err) throw err;
if(stdout) {
var object = {
name: config.partition,
description: stdout.toString()
};
io.emit('disk', stdout.toString());
}
});
};
global.ram = function(){
exec('top -l 1 | head -n 10 | grep PhysMem',function(err,stdout,stderr){
if(err) throw err;
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);
});
});
};
//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"){
setInterval(global[val.name],val.interval);
}
});