using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; using Tesla; using Newtonsoft.Json; namespace NikolaNet.Pages { public class IndexModel : PageModel { private readonly ILogger _logger; private string VehicleName; public Vehicle[] vehicles; public int currentIndex = 0; public DataResponse vehicleDataResponse; public string jsonData; public string token {get; set;} public string climateTemp {get; set;} public IndexModel(ILogger logger) { _logger = logger; } public Client setupClient() { if (string.IsNullOrEmpty(token)) { token = Request.Cookies["Auth"]; } Client c = new Client(); c.setToken($"Bearer {token}"); vehicles = c.ListVehicles(); c.vehicle_id = vehicles[currentIndex].id_s; return c; } public void OnGet() { } public void OnGetCommand() { Client c = setupClient(); if (Request.Query["command"].Equals(BasicCommands.ClimateOn)) { string parms = $"{{\"driver_temp\": \"{Request.Query["climateTemp"]}\", \"passenger_temp\":\"{Request.Query["climateTemp"]}\"}}"; c.RunCommand(ExtendedCommands.SetClimateTemp, parms); } c.RunCommand(Request.Query["command"]); Response.Redirect("/"); } public void OnPostSetToken() { this.token = Request.Form["token"]; Response.Cookies.Append("Auth", token); Response.Redirect("/"); } public JsonResult OnGetJsonData() { Client c = setupClient(); VehicleName = vehicles[currentIndex].display_name; vehicleDataResponse = c.GetVehicleData().response; if (vehicleDataResponse.gui_settings.gui_temperature_units.Equals("F")) { vehicleDataResponse.climate_state.outside_temp = (int)(vehicleDataResponse.climate_state.outside_temp * 1.8) + 32; vehicleDataResponse.climate_state.inside_temp = (int)(vehicleDataResponse.climate_state.inside_temp * 1.8) + 32; vehicleDataResponse.climate_state.driver_temp_setting = (int)(vehicleDataResponse.climate_state.driver_temp_setting * 1.8) + 32; } jsonData = JsonConvert.SerializeObject(vehicleDataResponse); return new JsonResult(jsonData); } } }