@@ -146,18 +146,18 @@ public async Task<string> FindTrainConnectionsAsync(
146146 result . AppendLine ( " ✓ Timetable retrieved" ) ;
147147 result . AppendLine ( ) ;
148148
149- // Step 4: Get changes for station A to check for delays/disruptions
149+ // Step 4: Get full changes for station A to check for delays/disruptions
150150 result . AppendLine ( $ "Step 4: Checking for delays and disruptions at { stationAInfo . Name } ...") ;
151151 string changesA ;
152152 try
153153 {
154- changesA = await GetRecentTimetableChangesAsync ( stationAInfo . EvaNo , cancellationToken ) ;
155- result . AppendLine ( " ✓ Recent changes retrieved" ) ;
154+ changesA = await GetFullChangesAsync ( stationAInfo . EvaNo , cancellationToken ) ;
155+ result . AppendLine ( " ✓ Full changes retrieved" ) ;
156156 }
157157 catch
158158 {
159159 changesA = string . Empty ;
160- result . AppendLine ( " ⚠ No recent changes available" ) ;
160+ result . AppendLine ( " ⚠ No changes available" ) ;
161161 }
162162 result . AppendLine ( ) ;
163163
@@ -353,21 +353,72 @@ private List<ConnectionInfo> FindConnectionsInTimetable(string timetableXml, str
353353 var departureTime = ParseTimetableDateTime ( actualDeparture ) ;
354354 var scheduledDepartureTime = ParseTimetableDateTime ( scheduledDeparture ) ;
355355
356- // Calculate delay
356+ // Look up live changes for this specific train from the full changes data
357357 int delay = 0 ;
358- if ( scheduledDepartureTime . HasValue && departureTime . HasValue )
358+ bool isCancelled = departureElement . Attribute ( "cs" ) ? . Value == "c" ;
359+ string messages = string . Join ( "; " , stop . Elements ( "m" )
360+ . Select ( m => m . Attribute ( "t" ) ? . Value )
361+ . Where ( m => ! string . IsNullOrEmpty ( m ) ) ) ;
362+
363+ // If we have changes data, look for this specific train and extract live information
364+ if ( changesDoc != null )
365+ {
366+ var changeStop = changesDoc . Descendants ( "s" )
367+ . FirstOrDefault ( s => s . Attribute ( "id" ) ? . Value == trainId ) ;
368+
369+ if ( changeStop != null )
370+ {
371+ var changeDp = changeStop . Element ( "dp" ) ;
372+ if ( changeDp != null )
373+ {
374+ // Get live departure time from changes
375+ var liveActualDeparture = changeDp . Attribute ( "ct" ) ? . Value ;
376+ var liveScheduledDeparture = changeDp . Attribute ( "pt" ) ? . Value ;
377+
378+ if ( ! string . IsNullOrEmpty ( liveActualDeparture ) && ! string . IsNullOrEmpty ( liveScheduledDeparture ) )
379+ {
380+ var liveDepartureTime = ParseTimetableDateTime ( liveActualDeparture ) ;
381+ var liveScheduledTime = ParseTimetableDateTime ( liveScheduledDeparture ) ;
382+
383+ if ( liveDepartureTime . HasValue && liveScheduledTime . HasValue )
384+ {
385+ delay = ( int ) ( liveDepartureTime . Value - liveScheduledTime . Value ) . TotalMinutes ;
386+ departureTime = liveDepartureTime ; // Use live time
387+ }
388+ }
389+
390+ // Check for cancellation status in changes
391+ var liveStatus = changeDp . Attribute ( "cs" ) ? . Value ;
392+ if ( liveStatus == "c" )
393+ {
394+ isCancelled = true ;
395+ }
396+
397+ // Get changed platform if available
398+ var liveChangedPlatform = changeDp . Attribute ( "cp" ) ? . Value ;
399+ if ( ! string . IsNullOrEmpty ( liveChangedPlatform ) )
400+ {
401+ changedPlatform = liveChangedPlatform ;
402+ }
403+ }
404+
405+ // Get messages from changes (these are more up-to-date)
406+ var changeMessages = string . Join ( "; " , changeStop . Elements ( "m" )
407+ . Select ( m => m . Attribute ( "t" ) ? . Value )
408+ . Where ( m => ! string . IsNullOrEmpty ( m ) ) ) ;
409+
410+ if ( ! string . IsNullOrEmpty ( changeMessages ) )
411+ {
412+ messages = changeMessages ;
413+ }
414+ }
415+ }
416+ // Fallback: if no changes data, calculate delay from timetable data
417+ else if ( scheduledDepartureTime . HasValue && departureTime . HasValue )
359418 {
360419 delay = ( int ) ( departureTime . Value - scheduledDepartureTime . Value ) . TotalMinutes ;
361420 }
362421
363- // Check for cancellation
364- var isCancelled = departureElement . Attribute ( "cs" ) ? . Value == "c" ;
365-
366- // Get messages
367- var messages = string . Join ( "; " , stop . Elements ( "m" )
368- . Select ( m => m . Attribute ( "t" ) ? . Value )
369- . Where ( m => ! string . IsNullOrEmpty ( m ) ) ) ;
370-
371422 connections . Add ( new ConnectionInfo
372423 {
373424 TrainId = trainId ,
0 commit comments