added Notification to WebUI
This commit is contained in:
		| @@ -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()) | ||||
|   | ||||
| @@ -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!"); | ||||
|         // … | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user