Source: plugins/outbound/alarmdepesche/base.js

const Operation = require('../../../models/core/core_operation');
const Alarmfax = require('./modules/alarmfax');
const EventEmitter = require('events');

class alarmdepesche extends EventEmitter {

    constructor() {
        super();

        this.isConfigured = false;
        this.config = null;
        global.plgManager.logger.info('-------- PLUGIN ALARMDEPESCHE -----------');
        this.plgManager = global.plgManager;
        this.logger = global.plgManager.logger;
        this.registerForEvents();
        this.loadConfig((success) => {
            if (success) {
                // set plugin as configured
                this.initializePlugin((initsuccess) => {
                    if (initsuccess) {
                        this.isConfigured = true;
                    }
                })
            }
        })
    }


    /**
     * Registration of Events
     * @memberof plugins/outbound/alarmdepesche
     */
    registerForEvents() {
        this.logger.info('PLUGIN | ALARMDEPESCHE | Registering events')

        this.plgManager.on('event_pluginConfig_refreshed', (namespace) => {
            if (namespace === 'alarmdepesche') {
                // plugins config has changed --> refresh
                this.loadConfig((success) => {
                    if (success) {
                        this.logger.info('PLUGIN | ALARMDEPESCHE | successfully reloaded plugin configuration');
                    } else {
                        this.logger.error('PLUGIN | ALARMDEPESCHE | could not reload plugin config!');
                    }
                });
            }
        })

        this.plgManager.on('event_new_alarm', function (alarmInfo) {
            if (this.getConfigValue('printDepesche') === 'true') {
                this.newalarm(alarmInfo);
            } else {
                this.logger.info('PLUGIN | ALARMDEPESCHE | ignoring ALARM as alarms for ALARMDEPESCHE have been disabled in Plugin settings');
            }
        }.bind(this));
    }

    /**
     * generates the pdf and informs plugin manager to send pdf to printer
     * @param {object} alarmInfo alarmInfo Object containing all alarm data (from core)
     * @memberof plugins/outbound/alarmdepesche
     */
    newalarm(alarmInfo) {
        this.logger.info('PLUGIN | ALARMDEPESCHE | Newalarm event fired by main');
        let alarmfax = new Alarmfax();
        alarmfax.operation = alarmInfo;
        if ((this.getConfigValue('withMap') === true) || (this.getConfigValue('withMap') === 'true')) {
            alarmfax.renderWithMap = true;
        } else {
            alarmfax.renderWithMap = false;
        }

        if (alarmfax.renderWithMap) {
            alarmfax.getMap((success, pathToPng) => {
                if (success) {
                    // successfully generated map file
                    console.log('PLUGIN | ALARMFAX | Generated map at: ' + pathToPng);
                    alarmfax.mapPngToJpg(pathToPng, (successJPG, pathToJpeg) => {
                        if (successJPG) {
                            console.log('PLUGIN | ALARMFAX | Converted map to jpeg at: ' + pathToJpeg);
                            alarmfax.generatePDF(true, pathToJpeg, (successPDF, pathToPDF) => {
                                if (successPDF) {
                                    console.log('PLUGIN | ALARMFAX | Successfully generated pdf -> informing plugin manager now to print pdf document');
                                    global.plgManager.event_print_pdf(pathToPDF);
                                } else {
                                    console.log('PLUGIN | ALARMFAX | Error generating PDF document');
                                    global.plgManager.event_new_admin_notification('Fehler bei der Generierung der ALARMDEPESCHE');
                                }
                            });
                        } else {
                            console.log('PLUGIN | ALARMFAX | ERROR generting jpeg');
                        }
                    });
                }
            })
        } else {
            console.log('PLUGIN | ALARMFAX | Map generation is disabled - skipping');
            alarmfax.generatePDF(false, null, (successPDF, pathToPDF) => {
                if (successPDF) {
                    console.log('PLUGIN | ALARMFAX | Successfully generated pdf -> informing plugin manager now to print pdf document');
                    global.plgManager.event_print_pdf(pathToPDF);
                } else {
                    console.log('PLUGIN | ALARMFAX | Error generating PDF document');
                    global.plgManager.event_new_admin_notification('Fehler bei der Generierung der ALARMDEPESCHE');
                }
            });
        }
    }

    /**
     * Loads configuration for KatSys Service
     * @memberof plugins/outbound/alarmdepesche
     */
    loadConfig(callback) {
        global.plgManager.loadConfigFromStore('alarmdepesche', (configRows) => {
            if (configRows !== null) {
                this.config = configRows[0];
                this.config = JSON.parse(this.config.configstore);
                callback(true);
            } else {
                callback(false);
            }
            global.plgManager.logger.info('PLUGIN | ALARMDEPESCHE | Configuration reloaded from core database');
        })
    }

    /**
     * returns config parameter for given fieldname
     * @param {string} fieldname 
     * @memberof plugins/outbound/alartmdepesche
     */
    getConfigValue(fieldname) {
        var retVal = null;
        this.config.fields.forEach((field) => {
            if (field.fieldname === fieldname) {
                retVal = field.value;
            }
        });
        return retVal;
    }

    /**
     * reconfigures plugin with new config settings
     * @param {function} callback success, true or false
     */
    initializePlugin(callback) {
        callback(true);
    }
}

module.exports = alarmdepesche;