const EventEmitter = require('events');
class ilsfax extends EventEmitter {
constructor() {
super();
global.plgManager.logger.info('-------- PLUGIN ILSFAX -----------');
this.ilsfaxProcess = null;
this.registerForEvents();
this.serviceProcId = null;
this.loadConfig();
this.prepareService();
this.wireServiceCommunication();
this.configService();
this.startService();
}
/**
* Registration of Events at the plugin manager (hook in to events provided by the plugin manager)
*/
registerForEvents() {
global.plgManager.logger.info('PLUGIN | ILSFAX | Registering events')
}
/**
* Plugin-specific implementation
*/
newalarm(alarmInfo) {
global.plgManager.logger.info('PLUGIN | ILSFAX | Sending new Alarm to Plugin Manager for dispatching');
global.plgManager.event_new_alarm(alarmInfo);
}
/**
* Loads configuration for KatSys Service
* TBD: Needs to be pulled from plugin config store
*/
loadConfig() {
this.katSysConfig = {
masterToken : 'h3hpkN4bwo8HG6PeGn9Mzqoerxw39kg5MzxNN5iNNpbyIpB6',
subToken : 'Uvx584wWcL0qOJnovd5NJprS',
pemFile : __dirname + '/pem/katsys-cloud.pem',
loglevel : 'debug' // debug = show everything, prod = show only errors
}
}
/**
* Wires messages from service to event model of plugin
* All Messages in service need to be wired here to be recognised in plugin
*/
wireServiceCommunication() {
this.ilsfaxProcess.on('message', function(msg) {
var payload = null;
try {
payload = JSON.parse(msg);
} catch (err) {
payload = err.message;
}
if (typeof payload.logmessage != 'undefined') {
global.plgManager.logger.info('PLUGIN | ILSFAX | SERVICE | ' + decodeURIComponent(payload.logmessage));
} else if (typeof payload.event != 'undefined') {
// service send structured data
if (payload.event.event === 'alarmParsed') {
var alarmInfo = payload.event.alarmInfo;
global.plgManager.logger.info('PLUGIN | ILSFAX | SERVICE | alarmParsed: ' + alarmInfo);
this.newalarm(alarmInfo);
}
} else {
global.plgManager.logger.info('PLUGIN | ILSFAX | SERVICE | UNSOLICITED MESSAGE: ' + msg);
}
}.bind(this));
}
/**
* Creates the service as a seperate node-process (fork)
* sets the serviceProcId of the forked process
*/
prepareService() {
global.plgManager.logger.info('PLUGIN | ILSFAX | Starting Service');
const { fork } = require('child_process');
this.ilsfaxProcess = fork(__dirname + '/service.js');
this.serviceProcId = this.ilsfaxProcess.pid;
this.ilsfaxProcess.title = 'ALARMiator Plugin - ILSFAX Connector';
global.plgManager.logger.info('PLUGIN | ILSFAX | Service started with pid ' + this.serviceProcId);
}
/**
* Sends the service configuration to the katsys process
*/
configService() {
this.ilsfaxProcess.send({ config: this.katSysConfig });
}
/**
* tells the service to connect to the katsys cloud
*/
startService() {
this.ilsfaxProcess.send({ start: 1});
}
}
module.exports = ilsfax;