Skip to content

Commit

Permalink
Telemetry Connection support for FW.2024.26
Browse files Browse the repository at this point in the history
  • Loading branch information
bassmaster187 committed Jul 30, 2024
1 parent d5de867 commit 85424f9
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 11 deletions.
124 changes: 114 additions & 10 deletions TeslaLogger/TelemetryConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.IO;
using Exceptionless;
using Newtonsoft.Json.Linq;
using System.Globalization;

namespace TeslaLogger
{
Expand All @@ -33,6 +34,12 @@ internal class TelemetryConnection
public DateTime lastDriving = DateTime.MinValue;
public DateTime lastMessageReceived = DateTime.MinValue;

public DateTime lastPackCurrentDate = DateTime.MinValue;
public double lastPackCurrent = 0.0;

public DateTime lastVehicleSpeedDate = DateTime.MinValue;
public double lastVehicleSpeed = 0.0;

public int lastposid = 0;


Expand Down Expand Up @@ -96,6 +103,8 @@ public bool OnlineTimeout()
public TelemetryConnection(Car car)
{
this.car = car;
if (car == null)
return;

t = new Thread(() => { Run(); });
t.Start();
Expand Down Expand Up @@ -266,17 +275,64 @@ public void InsertLocation(dynamic j, DateTime d, string resultContent, bool for
if (key == "Location")
{
dynamic locationValue = value["locationValue"];
latitude = locationValue["latitude"];
longitude = locationValue["longitude"];
if (locationValue != null)
{
latitude = locationValue["latitude"];
longitude = locationValue["longitude"];
}
else
{
string v = value["stringValue"];
if (v != null)
{
v = v.Replace("(", "").Replace(")", "");
v = v.Replace("\"", "");
var vs = v.Split(',');
if (vs.Length == 2)
{
// Split the string into latitude and longitude parts
string[] coordinates = v.Split(',');

// Parse latitude
string latitudeStr = coordinates[0].Trim();
latitude = Double.Parse(latitudeStr.TrimEnd('N', 'S'), CultureInfo.InvariantCulture);
if (latitudeStr.EndsWith("S"))
{
latitude = -latitude;
}

// Parse longitude
string longitudeStr = coordinates[1].Trim();
longitude = Double.Parse(longitudeStr.TrimEnd('E', 'W'), CultureInfo.InvariantCulture);
if (longitudeStr.EndsWith("W"))
{
longitude = -longitude;
}
}

}
}
}
else if (key == "VehicleSpeed")
{
string v1 = value["stringValue"];
if (Double.TryParse(v1, out double s))
speed = s;
if (v1 != null)
{
v1 = v1.Replace("\"", "");
if (Double.TryParse(v1, out double s))
speed = s;
}
}
}

if (speed == null)
{
var ts = d - lastVehicleSpeedDate;
if (ts.TotalSeconds < 20)
speed = lastVehicleSpeed;
}


if (latitude != null && longitude != null && (speed != null || force))
{
if (force && speed == null)
Expand Down Expand Up @@ -448,6 +504,7 @@ private void InsertCruiseStateTable(dynamic j, DateTime d, string resultContent)
if (value.ContainsKey("stringValue"))
{
string v1 = value["stringValue"];
v1 = v1.Replace("\"", "");

if (v1 != lastCruiseState)
{
Expand Down Expand Up @@ -526,6 +583,7 @@ private void InsertBatteryTable(dynamic j, DateTime date, string resultContent)
if (value.ContainsKey("stringValue"))
{
string v1 = value["stringValue"];
v1 = v1.Replace("\"", "");
double d = double.Parse(v1, Tools.ciEnUS);
cmd.Parameters.AddWithValue("@" + key, d);

Expand All @@ -552,6 +610,12 @@ private void InsertBatteryTable(dynamic j, DateTime date, string resultContent)
currentJSONUpdated = true;
BrickVoltageMax = d;
}
else if (key == "PackCurrent")
{
System.Diagnostics.Debug.WriteLine("PackCurrent: " + d);
lastPackCurrent = d;
lastPackCurrentDate = date;
}
}
}
}
Expand Down Expand Up @@ -623,6 +687,7 @@ void handleStatemachine(dynamic j, DateTime date, string resultContent)
if (value.ContainsKey("stringValue"))
{
string v1 = value["stringValue"];
v1 = v1.Replace("\"", "");

if (key == "ChargeState")
{
Expand All @@ -636,7 +701,7 @@ void handleStatemachine(dynamic j, DateTime date, string resultContent)

if (!acCharging)
{
var current = PackCurrent(j);
var current = PackCurrent(j, date);

Log($"AC Charging {current}A ***");
if (current > 2)
Expand Down Expand Up @@ -689,6 +754,9 @@ void handleStatemachine(dynamic j, DateTime date, string resultContent)
{
if (speed > 0)
{
lastVehicleSpeed = speed;
lastVehicleSpeedDate = date;

if (acCharging)
{
Log("Stop AC Charging by speed ***");
Expand Down Expand Up @@ -724,7 +792,7 @@ void handleStatemachine(dynamic j, DateTime date, string resultContent)

if (!dcCharging)
{
var current = PackCurrent(j);
var current = PackCurrent(j, date);
Log($"FastChargerPresent {current}A ***");

if (current > 5) {
Expand Down Expand Up @@ -763,16 +831,49 @@ void handleStatemachine(dynamic j, DateTime date, string resultContent)

}

internal static double? PackCurrent(dynamic o)
internal double? PackCurrent(dynamic o, DateTime date)
{
JToken j = null;
try
{
JToken j = o.SelectToken("$[?(@.key=='PackCurrent')].value.stringValue");
double val = j.Value<double>();
j = o.SelectToken("$[?(@.key=='PackCurrent')].value.stringValue");
if (j == null)
{
var ts = date - lastPackCurrentDate;
if (ts.TotalSeconds < 10)
{
return lastPackCurrent;
}

return null;
}

double? val = j.Value<double?>();
return val;
}
catch (Exception e)
{
try
{
string v = j.Value<string>();
if (!String.IsNullOrEmpty(v))
{
v = v.Replace("\"", "");
return Double.Parse(v, CultureInfo.InvariantCulture);
}
}
catch (Exception e2)
{
var ts = date - lastPackCurrentDate;
if (ts.TotalSeconds < 10)
{
return lastPackCurrent;
}

Logfile.Log("*** FT: PackCurrent2 " + e2.ToString());
e2.ToExceptionless().FirstCarUserID().Submit();
}

Logfile.Log("*** FT: PackCurrent " + e.ToString());
e.ToExceptionless().FirstCarUserID().Submit();
}
Expand Down Expand Up @@ -835,11 +936,14 @@ private void Login()
configname = "free2";

Log("Login to Telemetry Server / config: " + configname);
string vin = car.Vin;
// vin = "LRW3E7EK6NC483045"; // xxx

Dictionary<string, object> login = new Dictionary<string, object>{
{ "msg_type", "login"},
{ "vin", car.Vin},
{ "vin", vin},
{ "token", car.TaskerHash},
{ "FW", car.CurrentJSON.current_car_version.Substring(0, car.CurrentJSON.current_car_version.IndexOf(" ")).Trim()},
{ "accesstoken", car.Tesla_Token},
{ "regionurl", car.webhelper.apiaddress},
{ "config", configname},
Expand Down
3 changes: 2 additions & 1 deletion UnitTestsTeslalogger/UnitTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,8 @@ public void ParsePackCurrent()
dynamic j = JsonConvert.DeserializeObject(data);
dynamic jData = j["data"];

double? current = TelemetryConnection.PackCurrent(jData);
TelemetryConnection f = new TelemetryConnection(null);
double? current = f.PackCurrent(jData, DateTime.Now);
Assert.AreEqual(current, 121.9d);

}
Expand Down

0 comments on commit 85424f9

Please sign in to comment.