forked from jeremyfa/node-energenie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (46 loc) · 1.66 KB
/
index.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
var spawn = require('child_process').spawn;
var spawnSync = require('child_process').spawnSync;
module.exports = {
switchOn: function(socket, callback) {
if (socket == null) socket = 0;
else socket = parseInt(socket, 10);
var cmd = spawn('python', [__dirname+'/switch.py', 'on', socket]);
var err = '';
cmd.stderr.on('data', function(data) {
err += data;
});
cmd.on('close', function(code) {
if (callback != null) {
if (err.length > 0) callback(err);
else callback(null);
}
});
},
switchOff: function(socket, callback) {
if (socket == null) socket = 0;
else socket = parseInt(socket, 10);
var cmd = spawn('python', [__dirname+'/switch.py', 'off', socket]);
var err = '';
cmd.stderr.on('data', function(data) {
err += data;
});
cmd.on('close', function(code) {
if (callback != null) {
if (err.length > 0) callback(err);
else callback(null);
}
});
},
switchOnSync: function(socket) {
if (socket == null) socket = 0;
else socket = parseInt(socket, 10);
var result = spawnSync('python', [__dirname+'/switch.py', 'on', socket]);
if (result.stderr.length > 0) throw new Error(result.stderr);
},
switchOffSync: function(socket) {
if (socket == null) socket = 0;
else socket = parseInt(socket, 10);
var result = spawnSync('python', [__dirname+'/switch.py', 'off', socket]);
if (result.stderr.length > 0) throw new Error(result.stderr);
}
}