private void ProcessReconcilation(object state)
        {
            Instance instance = CreateInstance();

            if (!instance.IsValid)
            {
                return;
            }

            string result = String.Empty;

            try
            {
                result = EgateAPI.egAuthRequest(instance.ID, EgateAPI.ProtocolID, CreateRequest(OperationType.TotalsReconcilation).Append(" 1 1").ToString()).GetString();
            }
            finally
            {
                ReleaseInstance(instance);
            }

            if (!IsValidResult(result))
            {
                OnError(instance);
            }

            _reconcilation = ReconcilationState.Completed;
            OnMessage(String.Format("Результат сверки итогов : {0}", result), false);
        }
        private void AcquireKey()
        {
            if (Ready)
            {
                return;
            }

            Instance instance = null;

            try
            {
                instance = CreateInstance();
                string result = EgateAPI.egAuthRequest(instance.ID, EgateAPI.ProtocolID, CreateRequest(OperationType.RequestWorkKey).ToString()).GetString();
                if (!IsValidResult(result))
                {
                    OnError(instance);
                    return;
                }

                OnMessage(String.Format("Ответ на запрос ключа : {0}", result), false);

                //int code = GetStatusCode(result);
                //if (!AuthResponse.IsValidCode(code))
                //{
                //  OnError(instance);
                //  return;
                //}

                result = EgateAPI.egGetAuthResult(instance.ID).GetString();
                if (!IsValidResult(result))
                {
                    OnError(instance);
                    return;
                }

                AuthResponse response = null;
                try
                {
                    response = AuthResponse.Parse(result, null);
                }
                catch (Exception x)
                {
                    OnMessage(x.Message, true);
                }

                if ((response == null) || !AuthResponse.IsValidCode(response.GCSCode))
                {
                    return;
                }

                _keyAcqiured = true;
                OnMessage(String.Format("Ключ получен успешно : {0}", response), false);
            }
            finally
            {
                ReleaseInstance(instance);
            }
        }
        protected override void DisposeManagedResources()
        {
            if (!IsValid)
            {
                return;
            }

            EgateAPI.egReleaseInstance(_id);
            _id = 0;
        }
        private void ProcessCancelTransaction(object state)
        {
            string status = EgateAPI.egGetOpStatus(_current.ID, true).GetString();

            _cancel = (status[0] == EgateAPI.StatusResult) ? CancelState.Completed : CancelState.None;
            OnMessage(String.Format("Транзакция отменена : {0}", _cancel), false);

            if (_cancel == CancelState.Completed)
            {
                Gazprom.TransactionCancelled++;
            }
        }
 static BankingDevice()
 {
     try
     {
         uint v = EgateAPI.egGetVersion();
         Version = String.Format("API v{0}.{1}.{2}.{3} ({4})", (int)(v / 1000000), (int)((v % 1000000) / 10000), (int)((v % 10000) / 100), v % 100,
                                 EgateAPI.egGetVersionDescription().GetString());
     }
     catch (Exception e)
     {
         Version = String.Concat("Не удалось определить версию", Environment.NewLine, e.Message);
     }
 }
        public bool BeginTransaction(int receiptID, int amount)
        {
            AcquireKey();
            if (!Ready)
            {
                OnMessage("Оборудование не готово", true);
                return(false);
            }

            if (_state != EquipmentState.Idle)
            {
                OnMessage("Транзакция уже выполняется", true);
                return(false);
            }

            string request = CreateRequest(OperationType.Payment)
                             .Append(EgateAPI.ParameterSeparator)
                             .Append(amount)
                             .Append(EgateAPI.ParameterSeparator)
                             .Append(receiptID)
                             .ToString();

            OnMessage(String.Format("Транзакция начинается : {0}", request), false);

            _current = CreateInstance();
            string result = EgateAPI.egAuthRequestAsync(_current.ID, EgateAPI.ProtocolID, request).GetString();

            if (!result.StartsWith(EgateAPI.AsyncRequestStartedString))
            {
                //EgateAPI.egGetOpStatus(_instanceID, true);
                OnError(_current);
                return(false);
            }

            OnMessage(String.Format("Запрос отправлен : {0}", result), false);

            _cancel = CancelState.None;
            _state  = EquipmentState.Started;
            OnStateChanged(_state, String.Empty);
            _timer.Start();

            return(true);
        }
        private void ProcessTransaction(object sender, ElapsedEventArgs e)
        {
            string status = EgateAPI.egGetOpStatus(_current.ID, false).GetString();

            if (status[0] == EgateAPI.StatusResult)
            {
                _timer.Stop();
                OnMessage(String.Format("Запрос обработан : {0}", status.Substring(1)), false);

                AuthResponse response = null;
                try
                {
                    byte[] ba;
                    string result = EgateAPI.egGetAuthResult(_current.ID).GetString(out ba);
                    if (IsValidResult(result))
                    {
                        _lastTransactionResult = (_cancel == CancelState.Completed) ? TransactionResult.Cancelled : TransactionResult.OK;
                    }
                    else
                    {
                        _lastTransactionResult = TransactionResult.Error;
                        OnError(_current);
                    }

                    response = AuthResponse.Parse(result, ba);
                }
                catch (Exception x)
                {
                    OnMessage(x.Message, true);
                }
                finally
                {
                    OnTransactionCompleted(_lastTransactionResult, response);
                    _state = EquipmentState.CardTaken;
                    OnStateChanged(_state, status.Substring(1));
                }
            }
            else if (status[0] == EgateAPI.StatusBusy)
            {
                EquipmentState s = _state;
                switch (status[1])
                {
                case EgateAPI.StatusWaitCard:
                    //if (_state == EquipmentState.Started)
                    s = EquipmentState.WaitingForCard;
                    break;

                case EgateAPI.StatusCardEntered:
                    //if (_state == EquipmentState.WaitingForCard)
                    s = EquipmentState.CardInserted;
                    break;

                case EgateAPI.StatusPinEntered:
                    if (_state == EquipmentState.CardInserted)
                    {
                        s = EquipmentState.PinEntered;
                    }
                    break;

                case EgateAPI.StatusAuthCompleted:
                    if ((_state != EquipmentState.Started) && (_state != EquipmentState.WaitingForCard))
                    {
                        s = EquipmentState.AuthCompleted;
                    }
                    break;

                default:
                    s = _state;
                    break;
                }

                if (_state != s)
                {
                    _state = s;
                    OnStateChanged(_state, status.Substring(2));
                }
            }
        }
        private void OnError(Instance instance)
        {
            int error = EgateAPI.egGetLastError(instance.ID);

            OnMessage(new StringBuilder().AppendFormat("Ошибка #{0}", error).AppendLineIfNotEmpty(EgateAPI.egGetErrorDescription(error).GetString()).ToString(), true);
        }
 public Instance()
 {
     _id = EgateAPI.egInitInstance(Path.Combine(ApplicationServices.GetApplicationRootPath(), EgateAPI.EgateConfigPath));
 }