forked from Harsch-Systems/node-pi-plates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBASEplate.js
57 lines (46 loc) · 1.36 KB
/
BASEplate.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
const vasync = require('vasync');
const readline = require('readline');
const { spawn } = require('child_process');
const assert = require('assert');
let child = spawn('python3', ['-u', __dirname + '/plate_io.py']);
child.on('error', (err) => {
console.log('child error: ' + err);
});
child.on('exit', (code, signal) => {
console.log(`code: ${code} signal: ${signal}`);
});
child.stderr.on('data', (data) => {
console.log('stderr: ' + data);
});
const rl = readline.createInterface({
input: child.stdout
});
function do_cmd(task, cb) {
const cmd_str = JSON.stringify(task) + '\n';
child.stdin.write(cmd_str);
assert.equal(rl.listenerCount('line'), 0);
rl.once('line', (line) => {
const reply = JSON.parse(line);
cb(reply);
});
}
const queue = vasync.queue(do_cmd, 1);
class BASEplate {
constructor (addr, plate_type) {
this.addr = addr;
this.plate_type = plate_type;
this.queue = queue;
}
send (obj, receive_cb) {
// send a request to this plate using the form:
// {cmd: <pi-plate command>, args: {<command-specific args>}
// e.g. {cmd: "relayTOGGLE", args: { relay: 4}}
obj['plate_type'] = this.plate_type;
obj['addr'] = this.addr;
this.queue.push(obj, receive_cb);
}
shutdown () {
child.kill();
}
}
module.exports = BASEplate;