Source: plugins/outbound/divera247/base.js

const EventEmitter = require('events');
const Divera247Object = require('./divera24');
const { toNamespacedPath } = require('path');

/**
 * Class for communication with DIVERA247 Cloud Service
 * @class
 * @namespace plugins/inbound/divera247
 * @memberof plugins/inbound/divera247
 */
class divera247 extends EventEmitter {
    /**
     * @constructor
     */
    constructor() {
        super();
        // set plugin as not configured
        this.isConfigured = false;
        this.config = null;
        global.plgManager.logger.info('-------- PLUGIN DIVERA247 -----------');
        this.plgManager = global.plgManager;
        this.logger = global.plgManager.logger;
        this.registerForEvents();
        this.divera247Object = new Divera247Object(global.plgManager.logger);
        this.loadConfig((success) => {
            if (success) {
                // set plugin as configured
                this.initializePlugin((initsuccess) => {
                    if (initsuccess) {
                        this.isConfigured = true;
                    }
                })
            }
        })
    }


    /**
     * Registration of Events
     * @memberof plugins/inbound/divera247
     */
    registerForEvents() {
        this.logger.info('PLUGIN | DIVERA247 | Registering events')

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

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

        this.plgManager.on('event_new_state', function (stateInfo) {
            if (this.getConfigValue('sendState') === 'true') {
                this.newState(stateInfo);
            }
        }.bind(this));

        this.plgManager.on('event_new_admin_notification', function (notification) {
            if (this.getConfigValue('sendAdminNotifications') === 'true') {
                this.newAdminMessage(notification);
            }
        }.bind(this));
    }


    /**
     * send new alarm to divera 247 cloud
     * @param {object} alarmInfo alarmInfo Object containing all alarm data (from core)
     * @memberof plugins/inbound/divera247
     */
    newalarm(alarmInfo) {
        this.logger.info('PLUGIN | DIVERA247 | Newalarm event fired by main');
        this.divera247Object.type = alarmInfo.subject.value;
        this.divera247Object.title = alarmInfo.subject.value;
        this.divera247Object.priority = 1;
        this.divera247Object.text = alarmInfo.message.value;
        this.divera247Object.address = alarmInfo.location.value;
        if (this.getConfigValue('sendZveis') === 'true') {
            this.divera247Object.zveis = alarmInfo.zveis.value;
            this.divera247Object.ric = alarmInfo.zveis.value;
        } else {
           this.divera247Object.ric = this.getConfigValue('ric');
        }

        this.divera247Object.vehicle = alarmInfo.gear.value;
        this.divera247Object.number = alarmInfo.operationnumber.value;
        this.divera247Object.sendAlarm((success) => {
            if (success) {
                global.plgManager.logger.info('PLUGIN | DIVERA247 | Successfully sent alarm to Divera');
            } else {
                global.plgManager.logger.error('PLUGIN | DIVERA247 | Alarm NOT SENT to Divera');
            }
        });
    }

    /**
     * send new state to divera cloud
     * @param {object} stateInfo Stateinfo object
     * @memberof plugins/inbound/divera247
     */
    newState(stateInfo) {
       
        this.logger.info('PLUGIN | DIVERA247 | NewState event fired by main');
        this.divera247Object.sendState(stateInfo.radioStatusShort, stateInfo.issi, stateInfo.radioStatusHumanReadable, (success) => {
            if (success) {
                global.plgManager.logger.info('PLUGIN | DIVERA247 | Successfully sent state to Divera');
            } else {
                global.plgManager.logger.error('PLUGIN | DIVERA247 | State NOT SENT to Divera');
            }
        })
    }

    /**
     * Sends a message to configured user/group
     * @param {string} notification Content of message
     * @memberof plugins/inbound/divera247
     */
    newAdminMessage(notification) {
        global.plgManager.logger.info('PLUGIN | DIVERA247 | New Admin Notification event catched');
        if (this.getConfigValue('sendAdminNotifications') === 'true') {
            this.divera247Object.title = "ALARMiator Systemnachricht";
            this.divera247Object.text = notification,
            this.divera247Object.person = this.getConfigValue('adminName');
            this.divera247Object.sendMessage((success) => {
                if (success) {
                    global.plgManager.logger.info('PLUGIN | DIVERA247 | Successfully sent System Notification to Divera');
                } else {
                    global.plgManager.logger.error('PLUGIN | DIVERA247 | System Notification NOT SENT to Divera');
                }
            })
        } else {
            global.plgManager.logger.info('PLUGIN | DIVERA247 | System Notifications not configured to be sent to DIVERA247');

        }
    }

    /**
     * Loads configuration for KatSys Service
     * @memberof plugins/inbound/divera247
     */
    loadConfig(callback) {
        global.plgManager.loadConfigFromStore('divera247', (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 | DIVERA247 | Configuration reloaded from core database');
        })
    }

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

    /**
     * reconfigures divera plugin with new config settings
     * @param {function} callback success, true or false
     */
    initializePlugin(callback) {
        this.divera247Object.alarmAccessKey = this.getConfigValue('httpApiKey');
        if (this.divera247Object.alarmAccessKey !== null) {
            callback(true);
        } else {
            callback(false);
        }
    }

}

module.exports = divera247;