2023-01-19 19:28:40 +00:00
|
|
|
// This code executes in its own worker or thread
|
|
|
|
self.addEventListener("install", event => {
|
2023-02-02 23:21:02 +00:00
|
|
|
checkNotificationPromise();
|
|
|
|
interval = setInterval(() => fetchAndCacheLatestNews(), 1000);
|
2023-01-19 19:28:40 +00:00
|
|
|
});
|
2023-02-02 23:21:02 +00:00
|
|
|
|
|
|
|
self.addEventListener("fetch", (e) => {
|
|
|
|
console.log(`[Service Worker] Fetched resource ${e.request.url}`);
|
2023-01-19 19:28:40 +00:00
|
|
|
});
|
2023-02-02 23:21:02 +00:00
|
|
|
|
|
|
|
let interval = 0;
|
|
|
|
|
|
|
|
function handleNotification(title, notif) {
|
|
|
|
if (checkNotificationPromise()) {
|
|
|
|
self.registration.showNotification(title, notif);
|
|
|
|
} else {
|
|
|
|
self.registration.showNotification(title, notif);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchAndCacheLatestNews() {
|
|
|
|
const response = await fetch('/api/teasense');
|
|
|
|
const data = await response.json();
|
|
|
|
const temp = data[data.length - 1].value;
|
|
|
|
if (temp) {
|
|
|
|
if (temp >= ALERT_THRESHOLD) {
|
|
|
|
handleNotification('TeaSense Alert', {
|
|
|
|
body: '♨️ ',
|
|
|
|
tag: 1,
|
|
|
|
});
|
|
|
|
} else if (temp < ALERT_THRESHOLD && temp > WARNING_THRESHOLD) {
|
|
|
|
handleNotification('TeaSense Warning', {
|
|
|
|
body: '🫖',
|
|
|
|
tag: 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkNotificationPromise() {
|
|
|
|
try {
|
|
|
|
Notification.requestPermission().then();
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ALERT_THRESHOLD = 80.0;
|
|
|
|
const WARNING_THRESHOLD = 75.0;
|