serialport-parsers.js
3.53 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const PARSER_READLINE_DELIMITER = '\r\n';
const PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = /\r\n(?:OK|ERROR)\r\n/;
const pdu = require('node-pdu');
const ParserReadline = require('@serialport/parser-readline');
const ParserRegex = require('@serialport/parser-regex');
const logger = require('komodo-sdk/logger');
const dbCops = require('./db-cops');
let port;
exports.setPort = function setPort(val) {
logger.info('SERIALPORT-PARSERS: setting port');
port = val;
};
exports.getPort = function getPort() {
return port;
};
function parsePdu(data) {
if (!data) return null;
try {
const result = pdu.parse(data.toString().trim() || '');
return result;
} catch (e) {
return null;
}
}
function onCSQ(data) {
const val = data.toString().trim().match(/\+CSQ:\s*(.*)/);
if (!val || !val[1]) return null;
logger.info('Signal quality extracted', { signalQuality: val[1] });
return val[1];
}
function onPdu(data, parsedData) {
const from = parsedData.getAddress && parsedData.getAddress().getPhone
? parsedData.getAddress().getPhone() : null;
const msg = parsedData.getData && parsedData.getData().getData
? parsedData.getData().getData() : null;
logger.verbose('PDU processed', { from, msg });
return { from, msg };
}
function onCOPS(data) {
const val = data.toString().trim().match(/\+COPS:\s*(.*)/);
if (!val || !val[1]) return null;
const cops = val[1];
if (!cops) return null;
const [mode, format, networkId] = cops.split(',');
const networkName = networkId ? dbCops[networkId] || networkId : null;
logger.verbose('COPS extracted', {
cops, mode, format, networkId, networkName,
});
return {
cops, mode, format, networkId, networkName,
};
}
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;
const pduParsed = parsePdu(data);
if (pduParsed) {
logger.verbose('Got a PDU data', { type: typeof pduParsed });
onPdu(data, pduParsed);
} else if (isResultCodeIs(data, 'CSQ')) {
logger.verbose('Got a signal quality report', { data: data.toString() });
onCSQ(data);
} else if (isResultCodeIs(data, 'COPS:')) {
logger.verbose('Got a COPS report', { data: data.toString() });
onCOPS(data);
} 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() });
}
});
const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX });
parserWaitForOkOrError.on('data', (data) => {
logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() });
});
exports.parserReadline = parserReadline;
exports.parserWaitForOkOrError = parserWaitForOkOrError;