modem-info.js 1.42 KB
const DEFAULT_MAX_LAST_DATA_AGE_MS = 2 * 60 * 1000;
const DEFAULT_INTERVAL_BETWEEN_IDLE_CHECK_MS = 30 * 1000;

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

const modemInfo = {
    device: config.modem.device,
    manufacturer: null,
    model: null,
    imei: null,
    imsi: null,
    cops: null,
    networkId: null,
    networkName: null,
    signalStrength: null,
    signalStrengthTs: null,
    signalStrengthTsReadable: null,
    startTime: new Date(),
    lastWriteTs: null,
    lastReadTs: null,
};

if (!config.disable_idle_check) {
    const intervalBeetwenCheckMs = config.interval_beetwen_signal_strength_ms
        || DEFAULT_INTERVAL_BETWEEN_IDLE_CHECK_MS;

    logger.info('Registering idle / stuck modem background check', { intervalBeetwenCheckMs });

    setInterval(() => {
        const deltaMs = new Date() - Math.max(
            modemInfo.startTime,
            Math.min(modemInfo.lastWriteTs, modemInfo.lastReadTs),
        );

        const maxLastDataAgeMs = config.max_last_data_age_ms || DEFAULT_MAX_LAST_DATA_AGE_MS;
        if (deltaMs >= maxLastDataAgeMs) {
            logger.warn(`Modem idle for more than ${maxLastDataAgeMs} ms. Stucked modem? Terminating!`, { deltaMs });
            process.exit(1);
        }
    }, intervalBeetwenCheckMs);
} else {
    logger.info('Skip idle / stuck modem background check by config.disable_idle_check');
}

module.exports = modemInfo;