-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·50 lines (40 loc) · 1.27 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
const isSupportedPlatform = process.platform !== 'darwin';
let CanApi = function() {};
if (isSupportedPlatform) {
CanApi = require('./build/Release/can_api.node').CanApi;
}
const createBuffer = require('./helpers/create_buffer');
const bitRates = require('./helpers/bit_rates');
const CanApiWrapper = function(channel, bitRate = bitRates.canBITRATE_500K) {
const CanApiInstance = new CanApi(channel, bitRate);
const ifSupportedPlatformExecute = (propertyName, defaultValue, ...args) => {
if (isSupportedPlatform) {
return CanApiInstance[propertyName](...args);
}
return defaultValue;
};
return {
readMessage: (options = {}) => {
const defaultOptions = {
timeout: 0,
};
return ifSupportedPlatformExecute(
'readMessage',
false,
Object.assign({}, defaultOptions, options),
);
},
sendMessage: (...args) => {
return ifSupportedPlatformExecute('sendMessage', true, ...args);
},
open: (...args) => {
return ifSupportedPlatformExecute('open', true, ...args);
},
close: (...args) => {
return ifSupportedPlatformExecute('close', false, ...args);
},
};
};
CanApiWrapper.createBuffer = createBuffer;
CanApiWrapper.bitRates = bitRates;
module.exports = CanApiWrapper;