Esempio n. 1
0
        public ResultStatus SetPanoramicRoofLevel(TeslaVehicle vehicle, PanoramicRoofState roofState, int percentOpen = 0)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                if (vehicle.Options.RoofType != RoofType.NONE)
                {
                    return(new ResultStatus {
                        Result = false, Reason = "No Panoramic Roof"
                    });
                }

                string response;

                if (roofState == PanoramicRoofState.MOVE)
                {
                    response = webClient.DownloadString(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(SUN_ROOF_CONTROL_PATH_WITH_PERCENT, vehicle.Id, roofState.GetEnumValue(), percentOpen)));
                }
                else
                {
                    response = webClient.DownloadString(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(SUN_ROOF_CONTROL_PATH, vehicle.Id, roofState.GetEnumValue())));
                }

                var result = ParseResultStatus(response);

                return(result);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 2
0
        public ResultStatus SetTemperatureSettings(TeslaVehicle vehicle, int driverTemp = 17, int passengerTemp = 17)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                const int TEMP_MAX = 32;
                const int TEMP_MIN = 17;

                driverTemp = Math.Max(driverTemp, TEMP_MIN);
                driverTemp = Math.Min(driverTemp, TEMP_MAX);

                passengerTemp = Math.Max(passengerTemp, TEMP_MIN);
                passengerTemp = Math.Min(passengerTemp, TEMP_MAX);

                var response = webClient.DownloadString(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(SET_TEMPERATURE_PATH, vehicle.Id, driverTemp, passengerTemp)));

                var result = ParseResultStatus(response);

                return(result);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 3
0
 private static void RequireVehicle(TeslaVehicle vehicle)
 {
     if (vehicle == null)
     {
         throw new ArgumentNullException("vehicle");
     }
 }
Esempio n. 4
0
        public async Task WakeUpAsync(TeslaVehicle vehicle)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                await webClient.DownloadStringTaskAsync(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(WAKE_UP_PATH, vehicle.Id)));
            }
            catch (Exception)
            {
            }
        }
Esempio n. 5
0
        public async Task <ResultStatus> FlashLightsAsync(TeslaVehicle vehicle)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                var response = await webClient.DownloadStringTaskAsync(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(FLASH_LIGHTS_PATH, vehicle.Id)));

                var result = ParseResultStatus(response);

                return(result);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 6
0
        public async Task <ResultStatus> OpenChargePortDoorAsync(TeslaVehicle vehicle)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                var response = await webClient.DownloadStringTaskAsync(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(CHARGE_PORT_DOOR_OPEN_PATH, vehicle.Id)));

                var result = ParseResultStatus(response);

                return(result);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 7
0
        public async Task <GuiSettingsStatus> LoadGuiStateStatusAsync(TeslaVehicle vehicle)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                var response = await webClient.DownloadStringTaskAsync(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(GUI_SETTINGS_PATH, vehicle.Id)));

                var status = ParseGuiStateStatus(response);

                return(status);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 8
0
        public async Task <MobileEnabledStatus> LoadMobileEnabledStatusAsync(TeslaVehicle vehicle)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                var response = await webClient.DownloadStringTaskAsync(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(MOBILE_ENABLED_PATH, vehicle.Id)));

                var status = ParseMobileEnabledStatus(response);

                return(status);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 9
0
        /// <summary>
        /// WARNING: Have not been able to test this
        /// speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range
        /// </summary>
        /// <param name="vehicle"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public string LoadStreamingValues(TeslaVehicle vehicle, string values)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                values = "speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range";

                var response = webClient.DownloadString(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format("stream/{0}/?values={1}", vehicle.VehicleId, values)));

                return(response);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 10
0
        public ResultStatus StopHVAC(TeslaVehicle vehicle)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                var response = webClient.DownloadString(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(HVAC_STOP_PATH, vehicle.Id)));

                var result = ParseResultStatus(response);

                return(result);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 11
0
        public async Task <VehicleStateStatus> LoadVehicleStateStatusAsync(TeslaVehicle vehicle)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                var response = await webClient.DownloadStringTaskAsync(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(VEHICLE_STATE_PATH, vehicle.Id)));

                response = RemoveComments(response);

                var status = ParseVehicleStateStatus(response);

                return(status);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 12
0
        public DriveStateStatus LoadDriveStateStatus(TeslaVehicle vehicle)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                var response = webClient.DownloadString(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(DRIVE_STATE_PATH, vehicle.Id)));

                response = RemoveComments(response);

                var status = ParseDriveStateStatus(response);

                return(status);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 13
0
        public ResultStatus SetChargeLimit(TeslaVehicle vehicle, int percent = 50)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            percent = Math.Max(percent, 50);
            percent = Math.Min(percent, 100);

            try
            {
                var response = webClient.DownloadString(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format(SET_CHARGE_LIMIT, vehicle.Id, percent)));

                var result = ParseResultStatus(response);

                return(result);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 14
0
        /// <summary>
        /// speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range
        /// </summary>
        /// <param name="vehicle"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public async Task <string> LoadStreamingValuesAsync(TeslaVehicle vehicle, string values)
        {
            RequireLogin();
            RequireVehicle(vehicle);

            try
            {
                values = "speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range";

                var response = await webClient.DownloadStringTaskAsync(Path.Combine(TESLA_SERVER(IsDebugMode), string.Format("stream/{0}/?values={1}", vehicle.VehicleId, values)));

                //ResultStatus result = ParseResultStatus(response);

                return(response);
                //return result;
            }
            catch (Exception)
            {
            }

            return(null);
        }
Esempio n. 15
0
        public async void StreamingTest(TeslaVehicle vehicle, string outputDirectory, string tripName, int outputFormatMode,
                                        string valuesToStream = null)
        {
            if (valuesToStream == null)
            {
                valuesToStream = "speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range";
            }

            if (vehicle.Tokens.Count == 0)
            {
                // RELOAD TOKENS
                await WakeUpAsync(vehicle);

                var cars = await LoadVehiclesAsync();

                vehicle = cars.FirstOrDefault(c => c.Id == vehicle.Id);
            }

            var strBasicAuthInfo = string.Format("{0}:{1}", Email, vehicle.Tokens[0]);

            var isStopStreaming = false;

            var outputFormat = (StreamingOutputFormat)Enum.Parse(typeof(StreamingOutputFormat), outputFormatMode.ToString(CultureInfo.InvariantCulture));

            var extension = "txt";

            switch (outputFormat)
            {
            case StreamingOutputFormat.PLAIN_TEXT:
                extension = "txt";
                break;

            case StreamingOutputFormat.KML_PLACEMARK:
            case StreamingOutputFormat.KML_PATH:
                extension = "kml";
                break;
            }

            // Find the Streamer using Reflection
            var t        = GetTypeWithStreamingOutputAttribute(outputFormat);
            var streamer = (AStreamer)Activator.CreateInstance(t);

            var filePath = Path.Combine(outputDirectory, string.Format("{0}_{1}.{2}", DateTime.Now.ToString("MM_dd_yyyy"), tripName, extension));

            streamer.Setup(filePath, valuesToStream, tripName);
            streamer.BeforeStreaming();

            while (!isStopStreaming)
            {
                var request = WebRequest.CreateHttp(new Uri(Path.Combine(TeslaPath.TESLA_STREAMING_SERVER, "stream", vehicle.VehicleId.ToString(CultureInfo.InvariantCulture), "?values=" + valuesToStream)));
                request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(strBasicAuthInfo));
                request.Timeout = 12500; // a bit more than the expected 2 minute max long poll

                var response = (HttpWebResponse)await request.GetResponseAsync();

                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        string line;

                        while ((line = reader.ReadLine()) != null)
                        {
                            // DATA RECEIVED
                            streamer.DataRecevied(line);
                        }
                    }
                    break;

                case HttpStatusCode.Unauthorized:
                {
                    // RELOAD TOKENS
                    await WakeUpAsync(vehicle);

                    var cars = await LoadVehiclesAsync();

                    vehicle = cars.FirstOrDefault(c => c.Id == vehicle.Id);
                }
                break;

                default:
                    isStopStreaming = true;
                    break;
                }
            }

            streamer.AfterStreaming();
        }