Skip to content

Commit

Permalink
clean powerwall impl
Browse files Browse the repository at this point in the history
  • Loading branch information
gemu2015 committed Dec 4, 2024
1 parent 3ce6b0e commit d7c1c51
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
46 changes: 46 additions & 0 deletions tasmota/include/Powerwall.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Powerwall {
String tesla_email;
String tesla_password;
String authCookie;
String cts1;
String cts2;

public:
Powerwall();
Expand All @@ -31,11 +33,33 @@ class Powerwall {
};


#ifndef POWERWALL_IP_CONFIG
#define POWERWALL_IP_CONFIG "192.168.188.60"
#endif

#ifndef TESLA_EMAIL
#define TESLA_EMAIL "email"
#endif

#ifndef TESLA_PASSWORD
#define TESLA_EMAIL "password"
#endif

#ifndef TESLA_POWERWALL_CTS1
#define TESLA_POWERWALL_CTS1 "cts1"
#endif

#ifndef TESLA_POWERWALL_CTS2
#define TESLA_POWERWALL_CTS2 "cts2"
#endif

Powerwall::Powerwall() {
powerwall_ip = POWERWALL_IP_CONFIG;
tesla_email = TESLA_EMAIL;
tesla_password = TESLA_PASSWORD;
authCookie = "";
cts1 = TESLA_POWERWALL_CTS1;
cts2 = TESLA_POWERWALL_CTS2;
}

String Powerwall::AuthCookie() {
Expand Down Expand Up @@ -423,6 +447,22 @@ String Powerwall::GetRequest(String url, String in_authCookie) {
free(string);
}
ssl_client.stop();

// custom replace
result.replace(cts1, "PW_CTS1");

result.replace(cts2, "PW_CTS2");

// shrink data size because it exceeds json parser maxsize
result.replace("communication_time", "ct");
result.replace("instant", "i");
result.replace("apparent", "a");
result.replace("reactive", "r");

result.replace("nominal_full_pack_energy", "f_p_e");
result.replace("nominal_energy_remaining", "n_e_r");
result.replace("backup_reserve_percent", "b_r_p");

return result;
}

Expand All @@ -443,6 +483,12 @@ String Powerwall::GetRequest(String url) {
tesla_password = url.substring(pos + 1);
//AddLog(PWL_LOGLVL, PSTR("PWL: %s - %s - %s"), powerwall_ip.c_str(), tesla_email.c_str(), tesla_password.c_str());
return "";
} if (url[1] == 'C') {
url = url.substring(2);
uint16_t pos = strcspn(url.c_str(), ",");
cts1 = url.substring(0, pos);
cts2 = url.substring(pos + 1);
return "";
} else {
url = url.substring(1);
return Pwl_test(url);
Expand Down
2 changes: 1 addition & 1 deletion tasmota/tasmota_support/support.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ uint32_t JsonParsePath(JsonParserObject *jobj, const char *spath, char delim, fl
uint32_t res = 0;
const char *cp = spath;
#ifdef DEBUG_JSON_PARSE_PATH
AddLog(LOG_LEVEL_INFO, PSTR("JSON: parsing json key: %s from json: %s"), cp, jpath);
AddLog(LOG_LEVEL_INFO, PSTR("JSON: parsing json key: %s from json: %s"), cp, spath);
#endif
JsonParserObject obj = *jobj;
JsonParserObject lastobj = obj;
Expand Down
15 changes: 0 additions & 15 deletions tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13048,21 +13048,6 @@ int32_t call2pwl(const char *url) {
String result = powerwall.GetRequest(String(url), cookie);
//AddLog(LOG_LEVEL_INFO, PSTR("PWL: result: %s"), result.c_str());

// shrink data size because it exceeds json parser maxsize
result.replace("communication_time", "ct");
result.replace("instant", "i");
result.replace("apparent", "a");
result.replace("reactive", "r");

// custom replace
#ifdef TESLA_POWERWALL_CTS1
result.replace(TESLA_POWERWALL_CTS1, "PW_CTS1");
#endif

#ifdef TESLA_POWERWALL_CTS2
result.replace(TESLA_POWERWALL_CTS2, "PW_CTS2");
#endif

if (result.length()>4095) {
AddLog(LOG_LEVEL_INFO, PSTR("PWL: result overflow: %d"), result.length());
}
Expand Down

0 comments on commit d7c1c51

Please sign in to comment.