fix: bearing maths
This commit is contained in:
@@ -52,10 +52,11 @@ jobs:
|
|||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: ./frontend
|
context: ./frontend
|
||||||
|
target: production
|
||||||
push: true
|
push: true
|
||||||
build-args: |
|
build-args: |
|
||||||
VITE_API_URL=${{ secrets.PROD_API_URL }}
|
VITE_API_URL=${{ secrets.PROD_API_URL || 'https://trenes.millaguie.net/api' }}
|
||||||
VITE_WS_URL=${{ secrets.PROD_WS_URL }}
|
VITE_WS_URL=${{ secrets.PROD_WS_URL || 'https://trenes.millaguie.net' }}
|
||||||
APP_VERSION=${{ steps.version.outputs.version }}
|
APP_VERSION=${{ steps.version.outputs.version }}
|
||||||
BUILD_DATE=${{ steps.date.outputs.date }}
|
BUILD_DATE=${{ steps.date.outputs.date }}
|
||||||
GIT_COMMIT=${{ github.sha }}
|
GIT_COMMIT=${{ github.sha }}
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
version: '3.8'
|
|
||||||
|
|
||||||
# Docker Compose para producción
|
# Docker Compose para producción
|
||||||
# Uso: docker compose -f docker-compose.prod.yml up -d --build
|
# Uso: docker compose -f docker-compose.prod.yml up -d
|
||||||
#
|
#
|
||||||
# Requisitos previos:
|
# Requisitos previos:
|
||||||
# 1. Configurar .env con valores de producción (ver .env.example)
|
# 1. Configurar .env con valores de producción (ver .env.example)
|
||||||
# 2. Configurar certificados SSL en ./nginx/ssl/ o usar certbot
|
# 2. Login al registry: docker login tea.millaguie.net
|
||||||
# 3. Configurar nginx/prod.conf con tu dominio
|
# 3. Configurar nginx/prod.conf con tu dominio
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# Base de datos PostgreSQL con extensión PostGIS
|
# Base de datos PostgreSQL con extensión PostGIS
|
||||||
postgres:
|
postgres:
|
||||||
image: postgis/postgis:16-3.4-alpine # IMPORTANTE: usar versión 16 para compatibilidad
|
image: postgis/postgis:16-3.4-alpine
|
||||||
container_name: trenes-postgres
|
container_name: trenes-postgres
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
@@ -68,17 +66,42 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
networks:
|
networks:
|
||||||
- trenes-network
|
- trenes-network
|
||||||
profiles:
|
|
||||||
- migration
|
# API Backend
|
||||||
|
api:
|
||||||
|
image: tea.millaguie.net/millaguie/trenes-backend:${IMAGE_TAG:-latest}
|
||||||
|
container_name: trenes-api
|
||||||
|
restart: unless-stopped
|
||||||
|
command: ["node", "src/api/server.js"]
|
||||||
|
environment:
|
||||||
|
NODE_ENV: production
|
||||||
|
PORT: 3000
|
||||||
|
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
||||||
|
REDIS_URL: redis://redis:6379
|
||||||
|
CORS_ORIGIN: ${CORS_ORIGINS:-https://localhost}
|
||||||
|
JWT_SECRET: ${JWT_SECRET:-change_me_in_production}
|
||||||
|
LOG_LEVEL: info
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
flyway:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:3000/health"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
networks:
|
||||||
|
- trenes-network
|
||||||
|
|
||||||
# Worker para polling GTFS-RT Vehicle Positions
|
# Worker para polling GTFS-RT Vehicle Positions
|
||||||
worker:
|
worker:
|
||||||
build:
|
image: tea.millaguie.net/millaguie/trenes-backend:${IMAGE_TAG:-latest}
|
||||||
context: ./backend
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
target: worker
|
|
||||||
container_name: trenes-worker
|
container_name: trenes-worker
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
command: ["node", "src/worker/gtfs-poller.js"]
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
||||||
@@ -91,24 +114,23 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
flyway:
|
||||||
|
condition: service_completed_successfully
|
||||||
networks:
|
networks:
|
||||||
- trenes-network
|
- trenes-network
|
||||||
|
|
||||||
# Worker para sincronización GTFS Static
|
# Worker para sincronización GTFS Static
|
||||||
gtfs-static-syncer:
|
gtfs-static-syncer:
|
||||||
build:
|
image: tea.millaguie.net/millaguie/trenes-backend:${IMAGE_TAG:-latest}
|
||||||
context: ./backend
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
target: worker
|
|
||||||
container_name: trenes-gtfs-static-syncer
|
container_name: trenes-gtfs-static-syncer
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command: node src/worker/gtfs-static-syncer.js
|
command: ["node", "src/worker/gtfs-static-syncer.js"]
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
||||||
REDIS_URL: redis://redis:6379
|
REDIS_URL: redis://redis:6379
|
||||||
GTFS_STATIC_URL: https://data.renfe.com/dataset/horarios-trenes-largo-recorrido-ave/resource/horarios-trenes-largo-recorrido-ave-gtfs.zip
|
GTFS_STATIC_URL: https://data.renfe.com/dataset/horarios-trenes-largo-recorrido-ave/resource/horarios-trenes-largo-recorrido-ave-gtfs.zip
|
||||||
SYNC_SCHEDULE: 0 3 * * *
|
SYNC_SCHEDULE: "0 3 * * *"
|
||||||
LOG_LEVEL: info
|
LOG_LEVEL: info
|
||||||
volumes:
|
volumes:
|
||||||
- gtfs_static_data:/tmp/gtfs
|
- gtfs_static_data:/tmp/gtfs
|
||||||
@@ -117,18 +139,17 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
flyway:
|
||||||
|
condition: service_completed_successfully
|
||||||
networks:
|
networks:
|
||||||
- trenes-network
|
- trenes-network
|
||||||
|
|
||||||
# Worker para polling GTFS-RT Trip Updates
|
# Worker para polling GTFS-RT Trip Updates
|
||||||
trip-updates-poller:
|
trip-updates-poller:
|
||||||
build:
|
image: tea.millaguie.net/millaguie/trenes-backend:${IMAGE_TAG:-latest}
|
||||||
context: ./backend
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
target: worker
|
|
||||||
container_name: trenes-trip-updates-poller
|
container_name: trenes-trip-updates-poller
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command: node src/worker/trip-updates-poller.js
|
command: ["node", "src/worker/trip-updates-poller.js"]
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
||||||
@@ -141,18 +162,17 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
flyway:
|
||||||
|
condition: service_completed_successfully
|
||||||
networks:
|
networks:
|
||||||
- trenes-network
|
- trenes-network
|
||||||
|
|
||||||
# Worker para polling GTFS-RT Service Alerts
|
# Worker para polling GTFS-RT Service Alerts
|
||||||
alerts-poller:
|
alerts-poller:
|
||||||
build:
|
image: tea.millaguie.net/millaguie/trenes-backend:${IMAGE_TAG:-latest}
|
||||||
context: ./backend
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
target: worker
|
|
||||||
container_name: trenes-alerts-poller
|
container_name: trenes-alerts-poller
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command: node src/worker/alerts-poller.js
|
command: ["node", "src/worker/alerts-poller.js"]
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
||||||
@@ -165,18 +185,17 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
flyway:
|
||||||
|
condition: service_completed_successfully
|
||||||
networks:
|
networks:
|
||||||
- trenes-network
|
- trenes-network
|
||||||
|
|
||||||
# Worker para datos de flota Renfe
|
# Worker para datos de flota Renfe
|
||||||
renfe-fleet-poller:
|
renfe-fleet-poller:
|
||||||
build:
|
image: tea.millaguie.net/millaguie/trenes-backend:${IMAGE_TAG:-latest}
|
||||||
context: ./backend
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
target: worker
|
|
||||||
container_name: trenes-renfe-fleet-poller
|
container_name: trenes-renfe-fleet-poller
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command: node src/worker/renfe-fleet-poller.js
|
command: ["node", "src/worker/renfe-fleet-poller.js"]
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
||||||
@@ -187,18 +206,17 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
flyway:
|
||||||
|
condition: service_completed_successfully
|
||||||
networks:
|
networks:
|
||||||
- trenes-network
|
- trenes-network
|
||||||
|
|
||||||
# Worker para refrescar vistas de analytics
|
# Worker para refrescar vistas de analytics
|
||||||
analytics-refresher:
|
analytics-refresher:
|
||||||
build:
|
image: tea.millaguie.net/millaguie/trenes-backend:${IMAGE_TAG:-latest}
|
||||||
context: ./backend
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
target: worker
|
|
||||||
container_name: trenes-analytics-refresher
|
container_name: trenes-analytics-refresher
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command: node src/worker/analytics-refresher.js
|
command: ["node", "src/worker/analytics-refresher.js"]
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
||||||
@@ -210,49 +228,14 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
networks:
|
flyway:
|
||||||
- trenes-network
|
condition: service_completed_successfully
|
||||||
|
|
||||||
# API Backend
|
|
||||||
api:
|
|
||||||
build:
|
|
||||||
context: ./backend
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
target: api
|
|
||||||
container_name: trenes-api
|
|
||||||
restart: unless-stopped
|
|
||||||
environment:
|
|
||||||
NODE_ENV: production
|
|
||||||
PORT: 3000
|
|
||||||
DATABASE_URL: postgresql://${POSTGRES_USER:-trenes}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-trenes}
|
|
||||||
REDIS_URL: redis://redis:6379
|
|
||||||
CORS_ORIGIN: ${CORS_ORIGINS:-https://localhost}
|
|
||||||
JWT_SECRET: ${JWT_SECRET}
|
|
||||||
LOG_LEVEL: info
|
|
||||||
depends_on:
|
|
||||||
postgres:
|
|
||||||
condition: service_healthy
|
|
||||||
redis:
|
|
||||||
condition: service_healthy
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:3000/health"]
|
|
||||||
interval: 30s
|
|
||||||
timeout: 10s
|
|
||||||
retries: 3
|
|
||||||
networks:
|
networks:
|
||||||
- trenes-network
|
- trenes-network
|
||||||
|
|
||||||
# Frontend
|
# Frontend
|
||||||
# IMPORTANTE: Las variables VITE_* deben pasarse como build args, no como environment
|
|
||||||
# ya que se procesan en tiempo de compilación, no en runtime
|
|
||||||
frontend:
|
frontend:
|
||||||
build:
|
image: tea.millaguie.net/millaguie/trenes-frontend:${IMAGE_TAG:-latest}
|
||||||
context: ./frontend
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
target: production
|
|
||||||
args:
|
|
||||||
VITE_API_URL: ${VITE_API_URL}
|
|
||||||
VITE_WS_URL: ${VITE_WS_URL}
|
|
||||||
container_name: trenes-frontend
|
container_name: trenes-frontend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
depends_on:
|
depends_on:
|
||||||
@@ -267,7 +250,7 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
volumes:
|
volumes:
|
||||||
- ./nginx/prod.conf:/etc/nginx/conf.d/default.conf:ro
|
- ./nginx/prod.conf:/etc/nginx/conf.d/default.conf:ro
|
||||||
- letsencrypt_certs:/etc/letsencrypt:ro
|
- certbot_certs:/etc/letsencrypt:ro
|
||||||
- certbot_webroot:/var/www/certbot:ro
|
- certbot_webroot:/var/www/certbot:ro
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "80:80"
|
||||||
@@ -288,7 +271,7 @@ services:
|
|||||||
image: certbot/certbot
|
image: certbot/certbot
|
||||||
container_name: trenes-certbot
|
container_name: trenes-certbot
|
||||||
volumes:
|
volumes:
|
||||||
- letsencrypt_certs:/etc/letsencrypt
|
- certbot_certs:/etc/letsencrypt
|
||||||
- certbot_webroot:/var/www/certbot
|
- certbot_webroot:/var/www/certbot
|
||||||
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
|
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
|
||||||
networks:
|
networks:
|
||||||
@@ -301,10 +284,10 @@ volumes:
|
|||||||
driver: local
|
driver: local
|
||||||
gtfs_static_data:
|
gtfs_static_data:
|
||||||
driver: local
|
driver: local
|
||||||
letsencrypt_certs:
|
certbot_certs:
|
||||||
driver: local
|
external: true
|
||||||
certbot_webroot:
|
certbot_webroot:
|
||||||
driver: local
|
external: true
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
trenes-network:
|
trenes-network:
|
||||||
|
|||||||
@@ -68,8 +68,8 @@ function App() {
|
|||||||
{activeView === 'dashboard'
|
{activeView === 'dashboard'
|
||||||
? 'Dashboard de Trenes'
|
? 'Dashboard de Trenes'
|
||||||
: isTimelineMode
|
: isTimelineMode
|
||||||
? 'Reproduccion Historica - Espana'
|
? 'Reproduccion Historica - España'
|
||||||
: 'Trenes en Tiempo Real - Espana'}
|
: 'Trenes en Tiempo Real - España'}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import { calculateBearing, calculateDistance, MIN_DISTANCE_FOR_BEARING } from '.
|
|||||||
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000';
|
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pre-calculate bearings for historical data based on consecutive positions
|
* Pre-calculate bearings for historical data based on the last different position
|
||||||
|
* Searches backwards to find a position with significant distance for accurate bearing
|
||||||
* @param {Array} positions - Array of positions sorted by timestamp ASC
|
* @param {Array} positions - Array of positions sorted by timestamp ASC
|
||||||
* @returns {Array} - Positions with calculated bearings
|
* @returns {Array} - Positions with calculated bearings
|
||||||
*/
|
*/
|
||||||
@@ -26,19 +27,43 @@ function calculateHistoricalBearings(positions) {
|
|||||||
// Sort by timestamp (should already be sorted, but ensure)
|
// Sort by timestamp (should already be sorted, but ensure)
|
||||||
trainPos.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
|
trainPos.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
|
||||||
|
|
||||||
|
let lastSignificantPos = null; // Track last position with significant movement
|
||||||
|
let lastBearing = null; // Track last calculated bearing
|
||||||
|
|
||||||
for (let i = 0; i < trainPos.length; i++) {
|
for (let i = 0; i < trainPos.length; i++) {
|
||||||
const current = trainPos[i];
|
const current = trainPos[i];
|
||||||
let bearing = current.bearing; // Use existing bearing if available
|
let bearing = current.bearing; // Use existing bearing if available
|
||||||
|
|
||||||
if (bearing === null || bearing === undefined) {
|
if (bearing === null || bearing === undefined) {
|
||||||
// Calculate bearing from previous position
|
// Search backwards for the last position with significant distance
|
||||||
if (i > 0) {
|
if (lastSignificantPos) {
|
||||||
const prev = trainPos[i - 1];
|
const distance = calculateDistance(
|
||||||
const distance = calculateDistance(prev.latitude, prev.longitude, current.latitude, current.longitude);
|
lastSignificantPos.latitude,
|
||||||
|
lastSignificantPos.longitude,
|
||||||
|
current.latitude,
|
||||||
|
current.longitude
|
||||||
|
);
|
||||||
if (distance >= MIN_DISTANCE_FOR_BEARING) {
|
if (distance >= MIN_DISTANCE_FOR_BEARING) {
|
||||||
bearing = calculateBearing(prev.latitude, prev.longitude, current.latitude, current.longitude);
|
bearing = calculateBearing(
|
||||||
|
lastSignificantPos.latitude,
|
||||||
|
lastSignificantPos.longitude,
|
||||||
|
current.latitude,
|
||||||
|
current.longitude
|
||||||
|
);
|
||||||
|
lastSignificantPos = current;
|
||||||
|
lastBearing = bearing;
|
||||||
|
} else {
|
||||||
|
// No significant movement, keep the last known bearing
|
||||||
|
bearing = lastBearing;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// First position for this train
|
||||||
|
lastSignificantPos = current;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Has bearing from API, update tracking
|
||||||
|
lastSignificantPos = current;
|
||||||
|
lastBearing = bearing;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push({
|
result.push({
|
||||||
|
|||||||
@@ -6,28 +6,31 @@ const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000';
|
|||||||
const WS_URL = import.meta.env.VITE_WS_URL || 'http://localhost:3000';
|
const WS_URL = import.meta.env.VITE_WS_URL || 'http://localhost:3000';
|
||||||
|
|
||||||
// Calculate bearing for trains based on previous positions
|
// Calculate bearing for trains based on previous positions
|
||||||
|
// Only updates the stored position when there's significant movement
|
||||||
function addCalculatedBearings(newTrains, previousPositions) {
|
function addCalculatedBearings(newTrains, previousPositions) {
|
||||||
return newTrains.map(train => {
|
return newTrains.map(train => {
|
||||||
// If train already has bearing from API, use it
|
// If train already has bearing from API, use it
|
||||||
if (train.bearing !== null && train.bearing !== undefined) {
|
if (train.bearing !== null && train.bearing !== undefined) {
|
||||||
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude });
|
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude, bearing: train.bearing });
|
||||||
return train;
|
return train;
|
||||||
}
|
}
|
||||||
|
|
||||||
const prevPos = previousPositions.get(train.train_id);
|
const prevPos = previousPositions.get(train.train_id);
|
||||||
let calculatedBearing = null;
|
let calculatedBearing = prevPos?.bearing ?? null;
|
||||||
|
|
||||||
if (prevPos) {
|
if (prevPos) {
|
||||||
const distance = calculateDistance(prevPos.lat, prevPos.lon, train.latitude, train.longitude);
|
const distance = calculateDistance(prevPos.lat, prevPos.lon, train.latitude, train.longitude);
|
||||||
// Only calculate bearing if the train moved enough
|
// Only calculate bearing and update position if the train moved enough
|
||||||
if (distance >= MIN_DISTANCE_FOR_BEARING) {
|
if (distance >= MIN_DISTANCE_FOR_BEARING) {
|
||||||
calculatedBearing = calculateBearing(prevPos.lat, prevPos.lon, train.latitude, train.longitude);
|
calculatedBearing = calculateBearing(prevPos.lat, prevPos.lon, train.latitude, train.longitude);
|
||||||
|
// Only update stored position when there's significant movement
|
||||||
|
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude, bearing: calculatedBearing });
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// First time seeing this train, store position without bearing
|
||||||
|
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude, bearing: null });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update previous position
|
|
||||||
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude });
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...train,
|
...train,
|
||||||
bearing: calculatedBearing,
|
bearing: calculatedBearing,
|
||||||
|
|||||||
Reference in New Issue
Block a user