public bool WebApi(string mail, string pass, string uuid) { NameValueCollection nvc = new NameValueCollection(); string str; string api = ConfigurationManager.AppSettings["WebApi"]; string sec = ConfigurationManager.AppSettings["CliSec"]; byte[] buf; #region Anmeldung nvc.Add("username", mail); nvc.Add("password", pass); nvc.Add("grant_type", "password"); nvc.Add("client_id", "1"); nvc.Add("client_secret", sec); nvc.Add("scope", "*"); try { buf = _client.UploadValues(api + "oauth/token", nvc); str = Encoding.UTF8.GetString(buf); Debug.WriteLine("Oauth token: {0}", str); Log(string.Format("Oauth token: {0}", str), 1); using (MemoryStream ms = new MemoryStream(buf)) { DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(LsOAuth)); LsOAuth lsoa = (LsOAuth)dcjs.ReadObject(ms); _client.Headers["Authorization"] = string.Format("{0} {1}", lsoa.Type, lsoa.Token); ms.Close(); } } catch (Exception ex) { Err(ex.Message); Log(ex.ToString(), 9); return(false); } #endregion try { #region Benutzer buf = _client.DownloadData(api + "users/me"); str = Encoding.UTF8.GetString(buf); Debug.WriteLine("User info: {0}", str); Log(string.Format("User info: {0}", str), 1); using (MemoryStream ms = new MemoryStream(buf)) { DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(LsUserMe)); LsUserMe ku = (LsUserMe)dcjs.ReadObject(ms); Broker = ku.Endpoint; ms.Close(); } #endregion #region Certificate buf = _client.DownloadData(api + "users/certificate"); str = Encoding.UTF8.GetString(buf); Debug.WriteLine("Certificate: {0}", str); Log(string.Format("Certificate: {0}", str), 1); using (MemoryStream ms = new MemoryStream(buf)) { DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(LsCertificate)); LsCertificate lsc = (LsCertificate)dcjs.ReadObject(ms); ms.Close(); if (!string.IsNullOrEmpty(lsc.Pkcs12)) { str = lsc.Pkcs12.Replace("\\/", "/"); buf = Convert.FromBase64String(str); File.WriteAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AWS" + ArgCfg() + ".p12"), buf); _certWX = new X509Certificate2(buf); } //string mac = Products.Count > 0 ? Products[0].MacAdr.Substring(5) : "000000"; //int xx = int.Parse(mac, System.Globalization.NumberStyles.HexNumber) ^ 0xE1588A; //Log(string.Format("AWS certificate done ({0})", xx), 2); //buf = _certWX.Export(X509ContentType.Cert); //File.WriteAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WX.p12"), buf); } #endregion #region Product items buf = _client.DownloadData(api + "product-items"); str = Encoding.UTF8.GetString(buf); Debug.WriteLine("Product items: {0}", str); Log(string.Format("Product items: {0}", str), 1); using (MemoryStream ms = new MemoryStream(buf)) { DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(List <LsProductItem>)); Products = (List <LsProductItem>)dcjs.ReadObject(ms); ms.Close(); } #endregion #region Status buf = null; foreach (LsProductItem pi in Products) { buf = _client.DownloadData(api + "product-items/" + pi.SerialNo + "/status"); str = Encoding.UTF8.GetString(buf); Debug.WriteLine("Status {0}: {1}", pi.Name, str); Log(string.Format("Status {0}: {1}", pi.Name, str), 1); } if (buf != null) { MemoryStream ms = new MemoryStream(buf); DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(LsMqtt)); LsMqtt jm = (LsMqtt)dcjs.ReadObject(ms); Json = str; Data = jm; //_msgPoll = false; ms.Close(); Recv(); } #endregion } catch (Exception ex) { Err(ex.Message); Log(ex.ToString(), 9); return(false); } buf = null; return(!string.IsNullOrEmpty(Broker) && _certWX != null && Products.Count > 0); }
public bool WorxApiV1(string mail, string pass, string uuid) { NameValueCollection nvc = new NameValueCollection(); string str; string api = ConfigurationManager.AppSettings["ApiWorx"]; string tok = ConfigurationManager.AppSettings["TokWorx"]; byte[] buf; //ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true; nvc.Add("email", mail); nvc.Add("password", pass); nvc.Add("type", "app"); nvc.Add("platform", "android"); nvc.Add("uuid", uuid); try { #region User auth _client.Headers["X-Auth-Token"] = tok; buf = _client.UploadValues(api + "users/auth", nvc); str = Encoding.UTF8.GetString(buf); Debug.WriteLine("User auth: {0}", str); Log(string.Format("User auth: {0}", str), 1); using (MemoryStream ms = new MemoryStream(buf)) { DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(WorxUser)); WorxUser wu = (WorxUser)dcjs.ReadObject(ms); Log(string.Format("User token '{0}'", wu.ApiToken)); Broker = wu.MqttEndpoint; _client.Headers["X-Auth-Token"] = wu.ApiToken; ms.Close(); } #endregion #region Product items buf = _client.DownloadData(api + "product-items"); str = Encoding.UTF8.GetString(buf); Log(string.Format("Product items: {0}", str), 1); Debug.WriteLine("Product items: {0}", str); str = string.Empty; using (MemoryStream ms = new MemoryStream(buf)) { DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(List <LsProductItem>)); Products = (List <LsProductItem>)dcjs.ReadObject(ms); ms.Close(); } #endregion #region Status foreach (LsProductItem pi in Products) { buf = _client.DownloadData(api + "product-items/" + pi.SerialNo + "/status"); str = Encoding.UTF8.GetString(buf); Log(string.Format("Status {0}: {1}", pi.Name, str), 1); Debug.WriteLine("Status {0}: {1}", pi.Name, str); } #endregion #region Certificate buf = _client.DownloadData(api + "users/certificate"); str = Encoding.UTF8.GetString(buf); Log(string.Format("AWS Certificate: {0}", str), 1); Debug.WriteLine("AWS Certificate: {0}", str); using (MemoryStream ms = new MemoryStream(buf)) { DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(LsCertificate)); LsCertificate lsc = (LsCertificate)dcjs.ReadObject(ms); ms.Close(); str = lsc.Pkcs12.Replace("\\/", "/"); buf = Convert.FromBase64String(str); File.WriteAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AWS.p12"), buf); _certWX = new X509Certificate2(buf); string mac = Products.Count > 0 ? Products[0].MacAdr.Substring(5) : "000000"; int xx = int.Parse(mac, System.Globalization.NumberStyles.HexNumber) ^ 0xE1588A; Log(string.Format("AWS certificate done ({0})", xx), 2); //buf = _certWX.Export(X509ContentType.Cert); //File.WriteAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WX.p12"), buf); } #endregion } catch (Exception ex) { Err(ex.Message); Log(ex.ToString(), 9); return(false); } buf = null; return(true); }