public void TestPortNumber()
        {
            IDeviceSpecsValidator deviceSpecsValidator = ServiceProvider.GetService <IDeviceSpecsValidator>();

            Assert.True(deviceSpecsValidator.IsValidPort(1));

            Assert.True(deviceSpecsValidator.IsValidPort(10));

            Assert.False(deviceSpecsValidator.IsValidPort(0));

            Assert.False(deviceSpecsValidator.IsValidPort(-1));
        }
Beispiel #2
0
        private GatewayDto GetInputModel()
        {
            GatewayDto gatewayDto = new GatewayDto();

            Console.Write("\nSerial number: ");

            gatewayDto.SerialNumber = Console.ReadLine();
            while (string.IsNullOrEmpty(gatewayDto.SerialNumber))
            {
                Console.WriteLine("\nSerial number must not be empty");
                Console.Write("\nSerial number: ");
                gatewayDto.SerialNumber = Console.ReadLine();
            }

            Console.Write("\nFirmware version: ");
            gatewayDto.FirmwareVersion = Console.ReadLine();
            if (string.IsNullOrEmpty(gatewayDto.FirmwareVersion))
            {
                gatewayDto.FirmwareVersion = null;
            }

            Console.Write("\nState: ");
            gatewayDto.State = Console.ReadLine();
            if (string.IsNullOrEmpty(gatewayDto.State))
            {
                gatewayDto.State = null;
            }

            Console.Write("\nIP: ");
            gatewayDto.IP = Console.ReadLine();
            if (string.IsNullOrEmpty(gatewayDto.IP))
            {
                gatewayDto.IP = null;
            }

            if (gatewayDto.IP != null)
            {
                while (!_deviceSpecsValidator.IsValidIPv4Address(gatewayDto.IP) && !_deviceSpecsValidator.IsValidIPv6Address(gatewayDto.IP))
                {
                    Console.WriteLine("\nInvalid IP address");
                    Console.Write("\nIP: ");
                    gatewayDto.IP = Console.ReadLine();
                }
            }

            Console.Write("\nPort: ");
            string portInput = Console.ReadLine();

            if (!string.IsNullOrEmpty(portInput))
            {
                bool validPort = int.TryParse(portInput, out int port) && _deviceSpecsValidator.IsValidPort(port);
                while (!validPort)
                {
                    Console.WriteLine("\nInvalid port number");
                    Console.Write("\nPort: ");
                    portInput = Console.ReadLine();
                    validPort = int.TryParse(portInput, out port) && _deviceSpecsValidator.IsValidPort(port);
                }

                gatewayDto.Port = port;
            }
            Console.WriteLine("Registering...\n");

            return(gatewayDto);
        }