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