WIP
This commit is contained in:
133
src/index.js
133
src/index.js
@@ -1,10 +1,10 @@
|
|||||||
import pdfLib from "pdf-to-printer";
|
import pdfLib from "pdf-to-printer";
|
||||||
import { jsPDF } from "jspdf";
|
import { jsPDF } from "jspdf";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import puppeteer from 'puppeteer';
|
import puppeteer from "puppeteer";
|
||||||
import express from 'express';
|
import express from "express";
|
||||||
import Joi from 'joi';
|
import Joi from "joi";
|
||||||
import TelegramBot from 'node-telegram-bot-api';
|
import TelegramBot from "node-telegram-bot-api";
|
||||||
|
|
||||||
const Logo = fs.readFileSync("./assets/Logo_fwaur.png", { encoding: "latin1" });
|
const Logo = fs.readFileSync("./assets/Logo_fwaur.png", { encoding: "latin1" });
|
||||||
const config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
|
const config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
|
||||||
@@ -21,107 +21,89 @@ const schema = Joi.object({
|
|||||||
});
|
});
|
||||||
const printerName = config.printer_device_id;
|
const printerName = config.printer_device_id;
|
||||||
const PORT = config.port || 2000;
|
const PORT = config.port || 2000;
|
||||||
const cacheFolder = "./cache/"
|
const cacheFolder = "./cache/";
|
||||||
|
|
||||||
let bot = null;
|
let bot = null;
|
||||||
|
|
||||||
const messageIDs = [1034133487]
|
const messageIDs = [1034133487];
|
||||||
|
|
||||||
function sendMessage(msg, idList) {
|
function sendMessage(msg, idList) {
|
||||||
idList.forEach((id) => {
|
idList.forEach((id) => {
|
||||||
bot.sendMessage(id, msg);
|
bot.sendMessage(id, msg);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.activate_telegram) {
|
if (config.activate_telegram) {
|
||||||
const token = '6150769224:AAGhBagPEi23QtBJl65YNuwEBBXwHguDV0E';
|
const token = "6150769224:AAGhBagPEi23QtBJl65YNuwEBBXwHguDV0E";
|
||||||
|
|
||||||
|
|
||||||
bot = new TelegramBot(token, { polling: true });
|
bot = new TelegramBot(token, { polling: true });
|
||||||
|
|
||||||
bot.setMyCommands([
|
bot.setMyCommands([
|
||||||
{ command: '/chatid', description: 'Zeigt deine Chat ID' },
|
{ command: "/chatid", description: "Zeigt deine Chat ID" },
|
||||||
{ command: '/druckerliste', description: 'Zeigt alle Drucker' },
|
{ command: "/druckerliste", description: "Zeigt alle Drucker" },
|
||||||
])
|
]);
|
||||||
|
|
||||||
bot.on('message', async (msg) => {
|
bot.on("message", async (msg) => {
|
||||||
const chatId = msg.chat.id;
|
const chatId = msg.chat.id;
|
||||||
|
|
||||||
switch (msg.text) {
|
switch (msg.text) {
|
||||||
case '/chatid':
|
case "/chatid":
|
||||||
bot.sendMessage(chatId, 'Deine Chat ID ist: ' + chatId);
|
bot.sendMessage(chatId, "Deine Chat ID ist: " + chatId);
|
||||||
break;
|
break;
|
||||||
case '/druckerliste':
|
case "/druckerliste":
|
||||||
let message = ""
|
let message = "";
|
||||||
|
|
||||||
const printers = await pdfLib.getPrinters()
|
const printers = await pdfLib.getPrinters();
|
||||||
printers.forEach((printer) => {
|
printers.forEach((printer) => {
|
||||||
message += printer.deviceId + "\n"
|
message += printer.deviceId + "\n";
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.sendMessage(chatId, 'Drucker Liste:\n' + message);
|
bot.sendMessage(chatId, "Drucker Liste:\n" + message);
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
sendMessage("[INFO]: Drucker Service wird gestartet", messageIDs);
|
||||||
|
|
||||||
sendMessage("[INFO]: Drucker Service wird gestartet", messageIDs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function printFile(_filePath, count = 1) {
|
async function printFile(_filePath, count = 1) {
|
||||||
await pdfLib.print(_filePath, {
|
await pdfLib.print(_filePath, {
|
||||||
printer: printerName,
|
printer: printerName,
|
||||||
copies: count,
|
copies: count,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createPDF(_alarmData) {
|
async function createPDF(_alarmData) {
|
||||||
const doc = new jsPDF({ orientation: "landscape" });
|
const doc = new jsPDF({ orientation: "landscape" });
|
||||||
//Header
|
//Header
|
||||||
doc.addImage(
|
doc.addImage(Logo, "PNG", 240, 5, 44.35, 15.25);
|
||||||
Logo,
|
|
||||||
"PNG",
|
|
||||||
240,
|
|
||||||
5,
|
|
||||||
44.35, 15.25
|
|
||||||
);
|
|
||||||
|
|
||||||
doc.setFontSize(14);
|
doc.setFontSize(14);
|
||||||
doc.text(_alarmData.foreign_id,
|
doc.text(_alarmData.foreign_id, 286, 205, { align: "right" });
|
||||||
286,
|
|
||||||
205, { align: "right" }
|
|
||||||
)
|
|
||||||
|
|
||||||
doc.setFontSize(20);
|
doc.setFontSize(20);
|
||||||
|
|
||||||
doc.text(_alarmData.title + " - " + new Date(_alarmData.ts_create * 1000).toLocaleString(),
|
doc.text(
|
||||||
|
_alarmData.title + " - " + new Date(_alarmData.ts_create * 1000).toLocaleString(),
|
||||||
10,
|
10,
|
||||||
15,
|
15
|
||||||
)
|
);
|
||||||
|
|
||||||
doc.setFontSize(14);
|
doc.setFontSize(14);
|
||||||
doc.text(_alarmData.text.split("RIC")[0],
|
doc.text(_alarmData.text.split("RIC")[0], 10, 22);
|
||||||
10,
|
|
||||||
22,
|
|
||||||
)
|
|
||||||
|
|
||||||
doc.setTextColor(0, 0, 0);
|
doc.setTextColor(0, 0, 0);
|
||||||
doc.setFontSize(14);
|
doc.setFontSize(14);
|
||||||
doc.text(_alarmData.address,
|
doc.text(_alarmData.address, 10, 205);
|
||||||
10,
|
|
||||||
205
|
|
||||||
)
|
|
||||||
|
|
||||||
await puppeteerGetImage(_alarmData.lat, _alarmData.lng)
|
await puppeteerGetImage(_alarmData.lat, _alarmData.lng);
|
||||||
|
|
||||||
const mapImg = fs.readFileSync(cacheFolder + "map.png", "base64")
|
const mapImg = fs.readFileSync(cacheFolder + "map.png", "base64");
|
||||||
|
|
||||||
doc.addImage(mapImg, 10, 25);
|
doc.addImage(mapImg, 10, 25);
|
||||||
|
|
||||||
doc.save(_alarmData.filePath);
|
doc.save(_alarmData.filePath);
|
||||||
};
|
}
|
||||||
|
|
||||||
async function puppeteerGetImage(lat, lng) {
|
async function puppeteerGetImage(lat, lng) {
|
||||||
const browser = await puppeteer.launch({ headless: true });
|
const browser = await puppeteer.launch({ headless: true });
|
||||||
@@ -133,12 +115,12 @@ async function puppeteerGetImage(lat, lng) {
|
|||||||
height: 650,
|
height: 650,
|
||||||
deviceScaleFactor: 1,
|
deviceScaleFactor: 1,
|
||||||
});
|
});
|
||||||
await page.goto(`http://www.openfiremap.de/?zoom=17&lat=${lat}&lon=${lng}&layers=B00000T`);
|
await page.goto(`https://umap.openstreetmap.de/de/map/feuerwehr-aurich_22583#18/${lat}/${lng}`);
|
||||||
await page.waitForNetworkIdle();
|
await page.waitForNetworkIdle();
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
document.body.innerHTML += `<div id="centerPoint" style="height: 15px;width: 15px;border-radius: 50%;background-color: red;position: absolute;border: 3px solid green;margin: auto;top: 0;bottom: 0;left: 0;right: 0;z-index: 100;"></div><style>#OpenLayers_Control_PanZoomBar_3, #OpenLayers_Control_LayerSwitcher_4, nam1, nam2, dasch, #OpenLayers_Control_Attribution_9, #OpenLayers_Control_Permalink_7, #OpenLayers_Control_Permalink_5 {display:none !important;} </style>`;
|
document.body.innerHTML += `<div id="centerPoint" style="height: 15px;width: 15px;border-radius: 50%;background-color: red;position: absolute;border: 3px solid green;margin: auto;top: 0;bottom: 0;left: 0;right: 0;z-index: 100;"></div><style>.leaflet-control-container {display:none !important;} </style>`;
|
||||||
})
|
});
|
||||||
await page.screenshot({ path: cacheFolder + 'map.png' });
|
await page.screenshot({ path: cacheFolder + "map.png" });
|
||||||
|
|
||||||
await browser.close();
|
await browser.close();
|
||||||
return;
|
return;
|
||||||
@@ -146,14 +128,13 @@ async function puppeteerGetImage(lat, lng) {
|
|||||||
|
|
||||||
async function main(data) {
|
async function main(data) {
|
||||||
if (config.activate_telegram) {
|
if (config.activate_telegram) {
|
||||||
sendMessage("[INFO]: Einsatdepesche wird Gedruckt - " + data.title, messageIDs)
|
sendMessage("[INFO]: Einsatdepesche wird Gedruckt - " + data.title, messageIDs);
|
||||||
}
|
}
|
||||||
const filePath = cacheFolder + data.foreign_id + ".pdf"
|
const filePath = cacheFolder + data.foreign_id + ".pdf";
|
||||||
await createPDF({ filePath, ...data })
|
await createPDF({ filePath, ...data });
|
||||||
console.log("PDF created, printing now")
|
console.log("PDF created, printing now");
|
||||||
console.log(cacheFolder + data.foreign_id + ".pdf")
|
console.log(cacheFolder + data.foreign_id + ".pdf");
|
||||||
await printFile(filePath, 2)
|
await printFile(filePath, 2);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Express Server
|
// Express Server
|
||||||
@@ -162,44 +143,42 @@ const app = express();
|
|||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.post('/depesche', async (req, res) => {
|
app.post("/depesche", async (req, res) => {
|
||||||
if (config.debug) {
|
if (config.debug) {
|
||||||
fs.writeFileSync(cacheFolder + req.body.foreign_id + "_body.json", JSON.stringify(req.body))
|
fs.writeFileSync(
|
||||||
|
cacheFolder + req.body.foreign_id + "_body.json",
|
||||||
|
JSON.stringify(req.body)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("[INFO] Received print request");
|
console.log("[INFO] Received print request");
|
||||||
|
|
||||||
if (req.body.foreign_id == "") {
|
if (req.body.foreign_id == "") {
|
||||||
req.body.foreign_id = "undefined"
|
req.body.foreign_id = "undefined";
|
||||||
}
|
}
|
||||||
|
|
||||||
let value = {}
|
let value = {};
|
||||||
try {
|
try {
|
||||||
value = await schema.validateAsync(req.body, { allowUnknown: true });
|
value = await schema.validateAsync(req.body, { allowUnknown: true });
|
||||||
}
|
} catch (err) {
|
||||||
catch (err) {
|
|
||||||
console.log("[ERROR]", err);
|
console.log("[ERROR]", err);
|
||||||
if (config.activate_telegram) {
|
if (config.activate_telegram) {
|
||||||
sendMessage("[ERROR] Validation Error (Drucker Service): " + err, messageIDs)
|
sendMessage("[ERROR] Validation Error (Drucker Service): " + err, messageIDs);
|
||||||
}
|
}
|
||||||
return res.status(403).send(err);
|
return res.status(403).send(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await main(value).catch((err) => {
|
||||||
|
|
||||||
await main(value).catch(
|
|
||||||
(err) => {
|
|
||||||
console.log(err);
|
console.log(err);
|
||||||
if (config.activate_telegram) {
|
if (config.activate_telegram) {
|
||||||
sendMessage("[ERROR] Internal Server Error (Drucker Service): " + err, messageIDs)
|
sendMessage("[ERROR] Internal Server Error (Drucker Service): " + err, messageIDs);
|
||||||
}
|
}
|
||||||
return res.status(500).send(err);
|
return res.status(500).send(err);
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
return res.send('ok');
|
return res.send("ok");
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`PDF Printing Service listening on port ${PORT}`);
|
console.log(`PDF Printing Service listening on port ${PORT}`);
|
||||||
})
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user