Пример #1
0
        private async Task ScanAsync(string scanId, ScanData data)
        {
            using (var lck = data.GetLock())
            {
                if (lck.IsCaptured)
                {
                    data.IncAttempts();

                    using (var service = CreateService())
                    {
                        var item  = data.Item;
                        var train = await service.FetchTrainAsync(item.Date, item.Source, item.Destination, item.TrainNumber);

                        if (train != null)
                        {
                            CoachType[] coachTypes;

                            if (!String.IsNullOrEmpty(item.CoachType))
                            {
                                var coachType = FindCoachType(train, item.CoachType);

                                if (coachType == null)
                                {
                                    HandleError(scanId, data, $"Coach type {item.CoachType} not found");
                                    return;
                                }

                                coachTypes = new[] { coachType };
                            }
                            else
                            {
                                coachTypes = train.CoachTypes;
                            }

                            var sessionId = await BookAsync(train, coachTypes, item.FirstName, item.LastName);

                            if (!String.IsNullOrEmpty(sessionId))
                            {
                                await _successCallbackAsync(item.CallbackId, sessionId);

                                Abort(scanId);
                            }
                            else
                            {
                                HandleError(scanId, data, "No vacant seats");
                            }
                        }
                        else
                        {
                            HandleError(scanId, data, $"Train {item.TrainNumber} not found");
                        }
                    }
                }
            }
        }
Пример #2
0
        private async Task ScanAsync(string scanId, ScanData data)
        {
            if (data.Error != null)
            {
                return;
            }

            using (var lck = data.GetLock())
            {
                if (lck.IsCaptured)
                {
                    data.IncAttempts();

                    using (var service = CreateService())
                    {
                        var item  = data.Item;
                        var train = await service.FetchTrainAsync(item.Date, item.Source, item.Destination, item.TrainNumber);

                        if (train != null)
                        {
                            if (train.CoachTypes != null && train.CoachTypes.Length > 0)
                            {
                                CoachType[] coachTypes = null;

                                if (!String.IsNullOrEmpty(item.CoachType))
                                {
                                    var coachType = FindCoachType(train, item.CoachType);

                                    if (coachType != null)
                                    {
                                        coachTypes = new[] { coachType };
                                    }
                                    //else if (!allowOtherCoachTypes)
                                    //{
                                    //	HandleError(scanId, data, "No vacant coaches " + item.CoachType, false);
                                    //	return;
                                    //}
                                }

                                if (coachTypes == null)
                                {
                                    coachTypes = train.CoachTypes;
                                }

                                var sessionId = await BookAsync(train, coachTypes, item.FirstName, item.LastName);

                                if (!String.IsNullOrEmpty(sessionId))
                                {
                                    var msg = $"Reserved ticket for [{item.ScanSource}]: {sessionId}";
                                    _log.Info(msg);
                                    ScanEvent?.Invoke(this, new ScanEventArgs(item.CallbackId, msg));
                                    //Abort(scanId); rebook again in case it is bought by requester
                                    return;
                                }
                            }

                            HandleError(scanId, data, "No vacant seats", false);
                        }
                        else
                        {
                            HandleError(scanId, data, $"Train {item.TrainNumber} not found", true);
                        }
                    }
                }
            }
        }