Ejemplo n.º 1
0
        public static T LoadResponse <T>(out DateTime?fileDate, bool decrypt = false)
        {
            fileDate = null;
            string file = GetSavedResponseFilePath(typeof(T), "");

            if (!File.Exists(file))
            {
                return(default(T));
            }

            fileDate = new FileInfo(file).LastWriteTime;
            string json = File.ReadAllText(file);

            T resp;

            try {
                if (decrypt)
                {
                    json = DecryptResponse(json);
                }
                resp = SerializeTool.DeSerializeJson <T>(json);
            }
            catch (Exception ex) {
                if (decrypt)
                {
                    throw new Exception("Error Loading Response, Maybe Wrong Password?", ex);
                }
                else
                {
                    throw new Exception("Error Loading Response", ex);
                }
            }
            return(resp);
        }
Ejemplo n.º 2
0
        private async Task <TessApiResult> SendCommand(string command, string commandText = null)
        {
            try {
                string url    = $"https://owner-api.teslamotors.com/api/1/vehicles/{myCarId.Value}/command/" + command;
                string result = await CallUrl(url, "POST", true, commandText);

                CommandResult cr = SerializeTool.DeSerializeJson <CommandResult>(result);
                TessTools.SaveResponse(cr, command);
                return(new TessApiResult(cr));
            }
            catch (Exception ex) {
                Log.Error("MyTess.SendCommand: " + command, ex);
                return(new TessApiResult(ex));
            }
        }
Ejemplo n.º 3
0
        public async Task <TessApiResult> WakeUp()
        {
            this.MyCarData = null;
            string url = $"https://owner-api.teslamotors.com/api/1/vehicles/{myCarId.Value}/wake_up";

            try {
                string result = await CallUrl(url, "POST");

                CarDataResponse pr = SerializeTool.DeSerializeJson <CarDataResponse>(result);
                TessTools.SaveResponse(pr);
                MyCarData = pr.response;

                return(new TessApiResult());
            }
            catch (Exception ex) {
                return(new TessApiResult(ex));
            }
        }
Ejemplo n.º 4
0
        public async Task <TessApiResult> ListProducts(bool loadFromDisk)
        {
            this.MyCar     = null;
            this.MyCarData = null;

            try {
                ProductResponse pr;
                if (loadFromDisk)
                {
                    pr = TessTools.LoadResponse <ProductResponse>(out _);
                }
                else
                {
                    // Optional https://owner-api.teslamotors.com/api/1/vehicles
                    string url    = "https://owner-api.teslamotors.com/api/1/products/";
                    string result = await CallUrl(url, "GET");

                    pr = SerializeTool.DeSerializeJson <ProductResponse>(result);
                    TessTools.SaveResponse(pr);
                }

                ProductList = pr.response;

                if (myCarId.HasValue)
                {
                    foreach (Product p in pr.response)
                    {
                        if (p.id == myCarId)
                        {
                            MyCar = p;
                        }
                    }
                }

                return(new TessApiResult());
            }
            catch (Exception ex) {
                Log.Error("MyTess.ListProducts", ex);
                return(new TessApiResult(ex));
            }
        }
Ejemplo n.º 5
0
        public async Task <TessApiResult> GetCarInfo(bool loadFromDisk)
        {
            this.MyCarData = null;
            try {
                CarDataResponse cdr = null;
                if (loadFromDisk)
                {
                    cdr = TessTools.LoadResponse <CarDataResponse>(out DiskDataDate);
                    Log.Debug("GetCarInfo - loadFromDisk cdr Null:" + (cdr == null));
                    if (cdr != null)
                    {
                        MyCarData = cdr.response;
                        return(new TessApiResult());
                    }
                }

                if (MyCar == null)
                {
                    return(new TessApiResult(false, "No Car Data"));
                }

                if (!loadFromDisk)
                {
                    string url    = $"https://owner-api.teslamotors.com/api/1/vehicles/{myCarId.Value}/vehicle_data";
                    string result = await CallUrl(url, "GET");

                    cdr = SerializeTool.DeSerializeJson <CarDataResponse>(result);
                }

                MyCarData = cdr?.response;
                if (cdr != null)
                {
                    TessTools.SaveResponse(cdr);
                }
                return(new TessApiResult());
            }
            catch (Exception ex) {
                Log.Error("MyTess.GetCarInfo", ex);
                return(new TessApiResult(ex));
            }
        }
Ejemplo n.º 6
0
        private async Task GetTokenAsync4Async(string tmpAccessToken)
        {
            var d = new Dictionary <string, string>();

            d.Add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
            d.Add("client_id", TESLA_CLIENT_ID);
            d.Add("client_secret", TESLA_CLIENT_SECRET);
            string json = new JavaScriptSerializer().Serialize(d);

            using (HttpClient client = new TessHttpClient(new TessClientHandler(null, null))) {
                client.Timeout = TimeSpan.FromSeconds(5);

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + tmpAccessToken);

                using (var content = new StringContent(json, Encoding.UTF8, "application/json")) {
                    HttpResponseMessage result = await client.PostAsync("https://owner-api.teslamotors.com/oauth/token", content);

                    string resultContent = result.Content.ReadAsStringAsync().Result;
                    Log.Debug("HttpStatus: " + result.StatusCode.ToString());

                    LoginResponse = SerializeTool.DeSerializeJson <LoginResponse>(resultContent);
                }
            }
        }