Skip to content

Commit

Permalink
Merge pull request bassmaster187#1455 from Adminius/warp
Browse files Browse the repository at this point in the history
New Wallbox: WARP
  • Loading branch information
bassmaster187 authored Jan 30, 2025
2 parents 1c02a2b + 2308c04 commit 8029e92
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 0 deletions.
2 changes: 2 additions & 0 deletions TeslaLogger/ElectricityMeterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static ElectricityMeterBase Instance(string type, string host, string par
return new ElectricityMeterEVCC(host, paramater);
else if (type == "smartevse3")
return new ElectricityMeterSmartEVSE3(host, paramater);
else if (type == "warp")
return new ElectricityMeterWARP(host, paramater);

return null;
}
Expand Down
226 changes: 226 additions & 0 deletions TeslaLogger/ElectricityMeterWARP.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Linq;
using System.Runtime.Caching;
using Exceptionless;
using Newtonsoft.Json;

namespace TeslaLogger
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Keine allgemeinen Ausnahmetypen abfangen", Justification = "<Pending>")]
class ElectricityMeterWARP : ElectricityMeterBase
{
string host;
string parameter;
internal int wallboxMeterId = 0;
internal int wallboxValueId = 209;
internal int gridMeterId = 1;
internal int gridValueId = 209;

internal string mockup_info_version;
internal string mockup_evse_state;
internal string mockup_wallbox_value_ids;
internal string mockup_wallbox_values;
internal string mockup_grid_value_ids;
internal string mockup_grid_values;


static WebClient client;

public ElectricityMeterWARP(string host, string parameter)
{
if (client == null)
{
client = new WebClient();
}

this.host = host;
this.parameter = parameter;

/*Example parameters:
"": wallbox internal meter with ID 0, value_id=209 and grid meter ID 1, value_id=209
"W:2:210": only wallbox meter with ID 2 and value_id=210
"G:2|W:0:210": Grid meter with ID 2 and default value_id=209, wallbox meter with ID 0 and value_id=210
Default wallbox meter ID is 0
Default grid meter ID is 1
Default "energy import" ID is 209
*/

var args = parameter.Split('|');
foreach (var p in args)
{
if (p.StartsWith("W", StringComparison.InvariantCultureIgnoreCase))
{
string[] parts = p.Split(':');
wallboxMeterId = parts.Length > 1 && int.TryParse(parts[1], out var tempMeterId) ? tempMeterId : 0;
wallboxValueId = parts.Length > 2 && int.TryParse(parts[2], out var tempValueId) ? tempValueId : 209;
}
if (p.StartsWith("G", StringComparison.InvariantCultureIgnoreCase))
{
string[] parts = p.Split(':');
gridMeterId = parts.Length > 1 && int.TryParse(parts[1], out var tempMeterId) ? tempMeterId : 1;
gridValueId = parts.Length > 2 && int.TryParse(parts[2], out var tempValueId) ? tempValueId : 209;
}
}
}

public override double? GetUtilityMeterReading_kWh()
{
string value_ids = null;
string values = null;

try
{
if (mockup_grid_value_ids == null && mockup_grid_values == null)
{
if (host == null)
return double.NaN;

string url1 = host + $"/meters/{gridMeterId}/value_ids";
value_ids = client.DownloadString(url1).Trim();

string url2 = host + $"/meters/{gridMeterId}/values";
values = client.DownloadString(url2).Trim();
}
else
{
value_ids = mockup_grid_value_ids;
values = mockup_grid_values;
}

List<int> ids = value_ids.Trim('[', ']').Split(',').Select(int.Parse).ToList();
int position = ids.IndexOf(gridValueId);

List<double> value = values.Trim('[', ']').Split(',').Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToList();

if (position >= 0 && position < value.Count)
{
return value[position];
}

return double.NaN;

}
catch (Exception ex)
{
ex.ToExceptionless().FirstCarUserID().Submit();
Logfile.ExceptionWriter(ex, values);
}
return double.NaN;
}

public override double? GetVehicleMeterReading_kWh()
{
string value_ids = null;
string values = null;

try
{
if (mockup_wallbox_value_ids == null && mockup_wallbox_values == null)
{
if (host == null)
return double.NaN;

string url1 = host + $"/meters/{wallboxMeterId}/value_ids";
value_ids = client.DownloadString(url1).Trim();

string url2 = host + $"/meters/{wallboxMeterId}/values";
values = client.DownloadString(url2).Trim();
}
else
{
value_ids = mockup_wallbox_value_ids;
values = mockup_wallbox_values;
}

List<int> ids = value_ids.Trim('[', ']').Split(',').Select(int.Parse).ToList();
int position = ids.IndexOf(wallboxValueId);

List<double> value = values.Trim('[', ']').Split(',').Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToList();

if (position >= 0 && position < value.Count)
{
return value[position];
}

return double.NaN;

}
catch (Exception ex)
{
ex.ToExceptionless().FirstCarUserID().Submit();
Logfile.ExceptionWriter(ex, values);
}
return double.NaN;
}

public override bool? IsCharging()
{
string evse_state = null;

try
{
if (mockup_evse_state == null && host != null)
{
string url = host + "/evse/state";
evse_state = client.DownloadString(url).Trim();
}
else
{
evse_state = mockup_evse_state;
}


dynamic jsonResult = JsonConvert.DeserializeObject(evse_state);

if (jsonResult == null)
return null;

return jsonResult["charger_state"] == 3 ? true : false;
}
catch (Exception ex)
{
ex.ToExceptionless().FirstCarUserID().Submit();
Logfile.ExceptionWriter(ex, evse_state);
}
return null;
}

public override string GetVersion()
{
string info_version = null;

try
{
if (mockup_info_version == null && host != null)
{
string url = host + "/info/version";
info_version = client.DownloadString(url).Trim();
}
else
{
info_version = mockup_info_version;
}


dynamic jsonResult = JsonConvert.DeserializeObject(info_version);
string value = jsonResult["firmware"];

if (value == null)
return "";

return value;
}
catch (Exception ex)
{
ex.ToExceptionless().FirstCarUserID().Submit();
Logfile.ExceptionWriter(ex, info_version);
}

return "";
}
}
}
1 change: 1 addition & 0 deletions TeslaLogger/TeslaLogger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<Compile Include="CaptchaSolverKey.cs" />
<Compile Include="Car.cs" />
<Compile Include="ElectricityMeterSmartEVSE3.cs" />
<Compile Include="ElectricityMeterWARP.cs" />
<Compile Include="MQTTAutoDiscovery.cs" />
<Compile Include="MQTT.cs" />
<Compile Include="CO2.cs" />
Expand Down
1 change: 1 addition & 0 deletions TeslaLogger/www/admin/wallbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function btn_save_click()
<option value="tesla-gen3">Tesla Wallbox Gen 3</option>
<option value="keba">Keba KeContact P30</option>
<option value="evcc">EVCC</option>
<option value="warp">WARP</option>
</select>
</td></tr>
<tr><td><?php t("Host"); ?>:</td><td><input id="host" type="text" /></td></tr>
Expand Down
24 changes: 24 additions & 0 deletions UnitTestsTeslalogger/UnitTestWallbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ public void EVCC_multiple()
Assert.AreEqual("0.133.0", version);
}

[TestMethod]
public void WARP()
{
var v = new ElectricityMeterWARP("", "");

v.mockup_info_version = System.IO.File.ReadAllText(@"..\..\testdata\warp_infos_version.txt");
v.mockup_evse_state = System.IO.File.ReadAllText(@"..\..\testdata\warp_evse_state.txt");
v.mockup_wallbox_value_ids = System.IO.File.ReadAllText(@"..\..\testdata\warp_wallbox_value_ids.txt");
v.mockup_wallbox_values = System.IO.File.ReadAllText(@"..\..\testdata\warp_wallbox_values.txt");
v.mockup_grid_value_ids = System.IO.File.ReadAllText(@"..\..\testdata\warp_grid_value_ids.txt");
v.mockup_grid_values = System.IO.File.ReadAllText(@"..\..\testdata\warp_grid_values.txt");

double? kwh = v.GetVehicleMeterReading_kWh();
var charging = v.IsCharging();
var utility_meter_kwh = v.GetUtilityMeterReading_kWh();
var version = v.GetVersion();
string ret = v.ToString();
Console.WriteLine(ret);
Assert.AreEqual(544.4099731, kwh);
Assert.AreEqual(false, charging);
Assert.AreEqual(5762.71875, utility_meter_kwh);
Assert.AreEqual("2.6.6+675aeb99", version);
}

[TestMethod]
public void Shelly3EM()
{
Expand Down
1 change: 1 addition & 0 deletions UnitTestsTeslalogger/testdata/warp_evse_state.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"iec61851_state":0,"charger_state":0,"contactor_state":0,"contactor_error":0,"allowed_charging_current":0,"error_state":0,"lock_state":0,"dc_fault_current_state":0}
1 change: 1 addition & 0 deletions UnitTestsTeslalogger/testdata/warp_grid_value_ids.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[74,209,211]
1 change: 1 addition & 0 deletions UnitTestsTeslalogger/testdata/warp_grid_values.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-17,5762.71875,13432.32129]
1 change: 1 addition & 0 deletions UnitTestsTeslalogger/testdata/warp_infos_version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"firmware":"2.6.6+675aeb99","config":"2.6.6","config_type":"warp"}
1 change: 1 addition & 0 deletions UnitTestsTeslalogger/testdata/warp_wallbox_value_ids.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,2,3,13,17,21,39,48,57,122,130,138,83,91,99,353,354,355,7,29,33,74,154,115,356,364,209,211,4,5,6,8,25,213,277,214,210,212,14,18,22]
1 change: 1 addition & 0 deletions UnitTestsTeslalogger/testdata/warp_wallbox_values.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[232.3265228,233.3556213,232.8416901,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,232.9503174,0,0,0,0,0,0,50.04000092,544.4099731,0.006,402.7033081,407.1804504,400.4727478,403.4683533,0,544.4160156,9.642000198,544.4160156,544.4099731,0.006,0,0,0]

0 comments on commit 8029e92

Please sign in to comment.