Beispiel #1
0
        private void MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            Json = Encoding.UTF8.GetString(e.Message);

            Debug.WriteLine(Json);
            try {
                MemoryStream ms = new MemoryStream(e.Message);
                DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(LsMqtt));
                LsMqtt jm = (LsMqtt)dcjs.ReadObject(ms);

                Data     = jm;
                _msgPoll = false;
                ms.Close();
                Recv();
            } catch (Exception ex) {
                string s;

                Log(ex.Message);
                s = Encoding.UTF8.GetString(e.Message);
                Log(s);
            }
        }
Beispiel #2
0
        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);
        }