Exemplo n.º 1
0
        private static bool FilterNodeByCommandType(LanNodeDto node)
        {
            switch (_command)
            {
            case Commands.Active:
                return(node.IsActive);

            case Commands.WiFi:
                return(node.IsWifi);


            case Commands.All:
            case Commands.Lan:
                return(true);

            default:
                return(true);
            }
        }
Exemplo n.º 2
0
        private static void ExecuteReadCommandsToUpdateLanNodesForDisplay()
        {
            const int maxRetries = 5;
            var       retries    = maxRetries;
            IEnumerable <DhcpClient>   dhcpClients   = null;
            IEnumerable <ActiveClient> activeClients = null;
            IEnumerable <WirelessNode> wirelessNodes = null;

            while (retries > 0)
            {
                try
                {
                    (dhcpClients, activeClients, wirelessNodes) = ReadRouterInfo();
                    break;
                }
                catch (System.Net.WebException ex)
                {
                    if (ex.Message.ToLower().Contains("unauthorized"))
                    {
                        Console.WriteLine(
                            $"{Environment.NewLine}Could not connect to router, check username & password:{Environment.NewLine}" +
                            $"{ex.Message} - {ex.InnerException?.Message}.{Environment.NewLine}");

                        retries = 0;
                    }
                    else
                    {
                        Console.WriteLine(
                            $"{Environment.NewLine}Could not read data from router, maybe router is rebooting or URL is incorrect:{Environment.NewLine}" +
                            $"{ex.Message} - {ex.InnerException?.Message}.{Environment.NewLine}" +
                            $"Attempt {maxRetries - retries + 1} of {maxRetries}");
                        retries--;

                        Thread.Sleep(5000);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(
                        $"{Environment.NewLine}Could not connect to router:{Environment.NewLine}" +
                        $"{e.Message} - {e.InnerException?.Message}.{Environment.NewLine}");

                    retries = 0;

                    _logger.Error(e);
                }
            }

            _lanNodesToDisplay = new List <LanNodeDto>();

            if (dhcpClients == null || activeClients == null || wirelessNodes == null)
            {
                Console.WriteLine("Could not read data from router.");

                return;
            }

            foreach (var dhcpClient in dhcpClients)
            {
                var lanNodeToDisplay = new LanNodeDto();
                lanNodeToDisplay.Update(dhcpClient);
                _lanNodesToDisplay.Add(lanNodeToDisplay);
            }

            foreach (var activeClient in activeClients)
            {
                var lanNodeToDisplay =
                    _lanNodesToDisplay.FirstOrDefault(node => node.MacAddress == activeClient.MacAddress);
                lanNodeToDisplay?.Update(activeClient);
            }

            foreach (var wirelessNode in wirelessNodes)
            {
                var lanNodeToDisplay =
                    _lanNodesToDisplay.FirstOrDefault(node => node.MacAddress == wirelessNode.MacAddress);
                lanNodeToDisplay?.Update(wirelessNode);
            }

            _lanNodesToDisplay.Sort();
        }