Пример #1
0
        /// <summary>
        /// Creates a port from the specified settings node tree.
        /// </summary>
        /// <param name="portNode">The port node.</param>
        /// <returns>The device port.</returns>
        /// <exception cref="System.Exception">
        /// Invalid port format.
        /// </exception>
        protected virtual IDevicePort CreatePort(SettingsNode portNode)
        {
            SettingsNode tcpAddrNode = null;

            if (portNode.ContainsName("Address"))
            {
                tcpAddrNode = portNode["Address"];
            }
            else if (portNode.ContainsName("IPAddress"))
            {
                tcpAddrNode = portNode["IPAddress"];
            }
            else if (portNode.ContainsName("IpAddress"))
            {
                tcpAddrNode = portNode["IpAddress"];
            }
            else if (portNode.ContainsName("TCPAddress"))
            {
                tcpAddrNode = portNode["TCPAddress"];
            }
            else if (portNode.ContainsName("TcpAddress"))
            {
                tcpAddrNode = portNode["TcpAddress"];
            }

            if (tcpAddrNode != null)
            {
                string    ipAddrName = tcpAddrNode.GetValueAs <string>();
                IPAddress ipAddress;

                if (!IPAddress.TryParse(ipAddrName, out ipAddress))
                {
                    throw new Exception(String.Format("'{0}' is an invalid IP address.", ipAddrName));
                }

                DevicePortTcp tcpPort = new DevicePortTcp(ipAddress);

                if (portNode.ContainsName("Port"))
                {
                    tcpPort.Port = portNode["Port"].GetValueAs <int>();
                }

                if (portNode.ContainsName("ReplyAddress"))
                {
                    string    replyAddrName = portNode["ReplyAddress"].GetValueAs <string>();
                    IPAddress replyAddress;

                    if (!IPAddress.TryParse(replyAddrName, out replyAddress))
                    {
                        throw new Exception(String.Format("'{0}' is an invalid reply IP address.", replyAddrName));
                    }

                    tcpPort.ReplyAddress = replyAddress;

                    if (portNode.ContainsName("ReplyPort"))
                    {
                        tcpPort.ReplyPort = portNode["ReplyPort"].GetValueAs <int>();
                    }
                }

                return(tcpPort);
            }

            if (portNode.ContainsName("Number"))
            {
                int portNum = portNode["Number"].GetValueAs <int>();
                return(new DevicePortNumber(portNum));
            }

            if (portNode.ContainsName("Name"))
            {
                string portName = portNode["Name"].GetValueAs <string>();
                return(new DevicePortName(portName));
            }

            if (portNode.HasAValue)
            {
                string portShorthand = portNode.GetValueAs <string>();
                return(CreatePort(portShorthand));
            }

            throw new Exception("Invalid port format.");
        }