Blame view
lib/serialport-parsers.js
6.32 KB
fe3033bbf
|
1 2 |
const PARSER_READLINE_DELIMITER = '\r '; |
fad855259
|
3 4 5 |
const PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = / (?:OK|ERROR)\r /; |
9ee5f03e0
|
6 |
|
caac94e43
|
7 |
const moment = require('moment'); |
62c6c2398
|
8 |
const nodePdu = require('node-pdu'); |
ee68aca72
|
9 |
// const pdu = require('pdu'); |
fe3033bbf
|
10 |
const ParserReadline = require('@serialport/parser-readline'); |
9ee5f03e0
|
11 |
const ParserRegex = require('@serialport/parser-regex'); |
fe3033bbf
|
12 |
const logger = require('komodo-sdk/logger'); |
9ee5f03e0
|
13 |
const dbCops = require('./db-cops'); |
caac94e43
|
14 |
const modemInfo = require('./modem-info'); |
9ee5f03e0
|
15 |
|
fe3033bbf
|
16 17 18 19 20 21 22 23 24 25 |
let port; exports.setPort = function setPort(val) { logger.info('SERIALPORT-PARSERS: setting port'); port = val; }; exports.getPort = function getPort() { return port; }; |
0ea228ab6
|
26 27 28 29 30 |
let ussdCallback = null; function setUssdCallback(cb) { ussdCallback = cb; } exports.setUssdCallback = setUssdCallback; |
1d6334a9b
|
31 32 33 34 35 |
let smsSentCallback = null; function setSmsSentCallback(cb) { smsSentCallback = cb; } exports.setSmsSentCallback = setSmsSentCallback; |
0ea228ab6
|
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
function isAlphaNumeric(str) { const len = str.length; // eslint-disable-next-line no-plusplus for (let i = 0; i < len; i++) { const code = str.charCodeAt(i); if (!(code > 47 && code < 58) // numeric (0-9) && !(code > 64 && code < 91) // upper alpha (A-Z) && !(code > 96 && code < 123)) { // lower alpha (a-z) return false; } } return true; } function parsePdu(_data) { const data = _data && _data.toString().trim().toUpperCase(); |
2e49a3d20
|
52 |
if (!data) return null; |
0ea228ab6
|
53 |
if (!isAlphaNumeric(data)) return null; |
2e49a3d20
|
54 55 |
try { |
0ea228ab6
|
56 |
const result = nodePdu.parse(data); |
2e49a3d20
|
57 58 59 60 61 |
return result; } catch (e) { return null; } } |
9ee5f03e0
|
62 63 |
function onCSQ(data) { const val = data.toString().trim().match(/\+CSQ:\s*(.*)/); |
cec4e78d3
|
64 |
if (!val || !val[1]) return null; |
9ee5f03e0
|
65 |
|
caac94e43
|
66 |
const [, signalStrength] = val; |
cec4e78d3
|
67 |
logger.info('Signal quality extracted', { signalQuality: val[1] }); |
caac94e43
|
68 69 70 71 72 73 |
modemInfo.signalStrength = signalStrength; modemInfo.signalStrengthTs = new Date(); modemInfo.signalStrengthTsReadable = moment(modemInfo.signalStrengthTs).format('YYYY-MM-DD HH:mm:ss'); return signalStrength; |
9ee5f03e0
|
74 |
} |
9d73afe29
|
75 |
function onPduDeliver(data, parsedData) { |
d87943efc
|
76 77 78 79 80 |
const from = parsedData.getAddress && parsedData.getAddress().getPhone ? parsedData.getAddress().getPhone() : null; const msg = parsedData.getData && parsedData.getData().getData ? parsedData.getData().getData() : null; |
8914d7abb
|
81 82 83 |
const ts = new Date(parsedData.getScts().getIsoString()); logger.verbose('PDU processed', { ts, from, msg }); |
d87943efc
|
84 85 |
return { from, msg }; } |
9ee5f03e0
|
86 |
function onCOPS(data) { |
cec4e78d3
|
87 88 89 90 |
const val = data.toString().trim().match(/\+COPS:\s*(.*)/); if (!val || !val[1]) return null; const cops = val[1]; |
9ee5f03e0
|
91 92 93 94 |
if (!cops) return null; const [mode, format, networkId] = cops.split(','); const networkName = networkId ? dbCops[networkId] || networkId : null; |
c5e93b5ea
|
95 |
logger.info('COPS extracted', { |
9ee5f03e0
|
96 97 |
cops, mode, format, networkId, networkName, }); |
caac94e43
|
98 |
modemInfo.cops = cops; |
fad855259
|
99 100 |
modemInfo.networkId = networkId || null; modemInfo.networkName = networkName || null; |
caac94e43
|
101 |
|
9ee5f03e0
|
102 103 104 105 |
return { cops, mode, format, networkId, networkName, }; } |
fe3033bbf
|
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
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) => { |
3522ae5bc
|
126 |
modemInfo.lastReadTs = new Date(); |
9883a918f
|
127 |
logger.verbose('INCOMING', { data: `${data.toString()}${PARSER_READLINE_DELIMITER}`, parser: 'parserReadLine' }); |
fe3033bbf
|
128 129 |
if (!data) return; |
2e49a3d20
|
130 131 |
const pduParsed = parsePdu(data); |
9d73afe29
|
132 |
if (pduParsed && pduParsed.constructor.name !== 'Deliver') { |
0ea228ab6
|
133 134 |
const pduType = pduParsed.getType(); logger.warn('WARN-9DA32C41: Unknown PDU message type name. PLEASE REPORT IT TO DEVELOPER AT TEKTRANS', { typeName: pduParsed.constructor.name, pduType, data: data.toString().trim() }); |
9d73afe29
|
135 |
} |
02b47ac39
|
136 |
if (pduParsed && pduParsed.constructor.name === 'Deliver' && pduParsed.getData().getSize()) { |
0ea228ab6
|
137 138 |
const pduType = pduParsed.getType(); logger.verbose('Got a PDU SMS-DELIVER', { pduType }); |
9d73afe29
|
139 |
onPduDeliver(data, pduParsed); |
beba81d88
|
140 141 |
} else if (data.toString().trim() === 'ERROR') { if (typeof smsSentCallback === 'function') smsSentCallback(data.toString()); |
2e49a3d20
|
142 |
} else if (isResultCodeIs(data, 'CSQ')) { |
fe3033bbf
|
143 |
logger.verbose('Got a signal quality report', { data: data.toString() }); |
9ee5f03e0
|
144 |
onCSQ(data); |
fe3033bbf
|
145 146 |
} else if (isResultCodeIs(data, 'COPS:')) { logger.verbose('Got a COPS report', { data: data.toString() }); |
9ee5f03e0
|
147 |
onCOPS(data); |
fe3033bbf
|
148 149 150 151 |
} 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() }); |
0ea228ab6
|
152 153 |
} else if (isResultCodeIs(data, 'CUSD')) { logger.verbose('Got a USSD command response', { data: data.toString() }); |
1d6334a9b
|
154 |
if (typeof ussdCallback === 'function') { |
0ea228ab6
|
155 156 157 158 159 |
logger.verbose('Calling USSD callback'); ussdCallback(data.toString()); } else { logger.verbose('Skip unwanted USSD response'); } |
1d6334a9b
|
160 161 162 |
} else if (isResultCodeIs(data, 'CMGS')) { logger.verbose('Got CMGS report', { data: data.toString() }); if (typeof smsSentCallback === 'function') smsSentCallback(data.toString()); |
37d0dbf8a
|
163 164 165 |
} else if (isResultCodeIs(data, 'CMS ERROR')) { logger.verbose('Got CMS ERROR report', { data: data.toString() }); if (typeof smsSentCallback === 'function') smsSentCallback(data.toString()); |
fe3033bbf
|
166 167 |
} }); |
9ee5f03e0
|
168 169 |
const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); parserWaitForOkOrError.on('data', (data) => { |
9883a918f
|
170 |
logger.verbose('INCOMING', { data: data.toString(), parser: 'parserWaitForOkOrError' }); |
9ee5f03e0
|
171 |
}); |
e77dfd454
|
172 173 |
exports.PARSER_READLINE_DELIMITER = PARSER_READLINE_DELIMITER; exports.PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = PARSER_WAIT_FOR_OK_OR_ERROR_REGEX; |
fe3033bbf
|
174 |
exports.parserReadline = parserReadline; |
9ee5f03e0
|
175 |
exports.parserWaitForOkOrError = parserWaitForOkOrError; |