button battery level warning
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
// pushFlicIntegration.js
|
||||
import { PUBLIC_VAPID_KEY, BACKEND_URL, FLIC_BUTTON_ID, FLIC_ACTIONS } from './config.js';
|
||||
import { PUBLIC_VAPID_KEY, BACKEND_URL, FLIC_BUTTON_ID, FLIC_ACTIONS, FLIC_BATTERY_THRESHOLD } from './config.js';
|
||||
|
||||
let pushSubscription = null; // Keep track locally if needed
|
||||
let actionHandlers = {}; // Store handlers for different Flic actions
|
||||
let lastBatteryWarningTimestamp = 0; // Track when last battery warning was shown
|
||||
|
||||
// --- Helper Functions ---
|
||||
|
||||
@@ -67,6 +68,31 @@ function arrayBufferToBase64(buffer) {
|
||||
return window.btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
||||
}
|
||||
|
||||
// Show a popup notification to the user
|
||||
function showBatteryWarning(batteryLevel) {
|
||||
// Only show warning once every 4 hours (to avoid annoying users)
|
||||
const now = Date.now();
|
||||
const fourHoursInMs = 4 * 60 * 60 * 1000;
|
||||
|
||||
if (now - lastBatteryWarningTimestamp < fourHoursInMs) {
|
||||
console.log(`[PushFlic] Battery warning suppressed (shown recently): ${batteryLevel}%`);
|
||||
return;
|
||||
}
|
||||
|
||||
lastBatteryWarningTimestamp = now;
|
||||
|
||||
// Show the notification
|
||||
const message = `Flic button battery is low (${batteryLevel}%). Please replace the battery soon.`;
|
||||
|
||||
// Show browser notification if permission granted
|
||||
if (Notification.permission === 'granted') {
|
||||
new Notification('Flic Button Low Battery', {
|
||||
body: message,
|
||||
icon: '/favicon.ico'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// --- Push Subscription Logic ---
|
||||
|
||||
async function subscribeToPush() {
|
||||
@@ -207,8 +233,13 @@ async function sendSubscriptionToServer(subscription, buttonId) {
|
||||
// --- Flic Action Handling ---
|
||||
|
||||
// Called by app.js when a message is received from the service worker
|
||||
export function handleFlicAction(action, buttonId, timestamp) {
|
||||
console.log(`[PushFlic] Received Action: ${action} from Button: ${buttonId} at ${timestamp}`);
|
||||
export function handleFlicAction(action, buttonId, timestamp, batteryLevel) {
|
||||
console.log(`[PushFlic] Received Action: ${action} from Button: ${buttonId} battery: ${batteryLevel}% at ${timestamp}`);
|
||||
|
||||
// Check if battery is below threshold and show warning if needed
|
||||
if (batteryLevel < FLIC_BATTERY_THRESHOLD) {
|
||||
showBatteryWarning(batteryLevel);
|
||||
}
|
||||
|
||||
// Ignore actions from buttons other than the configured one
|
||||
if (buttonId !== FLIC_BUTTON_ID) {
|
||||
|
||||
Reference in New Issue
Block a user