report-sender2.js 1.53 KB
const request = require('request');

const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const modemInfo = require('./modem-info');

function incomingSMS(ts, from, msg) {
    if (!ts || !from || !msg
        || !config || !config.report_url || !config.report_url.incoming_sms) return;

    const requestOptions = {
        url: config.report_url.incoming_sms,
        qs: {
            ts,
            // status: message.metadata.status,
            number: from,
            msg,
            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;