Skip to content

Commit

Permalink
Add: PDF auswertung
Browse files Browse the repository at this point in the history
Fix: Neue OSM Hydranteneintragsformat berücksichtigt
Breaking: .env Einträge hinzugefügt
  • Loading branch information
Retschga committed Feb 1, 2021
1 parent 72f261b commit b1004fb
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 336 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
https://www.ghostscript.com/download/gsdnld.html
sudo apt-get install ghostscript


# FWMonitor
Expand Down
242 changes: 62 additions & 180 deletions alarmWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ module.exports = function (_alarmManager) {
// ---------------- LIBRARIES ----------------
const chokidar = require('chokidar');
const moveFile = require('move-file');
var print = require('./printer')();

const EventEmitter = require('events');
var eventEmitter = new EventEmitter();

// ---------------- Timeout Funktion ----------------
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}



/**
* https://ali-dev.medium.com/how-to-use-promise-with-exec-in-node-js-a39c4d7bbf77
Expand Down Expand Up @@ -46,10 +53,27 @@ module.exports = function (_alarmManager) {
console.log("TIFF -> TXT FERTIG\n")
}

async function tiffToPdf(file, targetPath) {
await execShellCommand(`"${process.env.TESSERACT_PATH}" "${file}" "${targetPath}" -l deu PDF`);
console.log("TIFF -> PDF FERTIG\n")
}

function printPdf(path) {
for(let i = 0; i < parseInt(process.env.FAX_DRUCK_SEITENZAHL); i++) {
try {
debug('Seite ' + (i+1) + ' von ' + process.env.FAX_DRUCK_SEITENZAHL);
print.print(path);
} catch (error) {
console.error("DRUCKEN FEHLER", error);
}
}
}


async function startWin() {
// ---------------- PC Version ----------------
console.log("[APP] PC Version gestartet");
async function start() {

// ---------------- Raspberry PI Version ----------------
console.log("[APP] gestartet");

// Verzeichnisüberwachung
chokidar.watch(process.env.FOLDER_IN, {
Expand All @@ -68,6 +92,8 @@ module.exports = function (_alarmManager) {
console.log("\n[App] " + current_datetime.toString());
console.log(`[App] File ${path} has been added`);

await timeout(parseInt(process.env.FAX_INPUT_DELAY) * 1000);

let filetype = path.split('.').pop().toLowerCase();
let file = path.split(/[/\\]/g).pop().split('.')[0];

Expand All @@ -77,35 +103,58 @@ module.exports = function (_alarmManager) {
&& filetype != 'tif'
&& filetype != 'tiff'
&& filetype != 'txt'
) {

) {
console.log('FEHLER: Filetype nicht unterstützt!');
return;
}

// Dateirerchte setzen
if (RASPIVERSION == "true") {
await execShellCommand(`sudo chmod 777 ${path}`);
await timeout(1000);
}

// Datei ins Archiv verschieben
await moveFile( path, process.env.FOLDER_ARCHIVE + '/' + file + '.' + filetype);
path = process.env.FOLDER_ARCHIVE + '/' + file + '.' + filetype;

await timeout(1000);


if(filetype == 'pdf') {
// Drucken
if( process.env.FAX_DRUCK == 'true' )
printPdf(path);

// PDF -> TIFF
await convertPdfToTiff(path, './temp/' + file + '.tiff');
// TIFF -> Tesseract
await tiffToTxt('./temp/' + file + '.tiff', './temp/' + file);

// Move -> Archiv
moveFile( path, process.env.FOLDER_ARCHIVE + '/' + file + '.' + filetype);
moveFile( './temp/' + file + '.tiff', process.env.FOLDER_ARCHIVE + '/' + file + '.tiff');
path = process.env.FOLDER_ARCHIVE + '/' + file + '.tiff';

// TIFF -> Tesseract
await tiffToTxt(path, './temp/' + file);

// Textdatei verarbeiten
_alarmManager[0].parseFile('./temp/' + file + '.txt');

return;
}

if(filetype == 'tif' || filetype == 'tiff') {
if(filetype == 'tif' || filetype == 'tiff' || filetype == 'pdf') {
// TIFF -> Tesseract
await tiffToTxt('./temp/' + file + '.tiff', './temp/' + file);
await tiffToTxt(path, './temp/' + file);

// Move -> Archiv
moveFile( path, process.env.FOLDER_ARCHIVE + '/' + file + '.' + filetype);
(async () => {
// TIFF -> PDF
await tiffToPdf(path, './temp/' + file);
// Move -> Archiv
await moveFile( './temp/' + file + '.pdf', process.env.FOLDER_ARCHIVE + '/' + file + '.pdf');

/// Drucken
if( process.env.FAX_DRUCK == 'true' )
printPdf(process.env.FOLDER_ARCHIVE + '/' + file + '.pdf');
})();

// Textdatei verarbeiten
_alarmManager[0].parseFile('./temp/' + file + '.txt');
Expand All @@ -117,173 +166,6 @@ module.exports = function (_alarmManager) {
_alarmManager[0].parseFile(path);

})
}

async function startRpi() {

// ---------------- Raspberry PI Version ----------------
console.log("[APP] Raspberry Pi Version gestartet");

var sys = require('sys')
var exec = require('child_process').exec;

// Verzeichnisüberwachung
chokidar.watch(process.env.FOLDER_IN, {
ignored: /(^|[\/\\])\../,
usePolling: true,
interval: 3000,
binaryInterval: 3000,
awaitWriteFinish: {
stabilityThreshold: 2000,
pollInterval: 100
},
}).on('add', (path) => {

// Konsolenausgabe
let current_datetime = new Date()
console.log("\n[App] " + current_datetime.toString());
console.log(`[App] File ${path} has been added`);

// Delay, da Hylafax sonst offenbar Datei noch nicht fertig geschrieben hat
var delay = 20000;

// Prüfe ob TIF/TIFF Datei
if (path.split('.')[1] == "tiff" || path.split('.')[1] == "tif") {

// Dateipfad im Archiv
var file = process.env.FOLDER_ARCHIVE + "/" + String(path).split("/").pop();
// Lokalisiertes Datum
var d = new Date().toLocaleTimeString();

// Dateirechte setzen
setTimeout(function () {
console.log(`[App] ${d} sudo chmod 777 ${path}`);
exec(`sudo chmod 777 ${path}`, function (err, stdout, stderr) {
if (err) {
console.log('error:', err)
}
console.log("stdout -> " + stdout);
console.log("stderr -> " + stderr);
});
}, delay);


delay += 2000;
setTimeout(function () {

// Datei ins Archiv verschieben
console.log(`[App] ${d} sudo mv ${path} ` + process.env.FOLDER_ARCHIVE);
exec(`sudo mv ${path} ` + process.env.FOLDER_ARCHIVE, function (err, stdout, stderr) {
if (err) {
console.log('error:', err)
}
console.log("stdout -> " + stdout);
console.log("stderr -> " + stderr);
});

// Datei in PDF ausdrucken/konvertieren
delay = 10000; // Warte etwas da es sonst aus nicht geht (keine Ahnung warum)
setTimeout(function () {
console.log(`${d} PDF Druck: sudo /usr/bin/tiff2ps -a -p ${file} |lpr -P PDFPrint`);
exec(`sudo /usr/bin/tiff2ps -a -p ${file} |lpr -P PDFPrint`, function (err, stdout, stderr) {
if (err) {
console.log('error:', err)
}
console.log("stdout -> " + stdout);
console.log("stderr -> " + stderr);
});
}, delay);


// Tesseract ausführen
delay = 20000; // Warte etwas da es sonst aus nicht geht (keine Ahnung warum)
setTimeout(function () {

console.log("[App] Tesseract!");

console.log(`[App] ${d} sudo tesseract ${file} -l deu -psm 6 stdout`);
exec(`sudo tesseract ${file} -l deu -psm 6 stdout`, function (err, stdout, stderr) {
var text = stdout;
if (err) {
console.log('error:', err)
} else {

// Prüfe ob Text erkannt wurde
if (text != "" && text != null && text != undefined && text != " ") {

// Dateiname
var arr = String(file).split(".");
var filePath = arr[arr.length - 2];

// Schreibe Text in Datei
fs.writeFile(filePath + ".txt", text, function (err) {
if (err) {
return console.log(err);
}
console.log("[APP] Datei gespeichert");

// Textdatei verarbeiten
_alarmManager[0].parseFile(filePath + ".txt");

});

}
}

console.log("stdout -> " + stdout);
console.log("stderr -> " + stderr);

});

}, delay);

// Datei ausdrucken
delay += 10000;
setTimeout(function () {
console.log(`${d} Druck 1: sudo /usr/bin/tiff2ps -a -p ${file} |lpr -P Alarmdrucker`);
exec(`sudo /usr/bin/tiff2ps -a -p ${file} |lpr -P Alarmdrucker`, function (err, stdout, stderr) {
if (err) {
console.log('error:', err)
}
console.log("stdout -> " + stdout);
console.log("stderr -> " + stderr);
});
}, delay);

// Datei nochmal ausdrucken
delay += 10000;
setTimeout(function () {
console.log(`${d} Druck 2: sudo /usr/bin/tiff2ps -a -p ${file} |lpr -P Alarmdrucker`);
exec(`sudo /usr/bin/tiff2ps -a -p ${file} |lpr -P Alarmdrucker`, function (err, stdout, stderr) {
if (err) {
console.log('error:', err)
}
console.log("stdout -> " + stdout);
console.log("stderr -> " + stderr);
});
}, delay);


}, delay);

}

});

}


async function start() {

if (RASPIVERSION == "false") {

await startWin();

} else {

await startRpi();

}

}

Expand Down
Loading

0 comments on commit b1004fb

Please sign in to comment.