Commit fe3033bbf9c22322931e8437da588b0dcbc6be1b

Authored by Adhidarma Hadiwinoto
1 parent 9fd37cd21f
Exists in master

MODEM-TESTER: report type detection

Showing 2 changed files with 74 additions and 25 deletions Side-by-side Diff

... ... @@ -6,12 +6,6 @@ const SerialPort = require('serialport');
6 6 const config = require('komodo-sdk/config');
7 7 const logger = require('komodo-sdk/logger');
8 8  
9   -const ParserReadline = require('@serialport/parser-readline');
10   -
11   -const parserReadline = new ParserReadline({ delimiter: '\r\n' });
12   -parserReadline.on('data', (data) => {
13   - logger.verbose('INCOMING', { parser: 'parserReadLine', data: data.toString() });
14   -});
15 9  
16 10 const ParserRegex = require('@serialport/parser-regex');
17 11  
... ... @@ -20,9 +14,10 @@ parserWaitForOkOrError.on('data', (data) => {
20 14 logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() });
21 15 });
22 16  
23   -
24 17 const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout');
25 18  
  19 +const parsers = require('./serialport-parsers');
  20 +
26 21 const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 });
27 22 parserInterByteTimeout.on('data', (data) => {
28 23 logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() });
... ... @@ -83,20 +78,15 @@ port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) =>
83 78 // if (isNotBlacklistedCommand('CMGD')) await writeToPortDelayed('AT+CMGD=0,4\r', 2000);
84 79 */
85 80  
86   - let commands;
87   - if (config && config.modem_tester && config.modem_tester.commands) {
88   - ({ commands } = config.modem_tester);
89   - } else {
90   - commands = [
91   - 'AT&F\r',
92   - 'ATE0\r',
93   - 'AT+CGSN\r',
94   - 'AT+CIMI\r',
95   - 'AT+COPS?\r',
96   - 'AT+CNMI=1,1,2,1,1\r',
97   - 'AT+CSQ\r',
98   - ];
99   - }
  81 + const commands = [
  82 + 'AT&F\r',
  83 + 'ATE0\r',
  84 + 'AT+CGSN\r',
  85 + 'AT+CIMI\r',
  86 + 'AT+COPS?\r',
  87 + 'AT+CSQ\r',
  88 + ];
  89 +
100 90  
101 91 const commandsCount = commands.length;
102 92 // eslint-disable-next-line no-plusplus
... ... @@ -105,11 +95,19 @@ port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) =>
105 95 await writeToPortDelayed(commands[i], 2000);
106 96 }
107 97  
108   - setInterval(() => {
109   - writeToPort('AT+CSQ\r', 2000);
110   - }, 30000);
  98 + if (config && config.modem_tester && config.modem_tester.commands
  99 + && config.modem_tester.commands.length) {
  100 + const additionalCommandsLength = config.modem_tester.commands.length;
  101 + // eslint-disable-next-line no-plusplus
  102 + for (let i = 0; i < additionalCommandsLength; i++) {
  103 + // eslint-disable-next-line no-await-in-loop
  104 + await writeToPortDelayed(config.modem_tester.commands[i], 2000);
  105 + }
  106 + }
111 107 });
112 108  
  109 +parsers.setPort(port);
  110 +
113 111 if (config && config.modem_tester && config.modem_tester.parser === 'regex') {
114 112 logger.info('Using parserWaitForOkOrError');
115 113 port.pipe(parserWaitForOkOrError);
... ... @@ -118,5 +116,5 @@ if (config &amp;&amp; config.modem_tester &amp;&amp; config.modem_tester.parser === &#39;regex&#39;) {
118 116 port.pipe(parserInterByteTimeout);
119 117 } else {
120 118 logger.info('Using parserReadline');
121   - port.pipe(parserReadline);
  119 + port.pipe(parsers.parserReadline);
122 120 }
serialport-parsers.js
... ... @@ -0,0 +1,51 @@
  1 +const PARSER_READLINE_DELIMITER = '\r\n';
  2 +
  3 +const ParserReadline = require('@serialport/parser-readline');
  4 +const logger = require('komodo-sdk/logger');
  5 +
  6 +let port;
  7 +
  8 +exports.setPort = function setPort(val) {
  9 + logger.info('SERIALPORT-PARSERS: setting port');
  10 + port = val;
  11 +};
  12 +
  13 +exports.getPort = function getPort() {
  14 + return port;
  15 +};
  16 +
  17 +function isResultCodeIs(data, resultCode) {
  18 + if (!data) return false;
  19 + const cleanedData = (data.toString() || '').trim();
  20 + if (!cleanedData) return false;
  21 +
  22 + if (resultCode.indexOf('+') !== 0) {
  23 + // eslint-disable-next-line no-param-reassign
  24 + resultCode = `+${resultCode}`;
  25 + }
  26 +
  27 + if (resultCode.search(/:$/) < 0) {
  28 + // eslint-disable-next-line no-param-reassign
  29 + resultCode += ':';
  30 + }
  31 +
  32 + return cleanedData.indexOf(resultCode) === 0;
  33 +}
  34 +
  35 +const parserReadline = new ParserReadline({ delimiter: PARSER_READLINE_DELIMITER });
  36 +parserReadline.on('data', (data) => {
  37 + logger.verbose('INCOMING', { parser: 'parserReadLine', data: `${data.toString()}${PARSER_READLINE_DELIMITER}` });
  38 +
  39 + if (!data) return;
  40 + if (isResultCodeIs(data, 'CSQ')) {
  41 + logger.verbose('Got a signal quality report', { data: data.toString() });
  42 + } else if (isResultCodeIs(data, 'COPS:')) {
  43 + logger.verbose('Got a COPS report', { data: data.toString() });
  44 + } else if (isResultCodeIs(data, 'CMT')) {
  45 + logger.verbose('Got a new message report', { data: data.toString() });
  46 + } else if (isResultCodeIs(data, 'CMTI')) {
  47 + logger.verbose('Got a new message notification report', { data: data.toString() });
  48 + }
  49 +});
  50 +
  51 +exports.parserReadline = parserReadline;