Commit e509607c54ae1d6684ef4cca01ed398af9f97609
1 parent
d87943efc5
Exists in
master
MODEM-COMMANDS: CSQ
Showing 2 changed files with 39 additions and 3 deletions Inline Diff
lib/modem-commands.js
File was created | 1 | const MUTEX_COMMAND = 'COMMAND'; | |
2 | // const MUTEX_SUBCOMMAND = 'SUBCOMMAND'; | ||
3 | |||
4 | const logger = require('komodo-sdk/logger'); | ||
5 | const mutex = require('./mutex-common'); | ||
6 | // const parsers = require('./serialport-parsers'); | ||
7 | |||
8 | let port; | ||
9 | |||
10 | function writeToPort(data) { | ||
11 | return new Promise((resolve) => { | ||
12 | port.write(data, (err, bytesWritten) => { | ||
13 | if (err) logger.warn(`ERROR: ${err.toString()}`); | ||
14 | |||
15 | logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); | ||
16 | resolve(bytesWritten); | ||
17 | }); | ||
18 | }); | ||
19 | } | ||
20 | |||
21 | exports.setPort = function setPort(val) { | ||
22 | port = val; | ||
23 | }; | ||
24 | |||
25 | exports.querySignalQuality = function querySignalQuality() { | ||
26 | return new Promise(async (resolve) => { | ||
27 | if (!mutex.tryLock(MUTEX_COMMAND, 'querySignalQuality')) { | ||
28 | resolve(false); | ||
29 | return; | ||
30 | } | ||
31 | |||
32 | await writeToPort('AT+CSQ\r'); | ||
33 | mutex.unlock(MUTEX_COMMAND, 'querySignalQuality'); | ||
34 | resolve(true); | ||
35 | }); | ||
36 | }; | ||
37 |
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 | 11 | ||
11 | const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 }); | 12 | const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 }); |
12 | parserInterByteTimeout.on('data', (data) => { | 13 | parserInterByteTimeout.on('data', (data) => { |
13 | logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() }); | 14 | logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() }); |
14 | }); | 15 | }); |
15 | 16 | ||
16 | let port; | 17 | let port; |
17 | 18 | ||
18 | function sleep(ms) { | 19 | function sleep(ms) { |
19 | return new Promise((resolve) => { | 20 | return new Promise((resolve) => { |
20 | setTimeout(() => { | 21 | setTimeout(() => { |
21 | resolve(); | 22 | resolve(); |
22 | }, ms || 0); | 23 | }, ms || 0); |
23 | }); | 24 | }); |
24 | } | 25 | } |
25 | 26 | ||
26 | function writeToPort(data) { | 27 | function writeToPort(data) { |
27 | return new Promise((resolve) => { | 28 | return new Promise((resolve) => { |
28 | port.write(data, (err, bytesWritten) => { | 29 | port.write(data, (err, bytesWritten) => { |
29 | if (err) logger.warn(`ERROR: ${err.toString()}`); | 30 | if (err) logger.warn(`ERROR: ${err.toString()}`); |
30 | 31 | ||
31 | logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); | 32 | logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); |
32 | resolve(bytesWritten); | 33 | resolve(bytesWritten); |
33 | }); | 34 | }); |
34 | }); | 35 | }); |
35 | } | 36 | } |
36 | 37 | ||
37 | async function writeToPortDelayed(data, ms) { | 38 | async function writeToPortDelayed(data, ms) { |
38 | await sleep(ms || 500); | 39 | await sleep(ms || 500); |
39 | const result = writeToPort(data); | 40 | const result = writeToPort(data); |
40 | return result; | 41 | return result; |
41 | } | 42 | } |
42 | 43 | ||
43 | port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => { | 44 | port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => { |
44 | if (err) { | 45 | if (err) { |
45 | logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); | 46 | logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); |
46 | process.exit(1); | 47 | process.exit(1); |
47 | } | 48 | } |
48 | 49 | ||
49 | await writeToPortDelayed('AT\r'); | 50 | await writeToPortDelayed('AT\r'); |
50 | 51 | ||
51 | const commands = [ | 52 | const commands = [ |
52 | 'AT&FE0\r', | 53 | 'AT&FE0\r', |
53 | 'AT+CGSN\r', | 54 | 'AT+CGSN\r', |
54 | 'AT+CIMI\r', | 55 | 'AT+CIMI\r', |
55 | 'AT+COPS?\r', | 56 | 'AT+COPS?\r', |
56 | 'AT+CSQ\r', | ||
57 | ]; | 57 | ]; |
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 | setInterval(() => { | 77 | setInterval(() => { |
78 | writeToPort('AT+CSQ\r'); | 78 | modemCommands.querySignalQuality(); |
79 | }, 60000); | 79 | }, 30000); |
80 | }); | 80 | }); |
81 | 81 | ||
82 | parsers.setPort(port); | 82 | parsers.setPort(port); |
83 | 83 | ||
84 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { | 84 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { |
85 | logger.info('Using parserWaitForOkOrError'); | 85 | logger.info('Using parserWaitForOkOrError'); |
86 | port.pipe(parsers.parserWaitForOkOrError); | 86 | port.pipe(parsers.parserWaitForOkOrError); |
87 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { | 87 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { |
88 | logger.info('Using parserInterByteTimeout'); | 88 | logger.info('Using parserInterByteTimeout'); |
89 | port.pipe(parserInterByteTimeout); | 89 | port.pipe(parserInterByteTimeout); |
90 | } else { | 90 | } else { |
91 | logger.info('Using parserReadline'); | 91 | logger.info('Using parserReadline'); |
92 | port.pipe(parsers.parserReadline); | 92 | port.pipe(parsers.parserReadline); |
93 | } | 93 | } |