Exemple #1
0
 public CardStateEventArgs(RG_PIN_SATETS_16 pinStates, RG_DEVICE_STATUS_TYPE statusType, RG_CARD_INFO cardInfo, RG_CARD_MEMORY cardMemory)
 {
     this.pinStates  = pinStates;
     this.statusType = statusType;
     this.cardInfo   = cardInfo;
     this.cardMemory = cardMemory;
 }
        void AutoPollTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            RG_ENDPOINT portEndpoin = AutoPullDevice.Endpoint;
            byte        address     = AutoPullDevice.DeviceInfoShort.DeviceAddress;

            RG_PIN_SATETS_16      pinStates  = new RG_PIN_SATETS_16();
            RG_DEVICE_STATUS_TYPE statusType = RG_DEVICE_STATUS_TYPE.DS_UNKNONWN;
            RG_CARD_INFO          cardInfo   = new RG_CARD_INFO()
            {
                CardType = RG_CARD_TYPE_CODE.CT_UNKNOWN
            };
            RG_CARD_MEMORY cardMemory = new RG_CARD_MEMORY()
            {
                ProfileBlock = 0,
                MemBlock     = new byte[16]
            };

            uint errorCode = RG_GetStatus(ref portEndpoin, address,
                                          ref statusType, ref pinStates, ref cardInfo, ref cardMemory);

            if (errorCode != 0 && statusType == RG_DEVICE_STATUS_TYPE.DS_UNKNONWN)
            {
                throw new ApplicationException($"Ошибка {errorCode} при запросе статуса устройства");
            }

            var result = new CardStateEventArgs(pinStates, statusType, cardInfo, cardMemory);

            if (statusType != RG_DEVICE_STATUS_TYPE.DS_NOCARD)
            {
                logger.Debug($"Прочитана карта: {result.CardUid}");
            }
            СardStatusRead?.Invoke(this, result);
        }
        void AutoPollTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            RG_ENDPOINT portEndpoin = AutoPullDevice.Endpoint;
            byte        address     = AutoPullDevice.DeviceInfoShort.DeviceAddress;

            RG_PIN_SATETS_16      pinStates  = new RG_PIN_SATETS_16();
            RG_DEVICE_STATUS_TYPE statusType = RG_DEVICE_STATUS_TYPE.DS_UNKNONWN;
            RG_CARD_INFO          cardInfo   = new RG_CARD_INFO()
            {
                CardType = RG_CARD_TYPE_CODE.CT_UNKNOWN
            };
            RG_CARD_MEMORY cardMemory = new RG_CARD_MEMORY()
            {
                ProfileBlock = 0,
                MemBlock     = new byte[16]
            };

            if (ticksLeft <= 0)
            {
                if (stateNoCard)
                {
                    stateNoCard = false;
                    if (AutoPullDevice.DeviceInfoShort.DeviceAddress == 0)
                    {
                        random.NextBytes(lastUid);
                    }
                    else if (AutoPullDevice.DeviceInfoShort.DeviceAddress == 1)
                    {
                        NextStepReader1();
                    }
                    ticksLeft = 10;
                }
                else
                {
                    stateNoCard = true;
                    ticksLeft   = 25;
                }
            }
            else
            {
                ticksLeft--;
            }

            if (stateNoCard)
            {
                statusType = RG_DEVICE_STATUS_TYPE.DS_NOCARD;
            }
            else
            {
                statusType       = RG_DEVICE_STATUS_TYPE.DS_CARD;
                cardInfo.CardUid = lastUid;
            }

            var result = new CardStateEventArgs(pinStates, statusType, cardInfo, cardMemory);

            СardStatusRead?.Invoke(this, result);
        }
        private void poll(Action completeCallback)
        {
            try {
                while (!_pollStopEvent.WaitOne(200))
                {
                    if (_currentConnectoinContext != null)
                    {
                        RG_ENDPOINT portEndpoin = _currentConnectoinContext.ReaderPort;
                        byte        address     = _currentConnectoinContext.ReaderAddress;

                        RG_PIN_SATETS_16      pinStates  = new RG_PIN_SATETS_16();
                        RG_DEVICE_STATUS_TYPE statusType = RG_DEVICE_STATUS_TYPE.DS_UNKNONWN;
                        RG_CARD_INFO          cardInfo   = new RG_CARD_INFO()
                        {
                            CardType = RG_CARD_TYPE_CODE.CT_UNKNOWN
                        };
                        RG_CARD_MEMORY cardMemory = new RG_CARD_MEMORY()
                        {
                            ProfileBlock = 0,
                            MemBlock     = new byte[16]
                        };

                        uint errorCode = UnmanagedContext.Instance.RG_GetStatus(ref portEndpoin, address,
                                                                                ref statusType, ref pinStates, ref cardInfo, ref cardMemory);
                        if (errorCode != 0 && statusType == RG_DEVICE_STATUS_TYPE.DS_UNKNONWN)
                        {
                            throw new ApiCallException("Ошибка при запросе статуса устройства", errorCode);
                        }

                        Debug.WriteLine("Тампер: {0} Контрольный выход: {1}", pinStates.Pin00, pinStates.Pin01);

                        readerDataGroup.InvokeIfRequired(() => {
                            cardCodeTextBox.BackColor   = pinStates.Pin00 ? Color.DarkSalmon : Color.LightGreen;
                            cardTypeTextBox.BackColor   = pinStates.Pin00 ? Color.DarkSalmon : Color.LightGreen;
                            cardUidTextBox.BackColor    = pinStates.Pin00 ? Color.DarkSalmon : Color.LightGreen;
                            cardMemoryTextBox.BackColor = pinStates.Pin00 ? Color.DarkSalmon : Color.LightGreen;
                            profileNumBox.BackColor     = pinStates.Pin00 ? Color.DarkSalmon : Color.LightGreen;
                        });

                        switch (statusType)
                        {
                        case RG_DEVICE_STATUS_TYPE.DS_NOCARD: {
                            readerDataGroup.InvokeIfRequired(() => {
                                    cardCodeTextBox.Text   = "";
                                    cardTypeTextBox.Text   = "";
                                    cardUidTextBox.Text    = "";
                                    profileNumBox.Text     = "";
                                    cardMemoryTextBox.Text = "";
                                });
                            break;
                        }

                        case RG_DEVICE_STATUS_TYPE.DS_CARD: {
                            string enumFieldName = Enum.GetName(typeof(RG_CARD_TYPE_CODE), cardInfo.CardType);
                            string uidField      = BitConverter.ToString(cardInfo.CardUid);

                            readerDataGroup.InvokeIfRequired(() => {
                                    cardCodeTextBox.Text   = ((byte)cardInfo.CardType).ToString("X2");
                                    cardTypeTextBox.Text   = enumFieldName;
                                    cardUidTextBox.Text    = uidField;
                                    profileNumBox.Text     = "";
                                    cardMemoryTextBox.Text = "";
                                });

                            break;
                        }

                        case RG_DEVICE_STATUS_TYPE.DS_CARDAUTH: {
                            string enumFieldName = Enum.GetName(typeof(RG_CARD_TYPE_CODE), cardInfo.CardType);
                            string uidField      = BitConverter.ToString(cardInfo.CardUid);
                            string memoryData    = BitConverter.ToString(cardMemory.MemBlock);

                            readerDataGroup.InvokeIfRequired(() => {
                                    cardCodeTextBox.Text   = ((byte)cardInfo.CardType).ToString("X2");
                                    cardTypeTextBox.Text   = enumFieldName;
                                    cardUidTextBox.Text    = uidField;
                                    profileNumBox.Text     = cardMemory.ProfileBlock.ToString();
                                    cardMemoryTextBox.Text = memoryData;
                                });

                            break;
                        }

                        case RG_DEVICE_STATUS_TYPE.DS_CARDNOAUTH: {
                            string enumFieldName = Enum.GetName(typeof(RG_CARD_TYPE_CODE), cardInfo.CardType);
                            string uidField      = BitConverter.ToString(cardInfo.CardUid);

                            readerDataGroup.InvokeIfRequired(() => {
                                    cardCodeTextBox.Text   = ((byte)cardInfo.CardType).ToString("X2");
                                    cardTypeTextBox.Text   = enumFieldName;
                                    cardUidTextBox.Text    = uidField;
                                    profileNumBox.Text     = "";
                                    cardMemoryTextBox.Text = string.Format("Ошибка доступа 0x{0:X2}", errorCode - 2000);
                                });

                            break;
                        }

                        default:
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            finally {
                if (completeCallback != null)
                {
                    try {
                        completeCallback();
                    }
                    catch {
                    }
                }
            }
        }
        private void RequestStatusClick(object sender, EventArgs e)
        {
            if (_currentConnectoinContext != null)
            {
                try {
                    RG_ENDPOINT portEndpoin = _currentConnectoinContext.ReaderPort;
                    byte        address     = _currentConnectoinContext.ReaderAddress;


                    RG_PIN_SATETS_16      pinStates  = new RG_PIN_SATETS_16();
                    RG_DEVICE_STATUS_TYPE statusType = RG_DEVICE_STATUS_TYPE.DS_UNKNONWN;
                    RG_CARD_INFO          cardInfo   = new RG_CARD_INFO()
                    {
                        CardType = RG_CARD_TYPE_CODE.CT_UNKNOWN
                    };
                    RG_CARD_MEMORY cardMemory = new RG_CARD_MEMORY
                    {
                        ProfileBlock = 0,
                        MemBlock     = new byte[16]
                    };

                    uint errorCode = UnmanagedContext.Instance.RG_GetStatus(ref portEndpoin, address,
                                                                            ref statusType, ref pinStates, ref cardInfo, ref cardMemory);
                    if (errorCode != 0 && statusType == RG_DEVICE_STATUS_TYPE.DS_UNKNONWN)
                    {
                        throw new ApiCallException("Ошибка при запросе статуса устройства", errorCode);
                    }

                    Debug.WriteLine("Тампер: {0} Контрольный выход: {1}", pinStates.Pin00, pinStates.Pin01);

                    cardCodeTextBox.BackColor   = pinStates.Pin00 ? Color.DarkSalmon : SystemColors.Control;
                    cardTypeTextBox.BackColor   = pinStates.Pin00 ? Color.DarkSalmon : SystemColors.Control;
                    cardUidTextBox.BackColor    = pinStates.Pin00 ? Color.DarkSalmon : SystemColors.Control;
                    cardMemoryTextBox.BackColor = pinStates.Pin00 ? Color.DarkSalmon : SystemColors.Control;
                    profileNumBox.BackColor     = pinStates.Pin00 ? Color.DarkSalmon : SystemColors.Control;

                    switch (statusType)
                    {
                    case RG_DEVICE_STATUS_TYPE.DS_NOCARD: {
                        cardCodeTextBox.Text   = "";
                        cardTypeTextBox.Text   = "";
                        cardUidTextBox.Text    = "";
                        profileNumBox.Text     = "";
                        cardMemoryTextBox.Text = "";
                        break;
                    }

                    case RG_DEVICE_STATUS_TYPE.DS_CARD: {
                        string enumFieldName = Enum.GetName(typeof(RG_CARD_TYPE_CODE), cardInfo.CardType);
                        string uidField      = BitConverter.ToString(cardInfo.CardUid);

                        cardCodeTextBox.Text   = ((byte)cardInfo.CardType).ToString("X2");
                        cardTypeTextBox.Text   = enumFieldName;
                        cardUidTextBox.Text    = uidField;
                        profileNumBox.Text     = "";
                        cardMemoryTextBox.Text = "";
                        break;
                    }

                    case RG_DEVICE_STATUS_TYPE.DS_CARDAUTH: {
                        string enumFieldName = Enum.GetName(typeof(RG_CARD_TYPE_CODE), cardInfo.CardType);
                        string uidField      = BitConverter.ToString(cardInfo.CardUid);
                        string memoryData    = BitConverter.ToString(cardMemory.MemBlock);

                        cardCodeTextBox.Text   = ((byte)cardInfo.CardType).ToString("X2");
                        cardTypeTextBox.Text   = enumFieldName;
                        cardUidTextBox.Text    = uidField;
                        profileNumBox.Text     = cardMemory.ProfileBlock.ToString();
                        cardMemoryTextBox.Text = memoryData;

                        break;
                    }

                    case RG_DEVICE_STATUS_TYPE.DS_CARDNOAUTH: {
                        string enumFieldName = Enum.GetName(typeof(RG_CARD_TYPE_CODE), cardInfo.CardType);
                        string uidField      = BitConverter.ToString(cardInfo.CardUid);

                        cardCodeTextBox.Text   = ((byte)cardInfo.CardType).ToString("X2");
                        cardTypeTextBox.Text   = enumFieldName;
                        cardUidTextBox.Text    = uidField;
                        profileNumBox.Text     = "";
                        cardMemoryTextBox.Text = string.Format("Ошибка доступа 0x{0:X2}", errorCode - 2000);


                        break;
                    }
                    }
                }
                catch (Exception ex) {
                    MessageBox.Show(this,
                                    string.Format("({1}) {0}", ex.Message,
                                                  (ex is ApiCallException) ? (ex as ApiCallException).ApiCallErrorCode.ToString() : "..."),
                                    "Ошибка",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }