diff --git a/modem-tester.js b/modem-tester.js index e5ea8e6..1e4d432 100644 --- a/modem-tester.js +++ b/modem-tester.js @@ -6,12 +6,6 @@ const SerialPort = require('serialport'); const config = require('komodo-sdk/config'); const logger = require('komodo-sdk/logger'); -const ParserReadline = require('@serialport/parser-readline'); - -const parserReadline = new ParserReadline({ delimiter: '\r\n' }); -parserReadline.on('data', (data) => { - logger.verbose('INCOMING', { parser: 'parserReadLine', data: data.toString() }); -}); const ParserRegex = require('@serialport/parser-regex'); @@ -20,9 +14,10 @@ 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'); + const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 }); parserInterByteTimeout.on('data', (data) => { logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() }); @@ -83,20 +78,15 @@ port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => // if (isNotBlacklistedCommand('CMGD')) await writeToPortDelayed('AT+CMGD=0,4\r', 2000); */ - let commands; - if (config && config.modem_tester && config.modem_tester.commands) { - ({ commands } = config.modem_tester); - } else { - commands = [ - 'AT&F\r', - 'ATE0\r', - 'AT+CGSN\r', - 'AT+CIMI\r', - 'AT+COPS?\r', - 'AT+CNMI=1,1,2,1,1\r', - 'AT+CSQ\r', - ]; - } + const commands = [ + 'AT&F\r', + 'ATE0\r', + 'AT+CGSN\r', + 'AT+CIMI\r', + 'AT+COPS?\r', + 'AT+CSQ\r', + ]; + const commandsCount = commands.length; // eslint-disable-next-line no-plusplus @@ -105,11 +95,19 @@ port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => await writeToPortDelayed(commands[i], 2000); } - setInterval(() => { - writeToPort('AT+CSQ\r', 2000); - }, 30000); + if (config && config.modem_tester && config.modem_tester.commands + && config.modem_tester.commands.length) { + const additionalCommandsLength = config.modem_tester.commands.length; + // eslint-disable-next-line no-plusplus + for (let i = 0; i < additionalCommandsLength; i++) { + // eslint-disable-next-line no-await-in-loop + await writeToPortDelayed(config.modem_tester.commands[i], 2000); + } + } }); +parsers.setPort(port); + if (config && config.modem_tester && config.modem_tester.parser === 'regex') { logger.info('Using parserWaitForOkOrError'); port.pipe(parserWaitForOkOrError); @@ -118,5 +116,5 @@ if (config && config.modem_tester && config.modem_tester.parser === 'regex') { port.pipe(parserInterByteTimeout); } else { logger.info('Using parserReadline'); - port.pipe(parserReadline); + port.pipe(parsers.parserReadline); } diff --git a/serialport-parsers.js b/serialport-parsers.js new file mode 100644 index 0000000..f988cfc --- /dev/null +++ b/serialport-parsers.js @@ -0,0 +1,51 @@ +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;