report-sender.js 2.26 KB
/**
 * Report sender
 * @module report-sender
 * @deprecated
 */

const request = require('request');

const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');

logger.warn(`'${path.basename(__filename, '.js')}' is DEPRECATED, please use 'report-sender2'!`);
// eslint-disable-next-line no-console
console.trace(`'${path.basename(__filename, '.js')}' is DEPRECATED, please use 'report-sender2'!`);


function incomingSMS(message, modemInfo) {
    if (!message || !config || !config.report_url || !config.report_url.incoming_sms) return;

    if (!message.metadata) {
        const e = new Error('Missing metadata on incoming sms');
        e.smsObj = message;
        throw e;
    }

    if (!message.metadata.from) {
        const e = new Error('Missing metadata.from on incoming sms');
        e.smsObj = message;
        throw e;
    }

    if (!message.message) {
        const e = new Error('Missing message on incoming sms');
        e.smsObj = message;
        throw e;
    }

    const requestOptions = {
        url: config.report_url.incoming_sms,
        qs: {
            ts: message.metadata.ts,
            status: message.metadata.status,
            number: message.metadata.from,
            msg: message.message,
            modem: config.name,
            modem_imsi: modemInfo.imsi,
            modem_msisdn: modemInfo.msisdn,
            modem_device: config.modem.device,
            modem_network_id: modemInfo.networkId,
            modem_network_name: modemInfo.networkName,
            modem_signal_strength: modemInfo.signalStrength,
            uptime: Math.floor(process.uptime()),
            report_port: config.http_command_server.listen_port,
            report_apikey: config.http_command_server.apikey,
            report_path_sms: '/sms',
        },
    };

    logger.info('Sending report via HTTP', requestOptions);
    request(requestOptions, (err, res, body) => {
        if (err) {
            logger.warn(`Error sending report via HTTP. ${err.toString()}`);
            return;
        }

        if (res.statusCode !== 200) {
            logger.warn(`Error sending report via HTTP. Server respond with HTTP status code ${res.statusCode}`, { http_status_code: res.statusCode, body });
        }
    });
}

exports.incomingSMS = incomingSMS;