Commit 8f7f6a139765ede8f9b4c4413af76ef2d2423d3e

Authored by Adhidarma Hadiwinoto
1 parent b775379540
Exists in master

writeToPortAndWaitForReadline

Showing 2 changed files with 21 additions and 23 deletions Side-by-side Diff

lib/modem-commands.js
1 1 const MUTEX_COMMAND = 'COMMAND';
2 2 // const MUTEX_SUBCOMMAND = 'SUBCOMMAND';
3 3  
  4 +const ParserReadline = require('@serialport/parser-readline');
4 5 const ParserRegex = require('@serialport/parser-regex');
5 6  
6 7 const logger = require('komodo-sdk/logger');
... ... @@ -21,6 +22,22 @@ function writeToPort(data) {
21 22 });
22 23 }
23 24  
  25 +exports.writeToPortAndWaitForReadline = function writeToPortAndWaitForReadline(cmd, lockName) {
  26 + return new Promise(async (resolve) => {
  27 + const parser = new ParserReadline({ delimiter: parsers.PARSER_READLINE_DELIMITER });
  28 + parser.on('data', (data) => {
  29 + logger.verbose('INCOMING', { parser: 'customParserReadLine', data: `${data.toString()}${parsers.PARSER_READLINE_DELIMITER}` });
  30 + port.unpipe(parser);
  31 + mutex.unlock(lockName || MUTEX_COMMAND, cmd.trim());
  32 + resolve(data);
  33 + });
  34 +
  35 + await mutex.lock(lockName || MUTEX_COMMAND, cmd.trim());
  36 + port.pipe(parser);
  37 + await writeToPort(cmd);
  38 + });
  39 +};
  40 +
24 41 exports.sleep = function sleep(ms) {
25 42 return new Promise((resolve) => {
26 43 setTimeout(() => {
... ... @@ -49,28 +49,9 @@ port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) =>
49 49  
50 50 await writeToPortDelayed('AT\r');
51 51  
52   - const commands = [
53   - 'AT&FE0\r',
54   - 'AT+CMGF=0\r',
55   - 'AT+CNMI=1,2,0,1,0\r',
56   - ];
57   -
58   - const commandsCount = commands.length;
59   - // eslint-disable-next-line no-plusplus
60   - for (let i = 0; i < commandsCount; i++) {
61   - // eslint-disable-next-line no-await-in-loop
62   - await writeToPortDelayed(commands[i], 2000);
63   - }
64   -
65   - if (config && config.modem_tester && config.modem_tester.commands
66   - && config.modem_tester.commands.length) {
67   - const additionalCommandsLength = config.modem_tester.commands.length;
68   - // eslint-disable-next-line no-plusplus
69   - for (let i = 0; i < additionalCommandsLength; i++) {
70   - // eslint-disable-next-line no-await-in-loop
71   - await writeToPortDelayed(config.modem_tester.commands[i], 2000);
72   - }
73   - }
  52 + await modemCommands.writeToPortAndWaitForReadline('AT&FE0\r');
  53 + await modemCommands.writeToPortAndWaitForReadline('AT+CMGF=0\r');
  54 + await modemCommands.writeToPortAndWaitForReadline('AT+CNMI=1,2,0,1,0\r');
74 55  
75 56 const imei = await modemCommands.queryIMEI();
76 57 logger.info(`**** IMEI: ${imei}`);
... ... @@ -86,7 +67,7 @@ port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) =&gt;
86 67  
87 68 setInterval(() => {
88 69 modemCommands.querySignalQuality();
89   - }, 30000);
  70 + }, (config && config.interval_beetwen_signal_strength_ms) || 30000);
90 71 });
91 72  
92 73 parsers.setPort(port);