Blame view
lib/serialport-parsers.js
2.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 /; |
fe3033bbf
|
6 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; }; |
9ee5f03e0
|
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
function onCSQ(data) { const val = data.toString().trim().match(/\+CSQ:\s*(.*)/); if (!val) return null; logger.info('Signal quality extracted', { val }); return val; } function onCOPS(data) { const cops = data.toString().trim().match(/\+COPS:\s*(.*)/); // logger.info(`COPS: ${cops}`); 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
|
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 |
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() }); |
9ee5f03e0
|
70 |
onCSQ(data); |
fe3033bbf
|
71 72 |
} else if (isResultCodeIs(data, 'COPS:')) { logger.verbose('Got a COPS report', { data: data.toString() }); |
9ee5f03e0
|
73 |
onCOPS(data); |
fe3033bbf
|
74 75 76 77 78 79 |
} 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
|
80 81 82 83 |
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
|
84 |
exports.parserReadline = parserReadline; |
9ee5f03e0
|
85 |
exports.parserWaitForOkOrError = parserWaitForOkOrError; |