refactor commands to take dynamic translation descriptions #674

This commit is contained in:
proddy
2022-10-09 22:19:29 +02:00
parent 8d6c676fed
commit 65c9bf22dc
10 changed files with 52 additions and 42 deletions

View File

@@ -425,7 +425,7 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
if (commands.exact.count(longest->first) == 1) { if (commands.exact.count(longest->first) == 1) {
for (auto & name : longest->second->name_) { for (auto & name : longest->second->name_) {
result.replacement->push_back(std::move((name))); // TODO remove all moves? result.replacement->push_back(name);
} }
// Add a space because there are sub-commands for a command that has matched exactly // Add a space because there are sub-commands for a command that has matched exactly

View File

@@ -31,7 +31,6 @@ void AnalogSensor::start() {
} }
analogSetAttenuation(ADC_2_5db); // for all channels 1.5V analogSetAttenuation(ADC_2_5db); // for all channels 1.5V
LOG_INFO("Starting Analog sensor service"); LOG_INFO("Starting Analog sensor service");
// Add API call for /info // Add API call for /info
@@ -39,18 +38,18 @@ void AnalogSensor::start() {
EMSdevice::DeviceType::ANALOGSENSOR, EMSdevice::DeviceType::ANALOGSENSOR,
F_(info), F_(info),
[&](const char * value, const int8_t id, JsonObject & output) { return command_info(value, id, output); }, [&](const char * value, const int8_t id, JsonObject & output) { return command_info(value, id, output); },
F_(info_cmd)); FL_(info_cmd));
Command::add( Command::add(
EMSdevice::DeviceType::ANALOGSENSOR, EMSdevice::DeviceType::ANALOGSENSOR,
F_(setvalue), F_(setvalue),
[&](const char * value, const int8_t id) { return command_setvalue(value, id); }, [&](const char * value, const int8_t id) { return command_setvalue(value, id); },
("set io value"), // TODO translate this FL_(setiovalue_cmd),
CommandFlag::ADMIN_ONLY); CommandFlag::ADMIN_ONLY);
Command::add( Command::add(
EMSdevice::DeviceType::ANALOGSENSOR, EMSdevice::DeviceType::ANALOGSENSOR,
F_(commands), F_(commands),
[&](const char * value, const int8_t id, JsonObject & output) { return command_commands(value, id, output); }, [&](const char * value, const int8_t id, JsonObject & output) { return command_commands(value, id, output); },
F_(commands_cmd)); FL_(commands_cmd));
Mqtt::subscribe(EMSdevice::DeviceType::ANALOGSENSOR, "analogsensor/#", nullptr); // use empty function callback Mqtt::subscribe(EMSdevice::DeviceType::ANALOGSENSOR, "analogsensor/#", nullptr); // use empty function callback
} }

View File

@@ -299,7 +299,7 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
} }
// add a command to the list, which does not return json // add a command to the list, which does not return json
void Command::add(const uint8_t device_type, const char * cmd, const cmd_function_p cb, const char * description, uint8_t flags) { void Command::add(const uint8_t device_type, const char * cmd, const cmd_function_p cb, const char * const * description, uint8_t flags) {
// 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, cmd) != nullptr) { if (find_command(device_type, cmd) != nullptr) {
return; return;
@@ -314,7 +314,7 @@ void Command::add(const uint8_t device_type, const char * cmd, const cmd_functio
} }
// add a command to the list, which does return a json object as output // add a command to the list, which does return a json object as output
void Command::add(const uint8_t device_type, const char * cmd, const cmd_json_function_p cb, const char * description, uint8_t flags) { void Command::add(const uint8_t device_type, const char * cmd, const cmd_json_function_p cb, const char * const * description, uint8_t flags) {
// 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, cmd) != nullptr) { if (find_command(device_type, cmd) != nullptr) {
return; return;
@@ -348,7 +348,6 @@ Command::CmdFunction * Command::find_command(const uint8_t device_type, const ch
// list all commands for a specific device, output as json // list all commands for a specific device, output as json
bool Command::list(const uint8_t device_type, JsonObject & output) { bool Command::list(const uint8_t device_type, JsonObject & output) {
if (cmdfunctions_.empty()) { if (cmdfunctions_.empty()) {
output["message"] = "no commands available"; output["message"] = "no commands available";
return false; return false;
@@ -366,7 +365,7 @@ bool Command::list(const uint8_t device_type, JsonObject & output) {
for (const auto & cl : sorted_cmds) { for (const auto & cl : sorted_cmds) {
for (const auto & cf : cmdfunctions_) { for (const auto & cf : cmdfunctions_) {
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN) && cf.description_ && (cl == std::string(cf.cmd_))) { if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN) && cf.description_ && (cl == std::string(cf.cmd_))) {
output[cl] = cf.description_; output[cl] = Helpers::translated_fullname(cf.description_);
} }
} }
} }
@@ -425,7 +424,7 @@ void Command::show(uuid::console::Shell & shell, uint8_t device_type, bool verbo
shell.print(EMSdevice::tag_to_string(DeviceValueTAG::TAG_DEVICE_DATA_WW)); shell.print(EMSdevice::tag_to_string(DeviceValueTAG::TAG_DEVICE_DATA_WW));
shell.print(' '); shell.print(' ');
} }
shell.print((cf.description_)); shell.print(Helpers::translated_fullname(cf.description_));
if (!cf.has_flags(CommandFlag::ADMIN_ONLY)) { if (!cf.has_flags(CommandFlag::ADMIN_ONLY)) {
shell.print(' '); shell.print(' ');
shell.print(COLOR_BRIGHT_RED); shell.print(COLOR_BRIGHT_RED);

View File

@@ -59,14 +59,14 @@ class Command {
const char * cmd_; const char * cmd_;
const cmd_function_p cmdfunction_; const cmd_function_p cmdfunction_;
const cmd_json_function_p cmdfunction_json_; const cmd_json_function_p cmdfunction_json_;
const char * description_; const char * const * description_;
CmdFunction(const uint8_t device_type, CmdFunction(const uint8_t device_type,
const uint8_t flags, const uint8_t flags,
const char * cmd, const char * cmd,
const cmd_function_p cmdfunction, const cmd_function_p cmdfunction,
const cmd_json_function_p cmdfunction_json, const cmd_json_function_p cmdfunction_json,
const char * description) const char * const * description)
: device_type_(device_type) : device_type_(device_type)
, flags_(flags) , flags_(flags)
, cmd_(cmd) , cmd_(cmd)
@@ -97,12 +97,18 @@ class Command {
static uint8_t call(const uint8_t device_type, const char * cmd, const char * value); static uint8_t call(const uint8_t device_type, const char * cmd, const char * value);
// with normal call back function taking a value and id // with normal call back function taking a value and id
static void static void add(const uint8_t device_type,
add(const uint8_t device_type, const char * cmd, const cmd_function_p cb, const char * description, uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT); const char * cmd,
const cmd_function_p cb,
const char * const * description,
uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT);
// callback function taking value, id and a json object for its output // callback function taking value, id and a json object for its output
static void static void add(const uint8_t device_type,
add(const uint8_t device_type, const char * cmd, const cmd_json_function_p cb, const char * description, uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT); const char * cmd,
const cmd_json_function_p cb,
const char * const * description,
uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT);
static void show_all(uuid::console::Shell & shell); static void show_all(uuid::console::Shell & shell);
static Command::CmdFunction * find_command(const uint8_t device_type, const char * cmd); static Command::CmdFunction * find_command(const uint8_t device_type, const char * cmd);

View File

@@ -50,12 +50,12 @@ void DallasSensor::start() {
EMSdevice::DeviceType::DALLASSENSOR, EMSdevice::DeviceType::DALLASSENSOR,
F_(info), F_(info),
[&](const char * value, const int8_t id, JsonObject & output) { return command_info(value, id, output); }, [&](const char * value, const int8_t id, JsonObject & output) { return command_info(value, id, output); },
F_(info_cmd)); FL_(info_cmd));
Command::add( Command::add(
EMSdevice::DeviceType::DALLASSENSOR, EMSdevice::DeviceType::DALLASSENSOR,
F_(commands), F_(commands),
[&](const char * value, const int8_t id, JsonObject & output) { return command_commands(value, id, output); }, [&](const char * value, const int8_t id, JsonObject & output) { return command_commands(value, id, output); },
F_(commands_cmd)); FL_(commands_cmd));
Mqtt::subscribe(EMSdevice::DeviceType::DALLASSENSOR, "dallasssensor/#", nullptr); // use empty function callback Mqtt::subscribe(EMSdevice::DeviceType::DALLASSENSOR, "dallasssensor/#", nullptr); // use empty function callback
} }

View File

@@ -495,8 +495,7 @@ void EMSdevice::add_device_value(uint8_t tag,
} }
// add the command to our library // add the command to our library
// cmd is the short_name and the description is the fullname (not the custom fullname) Command::add(device_type_, short_name, f, fullname, flags);
Command::add(device_type_, short_name, f, Helpers::translated_fullname(fullname), flags);
} }
// single list of options // single list of options

View File

@@ -1052,7 +1052,7 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, const
[device_type](const char * value, const int8_t id, JsonObject & output) { [device_type](const char * value, const int8_t id, JsonObject & output) {
return command_info(device_type, output, id, EMSdevice::OUTPUT_TARGET::API_VERBOSE); return command_info(device_type, output, id, EMSdevice::OUTPUT_TARGET::API_VERBOSE);
}, },
F_(info_cmd)); FL_(info_cmd));
Command::add( Command::add(
device_type, device_type,
("values"), ("values"),
@@ -1065,12 +1065,12 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, const
device_type, device_type,
F_(commands), F_(commands),
[device_type](const char * value, const int8_t id, JsonObject & output) { return command_commands(device_type, output, id); }, [device_type](const char * value, const int8_t id, JsonObject & output) { return command_commands(device_type, output, id); },
F_(commands_cmd)); FL_(commands_cmd));
Command::add( Command::add(
device_type, device_type,
F_(entities), F_(entities),
[device_type](const char * value, const int8_t id, JsonObject & output) { return command_entities(device_type, output, id); }, [device_type](const char * value, const int8_t id, JsonObject & output) { return command_entities(device_type, output, id); },
F_(entities_cmd)); FL_(entities_cmd));
// MQTT subscribe to the device e.g. "ems-esp/boiler/#" // MQTT subscribe to the device e.g. "ems-esp/boiler/#"
Mqtt::subscribe(device_type, EMSdevice::device_type_2_device_name(device_type) + "/#", nullptr); Mqtt::subscribe(device_type, EMSdevice::device_type_2_device_name(device_type) + "/#", nullptr);

View File

@@ -219,11 +219,6 @@ MAKE_PSTR(uom_sqm, "sqm")
MAKE_PSTR(uom_m3, "m3") MAKE_PSTR(uom_m3, "m3")
MAKE_PSTR(uom_l, "l") MAKE_PSTR(uom_l, "l")
// commands
MAKE_PSTR(info_cmd, "lists all values")
MAKE_PSTR(commands_cmd, "lists all commands")
MAKE_PSTR(entities_cmd, "lists all entities")
// TAG mapping - maps to DeviceValueTAG_s in emsdevice.cpp // TAG mapping - maps to DeviceValueTAG_s in emsdevice.cpp
// use empty string if want to suppress showing tags // use empty string if want to suppress showing tags
// mqtt tags must not have spaces // mqtt tags must not have spaces

View File

@@ -31,6 +31,23 @@
// translations are in order en, de,nl, se.... // translations are in order en, de,nl, se....
// if there is no translation, it will default to en // if there is no translation, it will default to en
// commands
MAKE_PSTR_LIST(info_cmd, "lists all values")
MAKE_PSTR_LIST(commands_cmd, "lists all commands")
MAKE_PSTR_LIST(entities_cmd, "lists all entities")
MAKE_PSTR_LIST(send_cmd, "send a telegram")
MAKE_PSTR_LIST(setiovalue_cmd, "set io value")
MAKE_PSTR_LIST(changeloglevel_cmd, "change log level")
MAKE_PSTR_LIST(fetch_cmd, "refresh all EMS values")
MAKE_PSTR_LIST(restart_cmd, "restart EMS-ESP")
MAKE_PSTR_LIST(watch_cmd, "watch incoming telegrams")
MAKE_PSTR_LIST(publish_cmd, "publish all to MQTT")
MAKE_PSTR_LIST(system_info_cmd, "show system status")
#if defined(EMSESP_DEBUG)
MAKE_PSTR_LIST(test_cmd, "run a test")
#endif
// General // General
MAKE_PSTR_LIST(on, "on", "an", "aan", "") MAKE_PSTR_LIST(on, "on", "an", "aan", "")
MAKE_PSTR_LIST(off, "off", "aus", "uit", "av") MAKE_PSTR_LIST(off, "off", "aus", "uit", "av")

View File

@@ -249,8 +249,7 @@ void System::syslog_init() {
syslog_.hostname(hostname().c_str()); syslog_.hostname(hostname().c_str());
// register the command // register the command
// TODO translate this Command::add(EMSdevice::DeviceType::SYSTEM, F_(syslog), System::command_syslog_level, FL_(changeloglevel_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(syslog), System::command_syslog_level, ("change the syslog level"), CommandFlag::ADMIN_ONLY);
} else if (was_enabled) { } else if (was_enabled) {
// in case service is still running, this flushes the queue // in case service is still running, this flushes the queue
@@ -664,25 +663,21 @@ void System::system_check() {
// commands - takes static function pointers // commands - takes static function pointers
void System::commands_init() { void System::commands_init() {
// TODO translate this Command::add(EMSdevice::DeviceType::SYSTEM, F_(send), System::command_send, FL_(send_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(send), System::command_send, ("send a telegram"), 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_(fetch), System::command_fetch, ("refresh all EMS values"), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(restart), System::command_restart, FL_(restart_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(restart), System::command_restart, ("restart EMS-ESP"), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(watch), System::command_watch, FL_(watch_cmd));
Command::add(EMSdevice::DeviceType::SYSTEM, F_(watch), System::command_watch, ("watch incoming telegrams"));
// register syslog command in syslog init
// Command::add(EMSdevice::DeviceType::SYSTEM, F_(syslog), System::command_syslog_level, ("set syslog level"), CommandFlag::ADMIN_ONLY);
if (Mqtt::enabled()) { if (Mqtt::enabled()) {
Command::add(EMSdevice::DeviceType::SYSTEM, F_(publish), System::command_publish, ("force a MQTT publish")); Command::add(EMSdevice::DeviceType::SYSTEM, F_(publish), System::command_publish, FL_(publish_cmd));
} }
// these commands will return data in JSON format // these commands will return data in JSON format
Command::add(EMSdevice::DeviceType::SYSTEM, F_(info), System::command_info, ("show system status")); Command::add(EMSdevice::DeviceType::SYSTEM, F_(info), System::command_info, FL_(system_info_cmd));
Command::add(EMSdevice::DeviceType::SYSTEM, F_(commands), System::command_commands, ("fetch system commands")); Command::add(EMSdevice::DeviceType::SYSTEM, F_(commands), System::command_commands, FL_(commands_cmd));
#if defined(EMSESP_DEBUG) #if defined(EMSESP_DEBUG)
Command::add(EMSdevice::DeviceType::SYSTEM, ("test"), System::command_test, ("run a specific test")); Command::add(EMSdevice::DeviceType::SYSTEM, ("test"), System::command_test, FL_(test_cmd));
#endif #endif
// MQTT subscribe "ems-esp/system/#" // MQTT subscribe "ems-esp/system/#"