Commit 67916edcba4debfba1ca56710b53608b93716c8f
1 parent
332f24930f
Exists in
master
Some inits on MODEM-TESTER
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 | 'AT+CMGF=0\r', | ||
57 | 'AT+CNMI=1,2,0,1,0\r', | ||
56 | ]; | 58 | ]; |
57 | 59 | ||
58 | const commandsCount = commands.length; | 60 | const commandsCount = commands.length; |
59 | // eslint-disable-next-line no-plusplus | 61 | // eslint-disable-next-line no-plusplus |
60 | for (let i = 0; i < commandsCount; i++) { | 62 | for (let i = 0; i < commandsCount; i++) { |
61 | // eslint-disable-next-line no-await-in-loop | 63 | // eslint-disable-next-line no-await-in-loop |
62 | await writeToPortDelayed(commands[i], 2000); | 64 | await writeToPortDelayed(commands[i], 2000); |
63 | } | 65 | } |
64 | 66 | ||
65 | if (config && config.modem_tester && config.modem_tester.commands | 67 | if (config && config.modem_tester && config.modem_tester.commands |
66 | && config.modem_tester.commands.length) { | 68 | && config.modem_tester.commands.length) { |
67 | const additionalCommandsLength = config.modem_tester.commands.length; | 69 | const additionalCommandsLength = config.modem_tester.commands.length; |
68 | // eslint-disable-next-line no-plusplus | 70 | // eslint-disable-next-line no-plusplus |
69 | for (let i = 0; i < additionalCommandsLength; i++) { | 71 | for (let i = 0; i < additionalCommandsLength; i++) { |
70 | // eslint-disable-next-line no-await-in-loop | 72 | // eslint-disable-next-line no-await-in-loop |
71 | await writeToPortDelayed(config.modem_tester.commands[i], 2000); | 73 | await writeToPortDelayed(config.modem_tester.commands[i], 2000); |
72 | } | 74 | } |
73 | } | 75 | } |
74 | 76 | ||
75 | await modemCommands.queryCOPS(); | 77 | await modemCommands.queryCOPS(); |
76 | 78 | ||
77 | setInterval(() => { | 79 | setInterval(() => { |
78 | modemCommands.querySignalQuality(); | 80 | modemCommands.querySignalQuality(); |
79 | }, 30000); | 81 | }, 30000); |
80 | }); | 82 | }); |
81 | 83 | ||
82 | parsers.setPort(port); | 84 | parsers.setPort(port); |
83 | modemCommands.setPort(port); | 85 | modemCommands.setPort(port); |
84 | 86 | ||
85 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { | 87 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { |
86 | logger.info('Using parserWaitForOkOrError'); | 88 | logger.info('Using parserWaitForOkOrError'); |
87 | port.pipe(parsers.parserWaitForOkOrError); | 89 | port.pipe(parsers.parserWaitForOkOrError); |
88 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { | 90 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { |
89 | logger.info('Using parserInterByteTimeout'); | 91 | logger.info('Using parserInterByteTimeout'); |
90 | port.pipe(parserInterByteTimeout); | 92 | port.pipe(parserInterByteTimeout); |
91 | } else { | 93 | } else { |
92 | logger.info('Using parserReadline'); | 94 | logger.info('Using parserReadline'); |
93 | port.pipe(parsers.parserReadline); | 95 | port.pipe(parsers.parserReadline); |
94 | } | 96 | } |
95 | 97 |