Skip to content

Commit

Permalink
Merge pull request #4 from briangru/master
Browse files Browse the repository at this point in the history
Update TeslaLib from BrianGru's fork
  • Loading branch information
razfriman authored Sep 30, 2017
2 parents e2b4a81 + a6144b6 commit 9a13020
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 37 deletions.
73 changes: 45 additions & 28 deletions TeslaLib/LoginTokenCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ internal static class LoginTokenCache
private static readonly TimeSpan ExpirationTimeWindow = TimeSpan.FromDays(1);

private static Dictionary<String, LoginToken> Tokens = new Dictionary<String, LoginToken>();
private static bool haveReadCacheFile = false;
private static volatile bool haveReadCacheFile = false;
private static Object cacheLock = new Object();

private static void ReadCacheFile()
{
Expand All @@ -38,53 +39,69 @@ private static void ReadCacheFile()

private static void WriteCacheFile()
{
using (StreamWriter writer = File.CreateText(CacheFileName))
// Note: On an Android device, we either don't have a legal path or we simply can't write anything.
// Presumably the same behavior will happen with an IPhone. Can we use isolated storage on Mono?
try
{
JsonSerializer serializer = new JsonSerializer();
foreach (var pair in Tokens)
using (StreamWriter writer = File.CreateText(CacheFileName))
{
writer.WriteLine(pair.Key);
serializer.Serialize(writer, pair.Value);
writer.WriteLine();
JsonSerializer serializer = new JsonSerializer();
foreach (var pair in Tokens)
{
writer.WriteLine(pair.Key);
serializer.Serialize(writer, pair.Value);
writer.WriteLine();
}
}
}
catch (UnauthorizedAccessException)
{ }
}

public static LoginToken GetToken(String emailAddress)
{
if (!haveReadCacheFile)
lock (cacheLock)
{
ReadCacheFile();
haveReadCacheFile = true;
}
if (!haveReadCacheFile)
{
ReadCacheFile();
haveReadCacheFile = true;
}

LoginToken token;
if (!Tokens.TryGetValue(emailAddress, out token))
{
return null;
}
LoginToken token;
if (!Tokens.TryGetValue(emailAddress, out token))
{
return null;
}

// Ensure the LoginToken is still valid.
DateTime expirationTime = token.CreatedAt.ToLocalTime() + UnixTimeConverter.FromUnixTimeSpan(token.ExpiresIn);
if (DateTime.Now + ExpirationTimeWindow >= expirationTime)
{
Tokens.Remove(emailAddress);
WriteCacheFile();
token = null;
// Ensure the LoginToken is still valid.
DateTime expirationTime = token.CreatedAt.ToLocalTime() + UnixTimeConverter.FromUnixTimeSpan(token.ExpiresIn);
if (DateTime.Now + ExpirationTimeWindow >= expirationTime)
{
Tokens.Remove(emailAddress);
WriteCacheFile();
token = null;
}
return token;
}
return token;
}

public static void AddToken(String emailAddress, LoginToken token)
{
Tokens[emailAddress] = token;
WriteCacheFile();
lock (cacheLock)
{
Tokens[emailAddress] = token;
WriteCacheFile();
}
}

public static void ClearCache()
{
Tokens.Clear();
File.Delete(CacheFileName);
lock (cacheLock)
{
Tokens.Clear();
File.Delete(CacheFileName);
}
}
}
}
9 changes: 8 additions & 1 deletion TeslaLib/Models/ChargeStateStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ namespace TeslaLib.Models
*/
public class ChargeStateStatus
{
// Note: the ChargingState started coming back as null around June 2017, coinciding with a significant
// Tesla software update. They apparently upgraded from Linux kernel 2.6.36 to 4.4.35. They may have changed
// a lot of Tesla's software stack too.
[JsonProperty(PropertyName = "charging_state")]
[JsonConverter(typeof(StringEnumConverter))]
public ChargingState ChargingState { get; set; }
public ChargingState? ChargingState { get; set; }

[JsonProperty(PropertyName = "battery_current")]
public double? BatteryCurrent { get; set; }
Expand Down Expand Up @@ -257,5 +260,9 @@ public enum ChargingState

[EnumMember(Value = "Stopped")]
Stopped,

// As of September 2017, we started seeing this.
[EnumMember(Value = "NoPower")]
NoPower,
}
}
3 changes: 3 additions & 0 deletions TeslaLib/Models/DriveStateStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace TeslaLib.Models
{
// Values as of August 2017 with a 2014 Model S. Note the GPS_as_of and timestamp fields. Looks like
// Tesla dropped some digits from a time stamp originally, maybe.
// {"response":{"shift_state":null,"speed":null,"power":0,"latitude":47.6506,"longitude":-122.119804,"heading":193,"gps_as_of":1503881736,"timestamp":1503881737541}}
public class DriveStateStatus
{
public DriveStateStatus()
Expand Down
5 changes: 3 additions & 2 deletions TeslaLib/Models/VehicleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ public enum TeslaColor
PEARL_WHITE,

[EnumMember(Value = "PMR")]
MULTICOAT_READ,

MULTICOAT_RED,
//Red = MULTICOAT_RED,

[EnumMember(Value = "PSR")]
SIGNATURE_RED,
}
Expand Down
28 changes: 27 additions & 1 deletion TeslaLib/Models/VehicleStateStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

namespace TeslaLib.Models
{
// As of August 2017, a 2014 Model S returns:
// {"response":{"api_version":3,"autopark_state":"unavailable","autopark_state_v2":"unavailable","calendar_supported":true,
// "car_type":"s","car_version":"2017.32 9ea02cb","center_display_state":0,"dark_rims":false,"df":0,"dr":0,
// "exterior_color":"Red","ft":0,"has_spoiler":true,"locked":true,"notifications_supported":true,"odometer":32490.440953,
// "parsed_calendar_supported":true,"perf_config":"P2","pf":0,"pr":0,"rear_seat_heaters":0,"rear_seat_type":0,
// "remote_start":false,"remote_start_supported":true,"rhd":false,"roof_color":"None","rt":0,"seat_type":0,
// "spoiler_type":"Passive","sun_roof_installed":1,"sun_roof_percent_open":0,"sun_roof_state":"unknown",
// "third_row_seats":"None","timestamp":1503881911969,"valet_mode":false,"vehicle_name":"Hope Bringer","wheel_type":"Base19"}}
public class VehicleStateStatus
{

Expand Down Expand Up @@ -77,6 +85,16 @@ public VehicleStateStatus()

[JsonProperty(PropertyName = "third_row_seats")]
public String ThirdRowSeats { get; set; } // "None"

// Fields that exist as of August 2017:

[JsonProperty(PropertyName = "odometer")]
public Double Odometer { get; set; } // Value is in miles, regardless of the car's UI settings.

// Note: We should use the TeslaColor enum here, but this returns values like "Red" vs. "MULTICOAT_RED"
[JsonProperty(PropertyName = "exterior_color")]
//[JsonConverter(typeof(StringEnumConverter))]
public /*TeslaColor*/String ExteriorColor { get; set; }
}

public enum PanoramicRoofState
Expand All @@ -93,11 +111,19 @@ public enum PanoramicRoofState
[EnumMember(Value = "Close")]
CLOSE,

[EnumMember(Value = "MOve")]
[EnumMember(Value = "Move")]
MOVE,

[EnumMember(Value = "Unknown")]
UNKNOWN,

// As of September 2017, we started seeing "closed" and "moving" as values.
[EnumMember(Value = "Closed")]
Closed = CLOSE,

[EnumMember(Value = "Moving")]
Moving = MOVE,

}

public enum WheelType
Expand Down
5 changes: 2 additions & 3 deletions TeslaLib/TeslaLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.105.2.3\lib\net45\RestSharp.dll</HintPath>
Expand Down
7 changes: 6 additions & 1 deletion TeslaLib/TeslaVehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ public enum VehicleState
Offline,

[EnumMember(Value = "Waking")]
Waking
Waking,

// I saw this for half an hour while charging at a Supercharger then while driving. Perhaps the modem was
// offline, or Tesla's web service was offline?
[EnumMember(Value = "unknown")]
Unknown
}
}
2 changes: 1 addition & 1 deletion TeslaLib/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="8.0.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
<package id="RestSharp" version="105.2.3" targetFramework="net45" />
</packages>

0 comments on commit 9a13020

Please sign in to comment.