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