Пример #1
0
        private void buttonTestToken_Click(object sender, EventArgs e)
        {
            List <VehicleDetails> vehicles;

            textBoxToken.Text = textBoxToken.Text.Trim();
            if (textBoxToken.Text.Length < 1)
            {
                MessageBox.Show("Please enter a token.", "Missing token", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            try {
                SetCursorToWait();
                token             = new TeslaAccessToken(textBoxToken.Text);
                tesla.AccessToken = token;

                vehicles = tesla.LoadVehicles();

                StringBuilder details = new StringBuilder();

                details.AppendLine("Total " + vehicles.Count.ToString() + " vehicle(s) found.");
                foreach (var vehicle in vehicles)
                {
                    string s = "NAME: " + vehicle.DisplayName + ", MODEL: " + tesla.GetModel(vehicle) + ", VIN: " + vehicle.VIN + " STATE: " + vehicle.State;
                    details.AppendLine(s);

                    Debug.Write(vehicle.ToString());
                }

                if (vehicles.Count == 1)
                {
                    this.vehicleID = vehicles[0].ID;
                }

                VehicleData data = new VehicleData();
                data = tesla.GetAllVehicleData(this.vehicleID, token.AccessToken);
                Debug.WriteLine(data.ToString());

                SetCursorToDefault();
                MessageBox.Show(details.ToString(), "Vehicle details", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex) {
                SetCursorToDefault();
                Debug.WriteLine(ex.ToString());
                MessageBox.Show("Following error occured:\n" + ex.ToString());
            }
        }
Пример #2
0
        public bool Login()
        {
            RestClient client  = new RestClient(AUTHENTICATE_URI);
            var        request = new RestRequest("token");

            request.RequestFormat = DataFormat.Json;
            request.AddBody(
                new {
                grant_type    = GRANT_TYPE_PASSWORD,
                client_id     = CLIENT_ID,
                client_secret = CLIENT_SECRET,
                email         = loginEmail,
                password      = loginPassword
            }
                );

            try {
                var response = client.Post <TeslaAccessToken>(request);

                if (response.Data is null)
                {
                    loggedIn  = false;
                    lastError = response.ErrorMessage;
                    throw response.ErrorException;
                }

                token = response.Data;

                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    loggedIn  = false;
                    lastError = response.StatusDescription;
                }
            }
            catch (Exception ex) {
                Debug.WriteLine(ex.ToString());
            }

            this.loggedIn = true;

            return(true);
        }
Пример #3
0
 public bool RevokeToken(TeslaAccessToken token)
 {
     return(this.RevokeToken(token.AccessToken));
 }