Пример #1
0
        public async Task Process()
        {
            var pings = await pingRepository.GetUnprocessed();

            Ping previousPing = await pingRepository.GetLastPing(true);

            List <Ping> ridePings     = new List <Ping>();
            List <Ping> locationPings = new List <Ping>();

            foreach (var ping in pings)
            {
                if (!(previousPing is Ping))
                {
                    previousPing = ping;
                    continue;
                }

                var distance = utility.GetDistanceInMeters(previousPing, ping);
                var kmh      = utility.GetSpeed(previousPing.Time, ping.Time, distance);

                // Moving
                if (kmh >= Constants.MINIMUM_MOVING_SPEED)
                {
                    // Finish the locationPings before starting to move.
                    if (await IsValidLocation(locationPings))
                    {
                        await SaveLocationPings(locationPings);

                        // Reset the location list. These functions can append pings 1 at a time if needed
                        locationPings = new List <Ping>();

                        // Reset the ride list because if i moved inside a location i don't need to map that to a ride
                        ridePings = new List <Ping>();
                    }

                    ridePings.Add(ping);
                }
                // Not moving
                else
                {
                    if (await IsValidRide(ridePings))
                    {
                        await SaveRidePings(ridePings);

                        // Reset the location list. These functions can append pings 1 at a time if needed
                        ridePings = new List <Ping>();

                        // Reset the location list because if i'm standing at a trafic light don't create a location.
                        locationPings = new List <Ping>();
                    }
                    // If there is a ridePing stuck while at a valid location delete it so speed the process up
                    else if (ridePings.Count > 0 && await IsValidLocation(locationPings))
                    {
                        ridePings = new List <Ping>();
                    }

                    locationPings.Add(ping);
                }

                previousPing = ping;
            }

            // Save the last batch
            if (await IsValidLocation(locationPings))
            {
                await SaveLocationPings(locationPings);
            }

            if (await IsValidRide(ridePings))
            {
                await SaveRidePings(ridePings);
            }

            // Mark all as done
            if (pings.Count > 0)
            {
                var lastPing = await pingRepository.GetLastPing(true);

                var remainingPings = (await pingRepository.GetBetweenDates(pings[0].Time, lastPing.Time))
                                     .Where(p => p.Processed == 0);
                foreach (var ping in remainingPings)
                {
                    ping.Processed = 1;
                    pingRepository.Update(ping);
                }
                await pingRepository.SaveAsync();
            }

            // RecurringJob.AddOrUpdate<ProcessPings>("ProcessPings", x => x.Process(), Cron.Minutely);
        }