This commit is contained in:
Stefan080106
2023-04-20 22:09:17 +02:00

View File

@@ -23,118 +23,98 @@ 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/";
// Certificate // Certificate
const privateKey = fs.readFileSync('./assets/ssl/localhost/localhost.decrypted.key'); const privateKey = fs.readFileSync("./assets/ssl/localhost/localhost.decrypted.key");
const certificate = fs.readFileSync('./assets/ssl/localhost/localhost.crt'); const certificate = fs.readFileSync("./assets/ssl/localhost/localhost.crt");
const credentials = { const credentials = {
key: privateKey, key: privateKey,
cert: certificate cert: certificate,
}; };
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 });
@@ -146,12 +126,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;
@@ -159,14 +139,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
@@ -202,17 +181,15 @@ app.get('/depesche', async (req, res) => {
return res.status(500).send("No Data"); return res.status(500).send("No Data");
} }
await main(value).catch( await main(value).catch((err) => {
(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.get("/health", (req, res) => { app.get("/health", (req, res) => {