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"); } } } } }
//private async Task ScanAsync(string scanId, ScanData data) private async Task ScanAsync(UzService service, string scanId, ScanData data) { if (data.State == ScanEventType.Error) { return; } using (var lck = data.GetLock()) { if (lck.IsCaptured) { data.Attempts++; //using (var service = CreateService()) { var item = data.Item; var trains = await service.ListTrainsAsync(item.Date, item.Source, item.Destination); if (trains != null && trains.Length > 0) { var train = trains.GetByNumber(item.TrainNumber); if (train != null) { if (train.CoachTypes != null && train.CoachTypes.Length > 0) { CoachType[] coachTypes = null; if (!String.IsNullOrEmpty(item.CoachType)) { coachTypes = FindCoachTypes(train, item.CoachType); if (coachTypes.Length == 0 && item.ExactCoachType) { HandleError(scanId, data, "No vacant coaches " + item.CoachType, false); return; } } if (coachTypes == null || coachTypes.Length == 0) { coachTypes = train.CoachTypes; } var(sessionId, coach, seat) = await BookAsync(train, coachTypes, item.FirstName, item.LastName); if (!String.IsNullOrEmpty(sessionId)) { var msg = $"Reserved ticket for [{item.ScanSource}]:\nTrain: {train.Number} Coach: {coach.Number}-{coach.Type} Seat: {seat.Number} Price: {(seat.Price??0)/100m:##.00 UAH}\n{sessionId}"; _log.Info(msg); data.State = ScanEventType.Success; data.StateDescription = sessionId; ScanEvent?.Invoke(this, new ScanEventArgs(item.CallbackID, ScanEventType.Success, 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", false); } } else { HandleError(scanId, data, "No tickets selling for date/train", false); } } } } }
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); } } } } }