-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs20.js
77 lines (64 loc) · 2.02 KB
/
fs20.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
66
67
68
69
70
71
72
73
74
75
76
77
const ReadWriteLock = require("rwlock");
const SerialPort = require("serialport");
const FS20_EXIT_CODES_TEXT = [
/* 0x00 */ "Standby",
/* 0x01 */ "Send Active",
/* 0x02 */ "Invalid Command ID",
/* 0x03 */ "Invalid Command Length",
/* 0x04 */ "Invalid Parameters",
/* 0x05 */ "Duty Cycle Active",
/* 0x06 */ "Invalid Start Sign",
/* 0x07 */ "Invalid FS20 Command",
/* 0x08 */ "Too Slow Data Transmission",
/* 0x09 */ "Pause was < 10ms"
];
function FS20(deviceName, baudrate, isSimulating, connectHandler, disconnectHandler) {
var that = this;
const serialSettings = {
baudrate: baudrate,
parser: SerialPort.parsers.byteLength(4),
disconnectedCallback: disconnectHandler,
autoOpen: false
};
function internalConnectHandler(error) {
if(error && isSimulating)
return connectHandler(null);
connectHandler(error);
}
function setDeviceState(code, enable, callback) {
if(that.simulating) {
const exitCode = Math.floor(Math.random() * 10);
return callback(null, { code: exitCode, text: FS20_EXIT_CODES_TEXT[exitCode] || "" });
}
const binaryData = [
0x02, // Start Opcode
0x06, // Command length
0xF1, // Enable / Disable device
code >> 16 & 0xFF, // Room code 1
code >> 8 & 0xFF, // Room code 2
code & 0xFF, // Device code
enable ? 0x11 : 0x00, // Enable / Disable
0x00 // Additional parameter
];
that.mutex.writeLock(function(release) {
that.device.write(binaryData, function(err) {
if(err) {
release();
return callback(err, null);
}
that.device.on("data", function(recvData) {
release();
var exitCode = recvData[2];
callback(null, {code: exitCode, text: FS20_EXIT_CODES_TEXT[exitCode] || "Invalid Exit Code" });
that.device.removeAllListeners("data");
});
});
});
}
this.simulating = isSimulating || false;
this.device = new SerialPort(deviceName, serialSettings);
this.mutex = new ReadWriteLock ();
this.setDeviceState = setDeviceState;
this.device.open(internalConnectHandler);
}
module.exports = FS20;