public void CalculateBasicData(Trip trip) { //Set duration trip.Duration = trip.TripData[trip.TripData.Count - 1].Timestamp - trip.TripData[0].Timestamp; //Calculate distance List <Tuple <DateTime, GpsCoordinate> > trace = GetGpsTrend(trip); for (int i = 0; i < trace.Count - 1; ++i) { trip.Distance += trace[i].Item2.GetDistance(trace[i + 1].Item2); } //Calculate average speed trip.AvgSpeed = trip.Duration.TotalHours > 0 ? (trip.Distance / 1000) / trip.Duration.TotalHours : 0; //Set start and end location if (trace.Count >= 2) { trip.StartLocation = trace[0].Item2; trip.EndLocation = trace[trace.Count - 1].Item2; } //Calculate fuel consumption if (trip.Vehicle != null) { var speedTrend = GetSpeedTrendObd(trip); var fuelCalculator = new FuelConsumption(trip.Vehicle.FuelType == FuelType.Diesel); //Calculates and sets the average consumption fuelCalculator.CalcFuelConsumption(trip, speedTrend); } }
public IActionResult GetFuelTrend(int tripId) { var user = _repo.GetUser(User); if (user == null) { return(Unauthorized()); } var trip = _db.Trips.Include(t => t.Vehicle) .Include(t => t.TripData).ThenInclude(d => d.SignalType) .FirstOrDefault(t => t.User.Id == user.Id && t.TripId == tripId); if (trip != null) { var tripStatistic = new TripStatistic(); var fuelConsumption = new FuelConsumption(trip.Vehicle?.FuelType == FuelType.Diesel); var speedTrend = tripStatistic.GetSpeedTrendObd(trip); var consumption = fuelConsumption.CalcFuelConsumption(trip, speedTrend); return(Ok(JsonConvert.SerializeObject(consumption.OrderBy(c => c.Item1)))); } return(NotFound()); }
private void RecalculateFuelConsumption(Trip trip) { var tripStatistic = new TripStatistic(); var fuelConsumption = new FuelConsumption(trip.Vehicle?.FuelType == FuelType.Diesel); var speedTrend = tripStatistic.GetSpeedTrendObd(trip); fuelConsumption.CalcFuelConsumption(trip, speedTrend); }