Blame view
lib/serialport-parsers.js
3.73 KB
fe3033bbf
|
1 2 |
const PARSER_READLINE_DELIMITER = '\r '; |
9ee5f03e0
|
3 4 5 |
const PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = /\r (?:OK|ERROR)\r /; |
2e49a3d20
|
6 |
const pdu = require('node-pdu'); |
fe3033bbf
|
7 |
const ParserReadline = require('@serialport/parser-readline'); |
9ee5f03e0
|
8 |
const ParserRegex = require('@serialport/parser-regex'); |
fe3033bbf
|
9 |
const logger = require('komodo-sdk/logger'); |
9ee5f03e0
|
10 |
const dbCops = require('./db-cops'); |
fe3033bbf
|
11 12 13 14 15 16 17 18 19 20 |
let port; exports.setPort = function setPort(val) { logger.info('SERIALPORT-PARSERS: setting port'); port = val; }; exports.getPort = function getPort() { return port; }; |
2e49a3d20
|
21 22 23 24 25 26 27 28 29 30 |
function parsePdu(data) { if (!data) return null; try { const result = pdu.parse(data.toString().trim() || ''); return result; } catch (e) { return null; } } |
9ee5f03e0
|
31 32 |
function onCSQ(data) { const val = data.toString().trim().match(/\+CSQ:\s*(.*)/); |
cec4e78d3
|
33 |
if (!val || !val[1]) return null; |
9ee5f03e0
|
34 |
|
cec4e78d3
|
35 36 |
logger.info('Signal quality extracted', { signalQuality: val[1] }); return val[1]; |
9ee5f03e0
|
37 |
} |
d87943efc
|
38 39 40 41 42 43 44 45 46 47 |
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 }; } |
9ee5f03e0
|
48 |
function onCOPS(data) { |
cec4e78d3
|
49 50 51 52 |
const val = data.toString().trim().match(/\+COPS:\s*(.*)/); if (!val || !val[1]) return null; const cops = val[1]; |
9ee5f03e0
|
53 54 55 56 57 58 59 60 61 62 63 64 65 |
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, }; } |
fe3033bbf
|
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
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; |
2e49a3d20
|
89 90 91 |
const pduParsed = parsePdu(data); if (pduParsed) { |
2f1b961dd
|
92 |
logger.verbose('PDU parsed', { type: (typeof pduParsed.getType === 'function') && pduParsed.getType() }); |
e74c83d19
|
93 |
} |
2f1b961dd
|
94 |
if (pduParsed && pduParsed.getType && pduParsed.getType().getSrr()) { |
e74c83d19
|
95 |
logger.verbose('Got a PDU SMS-DELIVER', { type: pduParsed.getType() }); |
d87943efc
|
96 |
onPdu(data, pduParsed); |
2e49a3d20
|
97 |
} else if (isResultCodeIs(data, 'CSQ')) { |
fe3033bbf
|
98 |
logger.verbose('Got a signal quality report', { data: data.toString() }); |
9ee5f03e0
|
99 |
onCSQ(data); |
fe3033bbf
|
100 101 |
} else if (isResultCodeIs(data, 'COPS:')) { logger.verbose('Got a COPS report', { data: data.toString() }); |
9ee5f03e0
|
102 |
onCOPS(data); |
fe3033bbf
|
103 104 105 106 107 108 |
} 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() }); } }); |
9ee5f03e0
|
109 110 111 112 |
const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); parserWaitForOkOrError.on('data', (data) => { logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() }); }); |
fe3033bbf
|
113 |
exports.parserReadline = parserReadline; |
9ee5f03e0
|
114 |
exports.parserWaitForOkOrError = parserWaitForOkOrError; |