public ConnectToHomeNetwork(JsonNetwork net, TstatPairing pair) { InitializeComponent(); this.pair = pair; this.currentNetwork = net; }
public Boolean ConfigureHomeNetwork(JsonNetwork net, String pin, String password) { try { //Get UUID Thermostat t = getThermostatDetails(); // Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(pin, Encoding.UTF8.GetBytes(uuid), 1000); Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(pin, Encoding.UTF8.GetBytes(t.UUID), 1000); // generate an RC2 key byte[] key = pwdGen.GetBytes(16); byte[] iv = pwdGen.GetBytes(8); //Convert the network password from hex-ascii to binary //C17A0E3E5DD809B450FBF02B1E byte[] pass = Encoding.ASCII.GetBytes(password); String str = String.Empty; int missingDigits = 16 - pass.Length % 16; for (int i = 0; i < pass.Length + missingDigits; i++) { if (i < pass.Length) str += pass[i].ToString(); else str += missingDigits.ToString(); } byte[] cipher = EncryptStringToBytes_Aes(password, key, key); string hex = BitConverter.ToString(cipher); hex = hex.Replace("-", ""); //Get Network configuration HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://192.168.10.1/sys/network"); HttpWebResponse response = (HttpWebResponse)myReq.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); //Stream to String String result = reader.ReadToEnd(); reader.Close(); JObject jsonObj = JObject.Parse(result); net.Ipaddr = (String)jsonObj["ipaddr"]; net.Ipgw = (String)jsonObj["ipgw"]; net.Ipmask =(String)jsonObj["ipmask"]; result = pushNetworkConfiguration(net, hex); JObject ans = JObject.Parse(result); int res1 = (int)ans["success"]; if (res1 == 0) { return true; } else { return false; } } catch (Exception ex) { throw ex; } }
private String pushNetworkConfiguration(JsonNetwork net, string key) { HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://192.168.10.1/sys/network"); myReq = (HttpWebRequest)WebRequest.Create("http://192.168.10.1/sys/network"); myReq.Method = "POST"; myReq.ContentType = @"application/json; charset=utf-8"; String Body = getJson(net, key); byte[] data = Encoding.UTF8.GetBytes(Body); myReq.ContentLength = data.Length; //Request Stream Stream stream = myReq.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)myReq.GetResponse(); String status = response.StatusDescription; Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); //Stream to String String result= reader.ReadToEnd(); reader.Close(); responseStream.Close(); response.Close(); return result; }
private List<JsonNetwork> JsonNetDeserialize(string json) { JObject jsonObj = JObject.Parse(json); JArray networks = (JArray)jsonObj["networks"]; List<JsonNetwork> nets = new List<JsonNetwork>(); foreach (JToken token in networks) { String ssid = (String)token[0]; String bssid = (String)token[1]; int secMode = (int)token[2]; int Channel = (int)token[3]; int rssi = (int)token[4]; JsonNetwork net = new JsonNetwork(ssid, bssid, secMode, Channel, rssi); nets.Add(net); } return nets; }
private String getJson(JsonNetwork net, String key) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); //'{"version":"2.00","ssid":"Trantor","security":3,"key":"mysecret","ip":0,"ipaddr":"192.168.1.100","ipmask":"255.255.255.0","ipgw":"192.168.1.1"}' using (JsonWriter jw = new JsonTextWriter(sw)) { jw.Formatting = Formatting.Indented; jw.WriteStartObject(); jw.WritePropertyName("version"); jw.WriteValue("2"); //what is this??? jw.WritePropertyName("ssid"); jw.WriteValue(net.Ssid); jw.WritePropertyName("security"); jw.WriteValue(net.SecurityMode); jw.WritePropertyName("key"); jw.WriteValue(key); jw.WritePropertyName("ip"); jw.WriteValue(1); //verify this, and about other missing fields jw.WritePropertyName("ipaddr"); jw.WriteValue(net.Ipaddr); jw.WritePropertyName("ipmask"); jw.WriteValue(net.Ipmask); jw.WritePropertyName("ipgw"); jw.WriteValue(net.Ipgw); //jw.WriteEnd(); jw.WriteEndObject(); } return sb.ToString(); }
public void ConnectToNetwork(JsonNetwork net) { WlanClient.WlanInterface intfc = GetCurrentInterface(); String profileXML = GetProfileXML(net.Ssid); //what if profileXML is not found? if (profileXML.CompareTo("not found") == 0) { } else { intfc.ConnectSynchronously(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, net.Ssid, 15000); } }