From 51b80f08a51823325e77403fcc19655cad9bca74 Mon Sep 17 00:00:00 2001 From: Marcel Peterkau Date: Mon, 4 Dec 2023 22:05:39 +0100 Subject: [PATCH] added Notification to WebUI --- Software/data_src/static/js/dtc_table.js | 77 ++++++++++++++++++++++++ Software/data_src/static/js/websocket.js | 18 +----- 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/Software/data_src/static/js/dtc_table.js b/Software/data_src/static/js/dtc_table.js index 9fde968..43fee75 100644 --- a/Software/data_src/static/js/dtc_table.js +++ b/Software/data_src/static/js/dtc_table.js @@ -1,5 +1,82 @@ const jsonFilePath = "static/dtc_table.json"; +var dtcState = {}; + +function processDTCNotifications(dtcArray) { + for (var i = 0; i < dtcArray.length; i++) { + var dtcInfo = dtcArray[i].split(","); + var errorCode = dtcInfo[1]; + var activity = parseInt(dtcInfo[3]); + + if (dtcState[errorCode]) { + // Überprüfen, ob sich der Zustand von "previous" auf "active" geändert hat + if (activity !== dtcState[errorCode]) { + dtcState[errorCode] = activity; + if (activity === 1) showDTCNotification(errorCode); + } + } else { + // DTC ist neu, Zustand speichern und Benachrichtigung anzeigen + dtcState[errorCode] = activity; + showDTCNotification(errorCode); + } + } +} + +function showDTCNotification(dtctext, severity) { + // Überprüfen, ob der Browser die Notification API unterstützt + if ("Notification" in window) { + // Überprüfen, ob der Benutzer bereits Berechtigungen erteilt hat + if (Notification.permission === "granted") { + // Benachrichtigung anzeigen + showNotification(dtctext, severity); + } else if (Notification.permission !== "denied") { + // Aufforderung zur Erlaubnis einholen + Notification.requestPermission().then(function (permission) { + if (permission === "granted") { + // Benachrichtigung anzeigen + showNotification(dtctext, severity); + } else { + // Der Benutzer hat die Berechtigung abgelehnt oder das Dialogfeld geschlossen + console.log("Benachrichtigungsberechtigung wurde verweigert."); + } + }); + } else { + // Der Benutzer hat die Berechtigung bereits verweigert + console.log("Benachrichtigungsberechtigung wurde bereits verweigert."); + } + } +} + +// Funktion zum Anzeigen der Benachrichtigung +function showNotification(dtctext, severity) { + var severityIcon; + switch (severity) { + case 1: + severityIcon = "static/img/info.png"; + break; + case 2: + severityIcon = "static/img/warn.png"; + break; + case 3: + severityIcon = "static/img/critical.png"; + break; + default: + severityIcon = "static/img/none.png"; + } + + var options = { + body: dtctext, + icon: severityIcon, + }; + + var notification = new Notification("KTM Chain Oiler DTC", options); + + // Optional: Handle Click-Event + notification.onclick = function () { + console.log("Benachrichtigung wurde angeklickt."); + }; +} + function getDescriptionForDTCNumber(number, callback) { fetch(jsonFilePath) .then((response) => response.json()) diff --git a/Software/data_src/static/js/websocket.js b/Software/data_src/static/js/websocket.js index eccba0d..8c9147b 100644 --- a/Software/data_src/static/js/websocket.js +++ b/Software/data_src/static/js/websocket.js @@ -44,7 +44,7 @@ function onMessage(event) { if(dtcArray[0] != "0") { - notifyMe(); + processDTCNotifications(dtcArray); fillDTCTable(dtcArray); } console.log(dtcArray + "\n"); @@ -82,18 +82,4 @@ function do_resize(textbox) { else textbox.rows = rows; } -function notifyMe() { - if (!("Notification" in window)) { - alert("This browser does not support desktop notification"); - } else if (Notification.permission === "granted") { - const notification = new Notification("Hi there!"); - // … - } else if (Notification.permission !== "denied") { - Notification.requestPermission().then((permission) => { - if (permission === "granted") { - const notification = new Notification("Hi there!"); - // … - } - }); - } -} +