modem-tester.js
3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const REGEX_WAIT_FOR_OK_OR_ERROR = /\r\n(?:OK|ERROR)\r/;
// const REGEX_WAIT_FOR_OK_OR_ERROR = /\nOK\r/;
const SerialPort = require('serialport');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const ParserRegex = require('@serialport/parser-regex');
const parserWaitForOkOrError = new ParserRegex({ regex: REGEX_WAIT_FOR_OK_OR_ERROR });
parserWaitForOkOrError.on('data', (data) => {
logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() });
});
const ParserInterByteTimeout = require('@serialport/parser-inter-byte-timeout');
const parsers = require('./serialport-parsers');
const parserInterByteTimeout = new ParserInterByteTimeout({ interval: 1000 });
parserInterByteTimeout.on('data', (data) => {
logger.verbose('INCOMING', { parser: 'parserInterByteTimeout', data: data.toString() });
});
let port;
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms || 0);
});
}
function writeToPort(data) {
return new Promise((resolve) => {
port.write(data, (err, bytesWritten) => {
if (err) logger.warn(`ERROR: ${err.toString()}`);
logger.verbose('OUTGOING', { bytesWritten, data: data.toString() });
resolve(bytesWritten);
});
});
}
async function writeToPortDelayed(data, ms) {
await sleep(ms || 500);
const result = writeToPort(data);
return result;
}
/*
function isNotBlacklistedCommand(command) {
let [, cmd] = (command || '').trim().split('+');
cmd = (cmd || '').replace(/=.*$/, '');
return !config
|| !config.modem_tester
|| !config.modem_tester.skip_commands
|| (config.modem_tester.skip_commands.indexOf(cmd) < 0);
}
*/
port = new SerialPort(config.modem.device, { baudRate: 115200 }, async (err) => {
if (err) {
logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`);
process.exit(1);
}
await writeToPortDelayed('AT\r');
/*
if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT&F\r', 2000);
if (isNotBlacklistedCommand('CGSN')) await writeToPortDelayed('AT+CGSN\r', 2000);
if (isNotBlacklistedCommand('CIMI')) await writeToPortDelayed('AT+CIMI\r', 2000);
if (isNotBlacklistedCommand('CSQ')) await writeToPortDelayed('AT+CSQ\r', 2000);
if (isNotBlacklistedCommand('COPS?')) await writeToPortDelayed('AT+COPS?\r', 2000);
// if (isNotBlacklistedCommand('CMGD')) await writeToPortDelayed('AT+CMGD=0,4\r', 2000);
*/
const commands = [
'AT&F\r',
'ATE0\r',
'AT+CGSN\r',
'AT+CIMI\r',
'AT+COPS?\r',
'AT+CSQ\r',
];
const commandsCount = commands.length;
// eslint-disable-next-line no-plusplus
for (let i = 0; i < commandsCount; i++) {
// eslint-disable-next-line no-await-in-loop
await writeToPortDelayed(commands[i], 2000);
}
if (config && config.modem_tester && config.modem_tester.commands
&& config.modem_tester.commands.length) {
const additionalCommandsLength = config.modem_tester.commands.length;
// eslint-disable-next-line no-plusplus
for (let i = 0; i < additionalCommandsLength; i++) {
// eslint-disable-next-line no-await-in-loop
await writeToPortDelayed(config.modem_tester.commands[i], 2000);
}
}
});
parsers.setPort(port);
if (config && config.modem_tester && config.modem_tester.parser === 'regex') {
logger.info('Using parserWaitForOkOrError');
port.pipe(parserWaitForOkOrError);
} else if (config && config.modem_tester && config.modem_tester.parser === 'interbyte') {
logger.info('Using parserInterByteTimeout');
port.pipe(parserInterByteTimeout);
} else {
logger.info('Using parserReadline');
port.pipe(parsers.parserReadline);
}