Commit 9ee5f03e075d51419b3e1bf7a20fc83e600d2701

Authored by Adhidarma Hadiwinoto
1 parent 945145c001
Exists in master

MODEM-TESTER: onCops

Showing 4 changed files with 96 additions and 82 deletions Side-by-side Diff

... ... @@ -0,0 +1,3 @@
  1 +const config = require('komodo-sdk/config');
  2 +const logger = require('komodo-sdk/logger');
  3 +
lib/serialport-parsers.js
... ... @@ -0,0 +1,92 @@
  1 +const PARSER_READLINE_DELIMITER = '\r\n';
  2 +const PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = /\r\n(?:OK|ERROR)\r\n/;
  3 +
  4 +
  5 +const ParserReadline = require('@serialport/parser-readline');
  6 +const ParserRegex = require('@serialport/parser-regex');
  7 +
  8 +const logger = require('komodo-sdk/logger');
  9 +
  10 +const dbCops = require('./db-cops');
  11 +
  12 +let port;
  13 +
  14 +exports.setPort = function setPort(val) {
  15 + logger.info('SERIALPORT-PARSERS: setting port');
  16 + port = val;
  17 +};
  18 +
  19 +exports.getPort = function getPort() {
  20 + return port;
  21 +};
  22 +
  23 +function onCSQ(data) {
  24 + const val = data.toString().trim().match(/\+CSQ:\s*(.*)/);
  25 + if (!val) return null;
  26 +
  27 + logger.info('Signal quality extracted', { val });
  28 + return val;
  29 +}
  30 +
  31 +function onCOPS(data) {
  32 + const cops = data.toString().trim().match(/\+COPS:\s*(.*)/);
  33 + // logger.info(`COPS: ${cops}`);
  34 +
  35 + if (!cops) return null;
  36 + const [mode, format, networkId] = cops.split(',');
  37 + const networkName = networkId ? dbCops[networkId] || networkId : null;
  38 +
  39 + logger.verbose('COPS extracted', {
  40 + cops, mode, format, networkId, networkName,
  41 + });
  42 +
  43 + return {
  44 + cops, mode, format, networkId, networkName,
  45 + };
  46 +}
  47 +
  48 +
  49 +function isResultCodeIs(data, resultCode) {
  50 + if (!data) return false;
  51 + const cleanedData = (data.toString() || '').trim();
  52 + if (!cleanedData) return false;
  53 +
  54 + if (resultCode.indexOf('+') !== 0) {
  55 + // eslint-disable-next-line no-param-reassign
  56 + resultCode = `+${resultCode}`;
  57 + }
  58 +
  59 + if (resultCode.search(/:$/) < 0) {
  60 + // eslint-disable-next-line no-param-reassign
  61 + resultCode += ':';
  62 + }
  63 +
  64 + return cleanedData.indexOf(resultCode) === 0;
  65 +}
  66 +
  67 +const parserReadline = new ParserReadline({ delimiter: PARSER_READLINE_DELIMITER });
  68 +parserReadline.on('data', (data) => {
  69 + logger.verbose('INCOMING', { parser: 'parserReadLine', data: `${data.toString()}${PARSER_READLINE_DELIMITER}` });
  70 +
  71 + if (!data) return;
  72 + if (isResultCodeIs(data, 'CSQ')) {
  73 + logger.verbose('Got a signal quality report', { data: data.toString() });
  74 + onCSQ(data);
  75 + } else if (isResultCodeIs(data, 'COPS:')) {
  76 + logger.verbose('Got a COPS report', { data: data.toString() });
  77 + onCOPS(data);
  78 + } else if (isResultCodeIs(data, 'CMT')) {
  79 + logger.verbose('Got a new message report', { data: data.toString() });
  80 + } else if (isResultCodeIs(data, 'CMTI')) {
  81 + logger.verbose('Got a new message notification report', { data: data.toString() });
  82 + }
  83 +});
  84 +
  85 +const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX });
  86 +parserWaitForOkOrError.on('data', (data) => {
  87 + logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() });
  88 +});
  89 +
  90 +
  91 +exports.parserReadline = parserReadline;
  92 +exports.parserWaitForOkOrError = parserWaitForOkOrError;
1   -const REGEX_WAIT_FOR_OK_OR_ERROR = /\r\n(?:OK|ERROR)\r/;
2   -// const REGEX_WAIT_FOR_OK_OR_ERROR = /\nOK\r/;
3   -
4 1 const SerialPort = require('serialport');
5 2  
6 3 const config = require('komodo-sdk/config');
7 4 const logger = require('komodo-sdk/logger');
8 5  
9 6  
10   -const ParserRegex = require('@serialport/parser-regex');
11   -
12   -const parserWaitForOkOrError = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR });
13   -parserWaitForOkOrError.on('data', (data) => {
14   - logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() });
15   -});
16   -
17 7 const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout');
18 8  
19 9 const parsers = require('./serialport-parsers');
... ... @@ -50,17 +40,6 @@ async function writeToPortDelayed(data, ms) {
50 40 return result;
51 41 }
52 42  
53   -/*
54   -function isNotBlacklistedCommand(command) {
55   - let [, cmd] = (command || '').trim().split('+');
56   - cmd = (cmd || '').replace(/=.*$/, '');
57   - return !config
58   - || !config.modem_tester
59   - || !config.modem_tester.skip_commands
60   - || (config.modem_tester.skip_commands.indexOf(cmd) < 0);
61   -}
62   -*/
63   -
64 43 port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => {
65 44 if (err) {
66 45 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) =&gt;
69 48  
70 49 await writeToPortDelayed('AT\r');
71 50  
72   - /*
73   - if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT&F\r', 2000);
74   - if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT+CGSN\r', 2000);
75   - if (isNotBlacklistedCommand('CIMI')) await writeToPortDelayed('AT+CIMI\r', 2000);
76   - if (isNotBlacklistedCommand('CSQ')) await writeToPortDelayed('AT+CSQ\r', 2000);
77   - if (isNotBlacklistedCommand('COPS?')) await writeToPortDelayed('AT+COPS?\r', 2000);
78   - // if (isNotBlacklistedCommand('CMGD')) await writeToPortDelayed('AT+CMGD=0,4\r', 2000);
79   - */
80   -
81 51 const commands = [
82 52 'AT&F\r',
83 53 'ATE0\r',
... ... @@ -110,7 +80,7 @@ parsers.setPort(port);
110 80  
111 81 if (config && config.modem_tester && config.modem_tester.parser === 'regex') {
112 82 logger.info('Using parserWaitForOkOrError');
113   - port.pipe(parserWaitForOkOrError);
  83 + port.pipe(parsers.parserWaitForOkOrError);
114 84 } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') {
115 85 logger.info('Using parserInterByteTimeout');
116 86 port.pipe(parserInterByteTimeout);
serialport-parsers.js
... ... @@ -1,51 +0,0 @@
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;