forked from benkaiser/desktop-command-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
desktop-command-remote.js
65 lines (63 loc) · 2 KB
/
desktop-command-remote.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
var express = require('express.io');
var swig = require('swig');
var fs = require('fs');
var util = require('util');
// runs a system command
var exec = require('child_process').exec;
exports.Server = function(app, ext){
var self = this;
self.app = app || "error";
self.ext = ext || "";
// create the app and connect to the database
self.init = function(callback){
// create the app and the routes
self.createRoutes();
// static pages
self.app.use("/static", express.static(__dirname + '/static'));
callback();
}
// create all the routes for the server
self.createRoutes = function(callback){
self.app.get(self.ext + "/", function(req, res){
user = req.params.user;
res.send(swig.renderFile(__dirname + "/templates/controls.html", {user: user}));
});
// api and socket functions
self.app.io.sockets.on('connection', function(socket){
// recieve from controller
socket.on('control', function(data){
self.runCommand(data);
});
socket.on('init_controls', function(data){
socket.emit("init_controls", self.options);
});
});
// test function
self.app.get(self.ext + "/:user/api/test", function(req, res){
user = req.params.user;
res.send(swig.renderFile(__dirname + "/templates/test.html", {user: user}));
});
}
self.config = function(configFile){
self.options = require(configFile)();
}
self.runCommand = function(data){
switch(data.type){
case 'button':
exec(data.command, self.puts);
break;
case 'slider':
// compile value of slider into command
command = util.format(data.command, data.value);
exec(command, self.puts);
break;
}
}
self.puts = function(error, stdout, stderr){
stdout = stdout.trim();
// output the message to the console
console.log("Command Output:" + stdout);
// send the output back to the client, currently only passes stdout.
self.app.io.sockets.emit('log', {stdout: stdout});
}
}