/// <summary> /// Searches from an IP range. e.g. http://192.168.1, from http://x.x.x.1 through http://x.x.x.255, attempting to locate Wemo devices. /// </summary> /// <param name="ipAddressSeed">http://192.168.1</param> /// <returns></returns> public ConcurrentDictionary <string, string> GetListOfLocalWemoDevices(string ipAddressSeed) { if (string.IsNullOrWhiteSpace(ipAddressSeed)) { throw new Exception("The ipAddressSeed value is required!"); } var numProcs = Environment.ProcessorCount; var concurrencyLevel = numProcs * 2; var wemoDevices = new ConcurrentDictionary <string, string>(concurrencyLevel, 300); Parallel.For(1, 255, seed => { // Set the Ip Address var ipAddress = $"{ipAddressSeed}.{seed}"; // Verify the IP address is valid var validIpAddress = IPAddress.TryParse(ipAddress.Replace("http://", ""), out IPAddress address); // if the Ip address is not valid, then skip and get outta here... if (!validIpAddress) { return; } // Attempt to communicate with the Wemo device at the set Ip Address // Construct the HttpWebRequest - if not null we will use the supplied HttpWebRequest object - which is probably a Mock var request = GetResponseWebRequest ?? HttpRequest.CreateGetCommandHttpWebRequest($"{ipAddress}:{Port}{Event}", ContentType, SoapAction, Soap.WemoGetCommands.GetFriendlyName, RequestMethod); // Construct the Soap Request var reqContentSoap = Soap.GenerateGetRequest(Soap.WemoGetCommands.GetFriendlyName); var validWemoDevice = VerifyWemoDevice(request, reqContentSoap); // If we are not an actual Wemo device, then skip and get outta here... if (!validWemoDevice) { return; } var newRequest = GetResponseWebRequest ?? HttpRequest.CreateGetCommandHttpWebRequest($"{ipAddress}:{Port}{Event}", ContentType, SoapAction, Soap.WemoGetCommands.GetFriendlyName, RequestMethod); // Construct the Soap Request var response = ExecuteGetResponseAsync(newRequest, reqContentSoap).GetAwaiter().GetResult(); // If the Ip Address is truly a Wemo device, then deserialize and add it to the list if (response.StatusCode != "UnknownError") { var friendly = GetResponseObject <GetFriendlyNameResponse>(response); wemoDevices.TryAdd(ipAddress, friendly.FriendlyName); } }); return(wemoDevices); }
/// <summary> /// Searches from an IP range. e.g. http://192.168.1, from http://x.x.x.1 through http://x.x.x.255, attempting to locate Wemo devices. /// </summary> /// <param name="ipAddressSeed">http://192.168.1</param> /// <returns></returns> public async Task <ConcurrentDictionary <string, string> > GetListOfLocalWemoDevicesAsync(string ipAddressSeed) { if (string.IsNullOrWhiteSpace(ipAddressSeed)) { throw new Exception("The ipAddressSeed value is required!"); } var numProcs = Environment.ProcessorCount; var concurrencyLevel = numProcs * 2; var wemoDevices = new ConcurrentDictionary <string, string>(concurrencyLevel, 300); await Task.Run(() => { Parallel.For(1, 255, async seed => { // Set the Ip Address var ipAddress = $"{ipAddressSeed}.{seed}"; // Verify the IP address is valid var validIpAddress = IPAddress.TryParse(ipAddress.Replace("http://", ""), out IPAddress address); // if the Ip address is not valid, then skip and get outta here... if (!validIpAddress) { return; } // Attempt to communicate with the Wemo device at the set Ip Address var request = CreateHttpWebRequest(Soap.WemoGetCommands.GetFriendlyName, ipAddress); // Construct the Soap Request var reqContentSoap = Soap.GenerateGetRequest(Soap.WemoGetCommands.GetFriendlyName); // Verify Wemo Device var validWemoDevice = VerifyWemoDevice(request, reqContentSoap); // If we are not an actual Wemo device, then skip and get outta here... if (!validWemoDevice) { return; } // Attempt to communicate with the verified Wemo device - we need to use a new Request object var newRequest = CreateHttpWebRequest(Soap.WemoGetCommands.GetFriendlyName, ipAddress); var response = await ExecuteGetResponseAsync(newRequest, reqContentSoap); // If the Ip Address is truly a Wemo device, then deserialize and add it to the list if (response.StatusCode != "UnknownError") { var friendly = GetResponseObject <GetFriendlyNameResponse>(response); wemoDevices.TryAdd(ipAddress, friendly.FriendlyName); } }); }); return(wemoDevices); }
/// <summary> /// Check to see if a given IP address is for a Wemo device. /// </summary> /// <param name="ipAddress">e.g. http://192.168.1.101</param> /// <returns>A KeyValuePair<string,string> populated with the ipaddress and friendly name if an existing Wemo device</string> object .</returns> public async Task <KeyValuePair <string, string> > IsLocalWemoDeviceAsync(string ipAddress) { if (string.IsNullOrWhiteSpace(ipAddress)) { throw new Exception("The ipAddressSeed value is required!"); } KeyValuePair <string, string> keyValuePair; // Verify the IP address is valid var validIpAddress = IPAddress.TryParse(ipAddress.Replace("http://", ""), out IPAddress address); // if the Ip address is not valid, then skip and get outta here... if (!validIpAddress) { return(new KeyValuePair <string, string>(ipAddress, null)); } // Attempt to communicate with the Wemo device at the set Ip Address var request = CreateHttpWebRequest(Soap.WemoGetCommands.GetFriendlyName, ipAddress); // Construct the Soap Request var reqContentSoap = Soap.GenerateGetRequest(Soap.WemoGetCommands.GetFriendlyName); // Verify Wemo Device var validWemoDevice = VerifyWemoDevice(request, reqContentSoap); // If we are not an actual Wemo device, then skip and get outta here... if (!validWemoDevice) { return(new KeyValuePair <string, string>(ipAddress, null)); } // Attempt to communicate with the verified Wemo device - we need to use a new Request object var newRequest = CreateHttpWebRequest(Soap.WemoGetCommands.GetFriendlyName, ipAddress); var response = await ExecuteGetResponseAsync(newRequest, reqContentSoap); // If the Ip Address is truly a Wemo device, then deserialize and add it to the list if (response.StatusCode != "UnknownError") { var friendly = GetResponseObject <GetFriendlyNameResponse>(response); //wemoDevices.TryAdd(ipAddress, friendly.FriendlyName); keyValuePair = new KeyValuePair <string, string>(ipAddress, friendly.FriendlyName); } else { keyValuePair = new KeyValuePair <string, string>(ipAddress, null); } return(keyValuePair); }
public async Task <WemoResponse> GetResponseAsync(Soap.WemoGetCommands cmd, string ipAddress) { WemoResponse response; // Construct the HttpWebRequest var request = CreateHttpWebRequest(cmd, ipAddress); // Construct the Soap Request var reqContentSoap = Soap.GenerateGetRequest(cmd); response = await ExecuteGetResponseAsync(request, reqContentSoap); return(response); }
public async Task <WemoResponse> GetResponseAsync(Soap.WemoGetCommands cmd, string ipAddress) { WemoResponse response; // Construct the HttpWebRequest - if not null we will use the supplied HttpWebRequest object - which is probably a Mock var request = WebRequest ?? HttpRequest.CreateGetCommandHttpWebRequest($"{ipAddress}:{Port}{Event}", ContentType, SoapAction, cmd, RequestMethod); // Construct the Soap Request var reqContentSoap = Soap.GenerateGetRequest(cmd, "urn:Belkin:service:insight:1"); response = await ExecuteGetResponseAsync(request, reqContentSoap); return(response); }