|
|
|
|
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<IndexModel> _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 string GetVehicleName()
|
|
|
|
|
{
|
|
|
|
|
return VehicleName;
|
|
|
|
|
}
|
|
|
|
|
public IndexModel(ILogger<IndexModel> 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)) {
|
|
|
|
|
//c.RunCommand(ExtendedCommands.SetClimateTemp, $"{{\"driver_temp\": \"{Request.Form["climateTemp"]}\"}}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
c.RunCommand(Request.Query["command"]);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public void OnGetFrunk() {
|
|
|
|
|
}
|
|
|
|
|
public void OnPostSetToken() {
|
|
|
|
|
this.token = Request.Form["token"];
|
|
|
|
|
Response.Cookies.Append("Auth", token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|