-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
49 lines (45 loc) · 1.52 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
const EventEmitter = require("events").EventEmitter;
const spawn = require("child_process").spawn;
module.exports = class BalanceBoard extends EventEmitter {
constructor() {
super();
//Python code that tries to connect to balance board and recieves its data when connected
this.boardListener = null;
this.boardConnected = false;
}
//Try to connect to the balance board and recieve data. The sync button has to be pressed to connect to the board
connect() {
this.boardListener = spawn("python", ["boardListener.py"], {
cwd: __dirname
});
this.boardListener.stdout.on("data", data => {
try {
var balanceBoardFrame = JSON.parse(data.toString());
if (balanceBoardFrame.connected) {
//Recieved data from connected balance board
this.boardConnected = true;
this.emit("data", balanceBoardFrame);
} else {
//Balance board is disconnected
this.boardConnected = false;
this.emit("data", balanceBoardFrame);
}
} catch (error) {
//JSON.parse isnt always successful as a easy fix we catch these cases here
}
});
}
//Stop trying to connect to the balance board
disconnect() {
if (this.boardListener != null) {
this.boardListener.stdout.removeAllListeners("data");
this.boardListener.stdin.pause();
this.boardListener.kill();
this.boardListener = null;
}
}
//Check if the balance board is currently connected
isConnected() {
return this.boardConnected;
}
};