Commit dee2ba97ab01806e185fd42095dc842df0d7c5cf
1 parent
b8fc7b8781
Exists in
master
queryCOPS
Showing 2 changed files with 18 additions and 2 deletions Inline Diff
lib/modem-commands.js
1 | const MUTEX_COMMAND = 'COMMAND'; | 1 | const MUTEX_COMMAND = 'COMMAND'; |
2 | // const MUTEX_SUBCOMMAND = 'SUBCOMMAND'; | 2 | // const MUTEX_SUBCOMMAND = 'SUBCOMMAND'; |
3 | 3 | ||
4 | const logger = require('komodo-sdk/logger'); | 4 | const logger = require('komodo-sdk/logger'); |
5 | const mutex = require('./mutex-common'); | 5 | const mutex = require('./mutex-common'); |
6 | // const parsers = require('./serialport-parsers'); | 6 | // const parsers = require('./serialport-parsers'); |
7 | 7 | ||
8 | let port; | 8 | let port; |
9 | 9 | ||
10 | function writeToPort(data) { | 10 | function writeToPort(data) { |
11 | return new Promise((resolve) => { | 11 | return new Promise((resolve) => { |
12 | port.write(data, (err, bytesWritten) => { | 12 | port.write(data, (err, bytesWritten) => { |
13 | if (err) logger.warn(`ERROR: ${err.toString()}`); | 13 | if (err) logger.warn(`ERROR: ${err.toString()}`); |
14 | 14 | ||
15 | logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); | 15 | logger.verbose('OUTGOING', { bytesWritten, data: data.toString() }); |
16 | resolve(bytesWritten); | 16 | resolve(bytesWritten); |
17 | }); | 17 | }); |
18 | }); | 18 | }); |
19 | } | 19 | } |
20 | 20 | ||
21 | exports.sleep = function sleep(ms) { | ||
22 | return new Promise((resolve) => { | ||
23 | setTimeout(() => { | ||
24 | resolve(); | ||
25 | }, ms || 0); | ||
26 | }); | ||
27 | }; | ||
28 | |||
29 | |||
21 | exports.setPort = function setPort(val) { | 30 | exports.setPort = function setPort(val) { |
22 | port = val; | 31 | port = val; |
23 | }; | 32 | }; |
24 | 33 | ||
25 | exports.querySignalQuality = function querySignalQuality() { | 34 | exports.querySignalQuality = function querySignalQuality() { |
26 | return new Promise(async (resolve) => { | 35 | return new Promise(async (resolve) => { |
27 | if (!mutex.tryLock(MUTEX_COMMAND, 'querySignalQuality')) { | 36 | if (!mutex.tryLock(MUTEX_COMMAND, 'querySignalQuality')) { |
28 | resolve(false); | 37 | resolve(false); |
29 | return; | 38 | return; |
30 | } | 39 | } |
31 | 40 | ||
32 | await writeToPort('AT+CSQ\r'); | 41 | await writeToPort('AT+CSQ\r'); |
33 | mutex.unlock(MUTEX_COMMAND, 'querySignalQuality'); | 42 | mutex.unlock(MUTEX_COMMAND, 'querySignalQuality'); |
34 | resolve(true); | 43 | resolve(true); |
35 | }); | 44 | }); |
36 | }; | 45 | }; |
46 | |||
47 | exports.queryCOPS = function querySignalQuality() { | ||
48 | return new Promise(async (resolve) => { | ||
49 | await mutex.lock(MUTEX_COMMAND, 'queryCOPS'); | ||
50 | await writeToPort('AT+COPS?\r'); | ||
51 | mutex.unlock(MUTEX_COMMAND, 'queryCOPS'); | ||
52 | resolve(true); | ||
53 | }); | ||
54 | }; | ||
37 | 55 |
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+COPS?\r', | ||
57 | ]; | 56 | ]; |
58 | 57 | ||
59 | |||
60 | const commandsCount = commands.length; | 58 | const commandsCount = commands.length; |
61 | // eslint-disable-next-line no-plusplus | 59 | // eslint-disable-next-line no-plusplus |
62 | for (let i = 0; i < commandsCount; i++) { | 60 | for (let i = 0; i < commandsCount; i++) { |
63 | // eslint-disable-next-line no-await-in-loop | 61 | // eslint-disable-next-line no-await-in-loop |
64 | await writeToPortDelayed(commands[i], 2000); | 62 | await writeToPortDelayed(commands[i], 2000); |
65 | } | 63 | } |
66 | 64 | ||
67 | if (config && config.modem_tester && config.modem_tester.commands | 65 | if (config && config.modem_tester && config.modem_tester.commands |
68 | && config.modem_tester.commands.length) { | 66 | && config.modem_tester.commands.length) { |
69 | const additionalCommandsLength = config.modem_tester.commands.length; | 67 | const additionalCommandsLength = config.modem_tester.commands.length; |
70 | // eslint-disable-next-line no-plusplus | 68 | // eslint-disable-next-line no-plusplus |
71 | for (let i = 0; i < additionalCommandsLength; i++) { | 69 | for (let i = 0; i < additionalCommandsLength; i++) { |
72 | // eslint-disable-next-line no-await-in-loop | 70 | // eslint-disable-next-line no-await-in-loop |
73 | await writeToPortDelayed(config.modem_tester.commands[i], 2000); | 71 | await writeToPortDelayed(config.modem_tester.commands[i], 2000); |
74 | } | 72 | } |
75 | } | 73 | } |
76 | 74 | ||
77 | setInterval(() => { | 75 | setInterval(() => { |
78 | modemCommands.querySignalQuality(); | 76 | modemCommands.querySignalQuality(); |
79 | }, 30000); | 77 | }, 30000); |
80 | }); | 78 | }); |
81 | 79 | ||
82 | parsers.setPort(port); | 80 | parsers.setPort(port); |
83 | modemCommands.setPort(port); | 81 | modemCommands.setPort(port); |
84 | 82 | ||
85 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { | 83 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { |
86 | logger.info('Using parserWaitForOkOrError'); | 84 | logger.info('Using parserWaitForOkOrError'); |
87 | port.pipe(parsers.parserWaitForOkOrError); | 85 | port.pipe(parsers.parserWaitForOkOrError); |
88 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { | 86 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { |
89 | logger.info('Using parserInterByteTimeout'); | 87 | logger.info('Using parserInterByteTimeout'); |
90 | port.pipe(parserInterByteTimeout); | 88 | port.pipe(parserInterByteTimeout); |
91 | } else { | 89 | } else { |
92 | logger.info('Using parserReadline'); | 90 | logger.info('Using parserReadline'); |
93 | port.pipe(parsers.parserReadline); | 91 | port.pipe(parsers.parserReadline); |
94 | } | 92 | } |
95 | 93 |