modem-tester.js 3.06 KB
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 ParserReadline = require('@serialport/parser-readline');

const parserReadline = new ParserReadline({ delimiter: '\r\n' });
parserReadline.on('data', (data) => {
    logger.verbose('INCOMING', { parser: 'parserReadLine', data: data.toString() });
});

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 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+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);
});

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(parserReadline);
}