const EventEmitter = require('events');
const SMS77io = require('./sms77io');
/**
* Class offers printing functionality for plugin Printer
* @class
* @module plugins/outbound/printer
*/
class sms77io extends EventEmitter {
constructor() {
super();
global.plgManager.logger.info('-------- PLUGIN SMS77IO -----------');
this.registerForEvents();
this.config = null;
this.credits = 0;
this.loadConfig((success) => {
if (success) {
var httpApiKey = this.getConfigValue('httpApiKey');
const sms77ioController = new SMS77io(global.plgManager.logger);
sms77ioController.apikey = httpApiKey;
sms77ioController.checkSMS77credits((credits) => {
this.credits = credits;
})
}
});
}
/**
* Registration of Events at the plugin manager (hook in to events provided by the plugin manager)
*/
registerForEvents() {
global.plgManager.logger.info('PLUGIN | SMS77IO | Registering events')
global.plgManager.on('event_new_alarm', (alarmInfo) => {
// TBD actually print the pdf document
global.plgManager.logger.info('PLUGIN | SMS77IO | Recieved new ALARM, sending SMS now');
this.sendAlarmSMS(alarmInfo);
});
global.plgManager.on('event_plugin_notification', (namespace, message) => {
if (namespace === 'sms77io') {
global.plgManager.logger.info('PLUGIN | SMS77IO | Recieved Notification from PluginManager: ' + message);
}
});
global.plgManager.on('event_pluginConfig_refreshed', (namespace) => {
if (namespace === 'sms77io') {
global.plgManager.logger.info('PLUGIN | SMS77IO | Recieved Info about changed Configuration');
this.loadConfig((success) => {
global.plgManager.logger.info('PLUGIN | SMS77IO | Successfully reloaded plugin configuration');
});
}
});
}
/**
* Plugin-specific Getters and Setters
*
*/
// --- NONE ---
/**
* Plugin-specific implementation
*/
sendAlarmSMS(alarmInfo) {
var recievers = this.getConfigValue('smsRecievers');
var senderNumberOrName = this.getConfigValue('senderNumberOrName');
if(senderNumberOrName === null) {
senderNumberOrName = 'ALARMiator';
}
var text = alarmInfo.subject.value + ' - ' + alarmInfo.location.value + ' ' + alarmInfo.message.value;
text = text.substr(0, 160);
// var text = 'THL 1 - Kühlschrank leer - Adresse: Am Saueracker 36, 91244 Reichenschwand';
const sms77ioController = new SMS77io(global.plgManager.logger);
sms77ioController.apikey = this.getConfigValue('httpApiKey');
sms77ioController.sendSMS(text, senderNumberOrName, recievers, (success) => {
});
}
/**
* Loads configuration for KatSys Service
* TBD: Needs to be pulled from plugin config store
*/
loadConfig(callback) {
global.plgManager.loadConfigFromStore('sms77io', (configRows) => {
if (configRows !== null) {
this.config = configRows[0];
this.config = JSON.parse(this.config.configstore);
callback(true);
}
global.plgManager.logger.info('PLUGIN | SMS77IO | Configuration reloaded from core database');
})
}
/**
* returns config parameter for given fieldname
* @param {string} fieldname
*/
getConfigValue(fieldname) {
var retVal = null;
this.config.fields.forEach((field) => {
if (field.fieldname === fieldname) {
retVal = field.value;
}
});
return retVal;
}
}
module.exports = sms77io;