From 5daf0825a0f3781a2aa89d9e354719cd9292f188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szak=C3=A1ts=20Alp=C3=A1r=20Zsolt?= Date: Mon, 18 Aug 2025 21:47:06 +0200 Subject: [PATCH] Adds fleet status endpoint Adds an endpoint to retrieve fleet status information. This endpoint uses a Tesla API proxy to fetch the fleet status based on provided VINs. It handles authentication using a bearer token and sends a POST request to the /api/1/vehicles/fleet_status endpoint. Also introduces new data models to properly serialize/deserialize the fleet status response. --- Source/ProofOfConcept/Models/FleetResponse.cs | 25 +++++++++++++++++++ Source/ProofOfConcept/Program.cs | 16 ++++++++++++ Source/ProofOfConcept/Services/MQTTClient.cs | 6 ++--- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 Source/ProofOfConcept/Models/FleetResponse.cs diff --git a/Source/ProofOfConcept/Models/FleetResponse.cs b/Source/ProofOfConcept/Models/FleetResponse.cs new file mode 100644 index 0000000..61272eb --- /dev/null +++ b/Source/ProofOfConcept/Models/FleetResponse.cs @@ -0,0 +1,25 @@ +namespace ProofOfConcept.Models; + +using System.Collections.Generic; +using System.Text.Json.Serialization; + +public class FleetRootResponse +{ + [JsonPropertyName("response")] public FleetResponse FleetResponse { get; set; } = new FleetResponse(); +} + +public class FleetResponse +{ + [JsonPropertyName("key_paired_vins")] public List KeyPairedVins { get; set; } = new List(); + [JsonPropertyName("unpaired_vins")] public List UnpairedVins { get; set; } = new List(); + [JsonPropertyName("vehicle_info")] public Dictionary VehicleInfo { get; set; } = new Dictionary(); +} + +public class VehicleInfo +{ + [JsonPropertyName("firmware_version")] public string FirmwareVersion { get; set; } = ""; + [JsonPropertyName("vehicle_command_protocol_required")] public bool VehicleCommandProtocolRequired { get; set; } + [JsonPropertyName("discounted_device_data")] public bool DiscountedDeviceData { get; set; } + [JsonPropertyName("fleet_telemetry_version")] public string FleetTelemetryVersion { get; set; } = ""; + [JsonPropertyName("total_number_of_keys")] public int TotalNumberOfKeys { get; set; } +} \ No newline at end of file diff --git a/Source/ProofOfConcept/Program.cs b/Source/ProofOfConcept/Program.cs index 798b1dc..9b2025d 100644 --- a/Source/ProofOfConcept/Program.cs +++ b/Source/ProofOfConcept/Program.cs @@ -288,6 +288,22 @@ if (app.Environment.IsDevelopment()) }); } +app.MapGet("FleetStatus", async (IHttpClientFactory httpClientFactory) => +{ + string access_token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6InFEc3NoM2FTV0cyT05YTTdLMzFWV0VVRW5BNCJ9.eyJpc3MiOiJodHRwczovL2ZsZWV0LWF1dGgudGVzbGEuY29tL29hdXRoMi92My9udHMiLCJhenAiOiJiMjI0MGVlNC0zMzJhLTQyNTItOTFhYS1iYmNjMjRmNzhmZGIiLCJzdWIiOiJkZDg3Mzc4OC00ZjliLTQyY2UtYmRkNi00YzdmMjQxOGMwN2UiLCJhdWQiOlsiaHR0cHM6Ly9mbGVldC1hcGkucHJkLm5hLnZuLmNsb3VkLnRlc2xhLmNvbSIsImh0dHBzOi8vZmxlZXQtYXBpLnByZC5ldS52bi5jbG91ZC50ZXNsYS5jb20iLCJodHRwczovL2ZsZWV0LWF1dGgudGVzbGEuY29tL29hdXRoMi92My91c2VyaW5mbyJdLCJzY3AiOlsib2ZmbGluZV9hY2Nlc3MiLCJvcGVuaWQiLCJ2ZWhpY2xlX2RldmljZV9kYXRhIiwidmVoaWNsZV9sb2NhdGlvbiJdLCJhbXIiOlsicHdkIl0sImV4cCI6MTc1NTU3NDk5NCwiaWF0IjoxNzU1NTQ2MTk0LCJvdV9jb2RlIjoiRVUiLCJsb2NhbGUiOiJodS1IVSIsImFjY291bnRfdHlwZSI6InBlcnNvbiIsIm9wZW5fc291cmNlIjpmYWxzZSwiYWNjb3VudF9pZCI6IjE5YTBhZjRmLTY1ZDgtNDc2MC1hYjVmLTZjMzk3ZTViMTI4ZiIsImF1dGhfdGltZSI6MTc1NTU0NjE5NCwibm9uY2UiOiI2Mzg5MTExNDkyNjE4ODc5MzUuWVdNMllUazNORFV0T1RSaVlTMDBZMlUyTFdFNU1USXRZVEUwTnpFd056WTFabU16TkRZeFpqRTJaRE10TlRjeFpDMDBaRGN3TFRrMlptVXROMk0xWTJNNU5ERmtNV1k0In0.iMODkOXyzt0_TJfuBrojbYSU7Cx_lb8DRZ_zMz-yQxZlPUA-pITfqgqgwQEqagCUJ3wlfOSxi1OSitmxPrxr9MOT8A9KUgTOSIrPxD36JHWnBmQpbkDNqNkM4MLEbbM-95p8YnFJ0VuB9_fzz0CbJyOm_oUelA2JnNhIQ5uGfJojYPM2UKN0rUx6uMAuFgc4CslPY1P43crAXrFlH6B3D2Yk47w9Xeh-Vg_ZiX-nJqMzph_ciqYRuiySJMjM3ez7EheGy50BR_esz4muit89H3nq0rURWBN3-weauu2bcWnuYp0Nk5-zFuO3O_FKuLt-1HqYH3CAeTF2JRBJHim-hA"; + + HttpClient client = httpClientFactory.CreateClient("InsecureClient"); + client.BaseAddress = new Uri("https://tesla_command_proxy"); + client.DefaultRequestHeaders.Add("Authorization", $"Bearer {access_token}"); + + //Get fleet_status endpoint + var requestObject = new { vins = new string[] { "5YJ3E7EB7KF291652" } }; + + HttpResponseMessage response = await client.PostAsJsonAsync("/api/1/vehicles/fleet_status", requestObject); + + return Results.Ok(response.Content.ReadAsStringAsync()); +}); + //Map static assets app.MapStaticAssets(); diff --git a/Source/ProofOfConcept/Services/MQTTClient.cs b/Source/ProofOfConcept/Services/MQTTClient.cs index 8d26691..1d95d06 100644 --- a/Source/ProofOfConcept/Services/MQTTClient.cs +++ b/Source/ProofOfConcept/Services/MQTTClient.cs @@ -5,9 +5,9 @@ namespace ProofOfConcept.Services; public class MQTTClient : IHostedService { - private ILogger logger; - private MQTTClientConfiguration configuration; - private MQTTServerConfiguration serverConfiguration; + private readonly ILogger logger; + private readonly MQTTClientConfiguration configuration; + private readonly MQTTServerConfiguration serverConfiguration; private readonly IMqttClient client;