示例#1
0
        static void Main(string[] args)
        {
            if (args.Contains("--debug"))
            {
                SerialBeaconManager.SpoolDebugOutputToConsole();
            }
            Console.WriteLine("BlueCats Wallet Version {0}", _version);
            Console.WriteLine();


            // Discover serial beacons
            Console.WriteLine("Discovering connected beacons...");
            var availableBeacons = SerialBeaconManager.DiscoverSerialBeacons();

            while (!availableBeacons.Any())
            {
                Console.WriteLine("No beacons detected. Connect a beacon and press enter.");
                Console.ReadKey();
                Console.WriteLine("Rescanning for connected serial beacons...");
                availableBeacons = SerialBeaconManager.DiscoverSerialBeacons();
            }


            // Select a beacon
            if (availableBeacons.Count == 1)
            {
                _selectedBeacon = availableBeacons.First();
            }
            else
            {
                while (_selectedBeacon == null)
                {
                    for (var i = 0; i < availableBeacons.Count; i++)
                    {
                        Console.WriteLine("{0}) {1}", i + 1, availableBeacons[i]);
                    }
                    Console.WriteLine();
                    try
                    {
                        var choice = Convert.ToInt32(GetUserInput("Select a beacon")) - 1;
                        _selectedBeacon = availableBeacons.ElementAt(choice);
                    }
                    catch
                    {
                        Console.WriteLine("Invalid choice, enter a beacon number.");
                    }
                }
                Console.WriteLine();
            }


            // Attach to beacon and start listening
            try
            {
                _selectedBeacon.Attach();

                _selectedBeacon.BleConnectedEvent      += _selectedBeacon_BleConnectedEvent;
                _selectedBeacon.BleDisconnectedEvent   += _selectedBeacon_BleDisconnectedEvent;
                _selectedBeacon.BleDataRequestEvent    += _selectedBeacon_BleDataRequestEvent;
                _selectedBeacon.BleDataBlocksSentEvent += _selectedBeacon_BleDataBlocksSentEvent;
                _selectedBeacon.BleDataIndicatedEvent  += _selectedBeacon_BleDataIndicatedEvent;

                try
                {
                    _selectedBeacon.WriteEventsEnabled(true);
                }
                catch (Exception ex)
                {
                    // enable events not supported in older beacon fw
                    Debug.Print("* Error on command WriteEventsEnabled(true): {0}",
                                ex.GetBaseException().Message, null);
                }

                Console.WriteLine("Attached to {0}", _selectedBeacon);
            }
            catch
            {
                if (_selectedBeacon.IsAttached)
                {
                    _selectedBeacon.Detach();
                }

                Console.WriteLine("Failed to attach {0}", _selectedBeacon);
            }
            Console.WriteLine();


            // Display merchant info
            var merchantForMerchantId = Merchant.GenerateDemoMerchants(5);
            var merchants             = merchantForMerchantId.Values.ToList();
            var cards = Card.GenerateDemoCards(merchants);

            _demoDataSource = new DemoDataSource(merchantForMerchantId, cards);
            Console.WriteLine(_demoDataSource.ToString());


            // Display command prompt
            Console.WriteLine("Enter 'commands' to see a list of commands.");
            Console.WriteLine();

            var quit = false;

            while (!quit)
            {
                var line = GetUserInput(">");
                if (line == null)
                {
                    continue;
                }
                var parts = line.Split(' ');
                if (parts.Length > 0)
                {
                    quit = RunWalletCommand(parts);
                }
            }

            // Cleanup
            _selectedBeacon.BleConnectedEvent      -= _selectedBeacon_BleConnectedEvent;
            _selectedBeacon.BleDisconnectedEvent   -= _selectedBeacon_BleDisconnectedEvent;
            _selectedBeacon.BleDataRequestEvent    -= _selectedBeacon_BleDataRequestEvent;
            _selectedBeacon.BleDataBlocksSentEvent -= _selectedBeacon_BleDataBlocksSentEvent;
            _selectedBeacon.BleDataIndicatedEvent  -= _selectedBeacon_BleDataIndicatedEvent;

            if (_selectedBeacon.IsAttached)
            {
                _selectedBeacon.Detach();
            }
        }
示例#2
0
        private static bool RunWalletCommand(string[] args)
        {
            bool quit = false;

            if (string.Compare(args[0], "quit", true) == 0)
            {
                quit = true;
                Console.WriteLine("Quitting Wallet.");
            }
            else if (string.Compare(args[0], "reload", true) == 0)
            {
                foreach (Card card in _demoDataSource.Cards)
                {
                    card.CurrentBalance = card.OpeningBalance;
                }
                Console.WriteLine("Reloaded all cards.");
            }
            else if (string.Compare(args[0], "tender", true) == 0)
            {
                if (args.Length == 3)
                {
                    bool tender = true;
                    if (SerialBeaconManager.GetAttachedBeacons().Count <= 0)
                    {
                        Console.WriteLine("Serial beacon not attached.");
                        tender = false;
                    }

                    Merchant merchant = _demoDataSource.GetMerchant(args[1]);
                    if (merchant == null)
                    {
                        Console.WriteLine("Merchant {0} not found.", args[1]);
                        tender = false;
                    }

                    decimal totalAmount;
                    if (!Decimal.TryParse(args[2], out totalAmount))
                    {
                        Console.WriteLine("Invalid total amount {0}.", args[2]);
                        tender = false;
                    }

                    if (tender)
                    {
                        Transaction transaction = new Transaction
                        {
                            TotalAmount     = totalAmount,
                            RemainingAmount = totalAmount,
                            Merchant        = merchant
                        };
                        _demoDataSource.AddTransaction(transaction);

                        TenderCard(transaction);
                    }
                }
                else
                {
                    Console.WriteLine("Usage: tender merchantID amount");
                }
            }
            else if (string.Compare(args[0], "cancel", true) == 0)
            {
                if (args.Length <= 2)
                {
                    var transactionID = args.Length == 2 ? args[1] : null;
                    CancelTransaction(transactionID);
                }
                else
                {
                    Console.WriteLine("Usage: cancel [transactionID]");
                }
            }
            else if (string.Compare(args[0], "datasource", true) == 0)
            {
                Console.WriteLine(_demoDataSource);
            }
            else if (string.Compare(args[0], "firmware", true) == 0)
            {
                try
                {
                    if (_selectedBeacon != null)
                    {
                        var fwVer = _selectedBeacon.ReadFirmwareVersion();
                        Console.WriteLine("Serial Beacon firmware: v{0}", fwVer);
                    }
                    else
                    {
                        Console.WriteLine("Error: Beacon needs to be reset");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Serial beacon not responding");
                    Debug.Print("Serial beacon not responding: {0}", ex.GetBaseException().Message);
                }
            }
            else if (!string.IsNullOrEmpty(args[0]))
            {
                WriteWalletCommandsToConsole();
            }
            return(quit);
        }