const EventEmitter = require('events');
class extip extends EventEmitter {
constructor() {
super();
global.plgManager.logger.info('-------- PLUGIN EXTIP -----------');
this.subProcess = null;
//this.plgManager = global.plgManager;
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 | EXTEIP | Registering events')
}
/**
* Plugin-specific implementation
*/
refreshexternalip(ipInfo) {
global.plgManager.logger.info('PLUGIN | EXTEIP | refreshing external IP at Plugin Manager for dispatching ' + ipInfo);
global.plgManager.event_refresh_extip(ipInfo);
}
/**
* Loads configuration for KatSys Service
* TBD: Needs to be pulled from plugin config store
*/
loadConfig() {
this.config = {
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.subProcess.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 | EXTEIP | SERVICE | ' + decodeURIComponent(payload.logmessage));
} else if (typeof payload.event != 'undefined') {
// service sends structured data
if (payload.event.event === 'ipUpdated') {
var ipInfo = payload.event.ip;
global.plgManager.logger.info('PLUGIN | EXTEIP | SERVICE | ipUpdated: ' + ipInfo);
this.refreshexternalip(ipInfo);
}
} else {
global.plgManager.logger.info('PLUGIN | EXTEIP | 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 | EXTEIP | Starting Service');
const { fork } = require('child_process');
this.subProcess = fork(__dirname + '/service.js');
this.serviceProcId = this.subProcess.pid;
this.subProcess.title = 'ALARMiator Plugin - External IP Updater';
global.plgManager.logger.info('PLUGIN | EXTEIP | Service started with pid ' + this.serviceProcId);
}
/**
* Sends the service configuration to the katsys process
*/
configService() {
this.subProcess.send({ config: this.config });
}
/**
* tells the service to connect to the katsys cloud
*/
startService() {
this.subProcess.send({ start: 1});
}
}
module.exports = extip;