Example #1
0
        public async Task <LsClientWeb_LoginResponse> Login(LsClientWeb_LoginRequest request)
        {
            NameValueCollection nvc = new NameValueCollection
            {
                { "username", request.Username },
                { "password", request.Password },
                { "grant_type", request.GrantType },
                { "client_id", request.ClientId.ToString() },
                { "client_secret", request.ClientSecret },
                { "scope", request.Scope }
            };

            LsClientWeb_LoginResponse response = new LsClientWeb_LoginResponse();

            using (WebClient client = new WebClient())
            {
                var buf = await client.UploadValuesTaskAsync(new Uri(_configuration.GetValue <string>("WorxApi") + "oauth/token"), nvc);

                var str = Encoding.UTF8.GetString(buf);
                Debug.WriteLine("Oauth token: {0}", str);
                using (MemoryStream ms = new MemoryStream(buf))
                {
                    DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(LsOAuth));
                    LsOAuth lsoa = (LsOAuth)dcjs.ReadObject(ms);

                    response.BearerToken = string.Format("{0} {1}", lsoa.Type, lsoa.Token);
                }
            }

            response.BrokerUrl = await GetBrokerEndpoint(response.BearerToken);

            var certificateData = await GetMqttCertificate(response.BearerToken);

            response.CertWX = certificateData.Item2;

            // Save Userdata on DB
            _repoManager.GenericOperations.Save(new UserData()
            {
                Username = request.Username, X509Certificate2 = certificateData.Item1, Broker = response.BrokerUrl
            });

            return(response);
        }
Example #2
0
        public bool WebApi(string url, string clientSecret, string mail, string pass, string uuid)
        {
            NameValueCollection nvc = new NameValueCollection();
            string str;
            string api = url;
            string sec = clientSecret;

            byte[] buf;

            #region REGISTRATION
            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 USER
                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);
        }