Source: plugins/inbound/externalip/service.js

/* ------------------------

/**
 *  ILSFAX Konnector Service
 * 
 *  description:    This class is called in a separate node process 
 *                  in Plugin "ILSFAX Konnektor". This service checks
 *                  a defined inbound directory for new faximilies from ils
 *                  and parses them if a new faximile has been found.
 * 
 *  author:         Jens Dinstühler 2020
 *  version:        1.0.0 
 * 
 */


var serviceState = 0;   // 0 == not configured, 1 == configured but not connected, 2 == configured and connected
var config = null;     // holds configuration from plugin config store
// required for external IP check
const publicIp = require('public-ip');





/**
 * Log Function for inter process communication
 */
function LogAtMain(msg) {
    process.send('{"logmessage" : "' + msg + '"}');
}

/**
 * 
 * Wire messages coming from Plugin base.js
 * 
 */
process.on('message', (msg) => {
    if (typeof msg.config != 'undefined') {
        config = msg.config;
        serviceState = 1;
        LogAtMain('Loaded service configuration');
    }
    if (typeof msg.quit != 'undefined') {
        LogAtMain('Shut down EXTEIP service');
        process.quit();
    }
    if (typeof msg.start != 'undefined') {
        if(msg.start === 1) {
            if (serviceState === 1) {
                LogAtMain('Starting EXTEIP Service');
                // connectToKatsys();
                serviceState = 2;
            } else {
                LogAtMain('Serice not in configured and stopped state. Not starting service');
            }
        }
    }
});


/**
 * Wires events fired by ilsfax object to plugin-process
 * This is communication between two seperate node processes as the
 * plugin itself lives in main thread, the service lives in its own thread.
 */
function registerEvents() {
    // Nothing
}

function startService() {
    // Start check every 2 Minutes
    var minutes = 1, the_interval = minutes * 60 * 1000;
    setInterval(function() {
        LogAtMain(`Starting External IP (WAN) Check`);
        updateExternalP();
    }, the_interval);
}

function updateExternalP() {
    (async () => {
        var ipv4 = await publicIp.v4()
        LogAtMain(`external IP v4 (WAN): ` + ipv4);
        process.send('{ "event" : { "event" : "ipUpdated", "ip" : "' + ipv4 + '"} }');
    })();
}


/**
 * call register events
 */
registerEvents();
startService();