modems2.js 3.46 KB
'use strict';

const logger = global.KOMODO_LOGGER;

if (logger) {
    logger.verbose('MODEMS: logger set using global logger');
}

const modemList = {
    by_name: {},
    by_imsi: {},
    by_msisdn: {},
};

/**
 * Objek data sebuah modem.
 * 
 * @typedef   {Object} ModemData
 * @property  {string} name - nama modem
 * @property  {string} device - modem device path
 * @property  {string} imsi - IMSI modem
 * @property  {string} msisdn - MSISDN modem
 * @property  {string} networkId - operator network id
 * @property  {string} networkName - operator network name
 * @property  {string} signalStrength - modem signal strength
 * @property  {number} uptime - modem uptime in seconds
 * @property  {string} reportIp - IP modem
 * @property  {number} reportPort - TCP port modem
 * @property  {string} reportApikey - APIKEY modem
 * @property  {string} reportPathSms - url path untuk pengiriman SMS
 */

/**
 * Update data sebuah modem berdasarkan nama modem.
 * 
 * @param  {ModemData} val - objek data modem
 */
function touchByName(val) {
    if (!val || !val.name) return;
    if (typeof val.name !== 'string') return;
    if (!val.name.trim()) return;

    modemList.by_name[val.name] = val;
}
/**
 * Update data sebuah modem berdasarkan IMSI.
 * 
 * @param  {ModemData} val - objek data modem
 */
function touchByIMSI(val) {
    if (!val || !val.imsi) return;
    if (typeof val.imsi !== 'string') return;
    if (!val.imsi.trim()) return;

    if (logger && !modemList.by_imsi[val.imsi]) {
        logger.verbose(`New IMSI registration`, { 
            imsi: val.imsi, 
            name: val.name, 
            ip: val.reportIp, 
            device: val.device, 
            imsi_registered: Object.keys(modemList.by_imsi).length + 1, 
        });
    }

    const oldName = modemList.by_imsi[val.imsi] ? modemList.by_imsi[val.imsi].name : null;
    if (oldName && oldName !== val.name && modemList.by_name[oldName]) {
        logger.info(`Modem with IMSI ${val.imsi} move detected`, { 
            old_name: oldName,
            old_ip: modemList.by_name[oldName].reportIp,
            old_device: modemList.by_name[oldName].device,
            new_name: val.name,
            new_ip: val.reportIp,
            new_device: val.device, 
        });
        delete modemList.by_name[oldName];
    }

    modemList.by_imsi[val.imsi] = val;
}

/**
 * Update data sebuah modem berdasarkan MSISDN.
 * 
 * @param  {ModemData} val - objek data modem
 */
function touchByMSISDN(val) {
    if (!val || !val.msisdn) return;
    if (typeof val.msisdn !== 'string') return;
    if (!val.msisdn.trim()) return;

    modemList.by_msisdn[val.msisdn] = val;
}

/**
 * Update data sebuah modem.
 * 
 * @param  {ModemData} val - objek data modem
 * @see ModemData
 */
function touch(val) {
    if (!val || typeof val !== 'object') return;

    val._ts = new Date();

    if (!val.reportIp) {
        val.reportIp = '127.0.0.1';
    }

    touchByName(val);
    touchByIMSI(val);
    touchByMSISDN(val);
}
/**
 * Ambil data sebuah modem.
 * 
 * @param  {string} selector - selector pencarian, valid jika bernilai salah satu dari: name, imsi, msisdn
 * @param  {string} keyword - kata kunci modem yang ingin diambil
 * @returns {ModemData} data modem terkait
 */
function get(selector, keyword) {
    if (!selector || !keyword) return null;

    return modemList[`by_${selector}`] ? modemList[`by_${selector}`][keyword] : null;
}

function dump() {
    return modemList;
}

exports.touch = touch;
exports.set = touch;
exports.get = get;
exports.dump = dump;