Commit 9cfc0203ffbf83957d75e853a0f191816272f832
1 parent
caac94e435
Exists in
master
Async on background check
Showing 1 changed file with 3 additions and 3 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 | const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout'); | 6 | const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout'); |
7 | 7 | ||
8 | const parsers = require('./lib/serialport-parsers'); | 8 | const parsers = require('./lib/serialport-parsers'); |
9 | const modemCommands = require('./lib/modem-commands'); | 9 | const modemCommands = require('./lib/modem-commands'); |
10 | const modemInfo = require('./lib/modem-info'); | 10 | const modemInfo = require('./lib/modem-info'); |
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 | const port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => { | 17 | const port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => { |
18 | if (err) { | 18 | if (err) { |
19 | logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); | 19 | logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`); |
20 | process.exit(1); | 20 | process.exit(1); |
21 | } | 21 | } |
22 | 22 | ||
23 | await modemCommands.writeToPortAndWaitForOkOrError('AT\r'); | 23 | await modemCommands.writeToPortAndWaitForOkOrError('AT\r'); |
24 | await modemCommands.writeToPortAndWaitForOkOrError('AT&FE0\r'); | 24 | await modemCommands.writeToPortAndWaitForOkOrError('AT&FE0\r'); |
25 | await modemCommands.writeToPortAndWaitForOkOrError('AT+CMGF=0\r'); | 25 | await modemCommands.writeToPortAndWaitForOkOrError('AT+CMGF=0\r'); |
26 | await modemCommands.writeToPortAndWaitForOkOrError('AT+CNMI=1,2,0,1,0\r'); | 26 | await modemCommands.writeToPortAndWaitForOkOrError('AT+CNMI=1,2,0,1,0\r'); |
27 | 27 | ||
28 | await modemCommands.queryIMEIAndIMSI(); | 28 | await modemCommands.queryIMEIAndIMSI(); |
29 | await modemCommands.queryCOPSAndSignalQuality(); | 29 | await modemCommands.queryCOPSAndSignalQuality(); |
30 | logger.info('Modem state', modemInfo); | 30 | logger.info('Modem state', modemInfo); |
31 | 31 | ||
32 | setInterval(() => { | 32 | setInterval(async () => { |
33 | modemCommands.queryIMEIAndIMSI(); | 33 | await modemCommands.queryIMEIAndIMSI(); |
34 | modemCommands.queryCOPSAndSignalQuality(); | 34 | await modemCommands.queryCOPSAndSignalQuality(); |
35 | logger.info('Modem state', modemInfo); | 35 | logger.info('Modem state', modemInfo); |
36 | }, (config && config.interval_beetwen_signal_strength_ms) || 30000); | 36 | }, (config && config.interval_beetwen_signal_strength_ms) || 30000); |
37 | }); | 37 | }); |
38 | 38 | ||
39 | parsers.setPort(port); | 39 | parsers.setPort(port); |
40 | modemCommands.setPort(port); | 40 | modemCommands.setPort(port); |
41 | 41 | ||
42 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { | 42 | if (config && config.modem_tester && config.modem_tester.parser === 'regex') { |
43 | logger.info('Using parserWaitForOkOrError'); | 43 | logger.info('Using parserWaitForOkOrError'); |
44 | port.pipe(parsers.parserWaitForOkOrError); | 44 | port.pipe(parsers.parserWaitForOkOrError); |
45 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { | 45 | } else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') { |
46 | logger.info('Using parserInterByteTimeout'); | 46 | logger.info('Using parserInterByteTimeout'); |
47 | port.pipe(parserInterByteTimeout); | 47 | port.pipe(parserInterByteTimeout); |
48 | } else { | 48 | } else { |
49 | logger.info('Using parserReadline'); | 49 | logger.info('Using parserReadline'); |
50 | port.pipe(parsers.parserReadline); | 50 | port.pipe(parsers.parserReadline); |
51 | } | 51 | } |
52 | 52 |