forked from vigibot/vigiclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialSlave.js
60 lines (48 loc) · 1.37 KB
/
SerialSlave.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
const AbstractPlugin = require("../utils/AbstractPlugin.js");
const SP = require("serialport");
class SerialSlave extends AbstractPlugin {
constructor() {
super('SerialSlave');
// Config constants
// Attributes
this.rx = null;
this.hardwareConfig = null;
this.environment = null;
this.serial = null;
}
init(config) {
this.updateConfiguration(config);
this.serial = new SP(this.hardwareConfig.DEVROBOT, {
baudRate: this.hardwareConfig.DEVDEBIT,
lock: false
});
return new Promise((resolve, reject) => {
this.serial.on("open", () => {
this.log("Connected to " + this.hardwareConfig.DEVROBOT);
if (this.hardwareConfig.DEVTELEMETRIE) {
this.serial.on("data", (data) => {
this.rx.update(data, () => {
this.environment.apply(this.rx);
this.emit('dataToServer', 'serveurrobotrx', {
timestamp: Date.now(),
data: this.rx.arrayBuffer
});
}, this.log);
});
}
resolve();
});
});
}
updateConfiguration(config) {
this.rx = config.rx;
this.hardwareConfig = config.hardwareConf;
this.environment = config.environment;
}
forwardTxData(tx) {
if (this.hardwareConfig.DEVTELECOMMANDE) {
this.serial.write(tx);
}
}
}
module.exports = SerialSlave;