//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 void UpdateTrip(Int16 carId, Int64 tripId)
        {
            DBController dbc = new DBController();

            Trip trip = new Trip(tripId, carId);
            List<Fact> facts = dbc.GetFactsByTripIdNoQuality(tripId);

            //Getting the previous tripid and seconds to previous trip
            List<Int64> tripIds = dbc.GetTripIdsByCarId(carId);
            //In case this is the first trip - ignore computing measures for previous trip
            if (tripIds.Count > 1) {
                Int64 latestTrip = tripIds[tripIds.Count() - 2];
                Trip previousTrip = dbc.GetTripByCarIdAndTripId(carId, latestTrip);
                trip.SecondsToLag = MeasureCalculator.SecondsToLag(facts[0].Temporal.Timestamp, previousTrip.EndTemporal.Timestamp);
            } else {
                trip.SecondsToLag = new TimeSpan(0, 0, -1);
            }
            //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 Dictionary<string, double> MetricNormalized(Trip trip)
        {
            Dictionary<string, double> result = new Dictionary<string, double>();
            DBController dbc = new DBController();

            List<Fact> facts = dbc.GetFactsByTripIdNoQuality(trip.TripId);

            double tempint = trip.MetersDriven / 1000;

            result.Add("Speeding", 0);
            result.Add("Accelerations", 0);
            result.Add("Brakes", 0);
            result.Add("Jerks", 0);

            foreach (Fact fact in facts) {
                if (fact.Flag.Speeding) {
                    result["Speeding"]++;
                }
            }
            if (result["Speeding"] != 0) {
                result["Speeding"] = result["Speeding"] / tempint;
            }

            result["Accelerations"] = ((double)trip.AccelerationCount / tempint);
            result["Brakes"] = ((double)trip.BrakeCount / tempint);
            result["Jerks"] = ((double)trip.JerkCount / tempint);

            dbc.Close();

            return result;
        }