bootstrap.js
2.7 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
/**
* Modul modem bootstrap
* @module
* @since 2019-09-25
*/
const DEFAULT_BAUDRATE = 115200;
const fs = require('fs');
const SerialPort = require('serialport');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const parsers = require('./serialport-parsers');
const modemCommands = require('./modem-commands');
const ussdCodes = require('./modem-commands/ussd-codes');
const modemInfo = require('./modem-info');
const registerModem = require('./register-modem');
logger.info('Bootstraping modem');
if (!fs.existsSync(config.modem.device)) {
logger.error(`Modem not found on ${config.modem.device}. Terminating.`);
process.exit(1);
}
const port = new SerialPort(config.modem.device, {
baudRate: (config.modem && config.modem.options && config.modem.options.baudRate)
|| DEFAULT_BAUDRATE,
}, async (err) => {
if (err) {
logger.warn(`Error opening modem. ${err}. Terminating modem ${config.modem.device}.`);
process.exit(1);
}
global.MODEM_PORT = port;
parsers.setPort(port);
// modemCommands.setPort(port);
port.pipe(parsers.parserReadline);
await modemCommands.writeToPortAndWaitForOkOrError(`${modemCommands.CTRLZ}AT&FE0\r`);
await modemCommands.initATCommands();
await modemCommands.queryManufacturer();
await modemCommands.queryModel();
await modemCommands.queryIMEIAndIMSI();
await modemCommands.queryCOPSAndSignalQuality();
logger.verbose('Modem state', modemInfo);
registerModem();
if (config.debug_sms_destination_on_start) {
await modemCommands.sendSMS(config.debug_sms_destination_on_start,
`${config.name} (${modemInfo.imsi || 'UNKNOWN'}) started on ${new Date()}`);
}
if (config.debug_ussd_code_on_start) {
const ussdResponse = await modemCommands.executeUSSD(config.debug_ussd_code_on_start, 2);
logger.info('USSD RESPONSE', { command: config.debug_ussd_code_on_start, ussdResponse });
}
if ((config.bootstrap_sms_quota_check && modemInfo.networkName && modemInfo.networkName === 'TELKOMSEL') || config.bootstrap_tsel_sms_quota_check) {
const ussdResponse = await modemCommands.executeUSSD(ussdCodes.TSEL_SMS_QUOTA_CHECK, 1);
logger.info('USSD RESPONSE', { command: config.debug_ussd_code_on_start, ussdResponse });
}
setInterval(async () => {
await modemCommands.initATCommands();
await modemCommands.queryManufacturer();
await modemCommands.queryModel();
await modemCommands.queryIMEIAndIMSI();
await modemCommands.queryCOPSAndSignalQuality();
logger.info('Modem state', modemInfo);
registerModem();
}, config.interval_beetwen_signal_strength_ms || 30000);
});