mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
split out commands with and without json
This commit is contained in:
@@ -52,7 +52,7 @@ void WebAPIService::webAPIService(AsyncWebServerRequest * request) {
|
|||||||
String cmd = request->getParam(F_(cmd))->value();
|
String cmd = request->getParam(F_(cmd))->value();
|
||||||
|
|
||||||
// look up command in our list
|
// look up command in our list
|
||||||
if (!Command::find_command(device_type, cmd.c_str())) {
|
if (Command::find_command(device_type, cmd.c_str()) == nullptr) {
|
||||||
request->send(400, "text/plain", F("Invalid cmd"));
|
request->send(400, "text/plain", F("Invalid cmd"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,29 @@ std::vector<Command::CmdFunction> Command::cmdfunctions_;
|
|||||||
// calls a command, context is the device_type
|
// calls a command, context is the device_type
|
||||||
// id may be used to represent a heating circuit for example
|
// id may be used to represent a heating circuit for example
|
||||||
// returns false if error or not found
|
// returns false if error or not found
|
||||||
|
bool Command::call(const uint8_t device_type, const char * cmd, const char * value, const int8_t id) {
|
||||||
|
#ifdef EMSESP_DEBUG
|
||||||
|
std::string dname = EMSdevice::device_type_2_device_name(device_type);
|
||||||
|
if (value == nullptr) {
|
||||||
|
LOG_DEBUG(F("[DEBUG] Calling %s command %s"), dname.c_str(), cmd);
|
||||||
|
} else if (id == -1) {
|
||||||
|
LOG_DEBUG(F("[DEBUG] Calling %s command %s, value %s, id is default"), dname.c_str(), cmd, value);
|
||||||
|
} else {
|
||||||
|
LOG_DEBUG(F("[DEBUG] Calling %s command %s, value %s, id is %d"), dname.c_str(), cmd, value, id);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
auto cf = find_command(device_type, cmd);
|
||||||
|
if ((cf == nullptr) || (cf->cmdfunction_json_)) {
|
||||||
|
return false; // command not found, or requires a json
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((cf->cmdfunction_)(value, id));
|
||||||
|
}
|
||||||
|
|
||||||
|
// calls a command, context is the device_type. Takes a json object for output.
|
||||||
|
// id may be used to represent a heating circuit for example
|
||||||
|
// returns false if error or not found
|
||||||
bool Command::call(const uint8_t device_type, const char * cmd, const char * value, const int8_t id, JsonObject & json) {
|
bool Command::call(const uint8_t device_type, const char * cmd, const char * value, const int8_t id, JsonObject & json) {
|
||||||
#ifdef EMSESP_DEBUG
|
#ifdef EMSESP_DEBUG
|
||||||
std::string dname = EMSdevice::device_type_2_device_name(device_type);
|
std::string dname = EMSdevice::device_type_2_device_name(device_type);
|
||||||
@@ -37,31 +60,22 @@ bool Command::call(const uint8_t device_type, const char * cmd, const char * val
|
|||||||
} else if (id == -1) {
|
} else if (id == -1) {
|
||||||
LOG_DEBUG(F("[DEBUG] Calling %s command %s, value %s, id is default"), dname.c_str(), cmd, value);
|
LOG_DEBUG(F("[DEBUG] Calling %s command %s, value %s, id is default"), dname.c_str(), cmd, value);
|
||||||
} else {
|
} else {
|
||||||
LOG_DEBUG(F("[DEBUG] Calling %s command %s, value %s, id is %d in %s"), dname.c_str(), cmd, value, id);
|
LOG_DEBUG(F("[DEBUG] Calling %s command %s, value %s, id is %d"), dname.c_str(), cmd, value, id);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
bool ok = false;
|
|
||||||
if (!cmdfunctions_.empty()) {
|
auto cf = find_command(device_type, cmd);
|
||||||
for (const auto & cf : cmdfunctions_) {
|
if ((cf == nullptr) || (!cf->cmdfunction_json_)) {
|
||||||
if (cf.device_type_ == device_type) {
|
return false; // command not found or not json
|
||||||
// find a matching command and call it
|
|
||||||
if (uuid::read_flash_string(cf.cmd_) == cmd) {
|
|
||||||
// see if we this function wants to send its result to a json doc
|
|
||||||
if (cf.cmdfunction_json_) {
|
|
||||||
// check if json object is empty, if so quit
|
|
||||||
if (json.isNull()) {
|
|
||||||
LOG_WARNING(F("Ignore call for command %s in %s because no json"), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ok |= ((cf.cmdfunction_json_)(value, id, json));
|
|
||||||
} else {
|
|
||||||
ok |= ((cf.cmdfunction_)(value, id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ok;
|
|
||||||
|
// check if json object is empty, if so quit
|
||||||
|
if (json.isNull()) {
|
||||||
|
LOG_WARNING(F("Ignore call for command %s in %s because no json"), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((cf->cmdfunction_json_)(value, id, json));
|
||||||
}
|
}
|
||||||
|
|
||||||
// add a command to the list, which does not return json
|
// add a command to the list, which does not return json
|
||||||
@@ -77,24 +91,26 @@ void Command::add(const uint8_t device_type, const uint8_t device_id, const __Fl
|
|||||||
// add a command to the list, which does return json object as output
|
// add a command to the list, which does return json object as output
|
||||||
void Command::add_with_json(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_json_p cb) {
|
void Command::add_with_json(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_json_p cb) {
|
||||||
// if the command already exists for that device type don't add it
|
// if the command already exists for that device type don't add it
|
||||||
if (!find_command(device_type, uuid::read_flash_string(cmd).c_str())) {
|
if (find_command(device_type, uuid::read_flash_string(cmd).c_str()) != nullptr) {
|
||||||
cmdfunctions_.emplace_back(device_type, cmd, nullptr, cb);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmdfunctions_.emplace_back(device_type, cmd, nullptr, cb); // add command
|
||||||
}
|
}
|
||||||
|
|
||||||
// see if a command exists for that device type
|
// see if a command exists for that device type
|
||||||
bool Command::find_command(const uint8_t device_type, const char * cmd) {
|
Command::CmdFunction * Command::find_command(const uint8_t device_type, const char * cmd) {
|
||||||
if ((cmd == nullptr) || (strlen(cmd) == 0)) {
|
if ((cmd == nullptr) || (strlen(cmd) == 0) || (cmdfunctions_.empty())) {
|
||||||
return false;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto & cf : cmdfunctions_) {
|
for (auto & cf : cmdfunctions_) {
|
||||||
if (!strcmp_P(cmd, reinterpret_cast<PGM_P>(cf.cmd_)) && (cf.device_type_ == device_type)) {
|
if (!strcmp_P(cmd, reinterpret_cast<PGM_P>(cf.cmd_)) && (cf.device_type_ == device_type)) {
|
||||||
return true;
|
return &cf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false; // not found
|
return nullptr; // not found
|
||||||
}
|
}
|
||||||
|
|
||||||
// output list of all commands to console for a specific DeviceType
|
// output list of all commands to console for a specific DeviceType
|
||||||
|
|||||||
@@ -57,11 +57,13 @@ class Command {
|
|||||||
return cmdfunctions_;
|
return cmdfunctions_;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool call(const uint8_t device_type, const char * cmd, const char * value, const int8_t id, JsonObject & json);
|
static bool call(const uint8_t device_type, const char * cmd, const char * value, const int8_t id, JsonObject & json);
|
||||||
static void add(const uint8_t device_type, const uint8_t device_id, const __FlashStringHelper * cmd, cmdfunction_p cb);
|
static bool call(const uint8_t device_type, const char * cmd, const char * value, const int8_t id);
|
||||||
static void add_with_json(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_json_p cb);
|
static void add(const uint8_t device_type, const uint8_t device_id, const __FlashStringHelper * cmd, cmdfunction_p cb);
|
||||||
static void show_all(uuid::console::Shell & shell);
|
static void add_with_json(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_json_p cb);
|
||||||
static bool find_command(const uint8_t device_type, const char * cmd);
|
static void show_all(uuid::console::Shell & shell);
|
||||||
|
static Command::CmdFunction * find_command(const uint8_t device_type, const char * cmd);
|
||||||
|
|
||||||
static void show(uuid::console::Shell & shell, uint8_t device_type);
|
static void show(uuid::console::Shell & shell, uint8_t device_type);
|
||||||
static void show_devices(uuid::console::Shell & shell);
|
static void show_devices(uuid::console::Shell & shell);
|
||||||
static bool device_has_commands(const uint8_t device_type);
|
static bool device_has_commands(const uint8_t device_type);
|
||||||
|
|||||||
Reference in New Issue
Block a user