Пример #1
0
        private void SaveChanges(object sender, System.Windows.RoutedEventArgs e)
        {
            var client  = new RestClient("http://localhost:62140/Token");
            var request = new RestRequest(Method.POST);

            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            request.AddParameter("grant_type", "password");
            request.AddParameter("username", textBoxEmail.Text);
            request.AddParameter("password", textBoxPassword.Password);
            IRestResponse response = client.Execute(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Dictionary <string, string> tokenDictionary = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content);
                string accessToken = tokenDictionary["access_token"];
                var    clientAddNobreakToAccount  = new RestClient("http://localhost:62140/api/");
                var    requestAddNobreakToAccount = new RestRequest("Nobreaks/PostAddNobreakToAccount", Method.POST);


                requestAddNobreakToAccount.AddHeader("content-type", "application/x-www-form-urlencoded");
                requestAddNobreakToAccount.AddHeader("Authorization", "Bearer " + accessToken);
                requestAddNobreakToAccount.AddHeader("token_type", "bearer");

                INobreakServiceChannel ch = App.Channel;

                //var informacoesNobreak = ch.InformationInquiry();

                var nobreak = new Nobreak()
                {
                    Serial = textBoxSerial.Text,
                    Label  = textBoxNobreakNickname.Text,
                    //nobreak.UPSModel = informacoesNobreak.Modelo;
                    //nobreak.Version = informacoesNobreak.Versao;
                    CompanyName = "TS Shara"
                };
                var nbJson = JsonConvert.SerializeObject(nobreak);

                requestAddNobreakToAccount.AddParameter("text/json", nbJson, ParameterType.RequestBody);
                requestAddNobreakToAccount.RequestFormat = RestSharp.DataFormat.Json;

                response = clientAddNobreakToAccount.Execute(requestAddNobreakToAccount);

                var settingsWorkQuery = new SettingsWorkQuery();
                var currentSettings   = settingsWorkQuery.FindFirstData();
                currentSettings.UserEmail = textBoxEmail.Text;
                currentSettings.UserToken = accessToken;

                settingsWorkQuery.UpdateData(currentSettings);

                //MessageBox.Show(response.Content);
                NotifierHelper.ShowInformation(response.Content, 1);
            }
            else
            {
                Dictionary <string, string> responseError = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content);

                NotifierHelper.ShowInformation(responseError["error_description"], 2);
            }
        }
        private void setFields()
        {
            SettingsWork _settingsWork = settingsWorkQuery.FindFirstData();

            NetworkFailureTime        = _settingsWork.NetworkFailureTime;
            LowBatteryTime            = _settingsWork.LowBatteryTime;
            ShutdownNobreakTime       = _settingsWork.RestPCTime;
            ShutdownNobreakWithSOFlag = _settingsWork.ShutdownOSAlongWithPCFlag;
            StartAppWithSOFlag        = _settingsWork.StartAppAlongWithAppFlag;

            ShutdownFlag = ResolveFieldIntToBoolean(_settingsWork.AutonomyEndTimeFlag);
            if (ShutdownFlag)
            {
                HibernateFlag = false;
            }
            else
            {
                HibernateFlag = true;
            }
        }