private static double GetGpsDistance(List <TLGpsDataEvent> gpsDataEvents) { var distance = 0.0; var firstPos = gpsDataEvents.First(); foreach (var p in gpsDataEvents.Skip(1)) { distance += Math.Round(GeofenceHelper.CalculateDistance(firstPos.Lat, firstPos.Long, p.Lat, p.Long), 2); firstPos = p; } return(distance); }
public async Task Consume(ConsumeContext <TlFuelEevents> context) { //await _semaphore.WaitAsync(); using (var contextFScope = _dbContextScopeFactory.Create()) { _db = contextFScope.DbContexts.Get <SmartFleetObjectContext>(); var fuelRecordMsg = context.Message.Events.OrderBy(x => x.DateTimeUtc).Last(); var lastRecord = await _db.FuelConsumptions.OrderByDescending(x => x.DateTimeUtc) // ReSharper disable once TooManyChainedReferences .FirstOrDefaultAsync(x => x.VehicleId == fuelRecordMsg.VehicleId); var entity = SetFuelConsumptionobject(fuelRecordMsg); // ReSharper disable once ComplexConditionExpression if (lastRecord != null && fuelRecordMsg.MileStoneCalculated || lastRecord == null) { if (lastRecord != null) { var firstPos = context.Message.TlGpsDataEvents.First(); foreach (var p in context.Message.TlGpsDataEvents.Skip(1)) { entity.Milestone += Math.Round(GeofenceHelper.CalculateDistance(firstPos.Lat, firstPos.Long, p.Lat, p.Long)); firstPos = p; } } } else if (!fuelRecordMsg.MileStoneCalculated) { entity.TotalFuelConsumed = fuelRecordMsg.FuelConsumption; } _db.FuelConsumptions.Add(entity); await contextFScope.SaveChangesAsync().ConfigureAwait(false); // _semaphore.Release(); } }