diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..ef9e9a0 --- /dev/null +++ b/.env.example @@ -0,0 +1,11 @@ +# Server Configuration +PORT=3002 + +# OpenWeatherMap API Key +# Get your API key from: https://openweathermap.org/api +OPENWEATHERMAP_API_KEY=your_openweathermap_api_key_here + +# Default Location (Stockholm, Sweden) +DEFAULT_LATITUDE=59.3293 +DEFAULT_LONGITUDE=18.0686 +DEFAULT_LOCATION_NAME=Stockholm diff --git a/.gitignore b/.gitignore index 0d3dd35..8a9366d 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,6 @@ pids # User-specific files config.local.js + +# Archive directory (legacy/development files) +archive/ \ No newline at end of file diff --git a/README.md b/README.md index d344fe2..4848116 100644 --- a/README.md +++ b/README.md @@ -64,15 +64,35 @@ A modern digital signage system for displaying real-time transit departures and cd SignageHTML ``` -2. **Start the server** +2. **Install dependencies** ```bash - node server.js + npm install ``` -3. **Open in browser** +3. **Configure environment variables** + + Create a `.env` file in the root directory (or copy from `.env.example` if it exists): + ```bash + PORT=3002 + OPENWEATHERMAP_API_KEY=your_openweathermap_api_key_here + DEFAULT_LATITUDE=59.3293 + DEFAULT_LONGITUDE=18.0686 + DEFAULT_LOCATION_NAME=Stockholm + ``` + + Get your OpenWeatherMap API key from [openweathermap.org](https://openweathermap.org/api). + +4. **Start the server** + ```bash + npm start + # or for development with auto-reload: + npm run dev + ``` + +5. **Open in browser** Navigate to: `http://localhost:3002` -The server will start on port 3002 by default. The application will automatically load and begin fetching transit and weather data. +The server will start on port 3002 by default (or the port specified in `.env`). The application will automatically load and begin fetching transit and weather data. ## Configuration @@ -95,19 +115,24 @@ The server will start on port 3002 by default. The application will automaticall 2. Enter the Site Name and Site ID 3. Enable/disable the site as needed -### Changing Weather Location +### Environment Variables -Weather location is configured in `weather.js`. Update the default coordinates: +The application uses environment variables for configuration. Create a `.env` file in the root directory: -```javascript -window.weatherManager = new WeatherManager({ - latitude: 59.3293, // Stockholm default - longitude: 18.0686, - apiKey: 'your-api-key' -}); +```bash +# Server port (default: 3002) +PORT=3002 + +# OpenWeatherMap API key (required for weather features) +OPENWEATHERMAP_API_KEY=your_api_key_here + +# Default location coordinates (Stockholm, Sweden) +DEFAULT_LATITUDE=59.3293 +DEFAULT_LONGITUDE=18.0686 +DEFAULT_LOCATION_NAME=Stockholm ``` -Or use the settings panel to configure it through the UI. +**Note**: The OpenWeatherMap API key is injected into the HTML at runtime by the server. You can also configure the weather location through the settings panel in the UI. ### Screen Orientation @@ -138,10 +163,10 @@ git clone http://192.168.68.53:3000/kyle/SignageHTML.git cd SignageHTML # Make the setup script executable -chmod +x raspberry-pi-setup.sh +chmod +x scripts/raspberry-pi-setup.sh # Run the setup script as root -sudo ./raspberry-pi-setup.sh +sudo ./scripts/raspberry-pi-setup.sh # Reboot to apply all changes sudo reboot @@ -155,33 +180,44 @@ The system consists of the following components: 1. **Node.js Server** (`server.js`) - Handles API proxying (SL Transport API, OpenWeatherMap) - - Serves static files + - Serves static files from `public/` directory - Manages site configuration + - Route handlers organized in `server/routes/`: + - `departures.js` - Transit departure data endpoints + - `sites.js` - Site search and nearby sites + - `config.js` - Configuration management endpoints -2. **Configuration Manager** (`config.js`) +2. **Configuration Manager** (`public/js/components/ConfigManager.js`) - Manages system settings and UI customization - Handles site selection and map integration - Persists settings to localStorage -3. **Weather Component** (`weather.js`) +3. **Weather Component** (`public/js/components/WeatherManager.js`) - Displays weather data from OpenWeatherMap - Manages dark mode based on sunrise/sunset - Shows hourly forecasts -4. **Clock Component** (`clock.js`) +4. **Clock Component** (`public/js/components/Clock.js`) - Shows current time and date - Swedish locale formatting - Updates every second -5. **Departures Component** (`departures.js`) +5. **Departures Component** (`public/js/components/DeparturesManager.js`) - Fetches and displays transit departure information - Groups departures by line number - Handles direction indicators and timing -6. **Main UI** (`index.html`) +6. **Utilities** (`public/js/utils/`) + - `constants.js` - Application constants and configuration + - `logger.js` - Centralized logging utility + +7. **Main UI** (`index.html`) - Responsive layout with multiple orientation support - CSS Grid-based landscape optimization - Modern styling with animations + - External CSS in `public/css/`: + - `main.css` - Base styles, dark mode, orientation styles + - `components.css` - Component-specific styles ## API Integration @@ -196,7 +232,7 @@ Weather data is fetched from OpenWeatherMap: - Provides current conditions and hourly forecasts - Used for dark mode timing calculations -For detailed API response documentation, see [API_RESPONSE_DOCUMENTATION.md](API_RESPONSE_DOCUMENTATION.md). +For detailed API response documentation, see [docs/API_RESPONSE_DOCUMENTATION.md](docs/API_RESPONSE_DOCUMENTATION.md). ## UI Settings @@ -251,10 +287,13 @@ Settings are automatically saved to localStorage and persist across sessions. - Verify sites are configured in settings 3. **Weather data not loading** - - Check OpenWeatherMap API key in `weather.js` + - Check OpenWeatherMap API key in `.env` file + - Ensure `.env` file exists in the root directory + - Verify the API key is correctly set: `OPENWEATHERMAP_API_KEY=your_key_here` + - Restart the server after changing `.env` file - Verify internet connection - Look for errors in browser console (F12) - - Check API key quota/limits + - Check API key quota/limits on openweathermap.org 4. **Map not loading** - Ensure internet connection is active @@ -271,15 +310,38 @@ Settings are automatically saved to localStorage and persist across sessions. ### Project Structure ``` SignageHTML/ -├── index.html # Main HTML file with UI and styles -├── server.js # Node.js server for API proxying -├── config.js # Configuration management -├── clock.js # Clock component -├── weather.js # Weather component -├── departures.js # Departures component -├── sites-config.json # Persistent site configuration -├── package.json # Node.js dependencies -└── README.md # This file +├── index.html # Main HTML file +├── server.js # Node.js server entry point +├── server/ +│ └── routes/ # API route handlers +│ ├── departures.js # Departure data endpoints +│ ├── sites.js # Site search endpoints +│ └── config.js # Configuration endpoints +├── public/ # Static assets +│ ├── css/ +│ │ ├── main.css # Base styles, dark mode, orientations +│ │ └── components.css # Component-specific styles +│ └── js/ +│ ├── main.js # Application initialization +│ ├── components/ # Component classes +│ │ ├── Clock.js +│ │ ├── ConfigManager.js +│ │ ├── DeparturesManager.js +│ │ └── WeatherManager.js +│ └── utils/ # Utility modules +│ ├── constants.js +│ └── logger.js +├── config/ +│ └── sites.json # Persistent site configuration +├── docs/ +│ └── API_RESPONSE_DOCUMENTATION.md # API response documentation +├── scripts/ +│ └── raspberry-pi-setup.sh # Raspberry Pi deployment script +├── package.json # Node.js dependencies +├── .env # Environment variables (create from .env.example) +├── .env.example # Environment variable template +├── archive/ # Archived legacy files (see archive/README.md) +└── README.md # This file ``` ### Contributing diff --git a/card.JPG b/card.JPG deleted file mode 100644 index 32fb7eb..0000000 Binary files a/card.JPG and /dev/null differ diff --git a/sites-config.json b/config/sites.json similarity index 94% rename from sites-config.json rename to config/sites.json index 4bc9ae0..0921c11 100644 --- a/sites-config.json +++ b/config/sites.json @@ -2,7 +2,7 @@ "orientation": "normal", "darkMode": "auto", "backgroundImage": "https://images.unsplash.com/photo-1509356843151-3e7d96241e11?q=80&w=1000", - "backgroundOpacity": 0.3, + "backgroundOpacity": 0.45, "sites": [ { "id": "1411", diff --git a/create-deployment-package.sh b/create-deployment-package.sh deleted file mode 100644 index 55a3af5..0000000 --- a/create-deployment-package.sh +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/bash -# Script to create a deployment package for Raspberry Pi - -echo "Creating deployment package for SL Transport Departures Display..." - -# Create a temporary directory -TEMP_DIR="deployment-package" -mkdir -p $TEMP_DIR - -# Copy necessary files -echo "Copying files..." -cp index.html $TEMP_DIR/ -cp server.js $TEMP_DIR/ -cp clock.js $TEMP_DIR/ -cp config.js $TEMP_DIR/ -cp weather.js $TEMP_DIR/ -cp ticker.js $TEMP_DIR/ -cp package.json $TEMP_DIR/ -cp README.md $TEMP_DIR/ -cp documentation.md $TEMP_DIR/ -cp raspberry-pi-setup.sh $TEMP_DIR/ -cp .gitignore $TEMP_DIR/ - -# Copy any image files if they exist -if [ -d "images" ]; then - mkdir -p $TEMP_DIR/images - cp -r images/* $TEMP_DIR/images/ -fi - -# Create a version file with timestamp -echo "Creating version file..." -DATE=$(date +"%Y-%m-%d %H:%M:%S") -echo "SL Transport Departures Display" > $TEMP_DIR/version.txt -echo "Packaged on: $DATE" >> $TEMP_DIR/version.txt -echo "Version: 1.0.0" >> $TEMP_DIR/version.txt - -# Create a ZIP archive -echo "Creating ZIP archive..." -ZIP_FILE="sl-departures-display-$(date +"%Y%m%d").zip" -zip -r $ZIP_FILE $TEMP_DIR - -# Clean up -echo "Cleaning up..." -rm -rf $TEMP_DIR - -echo "Deployment package created: $ZIP_FILE" -echo "To deploy to Raspberry Pi:" -echo "1. Transfer the ZIP file to your Raspberry Pi" -echo "2. Unzip the file: unzip $ZIP_FILE" -echo "3. Navigate to the directory: cd deployment-package" -echo "4. Make the setup script executable: chmod +x raspberry-pi-setup.sh" -echo "5. Run the setup script: sudo ./raspberry-pi-setup.sh" diff --git a/departures.js b/departures.js deleted file mode 100644 index 539b949..0000000 --- a/departures.js +++ /dev/null @@ -1,766 +0,0 @@ -// Calculate minutes until arrival using expected time (or scheduled if expected not available) -function calculateMinutesUntilArrival(departure) { - const now = new Date(); - // Use expected time if available (accounts for delays), otherwise use scheduled - const arrivalTime = departure.expected ? new Date(departure.expected) : new Date(departure.scheduled); - return Math.round((arrivalTime - now) / (1000 * 60)); -} - -// Get the time to display (expected if available, otherwise scheduled) -function getDepartureTime(departure) { - return departure.expected || departure.scheduled; -} - -// Get transport icon based on transport mode -function getTransportIcon(transportMode) { - // Default to bus if not specified - const mode = transportMode ? transportMode.toLowerCase() : 'bus'; - - // Special case for line 7 - it's a tram - if (arguments.length > 1 && arguments[1] && arguments[1].designation === '7') { - return ''; - } - - // SVG icons for different transport modes - const icons = { - bus: '', - metro: '', - train: '', - tram: '', - ship: '' - }; - - return icons[mode] || icons.bus; -} - -// Create a departure card element -function createDepartureCard(departure) { - const departureCard = document.createElement('div'); - departureCard.dataset.journeyId = departure.journey.id; - - const displayTime = departure.display; - const departureTime = getDepartureTime(departure); - const timeDisplay = formatDateTime(departureTime); - - // Check if departure is within the next hour - const departureTimeDate = new Date(departureTime); - const now = new Date(); - const diffMinutes = Math.round((departureTimeDate - now) / (1000 * 60)); - const isWithinNextHour = diffMinutes <= 60; - - // Add condensed class if within next hour - departureCard.className = isWithinNextHour ? 'departure-card condensed' : 'departure-card'; - - // Calculate minutes until arrival using expected time (accounts for delays) - const minutesUntil = calculateMinutesUntilArrival(departure); - let countdownText = displayTime; - let countdownClass = ''; - - // Determine color class based on minutesUntil, regardless of displayTime format - if (minutesUntil <= 0 || displayTime === 'Nu' || displayTime.toLowerCase() === 'nu') { - countdownText = 'Nu'; - countdownClass = 'now'; - } else if (minutesUntil < 5) { - // Less than 5 minutes - red - const minMatch = displayTime.match(/(\d+)\s*min/i); - if (minMatch) { - countdownText = minutesUntil === 1 ? '1 min' : `${minutesUntil} min`; - } else { - countdownText = minutesUntil === 1 ? '1 min' : `${minutesUntil} min`; - } - countdownClass = 'urgent'; // Red: less than 5 minutes - } else { - // 5+ minutes - white - const isTimeOnly = /^\d{1,2}:\d{2}$/.test(displayTime); - if (isTimeOnly) { - countdownText = `${minutesUntil} min`; - } else { - // Use displayTime as-is (e.g., "5 min", "8 min") - countdownText = displayTime; - } - // No class = white (default) - } - - // Get transport icon based on transport mode and line - const transportIcon = getTransportIcon(departure.line?.transportMode, departure.line); - - // Create card based on time and display format - departureCard.innerHTML = ` -
- - ${transportIcon} - ${departure.line.designation} - ${departure.destination} - - - ${timeDisplay} - (${countdownText}) - -
- `; - - return departureCard; -} - -// Display departures grouped by line number - New card-based layout -function displayGroupedDeparturesByLine(groups, container) { - groups.forEach(group => { - // Create a card for this line number - const groupCard = document.createElement('div'); - groupCard.className = 'departure-card line-card'; - - // Get transport mode for styling - ensure we use the API value - const apiTransportMode = group.line?.transportMode || ''; - const transportMode = apiTransportMode.toLowerCase(); - - // Create large line number box on the left - const lineNumberBox = document.createElement('div'); - lineNumberBox.className = `line-number-box ${transportMode}`; - - // Get transport icon instead of text label - const transportIcon = getTransportIcon(group.line?.transportMode, group.line); - - lineNumberBox.innerHTML = ` -
${transportIcon}
-
${group.lineNumber}
- `; - groupCard.appendChild(lineNumberBox); - - // Create directions wrapper on the right - const directionsWrapper = document.createElement('div'); - directionsWrapper.className = 'directions-wrapper'; - - // Process each direction (up to 2 directions side-by-side) - const maxDirections = 2; - group.directions.slice(0, maxDirections).forEach(direction => { - // Sort departures by expected time (or scheduled if expected not available) - direction.departures.sort((a, b) => { - const timeA = getDepartureTime(a); - const timeB = getDepartureTime(b); - return new Date(timeA) - new Date(timeB); - }); - - // Create a row for this direction - const directionRow = document.createElement('div'); - directionRow.className = 'direction-row'; - - // Add direction info (arrow + destination) - const directionInfo = document.createElement('div'); - directionInfo.className = 'direction-info'; - - // Determine direction arrow and styling from API data - // Get direction from the first departure in this direction group - const firstDep = direction.departures[0]; - if (!firstDep) return; // Skip if no departures - - // Use direction_code from API: 1 = going TO that direction, 2 = going FROM that direction - // For arrows: direction_code 1 = left arrow, direction_code 2 = right arrow - const directionCode = firstDep.direction_code !== undefined ? firstDep.direction_code : - firstDep.directionCode !== undefined ? firstDep.directionCode : - null; - - // Map direction_code to arrow direction - // direction_code 1 = left arrow (←), direction_code 2 = right arrow (→) - const isRight = directionCode === 2; - - if (directionCode === null || directionCode === undefined) { - console.warn('No direction_code found for:', direction.destination, firstDep); - } - - const arrowBox = document.createElement('div'); - arrowBox.className = `direction-arrow-box ${isRight ? 'right' : 'left'}`; - arrowBox.textContent = isRight ? '→' : '←'; - - const destinationSpan = document.createElement('span'); - destinationSpan.className = 'direction-destination'; - destinationSpan.textContent = direction.destination; - - directionInfo.appendChild(arrowBox); - directionInfo.appendChild(destinationSpan); - directionRow.appendChild(directionInfo); - - // Add times container - const timesContainer = document.createElement('div'); - timesContainer.className = 'times-container'; - - // Get first two departures for time range - const firstDeparture = direction.departures[0]; - const secondDeparture = direction.departures[1]; - - if (firstDeparture) { - const displayTime = firstDeparture.display; - const departureTime = getDepartureTime(firstDeparture); - const timeDisplay = formatDateTime(departureTime); - - // Calculate minutes until arrival using expected time (accounts for delays) - const minutesUntil = calculateMinutesUntilArrival(firstDeparture); - let countdownText = displayTime; - let countdownClass = ''; - - // Determine color class based on minutesUntil, regardless of displayTime format - if (minutesUntil <= 0 || displayTime === 'Nu' || displayTime.toLowerCase() === 'nu') { - countdownText = 'Nu'; - countdownClass = 'now'; - } else if (minutesUntil < 5) { - // Use the number from displayTime if it's "X min", otherwise use calculated minutesUntil - const minMatch = displayTime.match(/(\d+)\s*min/i); - if (minMatch) { - countdownText = `${minMatch[1]}`; - } else { - countdownText = `${minutesUntil}`; - } - countdownClass = 'urgent'; // Red: less than 5 minutes - } else { - // 5+ minutes - use displayTime as-is or calculate - const minMatch = displayTime.match(/(\d+)\s*min/i); - if (minMatch) { - countdownText = `${minMatch[1]}`; - } else if (/^\d{1,2}:\d{2}$/.test(displayTime)) { - countdownText = `${minutesUntil}`; - } else { - countdownText = displayTime; - } - // No class = white (default) - } - - // Create time display element - const timeDisplayElement = document.createElement('div'); - timeDisplayElement.className = 'time-display'; - - const countdownSpan = document.createElement('span'); - countdownSpan.className = `countdown-large ${countdownClass}`; - countdownSpan.textContent = countdownText; - - timeDisplayElement.appendChild(countdownSpan); - - // Add time range (show expected times) - const timeRangeSpan = document.createElement('span'); - timeRangeSpan.className = 'time-range'; - if (secondDeparture) { - const secondTime = formatDateTime(getDepartureTime(secondDeparture)); - timeRangeSpan.textContent = `${timeDisplay} - ${secondTime}`; - } else { - timeRangeSpan.textContent = timeDisplay; - } - timeDisplayElement.appendChild(timeRangeSpan); - - timesContainer.appendChild(timeDisplayElement); - } - - directionRow.appendChild(timesContainer); - directionsWrapper.appendChild(directionRow); - }); - - groupCard.appendChild(directionsWrapper); - - // Add to container - container.appendChild(groupCard); - }); -} - -// Display grouped departures (legacy function) -function displayGroupedDepartures(groups, container) { - groups.forEach(group => { - // Sort departures by expected time (or scheduled if expected not available) - group.departures.sort((a, b) => { - const timeA = getDepartureTime(a); - const timeB = getDepartureTime(b); - return new Date(timeA) - new Date(timeB); - }); - - // Create a card for this group - const groupCard = document.createElement('div'); - groupCard.className = 'departure-card'; - - // Create card header - const header = document.createElement('div'); - header.className = 'departure-header'; - - // Get transport icon based on transport mode and line - const transportIcon = getTransportIcon(group.line?.transportMode, group.line); - - // Add line number with transport icon and destination - const lineNumber = document.createElement('span'); - lineNumber.className = 'line-number'; - lineNumber.innerHTML = `${transportIcon} ${group.line.designation} ${group.destination}`; - header.appendChild(lineNumber); - - // Add times container - const timesContainer = document.createElement('div'); - timesContainer.className = 'times-container'; - timesContainer.style.display = 'flex'; - timesContainer.style.flexDirection = 'column'; - timesContainer.style.alignItems = 'flex-end'; - - // Add up to 3 departure times - const maxTimes = 3; - group.departures.slice(0, maxTimes).forEach((departure, index) => { - const timeElement = document.createElement('span'); - timeElement.className = 'time'; - timeElement.style.fontSize = index === 0 ? '1.1em' : '0.9em'; - timeElement.style.marginBottom = '2px'; - - const displayTime = departure.display; - const departureTime = getDepartureTime(departure); - const timeDisplay = formatDateTime(departureTime); - - // Calculate minutes until arrival using expected time (accounts for delays) - const minutesUntil = calculateMinutesUntilArrival(departure); - let countdownText = displayTime; - let countdownClass = ''; - - // Determine color class based on minutesUntil, regardless of displayTime format - if (minutesUntil <= 0 || displayTime === 'Nu' || displayTime.toLowerCase() === 'nu') { - countdownText = 'Nu'; - countdownClass = 'now'; - } else if (minutesUntil < 5) { - // Less than 5 minutes - red - const minMatch = displayTime.match(/(\d+)\s*min/i); - if (minMatch) { - countdownText = minutesUntil === 1 ? '1 min' : `${minutesUntil} min`; - } else { - countdownText = minutesUntil === 1 ? '1 min' : `${minutesUntil} min`; - } - countdownClass = 'urgent'; // Red: less than 5 minutes - } else { - // 5+ minutes - white - const isTimeOnly = /^\d{1,2}:\d{2}$/.test(displayTime); - if (isTimeOnly) { - countdownText = `${minutesUntil} min`; - } else { - // Use displayTime as-is (e.g., "5 min", "8 min") - countdownText = displayTime; - } - // No class = white (default) - } - - timeElement.innerHTML = `${scheduledTime} (${countdownText})`; - - timesContainer.appendChild(timeElement); - }); - - // If there are more departures, show a count - if (group.departures.length > maxTimes) { - const moreElement = document.createElement('span'); - moreElement.style.fontSize = '0.8em'; - moreElement.style.color = '#666'; - moreElement.textContent = `+${group.departures.length - maxTimes} more`; - timesContainer.appendChild(moreElement); - } - - header.appendChild(timesContainer); - groupCard.appendChild(header); - - // No need to add destination and direction separately as they're now in the header - - // Add to container - container.appendChild(groupCard); - }); -} - -// Format date and time -function formatDateTime(dateTimeString) { - const date = new Date(dateTimeString); - return date.toLocaleTimeString('sv-SE', { hour: '2-digit', minute: '2-digit' }); -} - -// Format relative time (e.g., "in 5 minutes") -function formatRelativeTime(dateTimeString) { - const departureTime = new Date(dateTimeString); - const now = new Date(); - const diffMinutes = Math.round((departureTime - now) / (1000 * 60)); - - if (diffMinutes <= 0) { - return 'Nu'; - } else if (diffMinutes === 1) { - return 'In 1 minute'; - } else if (diffMinutes < 60) { - return `In ${diffMinutes} minutes`; - } else { - const hours = Math.floor(diffMinutes / 60); - const minutes = diffMinutes % 60; - if (minutes === 0) { - return `In ${hours} hour${hours > 1 ? 's' : ''}`; - } else { - return `In ${hours} hour${hours > 1 ? 's' : ''} and ${minutes} minute${minutes > 1 ? 's' : ''}`; - } - } -} - -// Group departures by line number -function groupDeparturesByLineNumber(departures) { - const groups = {}; - - departures.forEach(departure => { - const lineNumber = departure.line.designation; - - if (!groups[lineNumber]) { - groups[lineNumber] = { - line: departure.line, - directions: {} - }; - } - - // Get direction_code from API: 1 = going TO that direction, 2 = going FROM that direction - const departureDirection = departure.direction_code !== undefined ? departure.direction_code : - departure.directionCode !== undefined ? departure.directionCode : - departure.direction !== undefined ? departure.direction : - 1; // Default to 1 (left arrow) if not found - - const directionKey = `${departureDirection}-${departure.destination}`; - - if (!groups[lineNumber].directions[directionKey]) { - groups[lineNumber].directions[directionKey] = { - direction: departureDirection, - destination: departure.destination, - departures: [] - }; - } - - groups[lineNumber].directions[directionKey].departures.push(departure); - }); - - // Convert to array format and sort directions by direction_code (1 first, then 2) - return Object.entries(groups).map(([lineNumber, data]) => { - // Sort directions: direction 1 first, then direction 2 - const directionsArray = Object.values(data.directions); - directionsArray.sort((a, b) => { - // Sort by direction_code: 1 comes before 2 - const dirA = a.direction || 1; - const dirB = b.direction || 1; - return dirA - dirB; - }); - - return { - lineNumber: lineNumber, - line: data.line, - directions: directionsArray - }; - }); -} - -// Group departures by direction (legacy function kept for compatibility) -function groupDeparturesByDirection(departures) { - const groups = {}; - - departures.forEach(departure => { - const key = `${departure.line.designation}-${departure.direction}-${departure.destination}`; - - if (!groups[key]) { - groups[key] = { - line: departure.line, - direction: departure.direction, - destination: departure.destination, - departures: [] - }; - } - - groups[key].departures.push(departure); - }); - - return Object.values(groups); -} - -// Store the current departures data for comparison -let currentDepartures = []; - -// Display departures in the UI with smooth transitions -function displayDepartures(departures) { - if (!departures || departures.length === 0) { - departuresContainer.innerHTML = '
No departures found
'; - return; - } - - // If this is the first load, just display everything - if (currentDepartures.length === 0) { - departuresContainer.innerHTML = ''; - - departures.forEach(departure => { - const departureCard = createDepartureCard(departure); - departuresContainer.appendChild(departureCard); - }); - } else { - // Update only what has changed - updateExistingCards(departures); - } - - // Update the current departures for next comparison - currentDepartures = JSON.parse(JSON.stringify(departures)); -} - -// Update existing cards or add new ones -function updateExistingCards(newDepartures) { - // Get all current cards - const currentCards = departuresContainer.querySelectorAll('.departure-card'); - const currentCardIds = Array.from(currentCards).map(card => card.dataset.journeyId); - - // Process each new departure - newDepartures.forEach((departure, index) => { - const journeyId = departure.journey.id; - const existingCardIndex = currentCardIds.indexOf(journeyId.toString()); - - if (existingCardIndex !== -1) { - // Update existing card - const existingCard = currentCards[existingCardIndex]; - updateCardContent(existingCard, departure); - } else { - // This is a new departure, add it - const newCard = createDepartureCard(departure); - - // Add with fade-in effect - newCard.style.opacity = '0'; - - if (index === 0) { - // Add to the beginning - departuresContainer.prepend(newCard); - } else if (index >= departuresContainer.children.length) { - // Add to the end - departuresContainer.appendChild(newCard); - } else { - // Insert at specific position - departuresContainer.insertBefore(newCard, departuresContainer.children[index]); - } - - // Trigger fade-in - setTimeout(() => { - newCard.style.transition = 'opacity 0.5s ease-in'; - newCard.style.opacity = '1'; - }, 10); - } - }); - - // Remove cards that are no longer in the new data - const newDepartureIds = newDepartures.map(d => d.journey.id.toString()); - currentCards.forEach(card => { - if (!newDepartureIds.includes(card.dataset.journeyId)) { - // Fade out and remove - card.style.transition = 'opacity 0.5s ease-out'; - card.style.opacity = '0'; - setTimeout(() => { - if (card.parentNode) { - card.parentNode.removeChild(card); - } - }, 500); - } - }); -} - -// Update only the content that has changed in an existing card -function updateCardContent(card, departure) { - const displayTime = departure.display; - const departureTime = getDepartureTime(departure); - - // Calculate minutes until arrival using expected time (accounts for delays) - const minutesUntil = calculateMinutesUntilArrival(departure); - let countdownText = displayTime; - let countdownClass = ''; - - // Determine color class based on minutesUntil, regardless of displayTime format - if (minutesUntil <= 0 || displayTime === 'Nu' || displayTime.toLowerCase() === 'nu') { - countdownText = 'Nu'; - countdownClass = 'now'; - } else if (minutesUntil < 5) { - // Less than 5 minutes - red - const minMatch = displayTime.match(/(\d+)\s*min/i); - if (minMatch) { - countdownText = minutesUntil === 1 ? '1 min' : `${minutesUntil} min`; - } else { - countdownText = minutesUntil === 1 ? '1 min' : `${minutesUntil} min`; - } - countdownClass = 'urgent'; // Red: less than 5 minutes - } else { - // 5+ minutes - white - const isTimeOnly = /^\d{1,2}:\d{2}$/.test(displayTime); - if (isTimeOnly) { - countdownText = `${minutesUntil} min`; - } else { - // Use displayTime as-is (e.g., "5 min", "8 min") - countdownText = displayTime; - } - // No class = white (default) - } - - // Only update the countdown time which changes frequently - const countdownElement = card.querySelector('.countdown'); - - // Update class for "now" and "urgent" states - if (countdownElement) { - // Remove all state classes first - countdownElement.classList.remove('now', 'urgent'); - - // Add the appropriate class - if (countdownClass === 'now') { - countdownElement.classList.add('now'); - } else if (countdownClass === 'urgent') { - countdownElement.classList.add('urgent'); - } - - // Update with subtle highlight effect for changes - if (countdownElement.textContent !== `(${countdownText})`) { - countdownElement.textContent = `(${countdownText})`; - highlightElement(countdownElement); - } - } -} - -// Add a subtle highlight effect to show updated content -function highlightElement(element) { - element.style.transition = 'none'; - element.style.backgroundColor = 'rgba(255, 255, 0, 0.3)'; - - setTimeout(() => { - element.style.transition = 'background-color 1.5s ease-out'; - element.style.backgroundColor = 'transparent'; - }, 10); -} - -// Display multiple sites -function displayMultipleSites(sites) { - // Get configuration - const config = getConfig(); - const enabledSites = config.sites.filter(site => site.enabled); - - // Clear the container - departuresContainer.innerHTML = ''; - - // Process each site - sites.forEach(site => { - // Check if this site is enabled in the configuration - const siteConfig = enabledSites.find(s => s.id === site.siteId); - if (!siteConfig) return; - - // Create a site container - const siteContainer = document.createElement('div'); - siteContainer.className = 'site-container'; - - // Add site header with white tab - const siteHeader = document.createElement('div'); - siteHeader.className = 'site-header'; - siteHeader.innerHTML = `${site.siteName || siteConfig.name}`; - siteContainer.appendChild(siteHeader); - - // Process departures for this site - if (site.data && site.data.departures) { - // Group departures by line number using the existing function - const lineGroups = groupDeparturesByLineNumber(site.data.departures); - - // Use the new card-based layout function - displayGroupedDeparturesByLine(lineGroups, siteContainer); - } else if (site.error) { - // Display error for this site - const errorElement = document.createElement('div'); - errorElement.className = 'error'; - errorElement.textContent = `Error loading departures for ${site.siteName}: ${site.error}`; - siteContainer.appendChild(errorElement); - } - - // Add the site container to the main container - departuresContainer.appendChild(siteContainer); - }); -} - -// Get configuration -function getConfig() { - // Default configuration - const defaultConfig = { - combineSameDirection: true, - sites: [ - { - id: '1411', - name: 'Ambassaderna', - enabled: true - } - ] - }; - - // If we have a ConfigManager instance, use its config - if (window.configManager && window.configManager.config) { - return { - combineSameDirection: window.configManager.config.combineSameDirection !== undefined ? - window.configManager.config.combineSameDirection : defaultConfig.combineSameDirection, - sites: window.configManager.config.sites || defaultConfig.sites - }; - } - - return defaultConfig; -} - -// Fetch departures from our proxy server -async function fetchDepartures() { - try { - // Don't show loading status to avoid layout disruptions - // statusElement.textContent = 'Loading departures...'; - - const response = await fetch(API_URL); - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); - } - - const data = await response.json(); - - if (data.sites && Array.isArray(data.sites)) { - // Process multiple sites - displayMultipleSites(data.sites); - - const now = new Date(); - lastUpdatedElement.textContent = `Last updated: ${now.toLocaleTimeString('sv-SE')}`; - } else if (data.departures) { - // Legacy format - single site - displayDepartures(data.departures); - - const now = new Date(); - lastUpdatedElement.textContent = `Last updated: ${now.toLocaleTimeString('sv-SE')}`; - } else if (data.error) { - throw new Error(data.error); - } else { - throw new Error('Invalid response format from server'); - } - - } catch (error) { - console.error('Error fetching departures:', error); - // Don't update status element to avoid layout disruptions - // statusElement.textContent = ''; - departuresContainer.innerHTML = ` -
-

Failed to load departures. Please try again later.

-

Error: ${error.message}

-

Make sure the Node.js server is running: node server.js

-
- `; - } -} - -// Set up auto-refresh -function setupAutoRefresh() { - // Clear any existing timer - if (refreshTimer) { - clearInterval(refreshTimer); - } - - // Set up new timer - refreshTimer = setInterval(fetchDepartures, REFRESH_INTERVAL); -} - -// Initialize departures functionality -function initDepartures() { - // API endpoint (using our local proxy server) - window.API_URL = 'http://localhost:3002/api/departures'; - - // DOM elements - window.departuresContainer = document.getElementById('departures'); - window.statusElement = document.getElementById('status'); - window.lastUpdatedElement = document.getElementById('last-updated'); - - // Auto-refresh interval (in milliseconds) - 5 seconds - window.REFRESH_INTERVAL = 5000; - window.refreshTimer = null; - - // Initial fetch and setup - fetchDepartures(); - setupAutoRefresh(); -} - -// Initialize when the DOM is loaded -document.addEventListener('DOMContentLoaded', function() { - initDepartures(); -}); diff --git a/API_RESPONSE_DOCUMENTATION.md b/docs/API_RESPONSE_DOCUMENTATION.md similarity index 100% rename from API_RESPONSE_DOCUMENTATION.md rename to docs/API_RESPONSE_DOCUMENTATION.md diff --git a/documentation.md b/documentation.md deleted file mode 100644 index 589cef4..0000000 --- a/documentation.md +++ /dev/null @@ -1,275 +0,0 @@ -# SL Transport Departures Display System Documentation - -## System Overview - -This is a comprehensive digital signage system designed to display transit departures and weather information. The system is built with a modular architecture, making it easy to maintain and extend. It's specifically designed to work well on a Raspberry Pi for dedicated display purposes. - -## Architecture - -The system consists of the following components: - -1. **Node.js Server** - Handles API proxying and serves static files -2. **Configuration Manager** - Manages system settings and UI customization -3. **Weather Component** - Displays weather data and manages dark mode -4. **Clock Component** - Shows current time and date -5. **Departures Component** - Displays transit departure information -6. **Main UI** - Responsive layout with multiple orientation support - -## File Structure - -- `index.html` - Main HTML file containing the UI structure and inline JavaScript -- `server.js` - Node.js server for API proxying and static file serving -- `config.js` - Configuration management module -- `weather.js` - Weather display and dark mode management -- `clock.js` - Time and date display -- `departures.js` - Transit departure display component - -## Detailed Component Documentation - -### 1. Node.js Server (server.js) - -The server component acts as a proxy for external APIs and serves the static files for the application. - -#### Key Features: - -- **Port**: Runs on port 3002 -- **API Proxying**: - - `/api/departures` - Proxies requests to SL Transport API -- **Error Handling**: Provides structured error responses -- **Static File Serving**: Serves HTML, CSS, JavaScript, and image files -- **CORS Support**: Allows cross-origin requests - -#### API Endpoints: - -- `GET /api/departures` - Returns transit departure information -- `GET /` or `/index.html` - Serves the main application -- `GET /*.js|css|png|jpg|jpeg|gif|ico` - Serves static assets - -#### Implementation Details: - -The server handles malformed JSON responses from the SL Transport API by implementing custom JSON parsing and fixing. It also provides fallback data when API requests fail. - -### 2. Configuration Manager (config.js) - -The Configuration Manager handles all system settings and provides a UI for changing them. - -#### Key Features: - -- **Screen Orientation**: Controls display rotation (0°, 90°, 180°, 270°, landscape) -- **Dark Mode**: Automatic (based on sunrise/sunset), always on, or always off -- **Background Image**: Custom background with opacity control -- **Settings Persistence**: Saves settings to localStorage -- **Configuration UI**: Modal-based interface with live previews - -#### Configuration Options: - -- `orientation`: Screen orientation (`normal`, `vertical`, `upsidedown`, `vertical-reverse`, `landscape`) -- `darkMode`: Dark mode setting (`auto`, `on`, `off`) -- `backgroundImage`: URL or data URL of background image -- `backgroundOpacity`: Opacity value between 0 and 1 - -#### Implementation Details: - -The ConfigManager class creates a gear icon button that opens a modal dialog for changing settings. It applies settings immediately and dispatches events to notify other components of changes. - -### 3. Weather Component (weather.js) - -The Weather component displays current weather conditions, forecasts, and manages dark mode based on sunrise/sunset times. - -#### Key Features: - -- **Current Weather**: Temperature, condition, and icon -- **Hourly Forecast**: Weather predictions for upcoming hours -- **Sunrise/Sunset**: Calculates and displays sun times -- **Dark Mode Control**: Automatically switches between light/dark based on sun position -- **API Integration**: Uses OpenWeatherMap API -- **Fallback Data**: Provides default weather data when API is unavailable - -#### Implementation Details: - -The WeatherManager class fetches data from OpenWeatherMap API and updates the UI. It calculates sunrise/sunset times and uses them to determine if dark mode should be enabled. It dispatches events when dark mode changes. - -### 4. Clock Component (clock.js) - -The Clock component displays the current time and date. - -#### Key Features: - -- **Time Display**: Shows current time in HH:MM:SS format -- **Date Display**: Shows current date with weekday, month, day, and year -- **Timezone Support**: Configured for Stockholm timezone -- **Auto-Update**: Updates every second -- **Optional Time Sync**: Can synchronize with WorldTimeAPI - -#### Implementation Details: - -The Clock class creates and updates time and date elements. It uses the browser's Date object with the specified timezone and formats the output using toLocaleTimeString and toLocaleDateString. - -### 5. Departures Component (departures.js) - -The Departures component displays transit departure information from the SL Transport API. - -#### Key Features: - -- **Real-time Updates**: Fetches departure data periodically -- **Multiple Sites**: Supports displaying departures from multiple transit stops -- **Grouped Display**: Groups departures by line and direction -- **Countdown Timers**: Shows time until departure - -### 6. Main UI (index.html) - -The main UI integrates all components and provides responsive layouts for different screen orientations. - -#### Key Features: - -- **Responsive Design**: Adapts to different screen sizes and orientations -- **Dark Mode Support**: Changes colors based on dark mode setting -- **Departure Cards**: Displays transit departures with line numbers, destinations, and times -- **Weather Widget**: Shows current conditions and forecast -- **Auto-Refresh**: Periodically updates departure information - -#### Implementation Details: - -The HTML file contains the structure and styling for the application. It initializes all components and sets up event listeners for updates. The JavaScript in the file handles fetching and displaying departure information. - -## Setup Instructions - -### Prerequisites: - -- Node.js installed -- Internet connection for API access -- Browser with CSS3 support - -### Installation Steps: - -1. Clone or download all files to a directory -2. Run `node server.js` to start the server -3. Access the application at `http://localhost:3002` - -### Raspberry Pi Setup: - -1. Install Node.js on Raspberry Pi -2. Copy all files to a directory on the Pi -3. Set up auto-start for the server: - ``` - # Create a systemd service file - sudo nano /etc/systemd/system/sl-departures.service - - # Add the following content - [Unit] - Description=SL Departures Display - After=network.target - - [Service] - ExecStart=/usr/bin/node /path/to/server.js - WorkingDirectory=/path/to/directory - Restart=always - User=pi - - [Install] - WantedBy=multi-user.target - ``` -4. Enable and start the service: - ``` - sudo systemctl enable sl-departures - sudo systemctl start sl-departures - ``` -5. Configure Raspberry Pi to auto-start Chromium in kiosk mode: - ``` - # Edit autostart file - mkdir -p ~/.config/autostart - nano ~/.config/autostart/kiosk.desktop - - # Add the following content - [Desktop Entry] - Type=Application - Name=Kiosk - Exec=chromium-browser --kiosk --disable-restore-session-state http://localhost:3002 - ``` - -## Troubleshooting - -### Common Issues: - -1. **Server won't start** - - Check if port 3002 is already in use - - Ensure Node.js is installed correctly - -2. **No departures displayed** - - Verify internet connection - - Check server console for API errors - - Ensure the site ID is correct (currently set to 9636) - -3. **Weather data not loading** - - Check OpenWeatherMap API key - - Verify internet connection - - Look for errors in browser console - - - Look for JavaScript errors in console - -5. **Screen orientation issues** - - Ensure the content wrapper is properly created - - Check for CSS conflicts - - Verify browser supports CSS transforms - -## Customization Guide - -### Changing Transit Stop: - -To display departures for a different transit stop, modify the API_URL in server.js: - -```javascript -const API_URL = 'https://transport.integration.sl.se/v1/sites/YOUR_SITE_ID/departures'; -``` - -### Changing Weather Location: - -To display weather for a different location, modify the latitude and longitude in index.html: - -```javascript -window.weatherManager = new WeatherManager({ - latitude: YOUR_LATITUDE, - longitude: YOUR_LONGITUDE -}); -``` - -### Adding Custom Styles: - -Add custom CSS to the style section in index.html to modify the appearance. - -## System Architecture Diagram - -``` -+---------------------+ +----------------------+ -| | | | -| Browser Interface |<----->| Node.js Server | -| (index.html) | | (server.js) | -| | | | -+---------------------+ +----------------------+ - ^ ^ - | | - v v -+---------------------+ +----------------------+ -| | | | -| UI Components | | External APIs | -| - Clock | | - SL Transport API | -| - Weather | | - OpenWeatherMap | -| - Departures | | | -| - Config Manager | | | -+---------------------+ +----------------------+ -``` - -## Component Interaction Flow - -1. User loads the application in a browser -2. Node.js server serves the HTML, CSS, and JavaScript files -3. Browser initializes all components (Clock, Weather, Config, Departures) -4. Components make API requests through the Node.js server -5. Server proxies requests to external APIs and returns responses -6. Components update their UI based on the data -7. User can change settings through the Config Manager -8. Settings are applied immediately and saved to localStorage - -## Conclusion - -This documentation provides a comprehensive overview of the SL Transport Departures Display System. With this information, you should be able to understand, maintain, and recreate the system if needed. diff --git a/index.html b/index.html index aa2891c..dc3eb78 100644 --- a/index.html +++ b/index.html @@ -1,1618 +1,38 @@ - + SL Transport Departures - Ambassaderna (1411) - + - - - - - - + + + + + + + + + + + + + + + + + + @@ -1648,7 +68,7 @@
Clear
-
7.1 °C
+
7.1 °C
@@ -1656,53 +76,53 @@
Clear
-
7.1 °C
+
7.1 °C
19:00
Clear
-
6.8 °C
+
6.8 °C
20:00
Clear
-
6.5 °C
+
6.5 °C
21:00
Partly cloudy
-
6.2 °C
+
6.2 °C
22:00
Partly cloudy
-
6.0 °C
+
6.0 °C
23:00
Partly cloudy
-
5.8 °C
+
5.8 °C
00:00
Partly cloudy
-
5.5 °C
+
5.5 °C
- ☀️ Sunrise: 06:45 AM | 🌙 Sunset: 05:32 PM + ☀️ Sunrise: 06:45 AM | 🌙 Sunset: 05:32 PM
@@ -1711,97 +131,6 @@ - + + + diff --git a/istockphoto-522585615-612x612.jpg b/istockphoto-522585615-612x612.jpg deleted file mode 100644 index 04a119f..0000000 Binary files a/istockphoto-522585615-612x612.jpg and /dev/null differ diff --git a/package.json b/package.json index 8de3a11..ff2fdb4 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ ], "author": "", "license": "MIT", - "dependencies": {}, + "dependencies": { + "dotenv": "^16.3.1" + }, "devDependencies": { "nodemon": "^2.0.22" }, diff --git a/public/css/components.css b/public/css/components.css new file mode 100644 index 0000000..1f7e259 --- /dev/null +++ b/public/css/components.css @@ -0,0 +1,1242 @@ +/* Clock styles - Modern entryway kiosk design with blue glow - Ribbon banner */ + .clock-container { + background: linear-gradient(135deg, #003366 0%, #004080 50%, #0059b3 100%); + color: white; + padding: 8px 20px; /* Reduced padding for ribbon style */ + border-radius: 8px; /* Smaller radius for ribbon */ + margin-bottom: 8px; /* Reduced margin */ + text-align: center; + box-shadow: 0 0 20px rgba(0, 89, 179, 0.6), + 0 0 40px rgba(0, 89, 179, 0.4), + 0 4px 12px rgba(0, 0, 0, 0.15), + inset 0 1px 0 rgba(255, 255, 255, 0.3); + position: relative; + overflow: hidden; + backdrop-filter: blur(10px); + transition: box-shadow 0.3s ease; + border: 2px solid rgba(255, 255, 255, 0.2); + display: flex; + align-items: center; + justify-content: center; + gap: 15px; /* Space between time and date */ + } + + .clock-container::before { + content: ''; + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + background: linear-gradient(135deg, rgba(255, 255, 255, 0.15) 0%, rgba(255, 255, 255, 0.05) 50%, rgba(255, 255, 255, 0.1) 100%); + pointer-events: none; + } + + .clock-container:hover { + box-shadow: 0 0 30px rgba(0, 89, 179, 0.8), + 0 0 60px rgba(0, 89, 179, 0.5), + 0 4px 12px rgba(0, 0, 0, 0.15), + inset 0 1px 0 rgba(255, 255, 255, 0.3); + } + + body.dark-mode .clock-container { + background: linear-gradient(135deg, #001a33 0%, #002d5c 50%, #004080 100%); + box-shadow: 0 0 25px rgba(0, 89, 179, 0.7), + 0 0 50px rgba(0, 89, 179, 0.4), + 0 4px 12px rgba(0, 0, 0, 0.3), + inset 0 1px 0 rgba(255, 255, 255, 0.2); + } + + body.dark-mode .clock-container:hover { + box-shadow: 0 0 35px rgba(0, 89, 179, 0.9), + 0 0 70px rgba(0, 89, 179, 0.6), + 0 4px 12px rgba(0, 0, 0, 0.3), + inset 0 1px 0 rgba(255, 255, 255, 0.2); + } + + /* Configuration button styles */ + .config-button { + position: fixed; + bottom: 20px; /* Changed from top to bottom */ + right: 20px; + width: 40px; + height: 40px; + background-color: rgba(0, 97, 161, 0.3); /* Translucent by default */ + border-radius: 50%; + display: flex; + justify-content: center; + align-items: center; + cursor: pointer; + box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); + z-index: 100; + transition: transform 0.3s ease, background-color 0.3s ease, opacity 0.3s ease; + opacity: 0.5; /* Translucent */ + } + + .config-button:hover { + transform: rotate(30deg); + background-color: #0061a1; /* Full color on hover */ + opacity: 1; /* Fully opaque on hover */ + } + + .config-button svg { + transition: opacity 0.3s ease; + opacity: 0.7; /* Icon also translucent */ + } + + .config-button:hover svg { + opacity: 1; /* Full opacity on hover */ + } + + /* Configuration modal styles */ + .config-modal { + display: none; + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + background-color: rgba(0, 0, 0, 0.5); + z-index: 200; + justify-content: center; + align-items: center; + } + + .config-modal-content { + background-color: white; + border-radius: 8px; + width: 90%; + max-width: 600px; + max-height: 90vh; + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); + overflow: hidden; + display: flex; + flex-direction: column; + } + + body.dark-mode .config-modal-content { + background-color: #333; + } + + .config-modal-header { + background-color: #0061a1; + color: white; + padding: 15px 20px; + display: flex; + justify-content: space-between; + align-items: center; + flex-shrink: 0; + } + + .config-modal-header h2 { + margin: 0; + font-size: 1.5em; + } + + .config-modal-close { + font-size: 1.8em; + cursor: pointer; + line-height: 1; + } + + .config-modal-close:hover { + opacity: 0.8; + } + + /* Tab navigation */ + .config-tabs { + display: flex; + background-color: #f0f0f0; + border-bottom: 2px solid #ddd; + flex-shrink: 0; + } + + body.dark-mode .config-tabs { + background-color: #444; + border-bottom-color: #555; + } + + .config-tab { + flex: 1; + padding: 12px 20px; + background-color: transparent; + border: none; + border-bottom: 3px solid transparent; + cursor: pointer; + font-size: 0.95em; + font-weight: 500; + color: #666; + transition: all 0.2s ease; + } + + body.dark-mode .config-tab { + color: #aaa; + } + + .config-tab:hover { + background-color: #e0e0e0; + color: #333; + } + + body.dark-mode .config-tab:hover { + background-color: #555; + color: #f5f5f5; + } + + .config-tab.active { + background-color: white; + color: #0061a1; + border-bottom-color: #0061a1; + font-weight: 600; + } + + body.dark-mode .config-tab.active { + background-color: #333; + color: #4fc3f7; + border-bottom-color: #4fc3f7; + } + + .config-modal-body { + padding: 20px; + overflow-y: auto; + flex: 1; + min-height: 0; + max-height: calc(90vh - 200px); + } + + /* Custom scrollbar for modal body */ + .config-modal-body::-webkit-scrollbar { + width: 8px; + } + + .config-modal-body::-webkit-scrollbar-track { + background: #f1f1f1; + border-radius: 4px; + } + + .config-modal-body::-webkit-scrollbar-thumb { + background: #888; + border-radius: 4px; + } + + .config-modal-body::-webkit-scrollbar-thumb:hover { + background: #555; + } + + body.dark-mode .config-modal-body::-webkit-scrollbar-track { + background: #444; + } + + body.dark-mode .config-modal-body::-webkit-scrollbar-thumb { + background: #666; + } + + body.dark-mode .config-modal-body::-webkit-scrollbar-thumb:hover { + background: #888; + } + + .config-tab-content { + display: none; + } + + .config-tab-content.active { + display: block; + } + + .config-option { + margin-bottom: 15px; + } + + .config-option label { + display: block; + margin-bottom: 5px; + font-weight: bold; + } + + .config-option select, + .config-option input[type="text"] { + width: 100%; + padding: 8px; + border: 1px solid #ddd; + border-radius: 4px; + font-size: 1em; + box-sizing: border-box; + } + + body.dark-mode .config-option select, + body.dark-mode .config-option input[type="text"] { + background-color: #444; + color: #f5f5f5; + border-color: #555; + } + + .config-option input[type="range"] { + width: 100%; + } + + .config-option button { + padding: 5px 10px; + border: 1px solid #ddd; + border-radius: 4px; + background-color: #f5f5f5; + cursor: pointer; + font-size: 0.9em; + } + + body.dark-mode .config-option button { + background-color: #555; + color: #f5f5f5; + border-color: #666; + } + + .config-option button:hover { + background-color: #e0e0e0; + } + + body.dark-mode .config-option button:hover { + background-color: #666; + } + + /* Site search styles */ + #site-search-results { + border: 1px solid #ddd; + border-radius: 4px; + background: white; + margin-top: 5px; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); + } + + body.dark-mode #site-search-results { + background-color: #444; + border-color: #555; + } + + .site-search-result { + padding: 10px; + border-bottom: 1px solid #eee; + cursor: pointer; + transition: background-color 0.2s; + } + + .site-search-result:last-child { + border-bottom: none; + } + + .site-search-result:hover { + background-color: #f5f5f5; + } + + body.dark-mode .site-search-result { + border-bottom-color: #555; + } + + body.dark-mode .site-search-result:hover { + background-color: #555; + } + + .site-search-result div:first-child { + font-weight: bold; + color: #0061a1; + margin-bottom: 4px; + } + + body.dark-mode .site-search-result div:first-child { + color: #4fc3f7; + } + + .site-search-result div:last-child { + font-size: 0.85em; + color: #666; + } + + body.dark-mode .site-search-result div:last-child { + color: #aaa; + } + + #search-site-button { + background-color: #0061a1; + color: white; + border: none; + border-radius: 4px; + cursor: pointer; + font-weight: 500; + transition: background-color 0.2s; + } + + #search-site-button:hover { + background-color: #004d80; + } + + body.dark-mode #search-site-button { + background-color: #4fc3f7; + color: #333; + } + + body.dark-mode #search-site-button:hover { + background-color: #29b6f6; + } + + /* Map selector button */ + #select-from-map-button { + background-color: #28a745; + color: white; + border: none; + border-radius: 4px; + cursor: pointer; + font-weight: 500; + transition: background-color 0.2s; + } + + #select-from-map-button:hover { + background-color: #218838; + } + + body.dark-mode #select-from-map-button { + background-color: #34ce57; + color: #333; + } + + body.dark-mode #select-from-map-button:hover { + background-color: #28a745; + } + + /* Map selector modal styles */ + #map-selector-modal .config-modal-content { + width: 90vw; + max-width: 1200px; + } + + #map-container { + z-index: 1; + } + + .map-modal-close { + font-size: 1.8em; + cursor: pointer; + line-height: 1; + color: white; + } + + .map-modal-close:hover { + opacity: 0.8; + } + + .config-modal-footer { + padding: 15px 20px; + background-color: #f5f5f5; + text-align: right; + border-top: 1px solid #ddd; + flex-shrink: 0; + } + + body.dark-mode .config-modal-footer { + background-color: #444; + border-top-color: #555; + } + + .config-modal-footer button { + padding: 8px 15px; + margin-left: 10px; + border: none; + border-radius: 4px; + cursor: pointer; + font-size: 1em; + } + + #config-save-button { + background-color: #0061a1; + color: white; + } + + #config-save-button:hover { + background-color: #004d80; + } + + #config-cancel-button { + background-color: #ddd; + } + + body.dark-mode #config-cancel-button { + background-color: #555; + color: #f5f5f5; + } + + #config-cancel-button:hover { + background-color: #ccc; + } + + body.dark-mode #config-cancel-button:hover { + background-color: #666; + } + .clock-time { + font-size: 2.2em; /* Further reduced for ribbon */ + font-weight: 700; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif; + white-space: nowrap; + display: inline-block; /* Changed to inline-block for same line */ + letter-spacing: 1px; + text-shadow: 0 2px 8px rgba(0, 0, 0, 0.15), + 0 1px 4px rgba(255, 255, 255, 0.3); + margin: 0; /* No margin for inline layout */ + position: relative; + z-index: 1; + line-height: 1; + color: #FFFFFF; + } + + .clock-date { + font-size: 2.2em; /* Match time size */ + font-weight: 400; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif; + display: inline-block; /* Changed to inline-block for same line */ + opacity: 0.98; + letter-spacing: 0.5px; + text-shadow: 0 1px 4px rgba(0, 0, 0, 0.15), + 0 1px 2px rgba(255, 255, 255, 0.2); + text-transform: capitalize; + position: relative; + z-index: 1; + line-height: 1; + margin: 0; /* No margin for inline layout */ + color: #FFFFFF; + } + + body.dark-mode .clock-time, + body.dark-mode .clock-date { + color: #E6F4FF; + text-shadow: 0 2px 8px rgba(0, 0, 0, 0.3), + 0 1px 4px rgba(0, 0, 0, 0.2); + } + + /* Responsive adjustments for smaller screens */ + @media (max-width: 768px) { + .clock-time { + font-size: 1.8em; + letter-spacing: 1px; + } + + .clock-date { + font-size: 1.8em; /* Match time size */ + } + + .clock-container { + padding: 6px 16px; + gap: 10px; + } + } + + @media (max-width: 480px) { + .clock-time { + font-size: 1.5em; + } + + .clock-date { + font-size: 1.5em; /* Match time size */ + } + + .clock-container { + padding: 6px 12px; + gap: 8px; + } + } + h2 { + color: #0061a1; + text-align: center; + } + .status { + text-align: center; + margin-bottom: 20px; + font-style: italic; + display: none; /* Hide the status message to avoid layout disruptions */ + } + .departure-container { + display: grid; + grid-template-columns: 1fr; + gap: 12px; + margin-bottom: 20px; + } + + /* Main content grid wrapper */ + .main-content-grid { + display: block; /* Default: single column for normal/vertical modes */ + } + + .departures-section { + width: 100%; + } + + .weather-section { + width: 100%; + } + + /* New card-based layout similar to card.JPG */ + .departure-card { + background-color: white; + border-radius: 6px; /* Further reduced radius */ + padding: 0; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15); + margin-bottom: 6px; /* Further reduced margin */ + min-height: 75px; /* Further reduced min height */ + display: flex; + overflow: hidden; + width: 100%; /* Ensure card uses full column width */ + box-sizing: border-box; /* Include padding/border in width */ + max-width: 100%; /* Prevent overflow */ + } + + body.dark-mode .departure-card { + background-color: #333; + } + + /* Large line number box on the left */ + .line-number-box { + background: linear-gradient(135deg, #0061a1 0%, #004d80 100%); /* Changed from green to blue */ + color: white; + min-width: 50px; /* Further reduced */ + width: 50px; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + padding: 4px; /* Further reduced padding */ + border-radius: 4px 0 0 4px; /* Further reduced radius */ + position: relative; + } + + /* Transport mode icon */ + .transport-mode-icon { + display: flex; + align-items: center; + justify-content: center; + margin-bottom: 2px; + opacity: 0.95; + } + + .transport-mode-icon .transport-icon { + width: 20px; /* Increased icon size */ + height: 20px; + fill: currentColor; + } + + /* Large line number */ + .line-number-large { + font-size: 1.6em; /* Increased - more space available */ + font-weight: bold; + line-height: 1; + color: #fff; /* Changed to white */ + } + + /* Transport-specific colors */ + .line-number-box.bus { + background: linear-gradient(135deg, #0061a1 0%, #004d80 100%); + } + + .line-number-box.bus .line-number-large { + color: white; + } + + .line-number-box.metro { + background: linear-gradient(135deg, #c41e3a 0%, #9a1629 100%); + } + + .line-number-box.metro .line-number-large { + color: white; + } + + .line-number-box.train { + background: linear-gradient(135deg, #0061a1 0%, #004d80 100%); + } + + .line-number-box.train .line-number-large { + color: white; + } + + .line-number-box.tram { + background: linear-gradient(135deg, #0061a1 0%, #004d80 100%); /* Blue like bus/train */ + } + + .line-number-box.tram .line-number-large { + color: white; + } + + .line-number-box.ship { + background: linear-gradient(135deg, #0061a1 0%, #004d80 100%); + } + + .line-number-box.ship .line-number-large { + color: white; + } + + /* Directions container on the right */ + .directions-wrapper { + flex: 1; + display: flex; + flex-direction: column; + padding: 6px 10px; /* Increased padding - more space available */ + gap: 5px; /* Increased gap */ + min-width: 0; /* Allow flex shrinking */ + overflow: hidden; /* Prevent overflow */ + } + + /* Single direction row */ + .direction-row { + display: flex; + align-items: center; + justify-content: space-between; + flex: 1; + min-height: 36px; /* Increased */ + gap: 8px; /* Increased gap between direction-info and times-container */ + min-width: 0; /* Allow flex shrinking */ + } + + /* Direction info (arrow + destination) */ + .direction-info { + display: flex; + align-items: center; + gap: 6px; /* Increased gap */ + flex: 1; + min-width: 0; /* Allow flex shrinking */ + overflow: hidden; /* Prevent overflow */ + } + + /* Direction arrow indicator - Increased size */ + .direction-arrow-box { + width: 32px; /* Increased */ + height: 32px; + border-radius: 4px; + display: flex; + align-items: center; + justify-content: center; + font-size: 1.4em; /* Increased */ + font-weight: bold; + flex-shrink: 0; + box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2); + border: 1.5px solid rgba(255, 255, 255, 0.3); + } + + .direction-arrow-box.left { + background: repeating-linear-gradient( + 45deg, + #fff5e6, + #fff5e6 4px, + #ffe6cc 4px, + #ffe6cc 8px + ); + color: #ff6600; + border-color: #ff6600; + } + + .direction-arrow-box.right { + background: repeating-linear-gradient( + 45deg, + #e6f2ff, + #e6f2ff 4px, + #cce6ff 4px, + #cce6ff 8px + ); + color: #0066cc; + border-color: #0066cc; + } + + body.dark-mode .direction-arrow-box.left { + background: repeating-linear-gradient( + 45deg, + #664422, + #664422 4px, + #553311 4px, + #553311 8px + ); + color: #ff8800; + border-color: #ff8800; + } + + body.dark-mode .direction-arrow-box.right { + background: repeating-linear-gradient( + 45deg, + #223366, + #223366 4px, + #112255 4px, + #112255 8px + ); + color: #4fc3f7; + border-color: #4fc3f7; + } + + /* Destination text */ + .direction-destination { + font-size: 1.0em; /* Reduced for better spacing with countdown */ + font-weight: 600; + color: #333; + white-space: nowrap; /* Prevent wrapping */ + overflow: hidden; + text-overflow: ellipsis; /* Show ellipsis if too long */ + flex: 1; + min-width: 0; /* Allow flex shrinking */ + } + + body.dark-mode .direction-destination { + color: #f5f5f5; + } + + /* Times container */ + .times-container { + display: flex; + flex-direction: column; + align-items: flex-end; + gap: 3px; /* Increased gap */ + min-width: 110px; /* Increased slightly */ + flex-shrink: 0; /* Prevent shrinking */ + max-width: 110px; /* Prevent overflow */ + } + + /* Time display */ + .time-display { + display: flex; + align-items: baseline; + gap: 4px; /* Reduced gap for better spacing */ + white-space: nowrap; /* Prevent wrapping */ + font-size: 0.95em; /* Slightly reduced */ + } + + /* Pulse animation for urgent and now states - sharp text with exterior glow only */ + @keyframes pulse-glow { + 0%, 100% { + text-shadow: 0 0 8px currentColor, 0 0 12px currentColor, 0 0 16px currentColor; + } + 50% { + text-shadow: 0 0 12px currentColor, 0 0 18px currentColor, 0 0 24px currentColor; + } + } + + @keyframes pulse-scale { + 0%, 100% { + transform: scale(1); + } + 50% { + transform: scale(1.05); + } + } + + .countdown-large { + font-size: 1.2em; /* Restored to previous size */ + font-weight: bold; + color: #333; /* Dark color for light mode */ + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + white-space: nowrap; /* Prevent wrapping */ + } + + .countdown-large.urgent { + color: #c41e3a; /* Red: less than 5 minutes */ + animation: pulse-glow 2s ease-in-out infinite, pulse-scale 2s ease-in-out infinite; + text-shadow: 0 0 8px #c41e3a, 0 0 12px #c41e3a, 0 0 16px rgba(196, 30, 58, 0.9); + } + + .countdown-large.now { + color: #00a651; /* Green: "Nu" (now) */ + animation: pulse-glow 2s ease-in-out infinite, pulse-scale 2s ease-in-out infinite; + text-shadow: 0 0 8px #00a651, 0 0 12px #00a651, 0 0 16px rgba(0, 166, 81, 0.9); + } + + body.dark-mode .countdown-large { + color: #f5f5f5; /* White for 5+ minutes in dark mode */ + } + + body.dark-mode .countdown-large.urgent { + color: #ff6b6b; /* Red: less than 5 minutes in dark mode */ + text-shadow: 0 0 8px #ff6b6b, 0 0 12px #ff6b6b, 0 0 16px rgba(255, 107, 107, 0.9); + } + + body.dark-mode .countdown-large.now { + color: #4ecdc4; /* Green: "Nu" in dark mode */ + text-shadow: 0 0 8px #4ecdc4, 0 0 12px #4ecdc4, 0 0 16px rgba(78, 205, 196, 0.9); + } + + /* Time range */ + .time-range { + font-size: 0.85em; /* Increased - more space available */ + color: #666; + font-weight: 500; + white-space: nowrap; /* Prevent wrapping */ + } + + body.dark-mode .time-range { + color: #aaa; + } + + /* Legacy styles for compatibility */ + .departure-header { + display: flex; + justify-content: space-between; + align-items: center; + width: 100%; + } + + .line-number { + background-color: #0061a1; + color: white; + padding: 6px 12px; + border-radius: 6px; + font-weight: bold; + font-size: 1.8em; + display: flex; + align-items: center; + gap: 8px; + margin-right: 10px; + } + + .transport-icon { + width: 24px; + height: 24px; + fill: currentColor; + } + + .line-destination { + font-size: 0.65em; + font-weight: normal; + margin-left: 6px; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + max-width: 120px; + display: inline-block; + vertical-align: middle; + } + + .time { + font-size: 1.5em; + font-weight: bold; + color: #333; + display: flex; + flex-direction: row; + align-items: center; + justify-content: flex-end; + gap: 8px; + } + + .arrival-time { + font-size: 1.3em; + } + + .countdown { + font-size: 1.1em; + color: white; /* Default: white for 5+ minutes */ + } + + .countdown.urgent { + color: #c41e3a; /* Red: less than 5 minutes */ + animation: pulse-glow 2s ease-in-out infinite, pulse-scale 2s ease-in-out infinite; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + text-shadow: 0 0 6px #c41e3a, 0 0 10px #c41e3a, 0 0 14px rgba(196, 30, 58, 0.9); + } + + .countdown.now { + color: #00a651; /* Green: "Nu" (now) */ + font-weight: bold; + animation: pulse-glow 2s ease-in-out infinite, pulse-scale 2s ease-in-out infinite; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + text-shadow: 0 0 6px #00a651, 0 0 10px #00a651, 0 0 14px rgba(0, 166, 81, 0.9); + } + + body.dark-mode .countdown { + color: #f5f5f5; /* White for 5+ minutes in dark mode */ + } + + body.dark-mode .countdown.urgent { + color: #ff6b6b; /* Red: less than 5 minutes in dark mode */ + text-shadow: 0 0 6px #ff6b6b, 0 0 10px #ff6b6b, 0 0 14px rgba(255, 107, 107, 0.9); + } + + body.dark-mode .countdown.now { + color: #4ecdc4; /* Green: "Nu" in dark mode */ + text-shadow: 0 0 6px #4ecdc4, 0 0 10px #4ecdc4, 0 0 14px rgba(78, 205, 196, 0.9); + } + + .destination { + font-weight: bold; + font-size: 1.4em; + margin: 10px 0; + } + + .direction { + color: #666; + } + + .details { + margin-top: 10px; + font-size: 0.9em; + color: #666; + } + + /* Weather widget styles */ + .weather-container { + margin: 20px 0; + border-radius: 8px; + overflow: hidden; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); + } + + body.dark-mode .weather-container { + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3); + } + + /* Custom weather display */ + #custom-weather { + padding: 8px; /* Reduced from 12px */ + background-color: rgba(30, 36, 50, 0.8); /* Semi-transparent background */ + color: white; + border-radius: 8px; + text-align: center; + } + + /* Ensure weather widget is visible in vertical-reverse mode */ + body.vertical-reverse #custom-weather { + width: auto; + max-width: none; + } + + #custom-weather .current-weather { + display: flex; + flex-direction: row; + align-items: center; + justify-content: center; + margin-bottom: 12px; + flex-wrap: wrap; + } + + #custom-weather .location-info { + display: flex; + align-items: center; + margin-right: 15px; + } + + #custom-weather h3 { + margin: 0; + margin-right: 8px; + font-size: 1em; /* Reduced from 1.1em */ + } + + #custom-weather .weather-icon { + display: flex; + align-items: center; + margin-right: 8px; /* Reduced from 10px */ + } + + #custom-weather .weather-icon img { + width: 48px; /* Reduced from 64px */ + height: 48px; + margin-right: 5px; + } + + /* Make sun icons RGB(255, 224, 0) - bright yellow */ + /* For clear sun (no clouds): apply full yellow filter */ + #custom-weather .weather-icon img.weather-clear-sun, + #custom-weather .weather-icon img[data-condition="Clear"], + #custom-weather .forecast-hour .icon img.weather-clear-sun, + #custom-weather .forecast-hour .icon img[data-condition="Clear"] { + filter: brightness(0) saturate(100%) invert(100%) sepia(100%) saturate(10000%) hue-rotate(0deg) brightness(1.12); + } + + /* For clouds with sun: preserve white clouds (#FFFFFF), color only sun parts yellow */ + /* Use brightness to preserve white areas, then selective saturation/hue for yellow sun */ + #custom-weather .weather-icon img.weather-clouds-sun, + #custom-weather .weather-icon img[data-condition*="Clouds"]:not([data-condition="Clear"]), + #custom-weather .forecast-hour .icon img.weather-clouds-sun, + #custom-weather .forecast-hour .icon img[data-condition*="Clouds"]:not([data-condition="Clear"]) { + /* High brightness preserves white clouds, saturation and hue-rotate color the sun yellow */ + filter: brightness(1.25) saturate(3) hue-rotate(-30deg) contrast(1.05); + } + + /* Make snow icons #F5F5F5 - light grey/white (RGB: 245, 245, 245) */ + /* Convert to light grey: brightness(0) -> black, invert(96%) -> #F5F5F5 equivalent */ + #custom-weather .weather-icon img[data-condition*="Snow"], + #custom-weather .weather-icon img.weather-snow, + #custom-weather .forecast-hour .icon img.weather-snow { + filter: brightness(0) saturate(0%) invert(96%); + } + + #custom-weather .temperature { + font-size: 1.5em; /* Reduced from 1.8em */ + font-weight: bold; + white-space: nowrap; + } + + #custom-weather .sun-times { + text-align: center; + margin-top: 6px; /* Reduced from 10px */ + font-size: 0.75em; /* Reduced from 0.85em */ + color: #aaa; + } + + /* Hourly forecast styles */ + #custom-weather .forecast { + display: flex; + justify-content: center; + overflow-x: auto; + padding-bottom: 6px; /* Reduced from 10px */ + margin-bottom: 6px; /* Reduced from 10px */ + scrollbar-width: thin; + scrollbar-color: #4fc3f7 #1e2432; + } + + #custom-weather .forecast::-webkit-scrollbar { + height: 6px; + } + + #custom-weather .forecast::-webkit-scrollbar-track { + background: #1e2432; + } + + #custom-weather .forecast::-webkit-scrollbar-thumb { + background-color: #4fc3f7; + border-radius: 6px; + } + + #custom-weather .forecast-hour { + text-align: center; + min-width: 50px; /* Reduced from 60px */ + margin-right: 6px; /* Reduced from 10px */ + flex-shrink: 0; + background-color: rgba(30, 40, 60, 0.5); + border: 1px solid #4fc3f7; + border-radius: 4px; /* Reduced from 6px */ + padding: 6px 4px; /* Reduced from 8px 5px */ + display: flex; + flex-direction: column; + align-items: center; + justify-content: space-between; + height: 90px; /* Reduced from 120px */ + } + + #custom-weather .forecast-hour:last-child { + margin-right: 0; + } + + #custom-weather .forecast-hour .time { + margin-bottom: 3px; /* Reduced from 5px */ + font-size: 0.75em; /* Reduced from 0.9em */ + font-weight: bold; + color: white; + display: block; + } + + #custom-weather .forecast-hour .icon { + margin: 3px 0; /* Reduced from 5px */ + } + + #custom-weather .forecast-hour .icon img { + width: 40px; /* Reduced from 56px */ + height: 40px; + } + + /* Sun icons already handled above - removing duplicate */ + + #custom-weather .forecast-hour .temp { + font-weight: bold; + font-size: 0.8em; /* Added size reduction */ + } + + #custom-weather .attribution { + margin-top: 10px; + font-size: 0.7em; + text-align: right; + } + + #custom-weather .attribution a { + color: #4fc3f7; + text-decoration: none; + } + + /* Sun times display in config */ + .sun-times { + margin-top: 5px; + color: #666; + } + + /* Background image settings */ + #background-image-url { + width: 100%; + padding: 8px; + border: 1px solid #ddd; + border-radius: 4px; + font-size: 1em; + margin-bottom: 10px; + } + + .background-preview { + width: 100%; + height: 120px; + border: 1px solid #ddd; + border-radius: 4px; + margin-top: 5px; + margin-bottom: 15px; + overflow: hidden; + display: flex; + align-items: center; + justify-content: center; + background-color: #f5f5f5; + } + + .background-preview img { + max-width: 100%; + max-height: 100%; + object-fit: contain; + } + + .background-preview .no-image { + color: #999; + font-style: italic; + } + + #background-opacity { + width: 100%; + margin-top: 5px; + } + + #opacity-value { + font-weight: normal; + color: #0077cc; + } + + .last-updated { + text-align: center; + font-size: 0.8em; + color: #666; + margin-top: 20px; + } + /* Site header styles */ + .site-header { + margin-bottom: 10px; /* Reduced to create more consistent spacing */ + position: relative; + } + + /* Site container styles */ + .site-container { + margin-bottom: 10px; /* Reduced to match other spacing */ + } + + .site-name { + display: inline-block; + background-color: white; + color: #0061a1; + font-weight: bold; + padding: 5px 15px; + border-radius: 4px; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); + position: relative; + z-index: 1; + } + + .error { + color: red; + text-align: center; + padding: 20px; + background-color: #ffeeee; + border-radius: 8px; + margin: 20px 0; + } diff --git a/public/css/main.css b/public/css/main.css new file mode 100644 index 0000000..dff081f --- /dev/null +++ b/public/css/main.css @@ -0,0 +1,349 @@ +/* Base styles */ + body { + font-family: Arial, sans-serif; + margin: 0 auto; + padding: 20px; + background-color: #f5f5f5; + color: #333; + transition: all 0.5s ease; + } + + /* Auto-apply landscape layout for wide screens */ + @media (min-width: 1200px) { + body:not(.vertical):not(.upsidedown):not(.vertical-reverse) { + max-width: 100%; + padding: 8px 12px 0 12px; /* Minimal padding to maximize space */ + padding-bottom: 0; + } + + body:not(.vertical):not(.upsidedown):not(.vertical-reverse) #content-wrapper { + display: grid; + grid-template-rows: auto 1fr auto; + gap: 8px; /* Reduced gap */ + height: 100vh; + max-height: 100vh; + overflow: hidden; + } + + body:not(.vertical):not(.upsidedown):not(.vertical-reverse) .clock-container { + grid-row: 1; + margin-bottom: 0; + padding: 6px 16px; /* Reduced padding */ + } + + body:not(.vertical):not(.upsidedown):not(.vertical-reverse) .main-content-grid { + grid-row: 2; + display: block; + overflow-y: auto; + overflow-x: hidden; + min-height: 0; + width: 100%; + } + + body:not(.vertical):not(.upsidedown):not(.vertical-reverse) .departure-container { + display: grid; + grid-template-columns: repeat(4, 1fr); /* Fixed 4 columns to use all space */ + gap: 6px; /* Minimal gap */ + margin-bottom: 0; + width: 100%; + box-sizing: border-box; + padding: 0; /* Remove any padding */ + } + + /* Ensure each column uses equal space */ + body:not(.vertical):not(.upsidedown):not(.vertical-reverse) .departure-container > * { + min-width: 0; /* Allow flex shrinking */ + max-width: 100%; /* Prevent overflow */ + } + + /* Weather fixed at bottom */ + body:not(.vertical):not(.upsidedown):not(.vertical-reverse) .weather-section { + grid-row: 3; + position: sticky; + bottom: 0; + background-color: inherit; + padding: 8px 0; /* Reduced padding */ + margin-top: 0; + } + + body:not(.vertical):not(.upsidedown):not(.vertical-reverse) .weather-container { + margin: 0; + max-width: 100%; + } + } + + /* Dark mode styles */ + body.dark-mode { + background-color: #222; + color: #f5f5f5; + } + + body.dark-mode .departure-card { + background-color: #333; + border-left-color: #0077cc; + } + + body.dark-mode .config-modal-content { + background-color: #333; + color: #f5f5f5; + } + + body.dark-mode .config-modal-body { + background-color: #333; + } + + body.dark-mode .config-modal-footer { + background-color: #444; + } + + body.dark-mode #config-cancel-button { + background-color: #555; + color: #f5f5f5; + } + + body.dark-mode .time, + body.dark-mode .destination { + color: #f5f5f5; + } + + body.dark-mode .direction, + body.dark-mode .details, + body.dark-mode .countdown, + body.dark-mode .last-updated { + color: #aaa; + } + + body.dark-mode h2 { + color: #0077cc; + } + + body.dark-mode .sun-times { + color: #aaa; + } + + body.dark-mode .line-number { + background-color: #0077cc; + } + + /* Normal orientation */ + body.normal { + max-width: 800px; + } + body.normal .departure-container { + display: grid; + grid-template-columns: 1fr; + gap: 10px; + } + + /* Landscape orientation - Optimized for wide screens */ + body.landscape { + max-width: 100%; + padding: 20px 40px; + } + + /* Main content area: clock at top, then two-column layout below */ + body.landscape #content-wrapper { + display: grid; + grid-template-rows: auto 1fr; + gap: 20px; + height: 100vh; + max-height: 100vh; + overflow: hidden; + } + + body.landscape .clock-container { + grid-row: 1; + margin-bottom: 0; + } + + /* Main content grid: departures on left, weather on right */ + body.landscape .main-content-grid { + grid-row: 2; + display: grid; + grid-template-columns: 1fr 380px; + gap: 20px; + overflow: hidden; + min-height: 0; + } + + /* Departures container: multi-column grid */ + body.landscape .departure-container { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(450px, 1fr)); + gap: 15px; + overflow-y: auto; + overflow-x: hidden; + padding-right: 10px; + min-height: 0; + } + + /* Weather container: fixed width, scrollable */ + body.landscape .weather-container { + overflow-y: auto; + overflow-x: hidden; + max-height: 100%; + position: sticky; + top: 0; + align-self: start; + } + + /* Better horizontal space usage in landscape */ + body.landscape .departure-card { + min-height: 120px; + } + + body.landscape .line-number-box { + min-width: 120px; + width: 120px; + } + + body.landscape .line-number-large { + font-size: 3.5em; + } + + /* Site containers in landscape should be more compact */ + body.landscape .site-container { + margin-bottom: 15px; + } + + body.landscape .site-header { + font-size: 1em; + padding: 8px 12px; + } + + /* Vertical orientation (90 degrees rotated) */ + body.vertical { + max-width: 100%; + height: 100vh; + padding: 0; + margin: 0; + overflow: hidden; + display: flex; + justify-content: center; + align-items: center; + } + + body.vertical #content-wrapper { + transform: rotate(90deg); + transform-origin: center center; + position: absolute; + width: 100vh; /* Use viewport height for width */ + height: 100vw; /* Use viewport width for height */ + max-width: 800px; /* Limit width for better readability */ + padding: 20px; + box-sizing: border-box; + overflow-y: auto; + background-color: transparent; /* Remove background color */ + left: 50%; + top: 50%; + margin-left: -50vh; /* Half of width */ + margin-top: -50vw; /* Half of height */ + } + + body.vertical .config-button { + transform: rotate(-90deg); + position: fixed; + right: 10px; + bottom: 10px; /* Changed from top to bottom */ + z-index: 1000; + } + + body.vertical .departure-container { + display: grid; + grid-template-columns: 1fr; + gap: 10px; + } + + /* Upside down orientation (180 degrees rotated) */ + body.upsidedown { + max-width: 100%; + height: 100vh; + padding: 0; + margin: 0; + overflow: hidden; + display: flex; + justify-content: center; + align-items: center; + } + + body.upsidedown #content-wrapper { + transform: rotate(180deg); + transform-origin: center center; + position: absolute; + width: 100%; + max-width: 800px; + padding: 20px; + box-sizing: border-box; + overflow-y: auto; + background-color: transparent; /* Remove background color */ + left: 50%; + top: 50%; + margin-left: -400px; /* Half of max-width */ + margin-top: -50vh; /* Half of viewport height */ + } + + body.upsidedown .config-button { + transform: rotate(-180deg); + position: fixed; + right: 10px; + bottom: 10px; /* Changed from top to bottom */ + z-index: 1000; + } + + body.upsidedown .departure-container { + display: grid; + grid-template-columns: 1fr; + gap: 10px; + } + + /* Vertical reverse orientation (270 degrees rotated) */ + body.vertical-reverse { + max-width: 100%; + height: 100vh; + padding: 0; + margin: 0; + overflow: hidden; + display: flex; + justify-content: center; + align-items: center; + } + + body.vertical-reverse #content-wrapper { + transform: rotate(270deg); + transform-origin: center center; + position: absolute; + width: 100vh; /* Use viewport height for width */ + height: 100vw; /* Use viewport width for height */ + max-width: none; /* Remove max-width limitation */ + padding: 20px; + box-sizing: border-box; + overflow: visible; /* Show all content */ + background-color: transparent; /* Remove background color to show background image */ + left: 50%; + top: 50%; + margin-left: -50vh; /* Half of width */ + margin-top: -50vw; /* Half of height */ + } + + body.vertical-reverse .config-button { + transform: rotate(-270deg); + position: fixed; + right: 10px; + bottom: 10px; /* Changed from top to bottom */ + z-index: 1000; + } + + body.vertical-reverse .departure-container { + display: grid; + grid-template-columns: 1fr; + gap: 10px; + width: 100%; /* Ensure full width */ + } + + /* Mode indicators - using a class instead of pseudo-element */ + .mode-indicator { + font-size: 0.7em; + color: #666; + font-weight: normal; + display: inline; + } diff --git a/clock.js b/public/js/components/Clock.js similarity index 100% rename from clock.js rename to public/js/components/Clock.js diff --git a/config.js b/public/js/components/ConfigManager.js similarity index 100% rename from config.js rename to public/js/components/ConfigManager.js diff --git a/public/js/components/DeparturesManager.js b/public/js/components/DeparturesManager.js new file mode 100644 index 0000000..f52147c --- /dev/null +++ b/public/js/components/DeparturesManager.js @@ -0,0 +1,636 @@ +/** + * DeparturesManager - Manages fetching and displaying transit departures + * Refactored from global functions to a class-based architecture + */ + +class DeparturesManager { + constructor(options = {}) { + this.options = { + apiUrl: options.apiUrl || (window.Constants ? `${window.Constants.API_BASE_URL}${window.Constants.ENDPOINTS.DEPARTURES}` : 'http://localhost:3002/api/departures'), + refreshInterval: options.refreshInterval || (window.Constants ? window.Constants.REFRESH.DEPARTURES : 5000), + containerId: options.containerId || 'departures', + statusId: options.statusId || 'status', + lastUpdatedId: options.lastUpdatedId || 'last-updated', + ...options + }; + + // DOM element references + this.container = null; + this.statusElement = null; + this.lastUpdatedElement = null; + + // State + this.currentDepartures = []; + this.refreshTimer = null; + + // Initialize + this.init(); + } + + /** + * Initialize the departures manager + */ + init() { + // Get DOM elements + this.container = document.getElementById(this.options.containerId); + this.statusElement = document.getElementById(this.options.statusId); + this.lastUpdatedElement = document.getElementById(this.options.lastUpdatedId); + + if (!this.container) { + if (window.logger) { + window.logger.error(`Departures container with ID "${this.options.containerId}" not found`); + } else { + console.error(`Departures container with ID "${this.options.containerId}" not found`); + } + return; + } + + // Initial fetch and setup + this.fetchDepartures(); + this.setupAutoRefresh(); + } + + /** + * Calculate minutes until arrival using expected time (or scheduled if expected not available) + * @param {Object} departure - Departure object + * @returns {number} Minutes until arrival + */ + calculateMinutesUntilArrival(departure) { + const now = new Date(); + const arrivalTime = departure.expected ? new Date(departure.expected) : new Date(departure.scheduled); + return Math.round((arrivalTime - now) / (1000 * 60)); + } + + /** + * Get the time to display (expected if available, otherwise scheduled) + * @param {Object} departure - Departure object + * @returns {string} ISO time string + */ + getDepartureTime(departure) { + return departure.expected || departure.scheduled; + } + + /** + * Get transport icon SVG based on transport mode + * @param {string} transportMode - Transport mode (bus, metro, train, tram, ship) + * @param {Object} line - Optional line object for special cases + * @returns {string} SVG icon HTML + */ + static getTransportIcon(transportMode, line = null) { + const mode = transportMode ? transportMode.toLowerCase() : 'bus'; + + // Special case for line 7 - it's a tram + if (line && line.designation === '7') { + return ''; + } + + const icons = { + bus: '', + metro: '', + train: '', + tram: '', + ship: '' + }; + + return icons[mode] || icons.bus; + } + + /** + * Format date and time + * @param {string} dateTimeString - ISO date string + * @returns {string} Formatted time string + */ + static formatDateTime(dateTimeString) { + const date = new Date(dateTimeString); + const locale = window.Constants?.DATE_FORMAT?.LOCALE || 'sv-SE'; + return date.toLocaleTimeString(locale, { hour: '2-digit', minute: '2-digit' }); + } + + /** + * Format relative time (e.g., "in 5 minutes") + * @param {string} dateTimeString - ISO date string + * @returns {string} Formatted relative time string + */ + static formatRelativeTime(dateTimeString) { + const departureTime = new Date(dateTimeString); + const now = new Date(); + const diffMinutes = Math.round((departureTime - now) / (1000 * 60)); + + if (diffMinutes <= 0) { + return 'Nu'; + } else if (diffMinutes === 1) { + return 'In 1 minute'; + } else if (diffMinutes < 60) { + return `In ${diffMinutes} minutes`; + } else { + const hours = Math.floor(diffMinutes / 60); + const minutes = diffMinutes % 60; + if (minutes === 0) { + return `In ${hours} hour${hours > 1 ? 's' : ''}`; + } else { + return `In ${hours} hour${hours > 1 ? 's' : ''} and ${minutes} minute${minutes > 1 ? 's' : ''}`; + } + } + } + + /** + * Get configuration from ConfigManager or return defaults + * @returns {Object} Configuration object + */ + getConfig() { + const defaultConfig = { + combineSameDirection: true, + sites: [ + { + id: window.Constants?.DEFAULT_SITE?.ID || '1411', + name: window.Constants?.DEFAULT_SITE?.NAME || 'Ambassaderna', + enabled: true + } + ] + }; + + if (window.configManager && window.configManager.config) { + return { + combineSameDirection: window.configManager.config.combineSameDirection !== undefined ? + window.configManager.config.combineSameDirection : defaultConfig.combineSameDirection, + sites: window.configManager.config.sites || defaultConfig.sites + }; + } + + return defaultConfig; + } + + /** + * Group departures by line number + * @param {Array} departures - Array of departure objects + * @returns {Array} Array of grouped departure objects + */ + groupDeparturesByLineNumber(departures) { + const groups = {}; + + departures.forEach(departure => { + const lineNumber = departure.line.designation; + + if (!groups[lineNumber]) { + groups[lineNumber] = { + line: departure.line, + directions: {} + }; + } + + const departureDirection = departure.direction_code !== undefined ? departure.direction_code : + departure.directionCode !== undefined ? departure.directionCode : + departure.direction !== undefined ? departure.direction : + 1; + + const directionKey = `${departureDirection}-${departure.destination}`; + + if (!groups[lineNumber].directions[directionKey]) { + groups[lineNumber].directions[directionKey] = { + direction: departureDirection, + destination: departure.destination, + departures: [] + }; + } + + groups[lineNumber].directions[directionKey].departures.push(departure); + }); + + return Object.entries(groups).map(([lineNumber, data]) => { + const directionsArray = Object.values(data.directions); + directionsArray.sort((a, b) => { + const dirA = a.direction || 1; + const dirB = b.direction || 1; + return dirA - dirB; + }); + + return { + lineNumber: lineNumber, + line: data.line, + directions: directionsArray + }; + }); + } + + /** + * Determine countdown text and class based on minutes until arrival + * @param {Object} departure - Departure object + * @returns {Object} Object with countdownText and countdownClass + */ + getCountdownInfo(departure) { + const displayTime = departure.display; + const minutesUntil = this.calculateMinutesUntilArrival(departure); + let countdownText = displayTime; + let countdownClass = ''; + + const urgentThreshold = window.Constants?.TIME_THRESHOLDS?.URGENT || 5; + + if (minutesUntil <= 0 || displayTime === 'Nu' || displayTime.toLowerCase() === 'nu') { + countdownText = 'Nu'; + countdownClass = 'now'; + } else if (minutesUntil < urgentThreshold) { + const minMatch = displayTime.match(/(\d+)\s*min/i); + if (minMatch) { + countdownText = minutesUntil === 1 ? '1 min' : `${minutesUntil} min`; + } else { + countdownText = minutesUntil === 1 ? '1 min' : `${minutesUntil} min`; + } + countdownClass = 'urgent'; + } else { + const isTimeOnly = /^\d{1,2}:\d{2}$/.test(displayTime); + if (isTimeOnly) { + countdownText = `${minutesUntil} min`; + } else { + countdownText = displayTime; + } + } + + return { countdownText, countdownClass }; + } + + /** + * Create a departure card element (legacy format) + * @param {Object} departure - Departure object + * @returns {HTMLElement} Departure card element + */ + createDepartureCard(departure) { + const departureCard = document.createElement('div'); + departureCard.dataset.journeyId = departure.journey.id; + + const displayTime = departure.display; + const departureTime = this.getDepartureTime(departure); + const timeDisplay = DeparturesManager.formatDateTime(departureTime); + + const departureTimeDate = new Date(departureTime); + const now = new Date(); + const diffMinutes = Math.round((departureTimeDate - now) / (1000 * 60)); + const withinHourThreshold = window.Constants?.TIME_THRESHOLDS?.WITHIN_HOUR || 60; + const isWithinNextHour = diffMinutes <= withinHourThreshold; + + departureCard.className = isWithinNextHour ? 'departure-card condensed' : 'departure-card'; + + const { countdownText, countdownClass } = this.getCountdownInfo(departure); + const transportIcon = DeparturesManager.getTransportIcon(departure.line?.transportMode, departure.line); + + departureCard.innerHTML = ` +
+ + ${transportIcon} + ${departure.line.designation} + ${departure.destination} + + + ${timeDisplay} + (${countdownText}) + +
+ `; + + return departureCard; + } + + /** + * Display grouped departures by line (new card-based layout) + * @param {Array} groups - Array of grouped departure objects + * @param {HTMLElement} container - Container element + */ + displayGroupedDeparturesByLine(groups, container) { + groups.forEach(group => { + const groupCard = document.createElement('div'); + groupCard.className = 'departure-card line-card'; + + const apiTransportMode = group.line?.transportMode || ''; + const transportMode = apiTransportMode.toLowerCase(); + + const lineNumberBox = document.createElement('div'); + lineNumberBox.className = `line-number-box ${transportMode}`; + + const transportIcon = DeparturesManager.getTransportIcon(group.line?.transportMode, group.line); + + lineNumberBox.innerHTML = ` +
${transportIcon}
+
${group.lineNumber}
+ `; + groupCard.appendChild(lineNumberBox); + + const directionsWrapper = document.createElement('div'); + directionsWrapper.className = 'directions-wrapper'; + + const maxDirections = 2; + group.directions.slice(0, maxDirections).forEach(direction => { + direction.departures.sort((a, b) => { + const timeA = this.getDepartureTime(a); + const timeB = this.getDepartureTime(b); + return new Date(timeA) - new Date(timeB); + }); + + const directionRow = document.createElement('div'); + directionRow.className = 'direction-row'; + + const directionInfo = document.createElement('div'); + directionInfo.className = 'direction-info'; + + const firstDep = direction.departures[0]; + if (!firstDep) return; + + const directionCode = firstDep.direction_code !== undefined ? firstDep.direction_code : + firstDep.directionCode !== undefined ? firstDep.directionCode : + null; + + const isRight = directionCode === 2; + + if (directionCode === null || directionCode === undefined) { + if (window.logger) { + window.logger.warn('No direction_code found for:', direction.destination, firstDep); + } else { + console.warn('No direction_code found for:', direction.destination, firstDep); + } + } + + const arrowBox = document.createElement('div'); + arrowBox.className = `direction-arrow-box ${isRight ? 'right' : 'left'}`; + arrowBox.textContent = isRight ? '→' : '←'; + + const destinationSpan = document.createElement('span'); + destinationSpan.className = 'direction-destination'; + destinationSpan.textContent = direction.destination; + + directionInfo.appendChild(arrowBox); + directionInfo.appendChild(destinationSpan); + directionRow.appendChild(directionInfo); + + const timesContainer = document.createElement('div'); + timesContainer.className = 'times-container'; + + const firstDeparture = direction.departures[0]; + const secondDeparture = direction.departures[1]; + + if (firstDeparture) { + const displayTime = firstDeparture.display; + const departureTime = this.getDepartureTime(firstDeparture); + const timeDisplay = DeparturesManager.formatDateTime(departureTime); + + const { countdownText, countdownClass } = this.getCountdownInfo(firstDeparture); + + const timeDisplayElement = document.createElement('div'); + timeDisplayElement.className = 'time-display'; + + const countdownSpan = document.createElement('span'); + countdownSpan.className = `countdown-large ${countdownClass}`; + countdownSpan.textContent = countdownText; + + timeDisplayElement.appendChild(countdownSpan); + + const timeRangeSpan = document.createElement('span'); + timeRangeSpan.className = 'time-range'; + if (secondDeparture) { + const secondTime = DeparturesManager.formatDateTime(this.getDepartureTime(secondDeparture)); + timeRangeSpan.textContent = `${timeDisplay} - ${secondTime}`; + } else { + timeRangeSpan.textContent = timeDisplay; + } + timeDisplayElement.appendChild(timeRangeSpan); + + timesContainer.appendChild(timeDisplayElement); + } + + directionRow.appendChild(timesContainer); + directionsWrapper.appendChild(directionRow); + }); + + groupCard.appendChild(directionsWrapper); + container.appendChild(groupCard); + }); + } + + /** + * Display departures in the UI + * @param {Array} departures - Array of departure objects + */ + displayDepartures(departures) { + if (!departures || departures.length === 0) { + this.container.innerHTML = '
No departures found
'; + return; + } + + if (this.currentDepartures.length === 0) { + this.container.innerHTML = ''; + + departures.forEach(departure => { + const departureCard = this.createDepartureCard(departure); + this.container.appendChild(departureCard); + }); + } else { + this.updateExistingCards(departures); + } + + this.currentDepartures = JSON.parse(JSON.stringify(departures)); + } + + /** + * Update existing cards or add new ones + * @param {Array} newDepartures - Array of new departure objects + */ + updateExistingCards(newDepartures) { + const currentCards = this.container.querySelectorAll('.departure-card'); + const currentCardIds = Array.from(currentCards).map(card => card.dataset.journeyId); + + newDepartures.forEach((departure, index) => { + const journeyId = departure.journey.id; + const existingCardIndex = currentCardIds.indexOf(journeyId.toString()); + + if (existingCardIndex !== -1) { + const existingCard = currentCards[existingCardIndex]; + this.updateCardContent(existingCard, departure); + } else { + const newCard = this.createDepartureCard(departure); + newCard.style.opacity = '0'; + + if (index === 0) { + this.container.prepend(newCard); + } else if (index >= this.container.children.length) { + this.container.appendChild(newCard); + } else { + this.container.insertBefore(newCard, this.container.children[index]); + } + + setTimeout(() => { + newCard.style.transition = 'opacity 0.5s ease-in'; + newCard.style.opacity = '1'; + }, 10); + } + }); + + const newDepartureIds = newDepartures.map(d => d.journey.id.toString()); + currentCards.forEach(card => { + if (!newDepartureIds.includes(card.dataset.journeyId)) { + card.style.transition = 'opacity 0.5s ease-out'; + card.style.opacity = '0'; + setTimeout(() => { + if (card.parentNode) { + card.parentNode.removeChild(card); + } + }, 500); + } + }); + } + + /** + * Update card content + * @param {HTMLElement} card - Card element + * @param {Object} departure - Departure object + */ + updateCardContent(card, departure) { + const { countdownText, countdownClass } = this.getCountdownInfo(departure); + const countdownElement = card.querySelector('.countdown'); + + if (countdownElement) { + countdownElement.classList.remove('now', 'urgent'); + + if (countdownClass === 'now') { + countdownElement.classList.add('now'); + } else if (countdownClass === 'urgent') { + countdownElement.classList.add('urgent'); + } + + if (countdownElement.textContent !== `(${countdownText})`) { + countdownElement.textContent = `(${countdownText})`; + this.highlightElement(countdownElement); + } + } + } + + /** + * Add highlight effect to element + * @param {HTMLElement} element - Element to highlight + */ + highlightElement(element) { + element.style.transition = 'none'; + element.style.backgroundColor = 'rgba(255, 255, 0, 0.3)'; + + setTimeout(() => { + element.style.transition = 'background-color 1.5s ease-out'; + element.style.backgroundColor = 'transparent'; + }, 10); + } + + /** + * Display multiple sites + * @param {Array} sites - Array of site objects + */ + displayMultipleSites(sites) { + const config = this.getConfig(); + const enabledSites = config.sites.filter(site => site.enabled); + + this.container.innerHTML = ''; + + sites.forEach(site => { + const siteConfig = enabledSites.find(s => s.id === site.siteId); + if (!siteConfig) return; + + const siteContainer = document.createElement('div'); + siteContainer.className = 'site-container'; + + const siteHeader = document.createElement('div'); + siteHeader.className = 'site-header'; + siteHeader.innerHTML = `${site.siteName || siteConfig.name}`; + siteContainer.appendChild(siteHeader); + + if (site.data && site.data.departures) { + const lineGroups = this.groupDeparturesByLineNumber(site.data.departures); + this.displayGroupedDeparturesByLine(lineGroups, siteContainer); + } else if (site.error) { + const errorElement = document.createElement('div'); + errorElement.className = 'error'; + errorElement.textContent = `Error loading departures for ${site.siteName}: ${site.error}`; + siteContainer.appendChild(errorElement); + } + + this.container.appendChild(siteContainer); + }); + } + + /** + * Fetch departures from API + */ + async fetchDepartures() { + try { + const response = await fetch(this.options.apiUrl); + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + + const data = await response.json(); + + if (data.sites && Array.isArray(data.sites)) { + this.displayMultipleSites(data.sites); + + if (this.lastUpdatedElement) { + const now = new Date(); + const locale = window.Constants?.DATE_FORMAT?.LOCALE || 'sv-SE'; + this.lastUpdatedElement.textContent = `Last updated: ${now.toLocaleTimeString(locale)}`; + } + } else if (data.departures) { + this.displayDepartures(data.departures); + + if (this.lastUpdatedElement) { + const now = new Date(); + const locale = window.Constants?.DATE_FORMAT?.LOCALE || 'sv-SE'; + this.lastUpdatedElement.textContent = `Last updated: ${now.toLocaleTimeString(locale)}`; + } + } else if (data.error) { + throw new Error(data.error); + } else { + throw new Error('Invalid response format from server'); + } + + } catch (error) { + if (window.logger) { + window.logger.error('Error fetching departures:', error); + } else { + console.error('Error fetching departures:', error); + } + + this.container.innerHTML = ` +
+

Failed to load departures. Please try again later.

+

Error: ${error.message}

+

Make sure the Node.js server is running: node server.js

+
+ `; + } + } + + /** + * Set up auto-refresh timer + */ + setupAutoRefresh() { + if (this.refreshTimer) { + clearInterval(this.refreshTimer); + } + + this.refreshTimer = setInterval(() => this.fetchDepartures(), this.options.refreshInterval); + } + + /** + * Stop auto-refresh + */ + stop() { + if (this.refreshTimer) { + clearInterval(this.refreshTimer); + this.refreshTimer = null; + } + } + + /** + * Restart auto-refresh with new interval + * @param {number} interval - New interval in milliseconds + */ + setRefreshInterval(interval) { + this.options.refreshInterval = interval; + this.setupAutoRefresh(); + } +} + +// Export the class +window.DeparturesManager = DeparturesManager; diff --git a/weather.js b/public/js/components/WeatherManager.js similarity index 97% rename from weather.js rename to public/js/components/WeatherManager.js index 637d834..9f2b81a 100644 --- a/weather.js +++ b/public/js/components/WeatherManager.js @@ -7,11 +7,14 @@ class WeatherManager { constructor(options = {}) { // Default options + // Get API key from options, window (injected by server from .env), or fallback + const apiKey = options.apiKey || window.OPENWEATHERMAP_API_KEY || '4d8fb5b93d4af21d66a2948710284366'; + this.options = { - latitude: 59.3293, // Stockholm latitude - longitude: 18.0686, // Stockholm longitude - apiKey: options.apiKey || '4d8fb5b93d4af21d66a2948710284366', // OpenWeatherMap API key - refreshInterval: 30 * 60 * 1000, // 30 minutes in milliseconds + latitude: options.latitude || (window.DEFAULT_LOCATION?.latitude) || 59.3293, // Stockholm latitude + longitude: options.longitude || (window.DEFAULT_LOCATION?.longitude) || 18.0686, // Stockholm longitude + apiKey: apiKey, // OpenWeatherMap API key (from .env via server injection, or fallback) + refreshInterval: options.refreshInterval || 30 * 60 * 1000, // 30 minutes in milliseconds ...options }; diff --git a/public/js/main.js b/public/js/main.js new file mode 100644 index 0000000..3c06ac9 --- /dev/null +++ b/public/js/main.js @@ -0,0 +1,122 @@ +/** + * Main application entry point + * Initializes all components when the DOM is ready + */ + +/** + * Function to ensure content wrapper exists for rotated orientations + */ +function ensureContentWrapper() { + if (!document.getElementById('content-wrapper')) { + if (window.logger) { + window.logger.info('Creating content wrapper'); + } else { + console.log('Creating content wrapper'); + } + const wrapper = document.createElement('div'); + wrapper.id = 'content-wrapper'; + + // Move all body children to the wrapper except excluded elements + const excludedElements = ['config-button', 'config-modal', 'background-overlay']; + + // Create an array of nodes to move (can't modify while iterating) + const nodesToMove = []; + for (let i = 0; i < document.body.children.length; i++) { + const child = document.body.children[i]; + if (!excludedElements.includes(child.id) && child.id !== 'content-wrapper') { + nodesToMove.push(child); + } + } + + // Move the nodes to the wrapper + nodesToMove.forEach(node => { + wrapper.appendChild(node); + }); + + // Add the wrapper back to the body + document.body.appendChild(wrapper); + } +} + +// Initialize components when the DOM is loaded +document.addEventListener('DOMContentLoaded', async function() { + if (window.logger) { + window.logger.info('DOM fully loaded'); + } else { + console.log('DOM fully loaded'); + } + + try { + // Initialize ConfigManager first + if (window.logger) { + window.logger.info('Creating ConfigManager...'); + } else { + console.log('Creating ConfigManager...'); + } + window.configManager = new ConfigManager({ + defaultOrientation: 'normal', + defaultDarkMode: 'auto' + }); + + // Note: ConfigManager already creates the config button and modal + + // Initialize Clock + const timezone = window.Constants?.TIMEZONE || 'Europe/Stockholm'; + window.clock = new Clock({ + elementId: 'clock', + timezone: timezone + }); + + // Initialize WeatherManager with location from window config or constants + const defaultLat = window.DEFAULT_LOCATION?.latitude || + (window.Constants?.DEFAULT_LOCATION?.LATITUDE) || 59.3293; + const defaultLon = window.DEFAULT_LOCATION?.longitude || + (window.Constants?.DEFAULT_LOCATION?.LONGITUDE) || 18.0686; + window.weatherManager = new WeatherManager({ + latitude: defaultLat, + longitude: defaultLon + }); + + // Initialize departures - use DeparturesManager + if (typeof DeparturesManager !== 'undefined') { + window.departuresManager = new DeparturesManager({ + containerId: 'departures', + statusId: 'status', + lastUpdatedId: 'last-updated' + }); + } else if (typeof initDepartures === 'function') { + // Fallback to legacy function if DeparturesManager not available + initDepartures(); + } + + // Set up event listeners + document.addEventListener('darkModeChanged', event => { + document.body.classList.toggle('dark-mode', event.detail.isDarkMode); + }); + + document.addEventListener('configChanged', event => { + if (['vertical', 'upsidedown', 'vertical-reverse'].includes(event.detail.config.orientation)) { + ensureContentWrapper(); + } + }); + + // Ensure content wrapper exists initially + ensureContentWrapper(); + + if (window.logger) { + window.logger.info('All components initialized successfully'); + } else { + console.log('All components initialized successfully'); + } + } catch (error) { + if (window.logger) { + window.logger.error('Error during initialization:', error); + } else { + console.error('Error during initialization:', error); + } + const errorDiv = document.createElement('div'); + errorDiv.className = 'error'; + errorDiv.textContent = `Initialization error: ${error.message}`; + document.body.appendChild(errorDiv); + } +}); diff --git a/public/js/utils/constants.js b/public/js/utils/constants.js new file mode 100644 index 0000000..b16daf7 --- /dev/null +++ b/public/js/utils/constants.js @@ -0,0 +1,89 @@ +/** + * Application constants and configuration values + * Centralizes magic numbers, API endpoints, and default settings + */ + +const Constants = { + // Server configuration + PORT: 3002, + API_BASE_URL: 'http://localhost:3002', + + // API Endpoints + ENDPOINTS: { + DEPARTURES: '/api/departures', + SITES_SEARCH: '/api/sites/search', + SITES_NEARBY: '/api/sites/nearby', + CONFIG_UPDATE: '/api/config/update' + }, + + // External API URLs + EXTERNAL_APIS: { + SL_TRANSPORT_BASE: 'https://transport.integration.sl.se/v1', + OPENWEATHERMAP_BASE: 'https://api.openweathermap.org/data/2.5' + }, + + // Refresh intervals (in milliseconds) + REFRESH: { + DEPARTURES: 5000, // 5 seconds + WEATHER: 30 * 60 * 1000, // 30 minutes + DARK_MODE_CHECK: 60000 // 1 minute + }, + + // Default location (Stockholm, Sweden) + DEFAULT_LOCATION: { + LATITUDE: 59.3293, + LONGITUDE: 18.0686, + NAME: 'Stockholm' + }, + + // Default site configuration + DEFAULT_SITE: { + ID: '1411', + NAME: 'Ambassaderna' + }, + + // Default search radius (in meters) + DEFAULT_SEARCH_RADIUS: 5000, // 5km + + // Transport mode identifiers + TRANSPORT_MODES: { + BUS: 'bus', + METRO: 'metro', + TRAIN: 'train', + TRAM: 'tram', + SHIP: 'ship' + }, + + // CSS class names (for consistency) + CSS_CLASSES: { + DARK_MODE: 'dark-mode', + ORIENTATION_NORMAL: 'normal', + ORIENTATION_LANDSCAPE: 'landscape', + ORIENTATION_VERTICAL: 'vertical', + ORIENTATION_UPSIDE_DOWN: 'upsidedown', + ORIENTATION_VERTICAL_REVERSE: 'vertical-reverse' + }, + + // Time thresholds (in minutes) + TIME_THRESHOLDS: { + URGENT: 5, // Less than 5 minutes is urgent (red) + WITHIN_HOUR: 60 // Within next hour + }, + + // LocalStorage keys + STORAGE_KEYS: { + CONFIG: 'sl-departures-config' + }, + + // Timezone + TIMEZONE: 'Europe/Stockholm', + + // Date/time formats + DATE_FORMAT: { + TIMEZONE: 'Europe/Stockholm', + LOCALE: 'sv-SE' + } +}; + +// Export constants +window.Constants = Constants; diff --git a/public/js/utils/logger.js b/public/js/utils/logger.js new file mode 100644 index 0000000..a31f533 --- /dev/null +++ b/public/js/utils/logger.js @@ -0,0 +1,100 @@ +/** + * Logger utility for consistent logging throughout the application + * Supports different log levels and can be configured for production vs development + */ + +class Logger { + constructor() { + this.level = this.getLogLevel(); + } + + /** + * Get the current log level from environment or default to 'info' + * @returns {string} Log level ('debug', 'info', 'warn', 'error') + */ + getLogLevel() { + // In production, you might want to check window.location.hostname + // or a global config. For now, default to 'info' + if (typeof window !== 'undefined' && window.LOG_LEVEL) { + return window.LOG_LEVEL; + } + // Default to 'info' level + return 'info'; + } + + /** + * Check if a log level should be output + * @param {string} level - Log level to check + * @returns {boolean} Whether to output this level + */ + shouldLog(level) { + const levels = ['debug', 'info', 'warn', 'error']; + const currentLevelIndex = levels.indexOf(this.level); + const messageLevelIndex = levels.indexOf(level); + return messageLevelIndex >= currentLevelIndex; + } + + /** + * Format a log message with timestamp + * @param {string} level - Log level + * @param {string} message - Message to log + * @param {any[]} args - Additional arguments + * @returns {string} Formatted message + */ + formatMessage(level, message, args = []) { + const timestamp = new Date().toISOString(); + const prefix = `[${timestamp}] [${level.toUpperCase()}]`; + return args.length > 0 ? [prefix, message, ...args] : [prefix, message]; + } + + /** + * Log a debug message + * @param {string} message - Message to log + * @param {...any} args - Additional arguments + */ + debug(message, ...args) { + if (this.shouldLog('debug')) { + console.debug(...this.formatMessage('debug', message, args)); + } + } + + /** + * Log an info message + * @param {string} message - Message to log + * @param {...any} args - Additional arguments + */ + info(message, ...args) { + if (this.shouldLog('info')) { + console.info(...this.formatMessage('info', message, args)); + } + } + + /** + * Log a warning message + * @param {string} message - Message to log + * @param {...any} args - Additional arguments + */ + warn(message, ...args) { + if (this.shouldLog('warn')) { + console.warn(...this.formatMessage('warn', message, args)); + } + } + + /** + * Log an error message + * @param {string} message - Message to log + * @param {...any} args - Additional arguments + */ + error(message, ...args) { + if (this.shouldLog('error')) { + console.error(...this.formatMessage('error', message, args)); + } + } +} + +// Create a singleton instance +const logger = new Logger(); + +// Export both the class and the singleton instance +window.Logger = Logger; +window.logger = logger; diff --git a/raspberry-pi-setup.sh b/scripts/raspberry-pi-setup.sh similarity index 100% rename from raspberry-pi-setup.sh rename to scripts/raspberry-pi-setup.sh diff --git a/server.js b/server.js index 0464f19..9234fbe 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,17 @@ -const http = require('http'); -const https = require('https'); -const url = require('url'); +// Load environment variables +require('dotenv').config(); -const PORT = 3002; +const http = require('http'); +const url = require('url'); +const fs = require('fs'); +const path = require('path'); + +// Route handlers +const departuresRouter = require('./server/routes/departures'); +const sitesRouter = require('./server/routes/sites'); +const configRouter = require('./server/routes/config'); + +const PORT = process.env.PORT || 3002; // Default configuration let config = { @@ -19,8 +28,9 @@ let config = { function loadSitesConfig() { try { const fs = require('fs'); - if (fs.existsSync('sites-config.json')) { - const configData = fs.readFileSync('sites-config.json', 'utf8'); + const configPath = path.join('config', 'sites.json'); + if (fs.existsSync(configPath)) { + const configData = fs.readFileSync(configPath, 'utf8'); const loadedConfig = JSON.parse(configData); // Handle old format (array of sites) @@ -40,92 +50,6 @@ function loadSitesConfig() { // Load configuration on startup loadSitesConfig(); -// Function to fetch data from the SL Transport API for a specific site -function fetchDeparturesForSite(siteId) { - return new Promise((resolve, reject) => { - const apiUrl = `https://transport.integration.sl.se/v1/sites/${siteId}/departures`; - console.log(`Fetching data from: ${apiUrl}`); - - https.get(apiUrl, (res) => { - let data = ''; - - res.on('data', (chunk) => { - data += chunk; - }); - - res.on('end', () => { - console.log('Raw API response:', data.substring(0, 200) + '...'); - - try { - try { - const parsedData = JSON.parse(data); - console.log('Successfully parsed as regular JSON'); - resolve(parsedData); - return; - } catch (jsonError) { - console.log('Not valid JSON, trying to fix format...'); - } - - if (data.startsWith('departures":')) { - data = '{' + data; - } else if (data.includes('departures":')) { - const startIndex = data.indexOf('departures":'); - if (startIndex > 0) { - data = '{' + data.substring(startIndex); - } - } - - data = data.replace(/}{\s*"/g, '},{"'); - data = data.replace(/"([^"]+)":\s*([^,{}\[\]]+)(?=")/g, '"$1": $2,'); - data = data.replace(/,\s*}/g, '}').replace(/,\s*\]/g, ']'); - - try { - const parsedData = JSON.parse(data); - console.log('Successfully parsed fixed JSON'); - - if (parsedData && parsedData.departures && parsedData.departures.length > 0) { - // Log first departure to see structure - console.log('Sample departure structure:', JSON.stringify(parsedData.departures[0], null, 2)); - - // Check for direction-related fields - const sample = parsedData.departures[0]; - console.log('Direction fields:', { - direction: sample.direction, - directionText: sample.directionText, - directionCode: sample.directionCode, - journeyDirection: sample.journey?.direction, - stopPoint: sample.stopPoint - }); - } - - resolve(parsedData); - } catch (parseError) { - console.error('Error parsing fixed JSON:', parseError); - resolve({ - departures: [], - error: 'Failed to parse API response: ' + parseError.message, - rawResponse: data.substring(0, 500) + '...' - }); - } - } catch (error) { - console.error('Error processing API response:', error); - resolve({ - departures: [], - error: 'Error processing API response: ' + error.message, - rawResponse: data.substring(0, 500) + '...' - }); - } - }); - }).on('error', (error) => { - console.error('Error fetching data from API:', error); - resolve({ - departures: [], - error: 'Error fetching data from API: ' + error.message - }); - }); - }); -} - // Create HTTP server const server = http.createServer(async (req, res) => { const parsedUrl = url.parse(req.url, true); @@ -139,302 +63,88 @@ const server = http.createServer(async (req, res) => { res.end(); return; } - - // Function to fetch data from all enabled sites - async function fetchAllDepartures() { - const enabledSites = config.sites.filter(site => site.enabled); - - if (enabledSites.length === 0) { - return { sites: [], error: 'No enabled sites configured' }; - } - - try { - const sitesPromises = enabledSites.map(async (site) => { - try { - const departureData = await fetchDeparturesForSite(site.id); - return { - siteId: site.id, - siteName: site.name, - data: departureData - }; - } catch (error) { - console.error(`Error fetching departures for site ${site.id}:`, error); - return { - siteId: site.id, - siteName: site.name, - error: error.message - }; - } - }); - - const results = await Promise.all(sitesPromises); - return { sites: results }; - } catch (error) { - console.error('Error fetching all departures:', error); - return { sites: [], error: error.message }; - } - } - // Handle API endpoints + // Handle API endpoints - use route handlers if (parsedUrl.pathname === '/api/departures') { - try { - const data = await fetchAllDepartures(); - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify(data)); - } catch (error) { - console.error('Error handling departures request:', error); - res.writeHead(500, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: error.message })); - } + await departuresRouter.handleDepartures(req, res, config); } else if (parsedUrl.pathname === '/api/sites/search') { - // Search for transit sites - const query = parsedUrl.query.q; - if (!query || query.length < 2) { - res.writeHead(400, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Query must be at least 2 characters' })); - return; - } - - const searchUrl = `https://transport.integration.sl.se/v1/sites?q=${encodeURIComponent(query)}`; - console.log(`Searching sites: ${searchUrl}`); - - https.get(searchUrl, (apiRes) => { - let data = ''; - - // Check for HTTP errors - if (apiRes.statusCode < 200 || apiRes.statusCode >= 300) { - console.error(`API returned status code: ${apiRes.statusCode}`); - res.writeHead(500, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: `API returned status ${apiRes.statusCode}`, sites: [] })); - return; - } - - apiRes.on('data', (chunk) => { - data += chunk; - }); - - apiRes.on('end', () => { - try { - console.log('Raw API response:', data.substring(0, 500)); - const parsedData = JSON.parse(data); - console.log('Parsed data:', JSON.stringify(parsedData).substring(0, 500)); - - // Handle different possible response formats - let sites = []; - - if (Array.isArray(parsedData)) { - // Response is directly an array - sites = parsedData.map(site => ({ - id: String(site.id || site.siteId || site.SiteId || ''), - name: site.name || site.siteName || site.Name || site.StopPointName || 'Unknown', - lat: site.lat || site.latitude || site.Lat || site.Latitude || null, - lon: site.lon || site.longitude || site.Lon || site.Longitude || null - })); - } else if (parsedData.sites && Array.isArray(parsedData.sites)) { - // Response has a sites property - sites = parsedData.sites.map(site => ({ - id: String(site.id || site.siteId || site.SiteId || ''), - name: site.name || site.siteName || site.Name || site.StopPointName || 'Unknown', - lat: site.lat || site.latitude || site.Lat || site.Latitude || null, - lon: site.lon || site.longitude || site.Lon || site.Longitude || null - })); - } else if (parsedData.ResponseData && parsedData.ResponseData.Result) { - // SL API format with ResponseData - sites = parsedData.ResponseData.Result.map(site => ({ - id: String(site.SiteId || site.id || ''), - name: site.Name || site.name || site.StopPointName || 'Unknown', - lat: site.Lat || site.lat || site.Latitude || site.latitude || null, - lon: site.Lon || site.lon || site.Longitude || site.longitude || null - })); - } else { - console.log('Unexpected response format:', parsedData); - sites = []; - } - - // Log sample site to see structure - if (sites.length > 0) { - console.log('Sample site structure:', JSON.stringify(sites[0], null, 2)); - const sitesWithCoords = sites.filter(s => s.lat && s.lon); - console.log(`Found ${sites.length} sites, ${sitesWithCoords.length} with coordinates`); - } else { - console.log('No sites found'); - } - - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ sites })); - } catch (error) { - console.error('Error parsing site search response:', error); - console.error('Response data:', data.substring(0, 500)); - res.writeHead(500, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Error parsing search results', details: error.message, sites: [] })); - } - }); - }).on('error', (error) => { - console.error('Error searching sites:', error); - res.writeHead(500, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Error searching sites', details: error.message, sites: [] })); - }); + sitesRouter.handleSiteSearch(req, res, parsedUrl); } else if (parsedUrl.pathname === '/api/sites/nearby') { - // Get nearby transit sites based on coordinates - const lat = parseFloat(parsedUrl.query.lat); - const lon = parseFloat(parsedUrl.query.lon); - const radius = parseInt(parsedUrl.query.radius) || 5000; // Default 5km radius - - if (isNaN(lat) || isNaN(lon)) { - res.writeHead(400, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Invalid latitude or longitude', sites: [] })); - return; - } - - // Use a broader search to get sites, then filter by distance - // For now, we'll search for common Stockholm area terms and filter - const searchTerms = ['Stockholm', 'T-Centralen', 'Gamla Stan', 'Södermalm']; - const allSites = []; - let completedSearches = 0; - - searchTerms.forEach(term => { - const searchUrl = `https://transport.integration.sl.se/v1/sites?q=${encodeURIComponent(term)}`; - - https.get(searchUrl, (apiRes) => { - let data = ''; - - apiRes.on('data', (chunk) => { - data += chunk; - }); - - apiRes.on('end', () => { - try { - const parsedData = JSON.parse(data); - let sites = []; - - if (Array.isArray(parsedData)) { - sites = parsedData; - } else if (parsedData.sites) { - sites = parsedData.sites; - } else if (parsedData.ResponseData && parsedData.ResponseData.Result) { - sites = parsedData.ResponseData.Result; - } - - sites.forEach(site => { - const siteLat = site.lat || site.latitude || site.Lat || site.Latitude; - const siteLon = site.lon || site.longitude || site.Lon || site.Longitude; - - if (siteLat && siteLon) { - // Calculate distance (simple haversine approximation) - const distance = Math.sqrt( - Math.pow((lat - siteLat) * 111000, 2) + - Math.pow((lon - siteLon) * 111000 * Math.cos(lat * Math.PI / 180), 2) - ); - - if (distance <= radius) { - allSites.push({ - id: String(site.id || site.siteId || site.SiteId || ''), - name: site.name || site.siteName || site.Name || site.StopPointName || 'Unknown', - lat: siteLat, - lon: siteLon - }); - } - } - }); - - completedSearches++; - if (completedSearches === searchTerms.length) { - // Remove duplicates - const uniqueSites = Array.from(new Map(allSites.map(s => [s.id, s])).values()); - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ sites: uniqueSites })); - } - } catch (error) { - completedSearches++; - if (completedSearches === searchTerms.length) { - const uniqueSites = Array.from(new Map(allSites.map(s => [s.id, s])).values()); - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ sites: uniqueSites })); - } - } - }); - }).on('error', () => { - completedSearches++; - if (completedSearches === searchTerms.length) { - const uniqueSites = Array.from(new Map(allSites.map(s => [s.id, s])).values()); - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ sites: uniqueSites })); - } - }); - }); + sitesRouter.handleNearbySites(req, res, parsedUrl); } else if (parsedUrl.pathname === '/api/config') { - // Return the current configuration - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify(config)); + configRouter.handleGetConfig(req, res, config); } else if (parsedUrl.pathname === '/api/config/update' && req.method === 'POST') { - // Update configuration - let body = ''; - req.on('data', chunk => { - body += chunk.toString(); - }); - - req.on('end', () => { - try { - const newConfig = JSON.parse(body); - if (newConfig.sites) { - config = newConfig; - - // Save to file - const fs = require('fs'); - fs.writeFileSync('sites-config.json', JSON.stringify(config, null, 2)); - - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ success: true, message: 'Configuration updated' })); - } else { - throw new Error('Invalid configuration format'); - } - } catch (error) { - console.error('Error updating configuration:', error); - res.writeHead(400, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: error.message })); - } - }); + configRouter.handleUpdateConfig(req, res, config); + } + else if (parsedUrl.pathname === '/api/config/client') { + configRouter.handleClientConfig(req, res); } // Serve static files else if (parsedUrl.pathname === '/' || parsedUrl.pathname === '/index.html') { - const fs = require('fs'); - fs.readFile('index.html', (err, data) => { + fs.readFile('index.html', 'utf8', (err, data) => { if (err) { res.writeHead(500, { 'Content-Type': 'text/plain' }); res.end('Error loading index.html'); return; } + // Always inject API key and config into HTML (even if empty, so window vars exist) + const apiKey = process.env.OPENWEATHERMAP_API_KEY || ''; + if (!data.includes('window.OPENWEATHERMAP_API_KEY')) { + // Inject before closing tag + const configScript = ` + `; + data = data.replace('', configScript + '\n'); + } res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(data); }); } - else if (/\.(js|css|png|jpg|jpeg|gif|ico)$/.test(parsedUrl.pathname)) { - const fs = require('fs'); - const filePath = parsedUrl.pathname.substring(1); - fs.readFile(filePath, (err, data) => { - if (err) { - res.writeHead(404, { 'Content-Type': 'text/plain' }); - res.end('File not found'); - return; - } + // Serve static files from public directory or root + else if (/\.(js|css|png|jpg|jpeg|gif|ico|svg|json)$/.test(parsedUrl.pathname)) { + let filePath = parsedUrl.pathname.substring(1); // Remove leading / + + // Try public directory first, then root directory for backward compatibility + const publicPath = path.join('public', filePath); + const rootPath = filePath; + + // Check if file exists in public directory first + fs.access(publicPath, fs.constants.F_OK, (publicErr) => { + const targetPath = publicErr ? rootPath : publicPath; - const ext = parsedUrl.pathname.split('.').pop(); - const contentType = { - 'js': 'text/javascript', - 'css': 'text/css', - 'png': 'image/png', - 'jpg': 'image/jpeg', - 'jpeg': 'image/jpeg', - 'gif': 'image/gif', - 'ico': 'image/x-icon' - }[ext] || 'text/plain'; - - res.writeHead(200, { 'Content-Type': contentType }); - res.end(data); + fs.readFile(targetPath, (err, data) => { + if (err) { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('File not found'); + return; + } + + const ext = parsedUrl.pathname.split('.').pop(); + const contentType = { + 'js': 'text/javascript', + 'css': 'text/css', + 'png': 'image/png', + 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'gif': 'image/gif', + 'ico': 'image/x-icon', + 'svg': 'image/svg+xml', + 'json': 'application/json' + }[ext] || 'text/plain'; + + res.writeHead(200, { 'Content-Type': contentType }); + res.end(data); + }); }); } else { diff --git a/server/routes/config.js b/server/routes/config.js new file mode 100644 index 0000000..6335267 --- /dev/null +++ b/server/routes/config.js @@ -0,0 +1,78 @@ +/** + * Config route handler + * Handles configuration get/update and client-side config + */ + +const fs = require('fs'); +const path = require('path'); + +/** + * Handle GET /api/config endpoint + * @param {http.IncomingMessage} req - HTTP request object + * @param {http.ServerResponse} res - HTTP response object + * @param {Object} config - Application configuration + */ +function handleGetConfig(req, res, config) { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(config)); +} + +/** + * Handle POST /api/config/update endpoint + * @param {http.IncomingMessage} req - HTTP request object + * @param {http.ServerResponse} res - HTTP response object + * @param {Object} config - Application configuration (will be modified) + */ +function handleUpdateConfig(req, res, config) { + let body = ''; + req.on('data', chunk => { + body += chunk.toString(); + }); + + req.on('end', () => { + try { + const newConfig = JSON.parse(body); + if (newConfig.sites) { + // Update config object (passed by reference) + Object.assign(config, newConfig); + + // Save to file + const configPath = path.join('config', 'sites.json'); + fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); + + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ success: true, message: 'Configuration updated' })); + } else { + throw new Error('Invalid configuration format'); + } + } catch (error) { + console.error('Error updating configuration:', error); + res.writeHead(400, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: error.message })); + } + }); +} + +/** + * Handle GET /api/config/client endpoint + * Returns client-side configuration (API keys, default location) + * @param {http.IncomingMessage} req - HTTP request object + * @param {http.ServerResponse} res - HTTP response object + */ +function handleClientConfig(req, res) { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + openweathermapApiKey: process.env.OPENWEATHERMAP_API_KEY || '', + defaultLocation: { + latitude: parseFloat(process.env.DEFAULT_LATITUDE) || 59.3293, + longitude: parseFloat(process.env.DEFAULT_LONGITUDE) || 18.0686, + name: process.env.DEFAULT_LOCATION_NAME || 'Stockholm' + } + })); +} + +module.exports = { + handleGetConfig, + handleUpdateConfig, + handleClientConfig +}; diff --git a/server/routes/departures.js b/server/routes/departures.js new file mode 100644 index 0000000..f27b322 --- /dev/null +++ b/server/routes/departures.js @@ -0,0 +1,152 @@ +/** + * Departures route handler + * Handles fetching and returning transit departure data + */ + +const https = require('https'); + +/** + * Fetch departures for a specific site from SL Transport API + * @param {string} siteId - The site ID to fetch departures for + * @returns {Promise} - Departure data + */ +function fetchDeparturesForSite(siteId) { + return new Promise((resolve, reject) => { + const apiUrl = `https://transport.integration.sl.se/v1/sites/${siteId}/departures`; + console.log(`Fetching data from: ${apiUrl}`); + + https.get(apiUrl, (res) => { + let data = ''; + + res.on('data', (chunk) => { + data += chunk; + }); + + res.on('end', () => { + console.log('Raw API response:', data.substring(0, 200) + '...'); + + try { + try { + const parsedData = JSON.parse(data); + console.log('Successfully parsed as regular JSON'); + resolve(parsedData); + return; + } catch (jsonError) { + console.log('Not valid JSON, trying to fix format...'); + } + + if (data.startsWith('departures":')) { + data = '{' + data; + } else if (data.includes('departures":')) { + const startIndex = data.indexOf('departures":'); + if (startIndex > 0) { + data = '{' + data.substring(startIndex); + } + } + + data = data.replace(/}{\s*"/g, '},{"'); + data = data.replace(/"([^"]+)":\s*([^,{}\[\]]+)(?=")/g, '"$1": $2,'); + data = data.replace(/,\s*}/g, '}').replace(/,\s*\]/g, ']'); + + try { + const parsedData = JSON.parse(data); + console.log('Successfully parsed fixed JSON'); + + if (parsedData && parsedData.departures && parsedData.departures.length > 0) { + console.log('Sample departure structure:', JSON.stringify(parsedData.departures[0], null, 2)); + + const sample = parsedData.departures[0]; + console.log('Direction fields:', { + direction: sample.direction, + directionText: sample.directionText, + directionCode: sample.directionCode, + destination: sample.destination + }); + } + + resolve(parsedData); + } catch (parseError) { + console.error('Failed to parse even after fixing:', parseError); + // Return empty departures array instead of rejecting to be more resilient + resolve({ + departures: [], + error: 'Failed to parse API response: ' + parseError.message + }); + } + } catch (error) { + console.error('Error processing API response:', error); + // Return empty departures array instead of rejecting to be more resilient + resolve({ + departures: [], + error: 'Error processing API response: ' + error.message + }); + } + }); + }).on('error', (error) => { + console.error('Error fetching departures:', error); + reject(error); + }); + }); +} + +/** + * Fetch departures for all enabled sites + * @param {Array} enabledSites - Array of enabled site configurations + * @returns {Promise} - Object with sites array containing departure data + */ +async function fetchAllDepartures(enabledSites) { + if (enabledSites.length === 0) { + return { sites: [], error: 'No enabled sites configured' }; + } + + try { + const sitesPromises = enabledSites.map(async (site) => { + try { + const departureData = await fetchDeparturesForSite(site.id); + return { + siteId: site.id, + siteName: site.name, + data: departureData + }; + } catch (error) { + console.error(`Error fetching departures for site ${site.id}:`, error); + return { + siteId: site.id, + siteName: site.name, + error: error.message + }; + } + }); + + const results = await Promise.all(sitesPromises); + return { sites: results }; + } catch (error) { + console.error('Error fetching all departures:', error); + return { sites: [], error: error.message }; + } +} + +/** + * Handle departures API endpoint + * @param {http.IncomingMessage} req - HTTP request object + * @param {http.ServerResponse} res - HTTP response object + * @param {Object} config - Application configuration + */ +async function handleDepartures(req, res, config) { + try { + const enabledSites = config.sites.filter(site => site.enabled); + const data = await fetchAllDepartures(enabledSites); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(data)); + } catch (error) { + console.error('Error handling departures request:', error); + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: error.message })); + } +} + +module.exports = { + handleDepartures, + fetchDeparturesForSite, + fetchAllDepartures +}; diff --git a/server/routes/sites.js b/server/routes/sites.js new file mode 100644 index 0000000..f7b2947 --- /dev/null +++ b/server/routes/sites.js @@ -0,0 +1,198 @@ +/** + * Sites route handler + * Handles site search and nearby sites queries + */ + +const https = require('https'); + +/** + * Normalize site data from API response to consistent format + * @param {Object} site - Raw site data from API + * @returns {Object} - Normalized site object + */ +function normalizeSite(site) { + return { + id: String(site.id || site.siteId || site.SiteId || ''), + name: site.name || site.siteName || site.Name || site.StopPointName || 'Unknown', + lat: site.lat || site.latitude || site.Lat || site.Latitude || null, + lon: site.lon || site.longitude || site.Lon || site.Longitude || null + }; +} + +/** + * Parse sites from API response (handles multiple response formats) + * @param {Object|Array} parsedData - Parsed JSON data from API + * @returns {Array} - Array of normalized sites + */ +function parseSitesFromResponse(parsedData) { + let sites = []; + + if (Array.isArray(parsedData)) { + sites = parsedData.map(normalizeSite); + } else if (parsedData.sites && Array.isArray(parsedData.sites)) { + sites = parsedData.sites.map(normalizeSite); + } else if (parsedData.ResponseData && parsedData.ResponseData.Result) { + sites = parsedData.ResponseData.Result.map(normalizeSite); + } + + return sites; +} + +/** + * Handle site search endpoint + * @param {http.IncomingMessage} req - HTTP request object + * @param {http.ServerResponse} res - HTTP response object + * @param {url.UrlWithParsedQuery} parsedUrl - Parsed URL object + */ +function handleSiteSearch(req, res, parsedUrl) { + const query = parsedUrl.query.q; + if (!query || query.length < 2) { + res.writeHead(400, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Query must be at least 2 characters' })); + return; + } + + const searchUrl = `https://transport.integration.sl.se/v1/sites?q=${encodeURIComponent(query)}`; + console.log(`Searching sites: ${searchUrl}`); + + https.get(searchUrl, (apiRes) => { + let data = ''; + + if (apiRes.statusCode < 200 || apiRes.statusCode >= 300) { + console.error(`API returned status code: ${apiRes.statusCode}`); + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: `API returned status ${apiRes.statusCode}`, sites: [] })); + return; + } + + apiRes.on('data', (chunk) => { + data += chunk; + }); + + apiRes.on('end', () => { + try { + console.log('Raw API response:', data.substring(0, 500)); + const parsedData = JSON.parse(data); + console.log('Parsed data:', JSON.stringify(parsedData).substring(0, 500)); + + const sites = parseSitesFromResponse(parsedData); + + if (sites.length > 0) { + console.log('Sample site structure:', JSON.stringify(sites[0], null, 2)); + const sitesWithCoords = sites.filter(s => s.lat && s.lon); + console.log(`Found ${sites.length} sites, ${sitesWithCoords.length} with coordinates`); + } else { + console.log('No sites found'); + } + + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ sites })); + } catch (error) { + console.error('Error parsing site search response:', error); + console.error('Response data:', data.substring(0, 500)); + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Error parsing search results', details: error.message, sites: [] })); + } + }); + }).on('error', (error) => { + console.error('Error searching sites:', error); + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Error searching sites', details: error.message, sites: [] })); + }); +} + +/** + * Calculate distance between two coordinates (simple approximation) + * @param {number} lat1 - Latitude of point 1 + * @param {number} lon1 - Longitude of point 1 + * @param {number} lat2 - Latitude of point 2 + * @param {number} lon2 - Longitude of point 2 + * @returns {number} - Distance in meters + */ +function calculateDistance(lat1, lon1, lat2, lon2) { + return Math.sqrt( + Math.pow((lat1 - lat2) * 111000, 2) + + Math.pow((lon1 - lon2) * 111000 * Math.cos(lat1 * Math.PI / 180), 2) + ); +} + +/** + * Handle nearby sites endpoint + * @param {http.IncomingMessage} req - HTTP request object + * @param {http.ServerResponse} res - HTTP response object + * @param {url.UrlWithParsedQuery} parsedUrl - Parsed URL object + */ +function handleNearbySites(req, res, parsedUrl) { + const lat = parseFloat(parsedUrl.query.lat); + const lon = parseFloat(parsedUrl.query.lon); + const radius = parseInt(parsedUrl.query.radius) || 5000; // Default 5km radius + + if (isNaN(lat) || isNaN(lon)) { + res.writeHead(400, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Invalid latitude or longitude', sites: [] })); + return; + } + + // Use a broader search to get sites, then filter by distance + const searchTerms = ['Stockholm', 'T-Centralen', 'Gamla Stan', 'Södermalm']; + const allSites = []; + let completedSearches = 0; + + searchTerms.forEach(term => { + const searchUrl = `https://transport.integration.sl.se/v1/sites?q=${encodeURIComponent(term)}`; + + https.get(searchUrl, (apiRes) => { + let data = ''; + + apiRes.on('data', (chunk) => { + data += chunk; + }); + + apiRes.on('end', () => { + try { + const parsedData = JSON.parse(data); + const sites = parseSitesFromResponse(parsedData); + + sites.forEach(site => { + if (site.lat && site.lon) { + const distance = calculateDistance(lat, lon, site.lat, site.lon); + + if (distance <= radius) { + allSites.push(site); + } + } + }); + + completedSearches++; + if (completedSearches === searchTerms.length) { + // Remove duplicates + const uniqueSites = Array.from(new Map(allSites.map(s => [s.id, s])).values()); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ sites: uniqueSites })); + } + } catch (error) { + completedSearches++; + if (completedSearches === searchTerms.length) { + const uniqueSites = Array.from(new Map(allSites.map(s => [s.id, s])).values()); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ sites: uniqueSites })); + } + } + }); + }).on('error', () => { + completedSearches++; + if (completedSearches === searchTerms.length) { + const uniqueSites = Array.from(new Map(allSites.map(s => [s.id, s])).values()); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ sites: uniqueSites })); + } + }); + }); +} + +module.exports = { + handleSiteSearch, + handleNearbySites, + normalizeSite, + parseSitesFromResponse +}; diff --git a/update-site-id.sh b/update-site-id.sh deleted file mode 100644 index fdc4a9a..0000000 --- a/update-site-id.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash -# Script to update the site ID for the SL Transport API - -# Check if a site ID was provided -if [ -z "$1" ]; then - echo "Error: No site ID provided." - echo "Usage: ./update-site-id.sh SITE_ID" - echo "Example: ./update-site-id.sh 1234" - exit 1 -fi - -# Validate that the site ID is numeric -if ! [[ "$1" =~ ^[0-9]+$ ]]; then - echo "Error: Site ID must be a number." - echo "Usage: ./update-site-id.sh SITE_ID" - echo "Example: ./update-site-id.sh 1234" - exit 1 -fi - -SITE_ID=$1 -SITE_NAME="" - -# Ask for site name (optional) -read -p "Enter site name (optional, press Enter to skip): " SITE_NAME - -# Update server.js -echo "Updating server.js with site ID: $SITE_ID" -sed -i "s|const API_URL = 'https://transport.integration.sl.se/v1/sites/[0-9]*/departures'|const API_URL = 'https://transport.integration.sl.se/v1/sites/$SITE_ID/departures'|g" server.js - -# Update index.html if site name was provided -if [ ! -z "$SITE_NAME" ]; then - echo "Updating index.html with site name: $SITE_NAME" - sed -i "s|

.*

|

$SITE_NAME (Site ID: $SITE_ID)

|g" index.html -fi - -# Update title in index.html -if [ ! -z "$SITE_NAME" ]; then - echo "Updating page title in index.html" - sed -i "s|SL Transport Departures - .*|SL Transport Departures - $SITE_NAME ($SITE_ID)|g" index.html -fi - -echo "Update complete!" -echo "Site ID: $SITE_ID" -if [ ! -z "$SITE_NAME" ]; then - echo "Site Name: $SITE_NAME" -fi -echo "Restart the server for changes to take effect: node server.js" diff --git a/update-weather-location.sh b/update-weather-location.sh deleted file mode 100644 index ff48f4e..0000000 --- a/update-weather-location.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash -# Script to update the weather location in the SL Transport Departures Display - -# Check if latitude and longitude were provided -if [ -z "$1" ] || [ -z "$2" ]; then - echo "Error: Latitude and longitude must be provided." - echo "Usage: ./update-weather-location.sh LATITUDE LONGITUDE [LOCATION_NAME]" - echo "Example: ./update-weather-location.sh 59.3293 18.0686 Stockholm" - exit 1 -fi - -# Validate that the latitude and longitude are numeric -if ! [[ "$1" =~ ^-?[0-9]+(\.[0-9]+)?$ ]] || ! [[ "$2" =~ ^-?[0-9]+(\.[0-9]+)?$ ]]; then - echo "Error: Latitude and longitude must be numbers." - echo "Usage: ./update-weather-location.sh LATITUDE LONGITUDE [LOCATION_NAME]" - echo "Example: ./update-weather-location.sh 59.3293 18.0686 Stockholm" - exit 1 -fi - -LATITUDE=$1 -LONGITUDE=$2 -LOCATION_NAME=${3:-""} - -# Update index.html with new latitude and longitude -echo "Updating weather location in index.html..." -sed -i "s/latitude: [0-9.-]\+/latitude: $LATITUDE/g" index.html -sed -i "s/longitude: [0-9.-]\+/longitude: $LONGITUDE/g" index.html - -# Update location name in the weather widget if provided -if [ ! -z "$LOCATION_NAME" ]; then - echo "Updating location name to: $LOCATION_NAME" - # This is a more complex replacement that might need manual verification - sed -i "s/

[^<]*<\/h3>/

$LOCATION_NAME<\/h3>/g" index.html -fi - -echo "Weather location updated successfully!" -echo "Latitude: $LATITUDE" -echo "Longitude: $LONGITUDE" -if [ ! -z "$LOCATION_NAME" ]; then - echo "Location Name: $LOCATION_NAME" -fi -echo "" -echo "Restart the application to see the changes." -echo "You can find latitude and longitude for any location at: https://www.latlong.net/"