// Client-side JavaScript for system status and updates // Add this to the public/js/app.js file // System status and updates functionality function initSystemStatus() { // Elements const versionElement = document.getElementById('system-version'); const uptimeElement = document.getElementById('uptime'); const transmissionStatusElement = document.getElementById('transmission-status'); const updateStatusElement = document.getElementById('update-status'); const updateAvailableDiv = document.getElementById('update-available'); const updateButton = document.getElementById('btn-update-now'); const refreshButton = document.getElementById('btn-refresh-status'); // Load system status function loadSystemStatus() { fetch('/api/system/status') .then(response => response.json()) .then(data => { if (data.status === 'success') { versionElement.textContent = data.data.version; uptimeElement.textContent = data.data.uptime; // Update transmission status with icon if (data.data.transmissionStatus === 'Connected') { transmissionStatusElement.innerHTML = ' Connected'; } else { transmissionStatusElement.innerHTML = ' Disconnected'; } } else { showToast('error', 'Failed to load system status'); } }) .catch(error => { console.error('Error fetching system status:', error); showToast('error', 'Failed to connect to server'); }); } // Check for updates function checkForUpdates() { updateStatusElement.innerHTML = ' Checking...'; updateAvailableDiv.classList.add('d-none'); fetch('/api/system/check-updates') .then(response => response.json()) .then(data => { if (data.status === 'success') { if (data.data.updateAvailable) { updateStatusElement.innerHTML = ' Update available'; updateAvailableDiv.classList.remove('d-none'); updateAvailableDiv.querySelector('span').textContent = `A new version is available: ${data.data.currentVersion} → ${data.data.remoteVersion}`; } else { updateStatusElement.innerHTML = ' Up to date'; } } else { updateStatusElement.innerHTML = ' Check failed'; showToast('error', data.message || 'Failed to check for updates'); } }) .catch(error => { console.error('Error checking for updates:', error); updateStatusElement.innerHTML = ' Check failed'; showToast('error', 'Failed to connect to server'); }); } // Apply update function applyUpdate() { // Show confirmation dialog if (!confirm('Are you sure you want to update the application? The service will restart.')) { return; } // Show loading state updateButton.disabled = true; updateButton.innerHTML = ' Updating...'; showToast('info', 'Applying update. Please wait...'); fetch('/api/system/update', { method: 'POST', headers: { 'Content-Type': 'application/json' } }) .then(response => response.json()) .then(data => { if (data.status === 'success') { showToast('success', 'Update applied successfully. The page will reload in 30 seconds.'); // Set a timer to reload the page after the service has time to restart setTimeout(() => { window.location.reload(); }, 30000); } else { updateButton.disabled = false; updateButton.innerHTML = ' Update Now'; showToast('error', data.message || 'Failed to apply update'); } }) .catch(error => { console.error('Error applying update:', error); updateButton.disabled = false; updateButton.innerHTML = ' Update Now'; showToast('error', 'Failed to connect to server'); }); } // Event listeners if (refreshButton) { refreshButton.addEventListener('click', () => { loadSystemStatus(); checkForUpdates(); }); } if (updateButton) { updateButton.addEventListener('click', applyUpdate); } // Initialize loadSystemStatus(); checkForUpdates(); // Set interval to refresh uptime every minute setInterval(loadSystemStatus, 60000); } // Call this function from the main init function // Add this line to your document ready or DOMContentLoaded handler: // initSystemStatus();