Commit 4bd91feaaf27a12c2cbb977b59908961762acb66
1 parent
5e461a00aa
Exists in
master
Merge AT&FE0
Showing 1 changed file with 1 additions and 2 deletions Inline Diff
modem-tester.js
1 | const SerialPort = require('serialport'); | 1 | const SerialPort = require('serialport'); |
2 | 2 | ||
3 | const config = require('komodo-sdk/config'); | 3 | const config = require('komodo-sdk/config'); |
4 | const logger = require('komodo-sdk/logger'); | 4 | const logger = require('komodo-sdk/logger'); |
5 | 5 | ||
6 | 6 | ||
7 | const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout'); | 7 | const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout'); |
8 | 8 | ||
9 | const parsers = require('./lib/serialport-parsers'); | 9 | const parsers = require('./lib/serialport-parsers'); |
10 | 10 | ||
11 | const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 }); | 11 | const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 }); |
12 | parserInterByteTimeout.on('data', (data) => { | 12 | parserInterByteTimeout.on('data', (data) => { |
13 | logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() }); | 13 | logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() }); |
14 | }); | 14 | }); |
15 | 15 | ||
16 | let port; | 16 | let port; |
17 | 17 | ||
18 | function sleep(ms) { | 18 | function sleep(ms) { |
19 | return new Promise((resolve) => { | 19 | return new Promise((resolve) => { |
20 | setTimeout(() => { | 20 | setTimeout(() => { |
21 | resolve(); | 21 | resolve(); |
22 | }, ms || 0); | 22 | }, ms || 0); |
23 | }); | 23 | }); |
24 | } | 24 | } |
25 | 25 | ||
26 | function writeToPort(data) { | 26 | function writeToPort(data) { |
27 | return new Promise((resolve) => { | 27 | return new Promise((resolve) => { |
28 | port.write(data, (err, bytesWritten) => { | 28 | port.write(data, (err, bytesWritten) => { |
29 | if (err) logger.warn(`ERROR: ${err.toString()}`); | 29 | if (err) logger.warn(`ERROR: ${err.toString()}`); |
30 | 30 | ||
31 | logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); | 31 | logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); |
32 | resolve(bytesWritten); | 32 | resolve(bytesWritten); |
33 | }); | 33 | }); |
34 | }); | 34 | }); |
35 | } | 35 | } |
36 | 36 | ||
37 | async function writeToPortDelayed(data, ms) { | 37 | async function writeToPortDelayed(data, ms) { |
38 | await sleep(ms || 500); | 38 | await sleep(ms || 500); |
39 | const result = writeToPort(data); | 39 | const result = writeToPort(data); |
40 | return result; | 40 | return result; |
41 | } | 41 | } |
42 | 42 | ||
43 | port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => { | 43 | port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => { |
44 | if (err) { | 44 | if (err) { |
45 | logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); | 45 | logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); |
46 | process.exit(1); | 46 | process.exit(1); |
47 | } | 47 | } |
48 | 48 | ||
49 | await writeToPortDelayed('AT\r'); | 49 | await writeToPortDelayed('AT\r'); |
50 | 50 | ||
51 | const commands = [ | 51 | const commands = [ |
52 | 'AT&F\r', | 52 | 'AT&FE0\r', |
53 | 'ATE1\r', | ||
54 | 'AT+CGSN\r', | 53 | 'AT+CGSN\r', |
55 | 'AT+CIMI\r', | 54 | 'AT+CIMI\r', |
56 | 'AT+COPS?\r', | 55 | 'AT+COPS?\r', |
57 | 'AT+CSQ\r', | 56 | 'AT+CSQ\r', |
58 | ]; | 57 | ]; |
59 | 58 | ||
60 | 59 | ||
61 | const commandsCount = commands.length; | 60 | const commandsCount = commands.length; |
62 | // eslint-disable-next-line no-plusplus | 61 | // eslint-disable-next-line no-plusplus |
63 | for (let i = 0; i < commandsCount; i++) { | 62 | for (let i = 0; i < commandsCount; i++) { |
64 | // eslint-disable-next-line no-await-in-loop | 63 | // eslint-disable-next-line no-await-in-loop |
65 | await writeToPortDelayed(commands[i], 2000); | 64 | await writeToPortDelayed(commands[i], 2000); |
66 | } | 65 | } |
67 | 66 | ||
68 | if (config && config.modem_tester && config.modem_tester.commands | 67 | if (config && config.modem_tester && config.modem_tester.commands |
69 | && config.modem_tester.commands.length) { | 68 | && config.modem_tester.commands.length) { |
70 | const additionalCommandsLength = config.modem_tester.commands.length; | 69 | const additionalCommandsLength = config.modem_tester.commands.length; |
71 | // eslint-disable-next-line no-plusplus | 70 | // eslint-disable-next-line no-plusplus |
72 | for (let i = 0; i < additionalCommandsLength; i++) { | 71 | for (let i = 0; i < additionalCommandsLength; i++) { |
73 | // eslint-disable-next-line no-await-in-loop | 72 | // eslint-disable-next-line no-await-in-loop |
74 | await writeToPortDelayed(config.modem_tester.commands[i], 2000); | 73 | await writeToPortDelayed(config.modem_tester.commands[i], 2000); |
75 | } | 74 | } |
76 | } | 75 | } |
77 | 76 | ||
78 | setTimeout(() => { | 77 | setTimeout(() => { |
79 | writeToPort('AT+CSQ\r'); | 78 | writeToPort('AT+CSQ\r'); |
80 | }, 60000); | 79 | }, 60000); |
81 | }); | 80 | }); |
82 | 81 | ||
83 | parsers.setPort(port); | 82 | parsers.setPort(port); |
84 | 83 | ||
85 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { | 84 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { |
86 | logger.info('Using parserWaitForOkOrError'); | 85 | logger.info('Using parserWaitForOkOrError'); |
87 | port.pipe(parsers.parserWaitForOkOrError); | 86 | port.pipe(parsers.parserWaitForOkOrError); |
88 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { | 87 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { |
89 | logger.info('Using parserInterByteTimeout'); | 88 | logger.info('Using parserInterByteTimeout'); |
90 | port.pipe(parserInterByteTimeout); | 89 | port.pipe(parserInterByteTimeout); |
91 | } else { | 90 | } else { |
92 | logger.info('Using parserReadline'); | 91 | logger.info('Using parserReadline'); |
93 | port.pipe(parsers.parserReadline); | 92 | port.pipe(parsers.parserReadline); |
94 | } | 93 | } |
95 | 94 |