diff --git a/lib/modem-new.js b/lib/modem-new.js new file mode 100644 index 0000000..d5f821f --- /dev/null +++ b/lib/modem-new.js @@ -0,0 +1,3 @@ +const config = require('komodo-sdk/config'); +const logger = require('komodo-sdk/logger'); + diff --git a/lib/serialport-parsers.js b/lib/serialport-parsers.js new file mode 100644 index 0000000..a50338b --- /dev/null +++ b/lib/serialport-parsers.js @@ -0,0 +1,92 @@ +const PARSER_READLINE_DELIMITER = '\r\n'; +const PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = /\r\n(?:OK|ERROR)\r\n/; + + +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 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, + }; +} + + +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() }); + 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; diff --git a/modem-tester.js b/modem-tester.js index 1e4d432..e48efc2 100644 --- a/modem-tester.js +++ b/modem-tester.js @@ -1,19 +1,9 @@ -const REGEX_WAIT_FOR_OK_OR_ERROR = /\r\n(?:OK|ERROR)\r/; -// const REGEX_WAIT_FOR_OK_OR_ERROR = /\nOK\r/; - const SerialPort = require('serialport'); const config = require('komodo-sdk/config'); const logger = require('komodo-sdk/logger'); -const ParserRegex = require('@serialport/parser-regex'); - -const parserWaitForOkOrError = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR }); -parserWaitForOkOrError.on('data', (data) => { - logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() }); -}); - const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout'); const parsers = require('./serialport-parsers'); @@ -50,17 +40,6 @@ async function writeToPortDelayed(data, ms) { return result; } -/* -function isNotBlacklistedCommand(command) { - let [, cmd] = (command || '').trim().split('+'); - cmd = (cmd || '').replace(/=.*$/, ''); - return !config - || !config.modem_tester - || !config.modem_tester.skip_commands - || (config.modem_tester.skip_commands.indexOf(cmd) < 0); -} -*/ - port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => { if (err) { logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); @@ -69,15 +48,6 @@ port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => await writeToPortDelayed('AT\r'); - /* - if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT&F\r', 2000); - if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT+CGSN\r', 2000); - if (isNotBlacklistedCommand('CIMI')) await writeToPortDelayed('AT+CIMI\r', 2000); - if (isNotBlacklistedCommand('CSQ')) await writeToPortDelayed('AT+CSQ\r', 2000); - if (isNotBlacklistedCommand('COPS?')) await writeToPortDelayed('AT+COPS?\r', 2000); - // if (isNotBlacklistedCommand('CMGD')) await writeToPortDelayed('AT+CMGD=0,4\r', 2000); - */ - const commands = [ 'AT&F\r', 'ATE0\r', @@ -110,7 +80,7 @@ parsers.setPort(port); if (config && config.modem_tester && config.modem_tester.parser === 'regex') { logger.info('Using parserWaitForOkOrError'); - port.pipe(parserWaitForOkOrError); + port.pipe(parsers.parserWaitForOkOrError); } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { logger.info('Using parserInterByteTimeout'); port.pipe(parserInterByteTimeout); diff --git a/serialport-parsers.js b/serialport-parsers.js deleted file mode 100644 index f988cfc..0000000 --- a/serialport-parsers.js +++ /dev/null @@ -1,51 +0,0 @@ -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;