Source: plugins/inbound/ilsfax/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

/**
 * 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 ILSFAX service');
        process.quit();
    }
    if (typeof msg.start != 'undefined') {
        if(msg.start === 1) {
            if (serviceState === 1) {
                // KatSys Service is configured, so start it
                LogAtMain('Starting ILSFAX Service');
                // connectToKatsys();
                serviceState = 2;
            } else {
                LogAtMain('Serice not in configured and stopped state. Not starting service');
            }
        }
    }
    
    // process.send('Message from plugin manager:', msg);
});


const IlsFax = require(__dirname + '/ilsfax.js');
var ilsFax = new IlsFax();


/**
 * 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() {
    
    ilsFax.on('watcher-initialized', () => {
        // Watcher for inbound faximile path has been initialized
        process.send('{ "event" : { "event" : "watcher-initialized"} }');
    })
    /*
    katsys.on('statusParsed', (statusInfo) => {
        // Connection State of Katsys Connection has changed
        process.send('{ "event" : { "event" : "statusParsed", "statusInfo" : ' + JSON.stringify(statusInfo) + ' } }');
    })
    katsys.on('zveiParsed', (zveiInfo) => {
        process.send('{ "event" : { "event" : "zveiParsed", "zveiInfo" : ' + JSON.stringify(zveiInfo) + ' } }');
    })
    katsys.on('alarmParsed', (zveiInfo) => {
        process.send('{ "event" : { "event" : "alarmParsed", "alarmInfo" : ' + JSON.stringify(alarmInfo) + ' } }');
    })
    */
}

function startService() {
    ilsFax.checkWorkingDirectories((result) => {
        LogAtMain('Checked Working Directories: ' + result);
        if (result === true) {
            ilsFax.initializeWatcher();
        } else {
            LogAtMain('Could not initialize Watcher');
        }
    });
}




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