This commit is contained in:
MichaelDvP
2024-10-22 18:36:13 +02:00
53 changed files with 1091 additions and 841 deletions

View File

@@ -379,30 +379,25 @@ static void setup_commands(std::shared_ptr<Commands> const & commands) {
}
});
// read <deviceID> <type ID> [offset] [length]
commands->add_command(ShellContext::MAIN,
CommandFlags::USER,
{F_(read)},
string_vector{F_(deviceid_mandatory), F_(typeid_mandatory), F_(offset_optional), F_(length_optional)},
[=](Shell & shell, const std::vector<std::string> & arguments) {
uint8_t device_id = Helpers::hextoint(arguments.front().c_str());
// loop through arguments and add to data as text, separated by a space
std::string data;
for (const auto & arg : arguments) {
if (!data.empty()) {
data += " ";
}
data += arg;
}
if (!EMSESP::valid_device(device_id)) {
if (!System::readCommand(data.c_str())) {
shell.printfln("Invalid deviceID");
return;
}
uint16_t type_id = Helpers::hextoint(arguments[1].c_str());
if (arguments.size() == 4) {
uint16_t offset = Helpers::hextoint(arguments[2].c_str());
uint8_t length = Helpers::hextoint(arguments.back().c_str());
EMSESP::send_read_request(type_id, device_id, offset, length, true);
} else if (arguments.size() == 3) {
uint16_t offset = Helpers::hextoint(arguments.back().c_str());
EMSESP::send_read_request(type_id, device_id, offset, 0, true);
} else {
EMSESP::send_read_request(type_id, device_id, 0, 0, true);
}
EMSESP::set_read_id(type_id);
});
commands->add_command(ShellContext::MAIN,

View File

@@ -99,7 +99,7 @@ char * Helpers::ultostr(char * ptr, uint32_t value, const uint8_t base) {
#endif
/**
* fast atoi returning a std::string
* fast itoa returning a std::string
* http://www.strudel.org.uk/itoa/
*
*/
@@ -122,8 +122,9 @@ std::string Helpers::itoa(int16_t value) {
}
/*
* fast itoa and optimized for ESP32
* fast itoa
* written by Lukás Chmela, Released under GPLv3. http://www.strudel.org.uk/itoa/ version 0.4
* optimized for ESP32
*/
char * Helpers::itoa(int32_t value, char * result, const uint8_t base) {
// check that the base if valid

View File

@@ -20,7 +20,6 @@
#define EMSESP_HELPERS_H
#include "telegram.h" // for EMS_VALUE_* settings
#include "common.h"
namespace emsesp {

View File

@@ -63,6 +63,7 @@ MAKE_WORD_TRANSLATION(info_cmd, "list all values (verbose)", "Liste aller Werte"
MAKE_WORD_TRANSLATION(commands_cmd, "list all commands", "Liste aller Kommandos", "lijst van alle commando's", "", "wyświetl wszystkie komendy", "Viser alle kommandoer", "", "Tüm komutları listele", "elencaa tutti i comandi", "zobraziť všetky príkazy") // TODO translate
MAKE_WORD_TRANSLATION(entities_cmd, "list all entities", "Liste aller Entitäten", "lijst van alle entiteiten", "", "wyświetl wszsytkie encje", "Viser alle enheter", "", "Tüm varlıkları listele", "elenca tutte le entità", "zobraziť všetky entity") // TODO translate
MAKE_WORD_TRANSLATION(send_cmd, "send a telegram", "Sende EMS-Telegramm", "stuur een telegram", "", "wyślij telegram", "send et telegram", "", "Bir telegram gönder", "invia un telegramma", "poslať telegram") // TODO translate
MAKE_WORD_TRANSLATION(read_cmd, "send read request", "", "", "", "", "", "", "", "", "") // TODO translate
MAKE_WORD_TRANSLATION(setiovalue_cmd, "set I/O value", "Setze Werte E/A", "instellen standaardwaarde", "", "ustaw wartość", "sett en io verdi", "", "Giriş/Çıkış değerlerini ayarla", "imposta valore io", "nastaviť hodnotu io") // TODO translate
MAKE_WORD_TRANSLATION(changeloglevel_cmd, "change log level", "Ändere Protokollebene", "aanpassen log niveau", "", "zmień poziom log-u", "endre loggnivå", "", "Kayıt seviyesini değiştir", "cambia livello registrazione", "zmeniť úroveň protokolu") // TODO translate
MAKE_WORD_TRANSLATION(fetch_cmd, "refresh all EMS values", "Aktualisiere alle EMS-Werte", "Verversen alle EMS waardes", "", "odśwież wszystkie wartości EMS", "oppfrisk alle EMS verdier", "", "Bütün EMS değerlerini yenile", "aggiornare tutti i valori EMS", "obnoviť všetky hodnoty EMS") // TODO translate
@@ -182,7 +183,7 @@ MAKE_WORD_TRANSLATION(eco, "eco", "Eco", "Eco", "Eko", "eko", "øko", "éco", "e
MAKE_WORD_TRANSLATION(ecoplus, "eco+", "Eco+", "Eco+", "Eko+", "eko+", "øko+", "éco+", "eko+", "Eco+")
MAKE_WORD_TRANSLATION(intelligent, "intelligent", "Intelligent", "intelligent", "Intelligent", "inteligentny", "intelligent", "intelligent", "akıllı", "intelligente", "inteligentný")
MAKE_WORD_TRANSLATION(flow, "flow", "Durchfluss", "volumestroom", "Flöde", "przepływ", "strømme", "débit", "akım", "flusso", "tok")
MAKE_WORD_TRANSLATION(manual, "manual", "Manuell", "handmatig", "Manuell", "ręczny", "manuell", "manuel", "manuel", "manuale", "manuálny")
MAKE_WORD_TRANSLATION(manual, "manual", "manuell", "handmatig", "Manuell", "ręczny", "manuell", "manuel", "manuel", "manuale", "manuálny")
MAKE_WORD_TRANSLATION(buffer, "buffer", "Speicher", "buffer", "Buffert", "bufor", "buffer", "buffer", "tampon", "Buffer", "zásobník")
MAKE_WORD_TRANSLATION(bufferedflow, "buffered flow", "Durchlaufspeicher", "doorstroombuffer", "Buffertflöde", "przepływ buforowany", "bufret strømning", "", "tampon akım", "memoria flusso", "zásobníkový prietok") // TODO translate
MAKE_WORD_TRANSLATION(layeredbuffer, "layered buffer", "Schichtspeicher", "gelaagde buffer", "Lagrad buffert", "bufor warstwowy", "lagdelt buffer", "", "katmanlı akım", "strato memoria", "vrstvený zásobník") // TODO translate

View File

@@ -409,7 +409,8 @@ void System::reload_settings() {
eth_phy_addr_ = settings.eth_phy_addr;
eth_clock_mode_ = settings.eth_clock_mode;
locale_ = settings.locale;
locale_ = settings.locale;
developer_mode_ = settings.developer_mode;
});
}
@@ -851,6 +852,7 @@ void System::system_check() {
// commands - takes static function pointers
// can be called via Console using 'call system <cmd>'
void System::commands_init() {
Command::add(EMSdevice::DeviceType::SYSTEM, F_(read), System::command_read, FL_(read_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(send), System::command_send, FL_(send_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(fetch), System::command_fetch, FL_(fetch_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(restart), System::command_restart, FL_(restart_cmd), CommandFlag::ADMIN_ONLY);
@@ -1689,6 +1691,7 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
#endif
node["modbusEnabled"] = settings.modbus_enabled;
node["forceHeatingOff"] = settings.boiler_heatingoff;
node["developerMode"] = settings.developer_mode;
});
// Devices - show EMS devices if we have any
@@ -2012,4 +2015,60 @@ bool System::uploadFirmwareURL(const char * url) {
return true; // OK
}
// read command, e.g. read <deviceID> <type ID> [offset] [length] from console or API
bool System::readCommand(const char * data) {
// extract <deviceID> <type ID> [offset] [length] from string
char * p;
char value[11];
// make a copy so we can iterate, max 15 chars (XX XXXX XX XX)
char data_args[15];
strlcpy(data_args, data, sizeof(data_args));
uint8_t device_id = 0; // is in hex
uint16_t type_id = 0; // is in hex
uint8_t length = 0;
uint8_t offset = 0;
// first check deviceID
if ((p = strtok(data_args, " ,"))) { // delimiter comma or space
strlcpy(value, p, 10); // get string
device_id = (uint8_t)Helpers::hextoint(value); // convert hex to int
if (!EMSESP::valid_device(device_id)) {
LOG_ERROR("Invalid device ID (%d) for read command", device_id);
return false; // invalid device
}
}
// iterate until end
uint8_t num_args = 0;
while (p != 0) {
if ((p = strtok(nullptr, " ,"))) { // delimiter comma or space
strlcpy(value, p, 10); // get string
if (num_args == 0) {
type_id = (uint16_t)Helpers::hextoint(value); // convert hex to int
} else if (num_args == 1) {
offset = Helpers::atoint(value); // decimal
} else if (num_args == 2) {
length = Helpers::atoint(value); // decimal
}
num_args++;
}
}
if (num_args == 0) {
return false; // invalid number of arguments
}
EMSESP::send_read_request(type_id, device_id, offset, length, true);
EMSESP::set_read_id(type_id);
return true;
}
// system read command
bool System::command_read(const char * value, const int8_t id) {
return readCommand(value);
}
} // namespace emsesp

View File

@@ -63,6 +63,7 @@ class System {
void loop();
// commands
static bool command_read(const char * value, const int8_t id);
static bool command_send(const char * value, const int8_t id);
static bool command_publish(const char * value, const int8_t id);
static bool command_fetch(const char * value, const int8_t id);
@@ -124,6 +125,8 @@ class System {
static bool is_valid_gpio(uint8_t pin, bool has_psram = false);
static bool load_board_profile(std::vector<int8_t> & data, const std::string & board_profile);
static bool readCommand(const char * data);
static void restart_requested(bool restart_requested) {
restart_requested_ = restart_requested;
}
@@ -170,6 +173,14 @@ class System {
readonly_mode_ = readonly_mode;
}
bool developer_mode() {
return developer_mode_;
}
void developer_mode(bool developer_mode) {
developer_mode_ = developer_mode;
}
// Boolean Format API/MQTT
uint8_t bool_format() {
return bool_format_;
@@ -398,6 +409,7 @@ class System {
uint16_t modbus_port_;
uint8_t modbus_max_clients_;
uint32_t modbus_timeout_;
bool developer_mode_;
// ethernet
uint8_t phy_type_;

View File

@@ -638,7 +638,7 @@ uint16_t TxService::read_next_tx(const uint8_t offset, const uint8_t length) {
// some telegrams only reply with one byte less, but have higher offsets (boiler 0x10)
// some reply with higher offset than requests and have not_set values intermediate (boiler 0xEA)
// We have th last byte received
// We have the last byte received
if (offset + old_length >= telegram_last_->offset + telegram_last_->message_data[0]) {
return 0;
}

View File

@@ -973,16 +973,16 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
if (single) {
// run dedicated tests only
EMSESP::webCustomEntityService.test(); // custom entities
EMSESP::webCustomizationService.test(); // set customizations - this will overwrite any settings in the FS
EMSESP::temperaturesensor_.test(); // add temperature sensors
EMSESP::webSchedulerService.test(); // run scheduler tests, and conditions
// EMSESP::webCustomEntityService.test(); // custom entities
// EMSESP::webCustomizationService.test(); // set customizations - this will overwrite any settings in the FS
// EMSESP::temperaturesensor_.test(); // add temperature sensors
// EMSESP::webSchedulerService.test(); // run scheduler tests, and conditions
// request.url("/rest/deviceEntities");
// EMSESP::webCustomizationService.device_entities(&request);
request.url("/rest/dashboardData");
EMSESP::webDataService.dashboard_data(&request);
// request.url("/rest/dashboardData");
// EMSESP::webDataService.dashboard_data(&request);
// COMMANDS
// shell.invoke_command("call system fetch");
@@ -999,28 +999,46 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
// EMSESP::webAPIService.webAPIService(&request);
// POST COMMANDS
// request.method(HTTP_POST);
request.method(HTTP_POST);
// char data1[] = "{\"device\":\"system\", \"cmd\":\"restart\",\"id\":-1}";
// deserializeJson(doc, data1);
// request.url("/api");
// EMSESP::webAPIService.webAPIService(&request, doc.as<JsonVariant>());
// char data2[] = "{\"action\":\"customSupport\", \"param\":\"hello\"}";
// deserializeJson(doc, data2);
// request.url("/rest/action");
// EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
char data2[] = "{\"action\":\"customSupport\", \"param\":\"hello\"}";
deserializeJson(doc, data2);
request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
// char data3[] = "{\"action\":\"export\", \"param\":\"schedule\"}";
// deserializeJson(doc, data3);
// request.url("/rest/action");
// EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
char data3[] = "{\"action\":\"export\", \"param\":\"schedule\"}";
deserializeJson(doc, data3);
request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
// char data4[] = "{\"action\":\"export\", \"param\":\"allvalues\"}";
// deserializeJson(doc, data4);
// request.url("/rest/action");
// EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
char data4[] = "{\"action\":\"export\", \"param\":\"allvalues\"}";
deserializeJson(doc, data4);
request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
char data5[] = "{\"action\":\"checkUpgrade\", \"param\":\"3.7.0-dev.99\"}";
deserializeJson(doc, data5);
request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
// char data6[] = "{\"device\":\"system\", \"cmd\":\"read\",\"value\":\"8 2 27 1\"}";
// deserializeJson(doc, data6);
// json = doc.as<JsonVariant>();
// request.url("/api");
// EMSESP::webAPIService.webAPIService(&request, json);
// char data7[] = "{\"device\":\"system\", \"cmd\":\"read\",\"value\":\"10 234\"}";
// deserializeJson(doc, data7);
// json = doc.as<JsonVariant>();
// request.url("/api");
// EMSESP::webAPIService.webAPIService(&request, json);
// shell.invoke_command("call system read \"8 2 27 1\"");
} else {
EMSESP::webCustomEntityService.test(); // custom entities

View File

@@ -1 +1 @@
#define EMSESP_APP_VERSION "3.7.0-dev.46"
#define EMSESP_APP_VERSION "3.7.0-dev.48"

View File

@@ -28,8 +28,9 @@ WebAPIService::WebAPIService(AsyncWebServer * server, SecurityManager * security
server->on(EMSESP_API_SERVICE_PATH, [this](AsyncWebServerRequest * request, JsonVariant json) { webAPIService(request, json); });
}
// POST|GET /{device}
// POST|GET /{device}/{entity}
// POST|GET api/
// POST|GET api/{device}
// POST|GET api/{device}/{entity}
void WebAPIService::webAPIService(AsyncWebServerRequest * request, JsonVariant json) {
JsonObject input;
// if no body then treat it as a secure GET

View File

@@ -237,12 +237,13 @@ void WebLogService::transmit(const QueuedLogMessage & message) {
void WebLogService::getSetValues(AsyncWebServerRequest * request, JsonVariant json) {
if ((request->method() == HTTP_GET) || (!json.is<JsonObject>())) {
// GET - return the values
auto * response = new AsyncJsonResponse(false);
JsonObject root = response->getRoot();
root["level"] = log_level();
root["max_messages"] = maximum_log_messages();
root["compact"] = compact();
root["psram"] = (EMSESP::system_.PSram() > 0);
auto * response = new AsyncJsonResponse(false);
JsonObject root = response->getRoot();
root["level"] = log_level();
root["max_messages"] = maximum_log_messages();
root["compact"] = compact();
root["psram"] = (EMSESP::system_.PSram() > 0);
root["developer_mode"] = EMSESP::system_.developer_mode();
response->setLength();
request->send(response);

View File

@@ -80,6 +80,7 @@ void WebSettings::read(WebSettings & settings, JsonObject root) {
root["modbus_port"] = settings.modbus_port;
root["modbus_max_clients"] = settings.modbus_max_clients;
root["modbus_timeout"] = settings.modbus_timeout;
root["developer_mode"] = settings.developer_mode;
}
// call on initialization and also when settings are updated via web or console
@@ -361,6 +362,9 @@ StateUpdateResult WebSettings::update(JsonObject root, WebSettings & settings) {
settings.readonly_mode = root["readonly_mode"];
EMSESP::system_.readonly_mode(settings.readonly_mode);
settings.developer_mode = root["developer_mode"];
EMSESP::system_.developer_mode(settings.developer_mode);
settings.bool_dashboard = root["bool_dashboard"] | EMSESP_DEFAULT_BOOL_FORMAT;
EMSESP::system_.bool_dashboard(settings.bool_dashboard);

View File

@@ -65,10 +65,13 @@ class WebSettings {
uint8_t bool_format;
uint8_t bool_dashboard;
uint8_t enum_format;
int8_t weblog_level;
uint8_t weblog_buffer;
bool weblog_compact;
bool fahrenheit;
int8_t weblog_level;
uint8_t weblog_buffer;
bool weblog_compact;
bool fahrenheit;
bool modbus_enabled;
uint16_t modbus_port;
uint8_t modbus_max_clients;
@@ -79,6 +82,8 @@ class WebSettings {
uint8_t eth_phy_addr;
uint8_t eth_clock_mode;
bool developer_mode; // developer mode
static void read(WebSettings & settings, JsonObject root);
static StateUpdateResult update(JsonObject root, WebSettings & settings);

View File

@@ -154,29 +154,37 @@ void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json)
auto * response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
// get action and any optional param
std::string action = json["action"];
std::string param = json["param"]; // is optional
// param is optional - https://arduinojson.org/news/2024/09/18/arduinojson-7-2/
std::string param;
bool has_param = false;
JsonVariant param_optional = json["param"];
if (json["param"].is<const char *>()) {
param = param_optional.as<std::string>();
has_param = true;
} else {
has_param = false;
}
// check if we're authenticated for admin tasks, some actions are only for admins
Authentication authentication = _securityManager->authenticateRequest(request);
bool is_admin = AuthenticationPredicates::IS_ADMIN(authentication);
bool ok = true;
// call action command
bool ok = false;
std::string action = json["action"];
if (action == "checkUpgrade") {
ok = checkUpgrade(root, param);
ok = checkUpgrade(root, param); // param could be empty, if so only send back version
} else if (action == "export") {
ok = exportData(root, param);
if (has_param) {
ok = exportData(root, param);
}
} else if (action == "customSupport") {
ok = customSupport(root);
} else if (action == "uploadURL" && is_admin) {
ok = uploadURL(param.c_str());
}
#if defined(EMSESP_UNITY)
// store the result so we can test with Unity later
storeResponse(output);
#endif
#if defined(EMSESP_STANDALONE) && !defined(EMSESP_UNITY)
Serial.printf("%sweb output: %s[%s]", COLOR_WHITE, COLOR_BRIGHT_CYAN, request->url().c_str());
Serial.printf(" %s(%d)%s ", ok ? COLOR_BRIGHT_GREEN : COLOR_BRIGHT_RED, ok ? 200 : 400, COLOR_YELLOW);
@@ -195,18 +203,21 @@ void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json)
}
// action = checkUpgrade
// returns true if there is an upgrade available
bool WebStatusService::checkUpgrade(JsonObject root, std::string & latest_version) {
version::Semver200_version settings_version(EMSESP_APP_VERSION);
version::Semver200_version this_version(latest_version);
root["emsesp_version"] = EMSESP_APP_VERSION;
if (!latest_version.empty()) {
#if defined(EMSESP_DEBUG)
emsesp::EMSESP::logger().debug("Checking for upgrade: %s > %s", EMSESP_APP_VERSION, latest_version.c_str());
emsesp::EMSESP::logger().debug("Checking for upgrade: %s > %s", EMSESP_APP_VERSION, latest_version.c_str());
#endif
root["upgradeable"] = (this_version > settings_version);
version::Semver200_version settings_version(EMSESP_APP_VERSION);
version::Semver200_version this_version(latest_version);
return true;
root["upgradeable"] = (this_version > settings_version);
}
return true; // always ok
}
// action = allvalues
@@ -261,8 +272,9 @@ bool WebStatusService::exportData(JsonObject root, std::string & type) {
root.clear(); // don't need the "type" key added to the output
allvalues(root);
} else {
return false;
return false; // error
}
return true;
}