Example #1
0
        private static void StartConnection(IOsdpConnection osdpConnection)
        {
            _controlPanel.Shutdown();

            _connectionId = _controlPanel.StartConnection(osdpConnection);

            foreach (var device in _settings.Devices)
            {
                _controlPanel.AddDevice(_connectionId, device.Address, device.UseCrc, device.UseSecureChannel,
                                        device.SecureChannelKey);
            }
        }
        public async Task DeviceGoesOnlineTest()
        {
            // Arrange
            var connection = new TestConnection();

            var  panel = new ControlPanel();
            Guid id    = panel.StartConnection(connection);

            panel.AddDevice(id, 0, true, false);

            // Act
            // Assert
            await TaskEx.WaitUntil(() => panel.IsOnline(id, 0), TimeSpan.FromMilliseconds(100),
                                   TimeSpan.FromSeconds(1));
        }
        public async Task StartConnectionTest()
        {
            // Arrange
            var connection = new TestConnection();
            var panel      = new ControlPanel();

            // Act
            Guid id = panel.StartConnection(connection);

            panel.AddDevice(id, 0, true, false);

            // Assert
            await TaskEx.WaitUntil(() => connection.NumberOfTimesCalledOpen == 1, TimeSpan.FromMilliseconds(100),
                                   TimeSpan.FromSeconds(3));
        }
Example #4
0
        private static async Task Main()
        {
            var builder = new ConfigurationBuilder()
                          .AddJsonFile($"appsettings.json", true, true);
            var    config        = builder.Build();
            var    osdpSection   = config.GetSection("OSDP");
            string portName      = osdpSection["PortName"];
            int    baudRate      = int.Parse(osdpSection["BaudRate"]);
            byte   deviceAddress = byte.Parse(osdpSection["DeviceAddress"]);
            byte   readerNumber  = byte.Parse(osdpSection["ReaderNumber"]);

            var panel = new ControlPanel();

            panel.ConnectionStatusChanged += (sender, eventArgs) =>
            {
                Console.WriteLine($"Device is {(eventArgs.IsConnected ? "Online" : "Offline")}");
            };
            panel.NakReplyReceived += (sender, args) =>
            {
                Console.WriteLine($"Received NAK {args.Nak}");
            };
            panel.ExtendedReadReplyReceived += (sender, eventArgs) =>
            {
                Task.Run(async() =>
                {
                    if (!_readyForSmartCardRead)
                    {
                        return;
                    }

                    try
                    {
                        if (eventArgs.ExtendedRead.Mode == 1 && eventArgs.ExtendedRead.PReply == 1)
                        {
                            _readyForSmartCardRead = false;

                            var response = await panel.ExtendedWriteData(_connectionId, deviceAddress,
                                                                         ExtendedWrite.ModeOneSmartCardScan(readerNumber));
                            if (eventArgs.ExtendedRead.Mode == 1 && response.ReplyData?.PReply == 1)
                            {
                                Console.WriteLine("Card Present");
                                while (true)
                                {
                                    Console.WriteLine("Enter APDU data, leave blank to terminate:");
                                    var data = Console.ReadLine()?.Trim();
                                    if (string.IsNullOrWhiteSpace(data))
                                    {
                                        break;
                                    }

                                    response = await panel.ExtendedWriteData(_connectionId, deviceAddress,
                                                                             ExtendedWrite.ModeOnePassAPDUCommand(readerNumber, StringToByteArray(data)));
                                    if (response.ReplyData == null)
                                    {
                                        break;
                                    }

                                    Console.WriteLine(
                                        $"Received extended reply {response.ReplyData.Mode}:{response.ReplyData.PReply}:{BitConverter.ToString(response.ReplyData.PData).Replace("-", string.Empty)}");
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    if (eventArgs.ExtendedRead.Mode == 1)
                    {
                        try
                        {
                            Console.WriteLine("Disconnecting from SmartCard");
                            await panel.ExtendedWriteData(_connectionId, deviceAddress,
                                                          ExtendedWrite.ModeOneTerminateSmartCardConnection(readerNumber));
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                        finally
                        {
                            _readyForSmartCardRead = true;
                        }
                    }
                });
            };
            panel.RawCardDataReplyReceived += (sender, eventArgs) =>
            {
                Console.WriteLine($"Raw card read {FormatData(eventArgs.RawCardData.Data)}");
            };

            _connectionId = panel.StartConnection(new SerialPortOsdpConnection(portName, baudRate));
            panel.AddDevice(_connectionId, 0, true, true);

            Timer timer = new Timer(5000);

            timer.Elapsed += (sender, eventArgs) =>
            {
                Task.Run(async() =>
                {
                    timer.Stop();
                    try
                    {
                        if (_readyForSmartCardRead && panel.IsOnline(_connectionId, deviceAddress))
                        {
                            Console.WriteLine("Checking SmartCard settings");
                            var response = await panel.ExtendedWriteData(_connectionId, deviceAddress, ExtendedWrite.ReadModeSetting());
                            if (response.ReplyData == null)
                            {
                                panel.ResetDevice(_connectionId, 0);
                            }
                            else if (response.ReplyData.Mode == 0 && response.ReplyData.PReply == 1 && response.ReplyData.PData[0] == 0)
                            {
                                await panel.ExtendedWriteData(_connectionId, 0, ExtendedWrite.ModeOneConfiguration());
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                    finally
                    {
                        timer.Start();
                    }
                });
            };

            timer.Start();

            await Task.Delay(TimeSpan.FromDays(7));
        }