Rock.Model.DeviceService.GetByIPAddress C# (CSharp) Method

GetByIPAddress() public method

Gets the device by IP address.
public GetByIPAddress ( string ipAddress, int deviceTypeValueId, bool skipReverseLookup = true ) : Device
ipAddress string A representing the ip address.
deviceTypeValueId int A representing the DeviceType of the device that you are searching for.
skipReverseLookup bool A indicating if a reverse lookup will be skipped. If true a DNS reverse lookup for the name of the system /// that belongs to the provided IP address will not be performed, otherwise false and a DNS reverse lookup will be performed.
return Device
        public Device GetByIPAddress( string ipAddress, int deviceTypeValueId, bool skipReverseLookup = true )
        {
            string hostValue = ipAddress;

            if ( !skipReverseLookup )
            {
                // Lookup the system's "name" (as seen in the DNS) using the given IP
                // address because when using DHCP the kiosk may have a different IP from time to time
                // -- however the fully qualified name should always be the same.
                try
                {
                    hostValue = System.Net.Dns.GetHostEntry( ipAddress ).HostName;
                }
                catch ( SocketException )
                {
                    // TODO: consider whether we want to log the IP address that caused this error.
                    // As per http://msdn.microsoft.com/en-us/library/ms143998.aspx it *may* mean
                    // a stale DNS record for an IPv4 address that actually belongs to a
                    // different host was going to be returned (there is a DNS PTR record for
                    // the IPv4 address, but no DNS A record for the IPv4 address).
                    hostValue = ipAddress;
                }
            }

            Device device = null;

            // If we still have an IPv4 address then try to find it based on IP
            if ( Regex.IsMatch( hostValue, @"\d+\.\d+\.\d+\.\d+" ) )
            {
                // find by IP
                device = Queryable()
                    .Where( d =>
                        d.DeviceTypeValueId == deviceTypeValueId &&
                        d.IPAddress == hostValue)
                    .FirstOrDefault();
            }
            else
            {
                // find by name
                device = Queryable()
                    .Where( d =>
                        d.DeviceTypeValueId == deviceTypeValueId &&
                        d.Name == hostValue )
                    .FirstOrDefault();
            }

            return device;
        }