public static Dictionary<DayOfWeek, TimeSpan> AverageTripTimePerDay() { Dictionary<DayOfWeek, TimeSpan> timePerDay = new Dictionary<DayOfWeek, TimeSpan>(); timePerDay.Add(DayOfWeek.Monday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Tuesday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Wednesday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Thursday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Friday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Saturday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Sunday, new TimeSpan(0, 0, 0)); DBController dbc = new DBController(); List<Int16> carIds = dbc.GetCarIds(); Int64 tripCount = dbc.GetTripCount(); dbc.Close(); foreach (Int16 carId in carIds) { Dictionary<DayOfWeek, TimeSpan> carData = WeekdayStatistics.Time(carId); foreach (KeyValuePair<DayOfWeek, TimeSpan> entry in carData) { timePerDay[entry.Key] += entry.Value; } } foreach (KeyValuePair<DayOfWeek, TimeSpan> day in timePerDay) { timePerDay[day.Key] = new TimeSpan(timePerDay[day.Key].Ticks / carIds.Count); } return timePerDay; }
public static Dictionary<DayOfWeek, double> AverageTripDistancePerDay() { Dictionary<DayOfWeek, double> distancePerDay = new Dictionary<DayOfWeek, double>(); distancePerDay.Add(DayOfWeek.Monday, 0); distancePerDay.Add(DayOfWeek.Tuesday, 0); distancePerDay.Add(DayOfWeek.Wednesday, 0); distancePerDay.Add(DayOfWeek.Thursday, 0); distancePerDay.Add(DayOfWeek.Friday, 0); distancePerDay.Add(DayOfWeek.Saturday, 0); distancePerDay.Add(DayOfWeek.Sunday, 0); DBController dbc = new DBController(); List<Int16> carIds = dbc.GetCarIds(); Int64 tripCount = dbc.GetTripCount(); dbc.Close(); foreach (Int16 carId in carIds) { Dictionary<DayOfWeek, double> carData = WeekdayStatistics.Distance(carId); foreach (KeyValuePair<DayOfWeek, double> entry in carData) { distancePerDay[entry.Key] += entry.Value; } } foreach (KeyValuePair<DayOfWeek, double> day in distancePerDay) { distancePerDay[day.Key] /= tripCount; } return distancePerDay; }
public static Dictionary<DayOfWeek, double> Distance(Int16 carId) { Dictionary<DayOfWeek, double> distancePerDay = new Dictionary<DayOfWeek, double>(); distancePerDay.Add(DayOfWeek.Monday, 0); distancePerDay.Add(DayOfWeek.Tuesday, 0); distancePerDay.Add(DayOfWeek.Wednesday, 0); distancePerDay.Add(DayOfWeek.Thursday, 0); distancePerDay.Add(DayOfWeek.Friday, 0); distancePerDay.Add(DayOfWeek.Saturday, 0); distancePerDay.Add(DayOfWeek.Sunday, 0); TimeSpan day = new TimeSpan(24, 0, 0); DBController dbc = new DBController(); List<Int64> tripIds = dbc.GetTripIdsByCarId(carId); dbc.Close(); foreach (Int64 tripId in tripIds) { List<Fact> entries = dbc.GetSpatioTemporalByCarIdAndTripId(carId, tripId); for (int i = 1; i < entries.Count; i++) { distancePerDay[entries[i].Temporal.Timestamp.DayOfWeek] += entries[i].Spatial.DistanceToLag; } } return distancePerDay; }
public static Dictionary<DayOfWeek, TimeSpan> Time(Int16 carId) { Dictionary<DayOfWeek, TimeSpan> timePerDay = new Dictionary<DayOfWeek, TimeSpan>(); timePerDay.Add(DayOfWeek.Monday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Tuesday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Wednesday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Thursday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Friday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Saturday, new TimeSpan(0, 0, 0)); timePerDay.Add(DayOfWeek.Sunday, new TimeSpan(0, 0, 0)); DBController dbc = new DBController(); List<Int64> tripIds = dbc.GetTripIdsByCarId(carId); dbc.Close(); foreach (Int64 tripId in tripIds) { List<TemporalInformation> entries = dbc.GetTimestampsByCarIdAndTripId(carId, tripId); for (int i = 1; i < entries.Count; i++) { timePerDay[entries[i].Timestamp.DayOfWeek] += entries[i].SecondsToLag; } } return timePerDay; }
//Updates trip without PreviousTripId, localtripId and Secondstolag public static void UpdateExistingTrip(Int16 carId, Int64 tripId) { DBController dbc = new DBController(); Trip trip = new Trip(tripId, carId); List<Fact> facts = dbc.GetFactsByTripIdNoQuality(tripId); //Calc the trip updates trip = UpdateTrip(trip, facts, dbc); //Compute the scores trip.OptimalScore = FinalScore.CalculateOptimalScore(trip); List<double> fullscores = FinalScore.CalculateTripScores(trip); trip.RoadTypeScore = fullscores[0]; trip.CriticalTimeScore = fullscores[1]; trip.SpeedingScore = fullscores[2]; trip.AccelerationScore = fullscores[3]; trip.BrakeScore = fullscores[4]; trip.JerkScore = fullscores[5]; trip.TripScore = fullscores[6]; //Update the trip in the database dbc.UpdateTripFactWithMeasures(trip); dbc.Close(); }
public static List<INFATITrip> CalculateTripsByCarId(Int16 carId) { DBController dbc = new DBController(); //Fetch all temporalinformation for a cardId List<TemporalInformation> datapoints = dbc.GetTimestampsByCarId(carId); dbc.Close(); //Instantiate the containers for trips and timestamps fetched by a single date at a time List<INFATITrip> allTrips = new List<INFATITrip>(); INFATITrip trip = new INFATITrip(carId); allTrips.Add(trip); //First case allTrips.Last().Timestamps.Add(new TemporalInformation(datapoints.ElementAt(0).EntryId, datapoints.ElementAt(0).Timestamp)); //Starting to iterate over all timestamps for (int i = 1; i < datapoints.Count(); i++) { //Compare the last seen timestamp to the current, if more than 300 seconds has past, create a new trip and store the current timestamp in it if (Math.Abs(ToUnixTime(allTrips.Last().Timestamps.Last().Timestamp) - ToUnixTime(datapoints.ElementAt(i).Timestamp)) <= 180) { allTrips.Last().Timestamps.Add(new TemporalInformation(datapoints.ElementAt(i).EntryId, datapoints.ElementAt(i).Timestamp)); } else { allTrips.Add(new INFATITrip(carId)); allTrips.Last().Timestamps.Add(new TemporalInformation(datapoints.ElementAt(i).EntryId, datapoints.ElementAt(i).Timestamp)); } } return allTrips; }
public static List<Int64> RemoveOutliers(Int16 carId, Int64 tripId) { List<Fact> facts = new List<Fact>(); DBController dbc = new DBController(); facts = dbc.GetFactsByCarIdAndTripId(carId, tripId); dbc.Close(); List<Int64> OutlierIds = new List<Int64>(); for (int i = 1; i < facts.Count(); i++) { double distanceTravelled = facts[i].Spatial.MPoint.GetDistanceTo(facts[i - 1].Spatial.MPoint); double currentSpeed = Math.Max(facts[i].Measure.Speed, facts[i - 1].Measure.Speed) + 15; currentSpeed = currentSpeed * 3.6; if (distanceTravelled / (facts[i].Temporal.Timestamp - facts[i - 1].Temporal.Timestamp).TotalSeconds > currentSpeed || facts[i].Temporal.Timestamp == facts[i - 1].Temporal.Timestamp) { OutlierIds.Add(facts[i].EntryId); } } if (OutlierIds.Count > facts.Count * 0.1) { //Console.WriteLine(tripId + " is invalid"); } return OutlierIds; }
public static Dictionary<DayOfWeek, int> Plots(Int16 carId) { Dictionary<DayOfWeek, int> plotsPerDay = new Dictionary<DayOfWeek, int>(); plotsPerDay.Add(DayOfWeek.Monday, 0); plotsPerDay.Add(DayOfWeek.Tuesday, 0); plotsPerDay.Add(DayOfWeek.Wednesday, 0); plotsPerDay.Add(DayOfWeek.Thursday, 0); plotsPerDay.Add(DayOfWeek.Friday, 0); plotsPerDay.Add(DayOfWeek.Saturday, 0); plotsPerDay.Add(DayOfWeek.Sunday, 0); //Fetch all timestamps for all trips on car DBController dbc = new DBController(); List<Int64> tripIds = dbc.GetTripIdsByCarId(carId); List<TemporalInformation> timestamps = new List<TemporalInformation>(); foreach (Int64 tripId in tripIds) { timestamps = dbc.GetTimestampsByCarIdAndTripId(carId, tripId); } dbc.Close(); foreach (TemporalInformation timestamp in timestamps) { plotsPerDay[timestamp.Timestamp.DayOfWeek] += 1; } return plotsPerDay; }
static void Main(string[] args) { DBController dbc = new DBController(); List<Trip> trips = dbc.GetAllTrips(); for(int i = 0; i > trips.Count; i++) { GPSFactUpdater.UpdateRawGPS((Int16)trips[i].CarId, trips[i].TripId); List<Fact> facts = new List<Fact>(); facts = dbc.GetFactsByTripId(trips[i].TripId); trips[i] = TripFactUpdater.UpdateTripWithCountsAndIntervals(trips[i], facts, dbc); dbc.UpdateTripFactWithCounts(trips[i]); dbc.UpdateTripFactWithIntervals(trips[i]); } /* List<Trip> trips = dbc.GetTripsByCarId(34); Console.WriteLine("AverageTripPercentage:"); Console.WriteLine(UserProfile.AverageTripPercentage(trips).ToString()); Console.WriteLine("AverageMetricPercentage:"); UserProfile.print(UserProfile.AverageMetricPercentage(trips)); Console.WriteLine("AverageMetricNormalized:"); UserProfile.print(UserProfile.AverageMetricNormalized(trips)); Console.WriteLine("AverageMetricDegree:"); UserProfile.print(UserProfile.AverageMetricDegree(trips)); Console.WriteLine("CorrelationMatrix:"); Double[,] matrix = MetricCorrelation.getCorrelationMatrix(trips); MetricCorrelation.printMatrix(matrix); */ Console.WriteLine("Aaaaand its done"); Console.ReadLine(); }
public static TimeSpan Duration(Int16 carId, Int64 tripId) { DBController dbc = new DBController(); List<TemporalInformation> timestamps = dbc.GetTimestampsByCarIdAndTripId(carId, tripId); dbc.Close(); return timestamps[timestamps.Count - 1].Timestamp - timestamps[0].Timestamp; }
public static void Update(Int16 CarId) { DBController dbc = new DBController(); List<Int64> tripIds = dbc.GetTripIdsByCarId(CarId); foreach (Int64 tripId in tripIds) { List<Fact> facts = dbc.GetFactsByTripId(tripId); dbc.UpdateGPSFactWithMeasures(UpdatedFacts(facts)); } dbc.Close(); }
public string GetCar(Int16 carid) { try { throw new Exception("something broke"); } catch (Exception e) { Console.WriteLine(e.ToString()); DBController dbc = new DBController(); dbc.AddLog("GetCar?carid={carid}", carid, null, null, e.ToString().Substring(0, Math.Min(e.ToString().Count(), 254)), ""); dbc.Close(); } return ""; }
public static Dictionary<int, List<Fact>> NegativeAccelerationsBelowThreshold(Int16 carId, Int64 tripId, double threshold, double ignorableSpeed, TimeSpan ignorableTime, double ignorableDistance) { Dictionary<int, List<Fact>> negativeAccelerationsBelowThreshold = new Dictionary<int, List<Fact>>(); int accelerationCount = 0; DBController dbc = new DBController(); List<Fact> speedData = dbc.GetSpeedInformationByCarIdAndTripId(carId, tripId); dbc.Close(); DateTime accelerationStartPoint = speedData[0].Temporal.Timestamp; double accelerationDistance = 0; bool belowThreshold = false; List<Fact> accelerationPoints = new List<Fact>(); //For the entire trip foreach (Fact fact in speedData) { //If entry indicates acceleration below threshold, and above ignorable speeds if (fact.Measure.Speed > ignorableSpeed && fact.Measure.Acceleration < threshold) { accelerationDistance += fact.Spatial.DistanceToLag; //If not previously below threshold, remember the time where driver began accelerating if (!belowThreshold) { accelerationStartPoint = fact.Temporal.Timestamp; accelerationPoints.Add(fact); belowThreshold = true; } //If driver is not below threshold } else { //If previously below threshold, add the facts result if (belowThreshold) { if (fact.Temporal.Timestamp - accelerationStartPoint >= ignorableTime && accelerationDistance > ignorableDistance) { negativeAccelerationsBelowThreshold.Add(accelerationCount, accelerationPoints); accelerationCount++; } accelerationPoints = new List<Fact>(); accelerationDistance = 0; belowThreshold = false; } } } //Handle case where last entry in trip is still speeding if (belowThreshold) { if (speedData[speedData.Count - 1].Temporal.Timestamp - accelerationStartPoint >= ignorableTime && accelerationDistance > ignorableDistance) { negativeAccelerationsBelowThreshold.Add(accelerationCount, accelerationPoints); } } return negativeAccelerationsBelowThreshold; }
public static void GetTimePlot(Int16 carId, Int64 tripId) { DBController dbc = new DBController(); List<TemporalInformation> temporalData = dbc.GetTimestampsByCarIdAndTripId(carId, tripId); dbc.Close(); List<TimeSpan> intervals = new List<TimeSpan>(); for (int i = 1; i < temporalData.Count; i++) { TimeSpan interval = temporalData[i].Timestamp - temporalData[i - 1].Timestamp; intervals.Add(interval); } }
public static TimeSpan AverageTotalTime() { TimeSpan totalTime = new TimeSpan(0, 0, 0); DBController dbc = new DBController(); List<Int16> carIds = dbc.GetCarIds(); dbc.Close(); foreach (Int16 carId in carIds) { totalTime += CarStatistics.TotalTime(carId); } return new TimeSpan(totalTime.Ticks / carIds.Count); }
public static void GetSatHdopPlot(Int16 carId, Int64 tripId) { DBController dbc = new DBController(); List<QualityInformation> qualityData = dbc.GetQualityByCarIdAndTripId(carId, tripId); dbc.Close(); List<Int16> Sat = new List<Int16>(); List<double> Hdop = new List<double>(); foreach(QualityInformation quality in qualityData) { Sat.Add(quality.Sat); Hdop.Add(quality.Hdop); } }
public static List<double> DistancePerTrip(Int16 carId) { DBController dbc = new DBController(); List<Int64> tripIds = dbc.GetTripIdsByCarId(carId); dbc.Close(); List<double> kilometersPerTrip = new List<double>(); foreach (Int64 tripId in tripIds) { kilometersPerTrip.Add(TripStatistics.Distance(carId, tripId)); } return kilometersPerTrip; }
public void CompetitionSignUp(Int16 carid, Int16 competitionid) { try { DBController dbc = new DBController(); dbc.CompetitionSignUp(carid, competitionid); dbc.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); DBController dbc = new DBController(); dbc.AddLog("CompetitionSignUp?carid={carid}&competitionid={competitionid}", carid, null, competitionid, e.ToString().Substring(0, Math.Min(e.ToString().Count(), 254)), ""); dbc.Close(); } return; }
public static double Distance(Int16 carId, Int64 tripId) { DBController dbc = new DBController(); List<SpatialInformation> entries = dbc.GetMPointsByCarIdAndTripId(carId, tripId); dbc.Close(); double distance = 0; for (int i = 1; i < entries.Count - 1; i++) { distance += entries[i].MPoint.GetDistanceTo(entries[i - 1].MPoint); } return distance /= 1000; }
public static List<TimeSpan> TimePerTrip(Int16 carId) { DBController dbc = new DBController(); List<Int64> tripIds = dbc.GetTripIdsByCarId(carId); dbc.Close(); List<TimeSpan> minutesPerTrip = new List<TimeSpan>(); foreach (Int64 tripId in tripIds) { minutesPerTrip.Add(TripStatistics.Duration(carId, tripId)); } return minutesPerTrip; }
public static double AverageTotalDistance() { double totalDistance = 0; DBController dbc = new DBController(); List<Int16> carIds = dbc.GetCarIds(); dbc.Close(); foreach (Int16 carId in carIds) { totalDistance += CarStatistics.TotalDistance(carId); } return totalDistance / carIds.Count; }
public void UpdateCarWithUsername(Int16 carid, string username) { try { DBController dbc = new DBController(); dbc.UpdateCarWithUsername(carid, username); dbc.Close(); } catch(Exception e) { Console.WriteLine(e.ToString()); DBController dbc = new DBController(); dbc.AddLog("UpdateCarWithUsername?carid={carid}&username={username}", carid, null, null, e.ToString().Substring(0, Math.Min(e.ToString().Count(), 254)), username); dbc.Close(); } return; }
public string AddTripToCompetition(Int16 competitionid, Int16 carid, Int64 tripid) { try { DBController dbc = new DBController(); dbc.UpdateWithCompetitionAttempt(competitionid, carid, tripid); dbc.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); DBController dbc = new DBController(); dbc.AddLog("AddTripToCompetition", carid, tripid, competitionid, e.ToString().Substring(0, Math.Min(e.ToString().Count(), 254)), ""); dbc.Close(); } return "succes"; }
public static Dictionary<SpatialInformation, SpatialInformation> ByTime(Int16 carId, Int64 tripId, TimeSpan MinimumGapTime) { Dictionary<SpatialInformation, SpatialInformation> tripGaps = new Dictionary<SpatialInformation, SpatialInformation>(); DBController dbc = new DBController(); List<Fact> facts = new List<Fact>(dbc.GetSpatioTemporalByCarIdAndTripId(carId, tripId)); dbc.Close(); for (int i = 1; i < facts.Count; i++) { if (facts[i].Temporal.Timestamp - facts[i - 1].Temporal.Timestamp > MinimumGapTime) { tripGaps.Add(facts[i-1].Spatial, facts[i].Spatial); } } return tripGaps; }
public static Dictionary<SpatialInformation, SpatialInformation> ByDistance(Int16 carId, Int64 tripId, double MinimumGapDistance) { Dictionary<SpatialInformation, SpatialInformation> tripGaps = new Dictionary<SpatialInformation, SpatialInformation>(); DBController dbc = new DBController(); List<Fact> facts = new List<Fact>(dbc.GetSpatioTemporalByCarIdAndTripId(carId, tripId)); dbc.Close(); for (int i = 1; i < facts.Count; i++) { if (facts[i].Spatial.Point.GetDistanceTo(facts[i - 1].Spatial.Point) > MinimumGapDistance) { tripGaps.Add(facts[i - 1].Spatial, facts[i].Spatial); } } return tripGaps; }
public void AddTest(Stream facts) { try { Console.WriteLine("Message Received"); StreamReader sr = new StreamReader(facts); string text = sr.ReadToEnd(); Console.WriteLine("It was: " + text); } catch (Exception e) { Console.WriteLine(e.ToString()); DBController dbc = new DBController(); dbc.AddLog("AddTest", null, null, null, e.ToString().Substring(0, 254), e.ToString().Substring(0, Math.Min(e.ToString().Count(), 499))); dbc.Close(); return; } return; }
public static double AverageDistancePerTrip(Int16 carId) { double totalDistance = 0; DBController dbc = new DBController(); Int64 tripCount = dbc.GetTripCountByCarId(carId); dbc.Close(); List<double> distancePerTrip = DistancePerTrip(carId); foreach (double distance in distancePerTrip) { totalDistance += distance; } return totalDistance / tripCount; }
public static double DistanceAbove(Int16 carId, Int64 tripId, double ignorableSpeed, TimeSpan ignorableTime, double ignorableDistance) { double distanceSped = 0; DBController dbc = new DBController(); List<Fact> speedData = dbc.GetSpeedInformationByCarIdAndTripId(carId, tripId); dbc.Close(); bool speeding = false; DateTime speedingStartPoint = speedData[0].Temporal.Timestamp; double speedingDistance = 0; //For the entire trip foreach (Fact fact in speedData) { //If entry indicates speeding if (fact.Measure.Speed > fact.Segment.MaxSpeed + ignorableSpeed) { speedingDistance += fact.Spatial.DistanceToLag; //If not previously speeding, remember the time where driver began speeding if (!speeding) { speedingStartPoint = fact.Temporal.Timestamp; speeding = true; } //If driver is not speeding } else { //If previously speeding, add the speeding-time to the total if (speeding) { if (fact.Temporal.Timestamp - speedingStartPoint >= ignorableTime && speedingDistance > ignorableDistance) { distanceSped += speedingDistance; } speedingDistance = 0; speeding = false; } } } //Handle case where last entry in trip is still speeding if (speeding) { if (speedData[speedData.Count - 1].Temporal.Timestamp - speedingStartPoint >= ignorableTime && speedingDistance > ignorableDistance) { distanceSped += speedingDistance; } } return distanceSped; }
public string GetAllCompetitions() { try { DBController dbc = new DBController(); List<Competition> allCompetitions = dbc.GetAllCompetitions(); dbc.Close(); return JsonConvert.SerializeObject(allCompetitions); } catch (Exception e) { Console.WriteLine(e.ToString()); DBController dbc = new DBController(); dbc.AddLog("GetAllCompetitions", null, null, null, e.ToString().Substring(0, Math.Min(e.ToString().Count(), 254)), ""); dbc.Close(); } return ""; }
public static List<Fact> Accelerations(Int16 carId, Int64 tripId) { DBController dbc = new DBController(); List<Fact> accelerationData = dbc.GetSpeedInformationByCarIdAndTripId(carId, tripId); dbc.Close(); for (int i = 1; i < accelerationData.Count(); i++) { //Acceleration = Velocity change / Time double velocityChange = accelerationData[i].Measure.Speed - accelerationData[i - 1].Measure.Speed; TimeSpan time = accelerationData[i].Temporal.Timestamp - accelerationData[i - 1].Temporal.Timestamp; double acceleration = velocityChange / time.Seconds; accelerationData[i].Measure.Acceleration = acceleration; } return accelerationData; }