private async Task <HttpResponseMessage> postAsync(SuplaDevice device, HttpClient httpclient) { IEnumerable <KeyValuePair <string, string> > list = device.Fields.Select((s, i) => new KeyValuePair <string, string>(s.name, s.value)); var content = new FormUrlEncodedContent(list); HttpResponseMessage responseMessage = null; try { responseMessage = await httpclient.PostAsync("http://192.168.4.1", content); } catch (Exception ex) { responseMessage = null; } return(responseMessage); }
private int findDevices(CancellationToken cancellationToken) { synchronizationContext.Post(new SendOrPostCallback(o => { devices.Clear(); dataGridView1.DataSource = null; }), null); WlanClient client = new WlanClient(); foreach (WlanClient.WlanInterface wlanIface in client.Interfaces) { wlanIface.Disconnect(); wlanIface.Scan(); } System.Threading.Thread.Sleep(2000); foreach (WlanClient.WlanInterface wlanIface in client.Interfaces) { Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0); foreach (Wlan.WlanAvailableNetwork network in networks) { string ssid = SuplaDevice.GetStringForSSID(network.dot11Ssid); if (SuplaDevice.isSuplaNetworkName(ssid)) { if (cancellationToken.IsCancellationRequested) { break; } synchronizationContext.Post(new SendOrPostCallback(o => { IEnumerable <SuplaDevice> d = devices.Where(n => n.SSID.Equals(ssid)); if (d == null || d.Count() == 0) { SuplaDevice device = new SuplaDevice(); device.Network = network; device.WlanIface = wlanIface; device.SSID = ssid; devices.Add(device); } }), null); } } } synchronizationContext.Post(new SendOrPostCallback(o => { dataGridView1.DataSource = devices; btnFind.Enabled = true; btnUpdate.Enabled = devices.Count() > 0; toolStripProgressBar1.Style = ProgressBarStyle.Continuous; toolStripStatusLabel1.Text = "Device count: " + devices.Count().ToString(); btnStop.Enabled = false; }), null); return(1); }
private void button1_Click(object sender, EventArgs e) { List <SuplaDevice> list = new List <SuplaDevice>(); WlanClient client = new WlanClient(); foreach (WlanClient.WlanInterface wlanIface in client.Interfaces) { Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0); foreach (Wlan.WlanAvailableNetwork network in networks) { string ssid = GetStringForSSID(network.dot11Ssid); if (isSuplaNetworkName(ssid)) { Console.WriteLine("Found WEP network with SSID {0}.", ssid); SuplaDevice device = new SuplaDevice(); device.network = network; device.wlanIface = wlanIface; device.ssid = ssid; list.Add(device); } } } foreach (SuplaDevice device in list) { string profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>none</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>", device.ssid); device.wlanIface.Disconnect(); System.Threading.Thread.Sleep(500); device.wlanIface.Connect(Wlan.WlanConnectionMode.TemporaryProfile, Wlan.Dot11BssType.Any, IntPtr.Zero, profileXml); bool Connected = false; DateTime now = new DateTime(); while (true) { System.Threading.Thread.Sleep(1000); try { if (device.wlanIface.CurrentConnection.profileName.Equals(device.ssid) && device.wlanIface.CurrentConnection.isState == WlanInterfaceState.Connected) { Connected = true; break; } if ((now - new DateTime()).TotalSeconds > 5) { break; } } catch (Win32Exception exception) { }; } Console.WriteLine("Connected {0}.", Connected); if (Connected) { HttpClient httpclient = new HttpClient(); Task <string> task = httpclient.GetStringAsync("http://192.168.4.1"); task.Wait(); HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(task.Result); if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0) { throw new Exception("Html parse error"); } if (htmlDoc.DocumentNode == null) { throw new Exception("DocumentNode is null"); } IEnumerable <HtmlNode> inputs = htmlDoc.DocumentNode.Descendants("input"); if (inputs == null) { throw new Exception("Unsupported device"); } IEnumerable <HtmlNode> selects = htmlDoc.DocumentNode.Descendants("select"); var upd = selects == null ? null : selects.Where(n => n.Attributes["name"] != null && n.Attributes["name"].Value == "upd"); if (upd != null && upd.Count() != 1) { throw new Exception("No update capabilities"); } var sid = inputs.Where(n => n.Attributes["name"] != null && n.Attributes["name"].Value == "sid"); var wpw = inputs.Where(n => n.Attributes["name"] != null && n.Attributes["name"].Value == "wpw"); var svr = inputs.Where(n => n.Attributes["name"] != null && n.Attributes["name"].Value == "svr"); string pattern = @"<h1>(.*)<\/h1><span>LAST STATE: (.*)<br>Firmware: (.*)<br>GUID: (.*)<br>MAC: (.*)<\/span>"; Match match = Regex.Match(task.Result, pattern); if (match.Groups.Count != 6 || sid.Count() != 1 || wpw.Count() != 1 || svr.Count() != 1) { throw new Exception("Unsupported device"); } device.name = match.Groups[1].Value.Trim(); device.lastState = match.Groups[2].Value.Trim(); device.firmware = match.Groups[3].Value.Trim(); device.guid = match.Groups[4].Value.Trim(); device.mac = match.Groups[5].Value.Trim(); } } }