const EventEmitter = require('events');
const PrinterObject = require('printer');
/**
* Class offers printing functionality for plugin Printer
* @class
* @module plugins/outbound/printer
*/
class printer extends EventEmitter {
constructor() {
super();
global.plgManager.logger.info('-------- PLUGIN PRINTER -----------');
this.registerForEvents();
this.config = null;
this.loadConfig((success) => {
});
}
/**
* Registration of Events at the plugin manager (hook in to events provided by the plugin manager)
*/
registerForEvents() {
global.plgManager.logger.info('PLUGIN | PRINTER | Registering events')
global.plgManager.on('event_print_pdf', (pathToPDFFile) => {
// TBD actually print the pdf document
});
global.plgManager.on('event_plugin_notification', (namespace, message) => {
if (namespace === 'printpdf') {
global.plgManager.logger.info('PLUGIN | PRINTER | Recieved Notification from PluginManager: ' + message);
}
});
global.plgManager.on('event_pluginConfig_refreshed', (namespace) => {
if (namespace === 'printpdf') {
global.plgManager.logger.info('PLUGIN | PRINTER | Recieved Info about changed Configuration');
this.loadConfig((success) => {
});
}
});
}
/**
* Plugin-specific Getters and Setters
*
*/
// --- NONE ---
/**
* Plugin-specific implementation
*/
/**
* refreshes default printer
*/
getDefaultPrinter() {
global.plgManager.logger.info('PLUGIN | PRINTER | default printer name: ' + (PrinterObject.getDefaultPrinterName() || 'not defined on your computer'));
try {
this.defaultPrinter = PrinterObject.getDefaultPrinterName();
} catch (err) {
this.defaultPrinter = null;
global.plgManager.logger.error('PLUGIN | PRINTER | Could not read default printer: ' + err.message);
}
}
/**
* refreshes list of available Printers
*/
getAllPrinters() {
try {
this.availablePrinters = PrinterObject.getPrinters();
} catch (err) {
this.availablePrinters = null;
global.plgManager.logger.error('PLUGIN | PRINTER | Could not read available printers: ' + err.message);
}
}
/**
* Loads configuration for KatSys Service
* TBD: Needs to be pulled from plugin config store
*/
loadConfig(callback) {
global.plgManager.loadConfigFromStore('printpdf', (configRows) => {
if (configRows !== null) {
this.config = configRows[0];
this.config = JSON.parse(this.config.configstore);
callback(true);
}
global.plgManager.logger.info('PLUGIN | PRINTER | 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 = printer;