Anandjadhav369/trafficflow-commander
0
1// Shared JavaScript functions2document.addEventListener('DOMContentLoaded', function() {3 // Initialize any shared functionality4 console.log('TrafficFlow Commander initialized');5 6 // Handle mobile menu toggle if needed7 const mobileMenuButton = document.getElementById('mobile-menu-button');8 const mobileMenu = document.getElementById('mobile-menu');9 10 if (mobileMenuButton && mobileMenu) {11 mobileMenuButton.addEventListener('click', function() {12 mobileMenu.classList.toggle('hidden');13 });14 }15 16 // Counter animation for stats17 const animateCounters = (entries, observer) => {18 entries.forEach(entry => {19 if (entry.isIntersecting) {20 const counters = entry.target.querySelectorAll('.stat-item');21 counters.forEach(counter => {22 counter.style.opacity = '1';23 counter.style.transform = 'translateY(0)';24 });25 observer.unobserve(entry.target);26 }27 });28 };29 30 const statsObserver = new IntersectionObserver(animateCounters, { threshold: 0.5 });31 const statsSection = document.querySelector('.stats-section');32 if (statsSection) statsObserver.observe(statsSection);33});34// Traffic Management Functions35let socket;36 37function connectWebSocket() {38 socket = new WebSocket('wss://yourserver.com/traffic-ws');39 40 socket.onmessage = (event) => {41 const data = JSON.parse(event.data);42 updateDashboard(data);43 };44 45 socket.onclose = () => {46 setTimeout(connectWebSocket, 5000);47 };48}49 50function updateDashboard(data) {51 // Update traffic map52 document.querySelectorAll('.intersection').forEach(intersection => {53 const intersectionData = data.intersections.find(i => i.id === intersection.dataset.id);54 if (intersectionData) {55 intersection.classList.toggle('congested', intersectionData.congested);56 }57 });58 59 // Update status indicators60 document.getElementById('emergency-count').textContent = data.emergencyVehicles.length;61 document.getElementById('congestion-level').textContent = 62 `${Math.round(data.congestionLevel * 100)}%`;63}64 65// Initialize WebSocket connection66connectWebSocket();67 68// API Functions69async function activateEmergencyRoute(vehicleId) {70 try {71 const response = await fetch('/api/emergency/route', {72 method: 'POST',73 headers: { 'Content-Type': 'application/json' },74 body: JSON.stringify({ vehicleId })75 });76 return await response.json();77 } catch (error) {78 console.error('Error activating emergency route:', error);79 }80}81 82async function adjustSignalTiming(intersectionId, timing) {83 try {84 const response = await fetch('/api/signals/adjust', {85 method: 'POST',86 headers: { 'Content-Type': 'application/json' },87 body: JSON.stringify({ intersectionId, timing })88 });89 return await response.json();90 } catch (error) {91 console.error('Error adjusting signal timing:', error);92 }93}94 95async function fetchAnalytics() {96 try {97 const response = await fetch('/api/analytics/congestion');98 return await response.json();99 } catch (error) {100 console.error('Error fetching analytics:', error);101 return null;102 }103}104 