public void PostProbeData_UpdateVehiclePosition_Success() { using (IDbContext idtoFakeContext = new IDTOFakeContext()) using (IUnitOfWork imuow = new UnitOfWork(idtoFakeContext)) { AddTripToRepo(imuow); LastVehiclePosition lvp = new LastVehiclePosition { VehicleName = "MDT2", PositionTimestamp = DateTime.UtcNow, Latitude = 44.646581369493, Longitude = -96.6830267664, Speed = 10, Heading = 180, Accuracy = 5 }; imuow.Repository <LastVehiclePosition>().Insert(lvp); imuow.Save(); DateTime newestPositionTimestamp = DateTime.UtcNow.AddMinutes(1); var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); long newestTimeStamp = Convert.ToInt64((newestPositionTimestamp - epoch).TotalMilliseconds); ProbeVehicleData probeData = new ProbeVehicleData { InboundVehicle = "MDT2" }; PositionSnapshot ps = new PositionSnapshot() { Accuracy = 5, Altitude = 123, Heading = 180, Latitude = 44.646581369493, Longitude = -96.6830267664, Satellites = 0, Speed = 14.77999305725097, TimeStamp = newestTimeStamp }; probeData.Positions.Add(ps); var mockTable = new Mock <IAzureTable <ProbeSnapshotEntry> >(); var cut = new ProbeController(idtoFakeContext, mockTable.Object); SetupControllerForTests(cut); HttpResponseMessage returnMessage = cut.PostProbeData(probeData); List <LastVehiclePosition> lvpList = imuow.Repository <LastVehiclePosition>().Query().Get().Where(v => v.VehicleName == "MDT2").ToList(); Assert.AreEqual(1, lvpList.Count); Assert.AreEqual(newestPositionTimestamp.ToLongTimeString(), lvpList.First().PositionTimestamp.ToLongTimeString()); } }
/// <summary> /// Calculates the distance from the Cap Trans stop destination (one of two possible) to the current /// location (from the azure table) and then estimates an arrival time. /// </summary> /// <param name="tConnect"></param> /// <param name="Uow"></param> /// <returns></returns> public async Task <DateTime> CalculateEstimatedTimeOfArrivalAsync(TConnect tConnect, IUnitOfWork Uow) { return(await Task.Factory.StartNew(() => { GeoCoordinate busStopLoc = GetCoordForInboundStep(tConnect, Uow); LastVehiclePosition lvp = Uow.Repository <LastVehiclePosition>().Query().Get().First(v => v.VehicleName == tConnect.InboundVehicle); DateTime currentLocTime = lvp.PositionTimestamp; GeoCoordinate currentLoc = new GeoCoordinate(lvp.Latitude, lvp.Longitude); DateTime eta = CalcPointBEtaFromPointA(busStopLoc, currentLocTime, currentLoc); return eta; })); }
private void SaveNewestProbeSnapshot(ProbeSnapshotEntry newestProbeSnapshot) { if (newestProbeSnapshot == null) { return; } //Find existing vehicle record LastVehiclePosition currentVehicle = Uow.Repository <LastVehiclePosition>().Query().Get().FirstOrDefault(v => v.VehicleName == newestProbeSnapshot.PartitionKey); if (currentVehicle != null) { //Update vehicles last position values currentVehicle.PositionTimestamp = newestProbeSnapshot.PositionTimestamp; currentVehicle.Latitude = newestProbeSnapshot.Latitude; currentVehicle.Longitude = newestProbeSnapshot.Longitude; currentVehicle.Speed = newestProbeSnapshot.Speed; currentVehicle.Heading = (short)newestProbeSnapshot.Heading; currentVehicle.Accuracy = newestProbeSnapshot.Accuracy; Uow.Repository <LastVehiclePosition>().Update(currentVehicle); } else { LastVehiclePosition lvp = new LastVehiclePosition { VehicleName = newestProbeSnapshot.PartitionKey, PositionTimestamp = newestProbeSnapshot.PositionTimestamp, Latitude = newestProbeSnapshot.Latitude, Longitude = newestProbeSnapshot.Longitude, Speed = newestProbeSnapshot.Speed, Heading = (short)newestProbeSnapshot.Heading, Accuracy = newestProbeSnapshot.Accuracy }; Uow.Repository <LastVehiclePosition>().Insert(lvp); } Uow.Save(); }
/// <summary> /// Check all TConnects with status of "Monitored" to see if they need a TConnectRequest issued. /// </summary> /// <param name="Uow"></param> private async Task MonitorTConnects(IUnitOfWork Uow) { List <TConnect> monitoredTConnects = Uow.Repository <TConnect>().Query().Include(s => s.OutboundStep).Include(s => s.InboundStep.Trip).Get() .Where(s => s.TConnectStatusId == (int)TConnectStatuses.Monitored).ToList(); foreach (TConnect mt in monitoredTConnects) { try { try { await MonitorLocationTimes(mt); MonitorTravelerLocation(mt); }catch (Exception ex2) { String innerExString = ""; String stackTraceString = ""; if (ex2.InnerException != null) { innerExString = ex2.InnerException.Message; if (ex2.InnerException.StackTrace != null) { stackTraceString = ex2.InnerException.StackTrace.ToString(); } } Diagnostics.WriteMainDiagnosticInfo(TraceEventType.Error, TraceEventId.TraceGeneral, " Error in MonitorLocationTimes for TConnect: " + mt.Id + ". " + ex2.Message + " InnerEx: " + innerExString + " StackTrace: " + stackTraceString); } if (DateTime.UtcNow > mt.EndWindow) { //remove the tracking of notifications when monitoring is done locationNotificationTrackingDictionary.Remove(mt.InboundStep.TripId); Diagnostics.WriteMainDiagnosticInfo(TraceEventType.Verbose, TraceEventId.TraceGeneral, "TConnect Window Done. tConnect.Id=" + mt.Id); //The time has gone past the latest time we could possible issue a Request for this. //Set it to Done so we stop checking. mt.TConnectStatusId = (int)TConnectStatuses.Done; Uow.Repository <TConnect>().Update(mt); Uow.Repository <TripEvent>().Insert(new TripEvent(mt.InboundStep.TripId, "Done monitoring T-Connect. Current time outside End Window")); Uow.Save(); } else if (mt.InboundStep.StartDate > DateTime.UtcNow) { //leg to monitor has not started yet. nothing to do. Diagnostics.WriteMainDiagnosticInfo(TraceEventType.Verbose, TraceEventId.TraceGeneral, "TConnect not yet started. tConnect.Id=" + mt.Id); } else { Diagnostics.WriteMainDiagnosticInfo(TraceEventType.Verbose, TraceEventId.TraceGeneral, "Monitored TConnect. tConnect.Id=" + mt.Id); DateTime newEta = await GetEtaForTConnectAsync(Uow, mt, busSchedulerAPIs); if (newEta.Year > new DateTime().Year) { //Check to make sure we have not generally "arrived" by comparing the time the probe was posted to the eta calculated. LastVehiclePosition lvp = Uow.Repository <LastVehiclePosition>().Query().Get() .FirstOrDefault(v => v.VehicleName == mt.InboundVehicle); if (lvp != null && newEta - lvp.PositionTimestamp <= System.TimeSpan.FromMinutes(1)) { //We are there. Time to stop monitoring. mt.TConnectStatusId = (int)TConnectStatuses.Done; Uow.Repository <TConnect>().Update(mt); Uow.Repository <TripEvent>().Insert(new TripEvent(mt.InboundStep.TripId, "Done monitoring T-Connect. Bus has arrived.")); Uow.Save(); } else { MonitorTConnect(Uow, mt, newEta); } } else { Uow.Repository <TripEvent>().Insert(new TripEvent(mt.InboundStep.TripId, "Unable to calculate ETA")); } } } catch (Exception ex) { String innerExString = ""; String stackTraceString = ""; if (ex.InnerException != null) { innerExString = ex.InnerException.Message; if (ex.InnerException.StackTrace != null) { stackTraceString = ex.InnerException.StackTrace.ToString(); } } Diagnostics.WriteMainDiagnosticInfo(TraceEventType.Error, TraceEventId.TraceGeneral, " Error in MonitorTConnects for TConnect: " + mt.Id + ". " + ex.Message + " InnerEx: " + innerExString + " StackTrace: " + stackTraceString); } } }
private void MonitorTravelerLocation(TConnect mt) { try { if (DateTime.UtcNow >= mt.InboundStep.StartDate && DateTime.Now <= mt.InboundStep.StartDate.AddMinutes(1)) { //get traveler location int travelerId = mt.InboundStep.Trip.TravelerId; var travelerLocations = Uow.Repository <TravelerLocation>().Query() .Get().Where(s => s.TravelerId == travelerId).OrderByDescending(s => s.PositionTimestamp); var lastTravelerLocation = travelerLocations.FirstOrDefault <TravelerLocation>(); if (lastTravelerLocation != null) { //get vehicle location nearest to the traveler location time LastVehiclePosition lvp = Uow.Repository <LastVehiclePosition>().Query().Get().First(v => v.VehicleName == mt.InboundVehicle); //get distance between them double dist_m = Conversions.distanceMeters(lastTravelerLocation.Latitude, lastTravelerLocation.Longitude, lvp.Latitude, lvp.Longitude); //get time between the vehilcle location and the traveler location TimeSpan tsDiff = lastTravelerLocation.PositionTimestamp - lvp.PositionTimestamp; //do a fuzzy match, like distance within 100 meters if (Math.Abs(tsDiff.TotalSeconds) < 30) { if (dist_m > 100) { //We are there. Time to stop monitoring. mt.TConnectStatusId = (int)TConnectStatuses.Done; Uow.Repository <TConnect>().Update(mt); Uow.Repository <TripEvent>().Insert(new TripEvent(mt.InboundStep.TripId, "Done monitoring T-Connect." + " Traveler never arrived and is " + dist_m.ToString() + " meters away." + " Traveler loc (" + lastTravelerLocation.Latitude.ToString() + "," + lastTravelerLocation.Longitude.ToString() + ") @ " + lastTravelerLocation.PositionTimestamp.ToString("hh:mm:ss") + " Vehicle loc (" + lvp.Latitude.ToString() + "," + lvp.Longitude.ToString() + ") @ " + lvp.PositionTimestamp.ToString("hh:mm:ss") )); Uow.Save(); } } } } }catch (Exception ex) { String innerExString = ""; String stackTraceString = ""; if (ex.InnerException != null) { innerExString = ex.InnerException.Message; if (ex.InnerException.StackTrace != null) { stackTraceString = ex.InnerException.StackTrace.ToString(); } } Diagnostics.WriteMainDiagnosticInfo(TraceEventType.Error, TraceEventId.TraceGeneral, " Error in MonitorTravelerLocation for TConnect: " + mt.Id + ". " + ex.Message + " InnerEx: " + innerExString + " StackTrace: " + stackTraceString); } }