private async Task TrackGhostVehicles(bool saveTrolleysToDB) { // Copy array to handle ghost route modification in loop var workGhostRoutes = new Syncromatics.Route[ghostRoutes.Count]; ghostRoutes.CopyTo(workGhostRoutes); foreach (var syncroRoute in workGhostRoutes) { await CheckForVehiclesOnRoute(syncroRoute, saveTrolleysToDB); } }
/// <summary> /// Copy of GetVehiclesOnRoute - kludge to handle 'ghost vehicles' not /// on route. /// </summary> /// <param name="route"></param> /// <param name="saveTrolleysToDB">True if time to save trolley positions</param> private async Task CheckForVehiclesOnRoute(Syncromatics.Route syncromaticsRoute, bool saveTrolleysToDB) { var vehicles = await syncromatics.GetVehiclesOnRoute(syncromaticsRoute.id); if (vehicles.Count == 0) { if (ghostRoutes.Contains(syncromaticsRoute)) { // Drive logged out of this route ghostRoutes.Remove(syncromaticsRoute); } return; } foreach (var vehicle in vehicles) { if (lastVehicleUpdateTime.ContainsKey(vehicle.id)) { // Check for stall (no update from Syncromatics) if (lastVehicleUpdateTime[vehicle.id] == vehicle.lastUpdated) { //Trace.WriteLine("Stalled vehicle, syncromatics # " + vehicle.name); continue; } lastVehicleUpdateTime[vehicle.id] = vehicle.lastUpdated; } else { lastVehicleUpdateTime.Add(vehicle.id, vehicle.lastUpdated); } var trolley = FindMatchingTrolley(vehicle); if (trolley != null) { Trace.WriteLine("Tracking Ghost trolley " + trolley.Number); trolley.CurrentLat = vehicle.lat; trolley.CurrentLon = vehicle.lon; trolley.Capacity = vehicle.capacity; trolley.PassengerLoad = vehicle.passengerLoad; var colorBlack = "#000000"; if (trolley.IconColorRGB != colorBlack) { trolley.IconColorRGB = colorBlack; saveTrolleysToDB = true; } trolley.LastBeaconTime = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow); if (saveTrolleysToDB) { await SaveTrolleyToDB(trolley); } TrolleyCache.UpdateTrolley(trolley); StopArrivalTime.UpdateTrolleyStopArrivalTime(trolley); if (!ghostRoutes.Contains(syncromaticsRoute)) { ghostRoutes.Add(syncromaticsRoute); } } } }