private ScheduleStop GetNextStop(string trainId, TrainMovementEventType eventType, string stanox, byte latestStopNumber)
        {
            // get tiplocs first to optimise next query
            var tiplocs = _tiplocRepository.GetTiplocsByStanox(stanox)
                .Select(t => t.TiplocId)
                .ToList();

            var stops = ((Activation)_trainActivationCache[trainId]).Stops
                .Where(s => tiplocs.Contains(s.Tiploc.TiplocId))
                .Where(s => s.StopNumber >= latestStopNumber);

            if (eventType == TrainMovementEventType.Arrival)
            {
                stops = stops.Where(s => s.StopNumber > 0);
            }

            stops = stops.OrderBy(s => s.StopNumber);

            return stops.FirstOrDefault();
        }
        /// <summary>
        /// Update a train movement
        /// </summary>
        /// <param name="trainId">id of live train</param>
        /// <param name="tiplocIds">reporting tiploc(s)</param>
        /// <param name="eventType">arrival or departure to update</param>
        /// <param name="actualTime">actual time to set</param>
        /// <param name="source">source of data</param>
        /// <returns>true if a row updated</returns>
        public bool UpdateMovement(Guid trainId, TDElement td, IEnumerable<short> tiplocIds, TrainMovementEventType eventType, DateTime actualTime, LiveTrainStopSource source = LiveTrainStopSource.TD)
        {
            const string sql = @"
                UPDATE [dbo].[LiveTrainStop]
                SET  [ActualTimestamp] = @actualTime
                    ,[Platform] = @platform
                    ,[LiveTrainStopSourceId] = @source
                WHERE   [TrainId] = @trainId
                    AND [ReportingTiplocId] IN @tiplocIds
                    AND [EventTypeId] = @eventType
                    AND [LiveTrainStopSourceId] != @source
                    AND [ActualTimeStamp] >= @timeBefore
                    AND [ActualTimeStamp] <= @timeAfter";

            return ExecuteNonQuery(sql, new
            {
                trainId,
                platform = td.PLATFORM,
                tiplocIds,
                eventType,
                actualTime,
                source,
                timeBefore = actualTime.AddMinutes(-145),
                timeAfter = actualTime.AddMinutes(30)
            }) > 0;
        }