-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer_control.js
54 lines (51 loc) · 1.53 KB
/
computer_control.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
//standard libraries
var Exec = require('child_process').exec;
//external dependencies
var Logger = require('log4js');
/**
* @namespace
*/
var ComputerControl = {};
ComputerControl.control = function control(){
var logger = Logger.getLogger('CompControl');
return function handleControl(req, res, next){
if (req.method !== 'POST'){
logger.debug('[control] not POST, passing');
next();
} else {
if (req.path === '/shutdown'){
logger.info('[control] shutting down computer');
Exec('sudo /sbin/shutdown -h now', function shutdown(error, stdout, stderr){
if (error){
logger.error('[shutdown] error with command',error);
}
if (stdout){
logger.debug('[shutdown] stdout:',stdout);
}
if (stderr){
logger.warn('[shutdown] stderr:',stderr);
}
});
res.sendStatus(200);
} else if (req.path === '/restart'){
logger.info('[control] restarting computer');
Exec('sudo /sbin/shutdown -r now', function restart(error, stdout, stderr){
if (error){
logger.error('[restart] error with command',error);
}
if (stdout){
logger.debug('[restart] stdout:',stdout);
}
if (stderr){
logger.warn('[restart] stderr:',stderr);
}
});
res.sendStatus(200);
} else {
logger.debug('[control] unrecognized action',req.path);
next();
}
}
};
};
module.exports = ComputerControl;