param([switch]$Credentials,
[switch]$TestUrl,
[switch]$ClimateOn,
[switch]$ClimateOff,
[switch]$Lock,
[switch]$Unlock,
[switch]$CloseSunroof,
[switch]$StopCharge,
[switch]$StartCharge
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version 2.0
$m_ts = $null
$m_carID = 0
function InvokeCarCommand($command, $successtext, $failtext)
{
$resp = Invoke-RestMethod -Uri "$uriRoot/vehicles/$m_carID/command/$command" -Method GET -WebSession $m_ts
if ($resp.result -eq "true") {
Write-Output $successtext
}
else {
Write-Output "Unable to ${failtext}. Reason returned: `"$($resp.reason)`""
}
}
if ($TestUrl) {
$uriRoot = "[URL]http://timdorr.apiary.io[/URL]"
}
else {
$uriRoot = "[URL]https://portal.vn.teslamotors.com[/URL]"
}
if ($Credentials) {
$creds = Get-Credential
$plaintextpwd = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($creds.Password))
$credsjson = @{ "user_session[email]" = "$($creds.UserName)"; "user_session[password]" = "$plaintextpwd" }
}
else {
$credsjson = @{ "user_session[email]" = "[EMAIL="[email protected]"][email protected][/EMAIL]"; "user_session[password]" = "MyPassword" }
}
# Login and establish session
$r = Invoke-RestMethod -Uri "$uriRoot/login" -Method GET -SessionVariable m_ts
# We ignore the return form.
$r = Invoke-RestMethod -Uri "$uriRoot/login" -Method POST -WebSession $m_ts -Body $credsjson
# Now, get the vehicle information
$vehicle = Invoke-RestMethod -Uri "$uriRoot/vehicles" -Method GET -WebSession $m_ts
if ($vehicle.Count -gt 1) {
Write-Output "You have multiple vehicles:"
$i = 1
foreach ($entry in $vehicle) {
$entry | Add-Member -NotePropertyName "Index" -NotePropertyValue $i -Force
$i++
}
$vehicle | Select-Object -Property Index,display_name,vin,state | Format-Table -Auto
$resp = Read-Host "Enter the index of the vehicle you wish to control"
$value = -1
if (-not [Int32]::TryParse($resp, [ref]$value)) {
throw "Entered value is not a valid number."
}
if (($value -lt 1) -or ($value -gt $vehicle.Count)) {
throw "Invalid entry selected."
}
$vehicle = $vehicle[$value - 1]
}
else {
Write-Output "Controlling vehicle $($vehicle.vin)."
}
$script:m_carID = $vehicle.id
$mobileEnabled = Invoke-RestMethod -Uri "$uriRoot/vehicles/$m_carID/mobile_enabled" -Method GET -WebSession $m_ts
if ($mobileEnabled.result -ne "true") {
throw "Mobile access is not enabled on the vehicle. Please enable it through the console of the vehicle and try again."
}
$physical_state = Invoke-RestMethod -Uri "$uriRoot/vehicles/$m_carID/command/vehicle_state" -Method GET -WebSession $m_ts
$climate_state = Invoke-RestMethod -Uri "$uriRoot/vehicles/$m_carID/command/climate_state" -Method GET -WebSession $m_ts
$charge_state = Invoke-RestMethod -Uri "$uriRoot/vehicles/$m_carID/command/charge_state" -Method GET -WebSession $m_ts
Write-Output "Current Vehicle State (before executing any commands):"
$physical_state | Select-Object -Property locked,sun_roof_percent_open,car_version,df,dr,pf,pr,ft,rt | Format-Table -Auto
$climate_state | Select-Object -Property inside_temp,outside_temp,fan_status,is_auto_conditioning_on | Format-Table -Auto
$charge_state | Select-Object -Property charging_state,battery_range,battery_level,charger_voltage,charger_actual_current,charger_power,time_to_full_charge,charge_rate | Format-Table -Auto
if ($ClimateOn) {
InvokeCarCommand "auto_conditioning_start" "Climate control turned on." "turn on climate control"
}
if ($ClimateOff) {
InvokeCarCommand "auto_conditioning_stop" "Climate control turned off." "turn off climate control"
}
if ($Lock) {
InvokeCarCommand "door_lock" "Doors are locked." "lock doors"
}
if ($Unlock) {
InvokeCarCommand "door_unlock" "Doors are unlocked." "unlock doors"
}
if ($CloseSunroof) {
InvokeCarCommand "sun_roof_control?state=close" "Sunroof is closed." "close sunroof"
}
if ($StopCharge) {
InvokeCarCommand "charge_stop" "Charging has been stopped." "stop charging"
}
if ($StartCharge) {
InvokeCarCommand "charge_start" "Charging has been started." "start charging"
}