serialport-parsers.js
1.61 KB
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
const PARSER_READLINE_DELIMITER = '\r\n';
const ParserReadline = require('@serialport/parser-readline');
const logger = require('komodo-sdk/logger');
let port;
exports.setPort = function setPort(val) {
logger.info('SERIALPORT-PARSERS: setting port');
port = val;
};
exports.getPort = function getPort() {
return port;
};
function isResultCodeIs(data, resultCode) {
if (!data) return false;
const cleanedData = (data.toString() || '').trim();
if (!cleanedData) return false;
if (resultCode.indexOf('+') !== 0) {
// eslint-disable-next-line no-param-reassign
resultCode = `+${resultCode}`;
}
if (resultCode.search(/:$/) < 0) {
// eslint-disable-next-line no-param-reassign
resultCode += ':';
}
return cleanedData.indexOf(resultCode) === 0;
}
const parserReadline = new ParserReadline({ delimiter: PARSER_READLINE_DELIMITER });
parserReadline.on('data', (data) => {
logger.verbose('INCOMING', { parser: 'parserReadLine', data: `${data.toString()}${PARSER_READLINE_DELIMITER}` });
if (!data) return;
if (isResultCodeIs(data, 'CSQ')) {
logger.verbose('Got a signal quality report', { data: data.toString() });
} else if (isResultCodeIs(data, 'COPS:')) {
logger.verbose('Got a COPS report', { data: data.toString() });
} else if (isResultCodeIs(data, 'CMT')) {
logger.verbose('Got a new message report', { data: data.toString() });
} else if (isResultCodeIs(data, 'CMTI')) {
logger.verbose('Got a new message notification report', { data: data.toString() });
}
});
exports.parserReadline = parserReadline;