我有一个应用程序,它使用我使用的财务打印机公司制作的假脱机程序来打印收据。
此假脱机程序使用命令行打印,我将其发送到文件中。
一旦我发送它并开始打印并且假脱机程序释放线程,同时继续打印。一旦完成,这个假脱机程序会生成一个日志文件,其中包含一些我需要保存在数据库中的数据。
我的问题是当我发送事件进行打印并开始时,但从未在生产上阅读文件日志,而未编译它完美无缺。
const exec = require('child_process').exec;
ipcMain.on("syncPrinter", function (event, content) {
var responsePrinter = {
status: null,
file_response: null
};
/* Send file with commands to print*/
exec('wspooler.exe -p4 -l -f FILE.200', function (err, stdout, stderr) {
/* if fails on send file to print*/
if (err !== null) {
responsePrinter = {
status: false,
file_response: null
};
event.returnValue = responsePrinter;
} else {
var cont = 0;
/* starts a interval to wait file log of printer*/
var interval = setInterval(function () {
/* Open general log of the spooler */
var spoolerLog = path.join(__dirname, 'spooler.log');
fs.readFile(spoolerLog, {encoding: 'utf-8'}, function (err, data) {
if (!err) {
var splited = data.split("\n");
/* Control if the last message on the genera log end the printing properly*/
if ((
splited[splited.length - 1].substring(16, 33) === 'Fin de la operaci'
&& splited[splited.length - 3].substring(16, 43) === 'Archivo FILE.200 finalizado'
) || (
splited[splited.length - 2].substring(16, 33) === 'Fin de la operaci'
&& splited[splited.length - 4].substring(16, 43) === 'Archivo FILE.200 finalizado'
)) {
/* IF THE PRINT SUCCESS THIS FILE MUST BE READ */
var filePath = path.join(__dirname, 'FILE.ans');
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, response) {
if (!err) {
responsePrinter.status = true;
responsePrinter.file_response = response;
/* send it to the frontend*/
event.returnValue = responsePrinter;
clearInterval(interval);
} else {
cont++;
}
});
} else {
cont++;
}
} else {
cont++;
}
});
/* Repeat 10 times to wait the file to save*/
if (cont > 10) {
responsePrinter = {
status: false,
file_response: null,
count: cont
};
clearInterval(interval);
event.returnValue = responsePrinter;
}
}, 4000);
}
});
});