update Push Notification

This commit is contained in:
2026-04-22 02:08:51 +02:00
parent 324b066f51
commit 66d22411a4
2 changed files with 8 additions and 13 deletions
+4 -4
View File
@@ -184,11 +184,11 @@ def on_message(client, userdata, msg):
# --- GRILLETTO PUSH: STATO NODO --- # --- GRILLETTO PUSH: STATO NODO ---
if payload.upper() == 'OFFLINE': if payload.upper() == 'OFFLINE':
if last_notified_errors.get(f"{cid}_NODE") != 'OFFLINE': if last_notified_errors.get(f"{cid}_NODE") != 'OFFLINE':
broadcast_push_notification(f"💀 NODO OFFLINE: {cid.upper()}", "Disconnesso dal broker.") broadcast_push_notification(f"💀 NODE OFFLINE: {cid.upper()}", "Connection lost with broker.")
last_notified_errors[f"{cid}_NODE"] = 'OFFLINE' last_notified_errors[f"{cid}_NODE"] = 'OFFLINE'
elif payload.upper() == 'ONLINE': elif payload.upper() == 'ONLINE':
if last_notified_errors.get(f"{cid}_NODE") == 'OFFLINE': if last_notified_errors.get(f"{cid}_NODE") == 'OFFLINE':
broadcast_push_notification(f"🌤️ NODO ONLINE: {cid.upper()}", "Tornato operativo.") broadcast_push_notification(f"🌤️ NODE ONLINE: {cid.upper()}", "Node is back online.")
del last_notified_errors[f"{cid}_NODE"] del last_notified_errors[f"{cid}_NODE"]
if payload.upper() not in ['OFF', 'OFFLINE', '']: if payload.upper() not in ['OFF', 'OFFLINE', '']:
@@ -221,10 +221,10 @@ def on_message(client, userdata, msg):
if last_notified_errors.get(status_key) != s_lower: if last_notified_errors.get(status_key) != s_lower:
msg_err = f"Servizio {svc_name} KO ({svc_status})" msg_err = f"Servizio {svc_name} KO ({svc_status})"
if s_lower == "error": msg_err += " - Auto-healing fallito! ⚠️" if s_lower == "error": msg_err += " - Auto-healing fallito! ⚠️"
broadcast_push_notification(f"🚨 ALLARME: {cid.upper()}", msg_err) broadcast_push_notification(f"🚨 ALARM: {cid.upper()}", msg_err)
last_notified_errors[status_key] = s_lower last_notified_errors[status_key] = s_lower
elif s_lower == "online" and status_key in last_notified_errors: elif s_lower == "online" and status_key in last_notified_errors:
broadcast_push_notification(f"✅ RIPRISTINO: {cid.upper()}", f"Servizio {svc_name} tornato ONLINE.") broadcast_push_notification(f"✅ RESTORED: {cid.upper()}", f"Service{svc_name} back ONLINE.")
del last_notified_errors[status_key] del last_notified_errors[status_key]
# ----------------------------------------- # -----------------------------------------
+4 -9
View File
@@ -1077,19 +1077,13 @@
}); });
} }
// --- FUNZIONE PER ISCRIVERSI ALLE PUSH --- // --- FUNZIONE PER ISCRIVERSI ALLE PUSH ---
async function subscribeToPush() { async function subscribeToPush() {
const lang = document.documentElement.lang || 'it';
const msg = {
it: { ok: "Notifiche attivate!", err: "Errore o permesso negato", title: "PUSH" },
en: { ok: "Notifications enabled!", err: "Error or permission denied", title: "PUSH" }
};
if (!('serviceWorker' in navigator)) return; if (!('serviceWorker' in navigator)) return;
const permission = await Notification.requestPermission(); const permission = await Notification.requestPermission();
if (permission !== 'granted') { if (permission !== 'granted') {
customAlert("Error", msg[lang].err, true); customAlert("Error", "Push notifications permission denied.", true);
return; return;
} }
@@ -1109,10 +1103,11 @@
body: JSON.stringify(subscription) body: JSON.stringify(subscription)
}); });
customAlert("Success", msg[lang].ok); customAlert("Success", "Push notifications enabled successfully!");
document.getElementById('push-btn').style.color = 'var(--success)'; document.getElementById('push-btn').style.color = 'var(--success)';
} catch (e) { } catch (e) {
console.error(e); console.error(e);
customAlert("Error", "Failed to enable notifications.", true);
} }
} }