add device_id to commands, fix #826

This commit is contained in:
MichaelDvP
2022-12-22 13:03:37 +01:00
parent cfa486d8cc
commit 9cbb810fe4
9 changed files with 49 additions and 21 deletions

View File

@@ -26,10 +26,13 @@
- Add commands for analog sensors outputs - Add commands for analog sensors outputs
- Support for multiple EMS-ESPs with MQTT and HA [[#759](https://github.com/emsesp/EMS-ESP32/issues/759)] - Support for multiple EMS-ESPs with MQTT and HA [[#759](https://github.com/emsesp/EMS-ESP32/issues/759)]
- Settings for heatpump silent mode and additional heater [[#802](https://github.com/emsesp/EMS-ESP32/issues/802)] [[#803](https://github.com/emsesp/EMS-ESP32/issues/803)] - Settings for heatpump silent mode and additional heater [[#802](https://github.com/emsesp/EMS-ESP32/issues/802)] [[#803](https://github.com/emsesp/EMS-ESP32/issues/803)]
- Zone module MZ100 [#826](https://github.com/emsesp/EMS-ESP32/issues/826)
## Fixed ## Fixed
- Factory Reset not working [#628](https://github.com/emsesp/EMS-ESP32/issues/628) - Factory Reset not working [#628](https://github.com/emsesp/EMS-ESP32/issues/628)
- Valid 4 byte values [#820](https://github.com/emsesp/EMS-ESP32/issues/820)
- Commands for multiple thermostats [#826](https://github.com/emsesp/EMS-ESP32/issues/826)
## Changed ## Changed

View File

@@ -47,6 +47,7 @@ void AnalogSensor::start() {
CommandFlag::HIDDEN); // this command is hidden CommandFlag::HIDDEN); // this command is hidden
Command::add( Command::add(
EMSdevice::DeviceType::ANALOGSENSOR, EMSdevice::DeviceType::ANALOGSENSOR,
0,
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); },
FL_(setiovalue_cmd), FL_(setiovalue_cmd),
@@ -120,6 +121,7 @@ void AnalogSensor::reload() {
if (sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT) { if (sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT) {
Command::add( Command::add(
EMSdevice::DeviceType::ANALOGSENSOR, EMSdevice::DeviceType::ANALOGSENSOR,
0,
sensor.name.c_str(), sensor.name.c_str(),
[&](const char * value, const int8_t id) { return command_setvalue(value, sensor.gpio); }, [&](const char * value, const int8_t id) { return command_setvalue(value, sensor.gpio); },
sensor.type == AnalogType::COUNTER ? FL_(counter) sensor.type == AnalogType::COUNTER ? FL_(counter)

View File

@@ -251,8 +251,15 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
auto dname = EMSdevice::device_type_2_device_name(device_type); auto dname = EMSdevice::device_type_2_device_name(device_type);
uint8_t device_id = 0;
for (const auto & emsdevice : emsesp::EMSESP::emsdevices) {
if (emsdevice->device_type() == device_type && emsdevice->has_cmd(id, cmd)) {
device_id = emsdevice->device_id();
}
}
// see if there is a command registered // see if there is a command registered
auto cf = find_command(device_type, cmd); auto cf = find_command(device_type, device_id, cmd);
// check if its a call to and end-point to a device // check if its a call to and end-point to a device
// except for system commands as this is a special device without any queryable entities (device values) // except for system commands as this is a special device without any queryable entities (device values)
@@ -287,7 +294,7 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
LOG_DEBUG(("%sCalling command %s"), ro.c_str(), info_s); LOG_DEBUG(("%sCalling command %s"), ro.c_str(), info_s);
} else { } else {
if (id > 0) { if (id > 0) {
LOG_DEBUG(("%sCalling command %s with value %s and id %d"), ro.c_str(), info_s, value, id); LOG_DEBUG(("%sCalling command %s with value %s and id %d on device 0x%02X"), ro.c_str(), info_s, value, id, device_id);
} else { } else {
LOG_DEBUG(("%sCalling command %s with value %s"), ro.c_str(), info_s, value); LOG_DEBUG(("%sCalling command %s with value %s"), ro.c_str(), info_s, value);
} }
@@ -321,9 +328,9 @@ 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 * const * description, uint8_t flags) { void Command::add(const uint8_t device_type, const uint8_t device_id, 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, device_id, cmd) != nullptr) {
return; return;
} }
@@ -332,28 +339,28 @@ void Command::add(const uint8_t device_type, const char * cmd, const cmd_functio
flags |= CommandFlag::HIDDEN; flags |= CommandFlag::HIDDEN;
} }
cmdfunctions_.emplace_back(device_type, flags, cmd, cb, nullptr, description); // callback for json is nullptr cmdfunctions_.emplace_back(device_type, device_id, flags, cmd, cb, nullptr, description); // callback for json is nullptr
} }
// 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 * const * 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, 0, cmd) != nullptr) {
return; return;
} }
cmdfunctions_.emplace_back(device_type, flags, cmd, nullptr, cb, description); // callback for json is included cmdfunctions_.emplace_back(device_type, 0, flags, cmd, nullptr, cb, description); // callback for json is included
} }
// see if a command exists for that device type // see if a command exists for that device type
// is not case sensitive // is not case sensitive
Command::CmdFunction * Command::find_command(const uint8_t device_type, const char * cmd) { Command::CmdFunction * Command::find_command(const uint8_t device_type, const uint8_t device_id, const char * cmd) {
if ((cmd == nullptr) || (strlen(cmd) == 0) || (cmdfunctions_.empty())) { if ((cmd == nullptr) || (strlen(cmd) == 0) || (cmdfunctions_.empty())) {
return nullptr; return nullptr;
} }
for (auto & cf : cmdfunctions_) { for (auto & cf : cmdfunctions_) {
if (Helpers::toLower(cmd) == Helpers::toLower(cf.cmd_) && (cf.device_type_ == device_type)) { if (Helpers::toLower(cmd) == Helpers::toLower(cf.cmd_) && (cf.device_type_ == device_type) && (!device_id || cf.device_id_ == device_id)) {
return &cf; return &cf;
} }
} }

View File

@@ -55,19 +55,22 @@ class Command {
public: public:
struct CmdFunction { struct CmdFunction {
uint8_t device_type_; // DeviceType:: uint8_t device_type_; // DeviceType::
uint8_t flags_; // mqtt flags for command subscriptions uint8_t device_id_;
uint8_t flags_; // mqtt flags for command subscriptions
const char * cmd_; const char * cmd_;
cmd_function_p cmdfunction_; cmd_function_p cmdfunction_;
cmd_json_function_p cmdfunction_json_; cmd_json_function_p cmdfunction_json_;
const char * const * description_; const char * const * description_;
CmdFunction(const uint8_t device_type, CmdFunction(const uint8_t device_type,
const uint8_t device_id,
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 * const * description) const char * const * description)
: device_type_(device_type) : device_type_(device_type)
, device_id_(device_id)
, flags_(flags) , flags_(flags)
, cmd_(cmd) , cmd_(cmd)
, cmdfunction_(cmdfunction) , cmdfunction_(cmdfunction)
@@ -98,6 +101,7 @@ class Command {
// with normal call back function taking a value and id // with normal call back function taking a value and id
static void add(const uint8_t device_type, static void add(const uint8_t device_type,
const uint8_t device_id,
const char * cmd, const char * cmd,
const cmd_function_p cb, const cmd_function_p cb,
const char * const * description, const char * const * description,
@@ -111,7 +115,7 @@ class Command {
uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT); 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 uint8_t device_id, const char * cmd);
static void erase_command(const uint8_t device_type, const char * cmd); static void erase_command(const uint8_t device_type, const char * cmd);
static void show(uuid::console::Shell & shell, uint8_t device_type, bool verbose); static void show(uuid::console::Shell & shell, uint8_t device_type, bool verbose);

View File

@@ -135,7 +135,7 @@
{159, DeviceType::MIXER, "MM50", DeviceFlags::EMS_DEVICE_FLAG_MMPLUS}, {159, DeviceType::MIXER, "MM50", DeviceFlags::EMS_DEVICE_FLAG_MMPLUS},
{160, DeviceType::MIXER, "MM100", DeviceFlags::EMS_DEVICE_FLAG_MMPLUS}, {160, DeviceType::MIXER, "MM100", DeviceFlags::EMS_DEVICE_FLAG_MMPLUS},
{161, DeviceType::MIXER, "MM200", DeviceFlags::EMS_DEVICE_FLAG_MMPLUS}, {161, DeviceType::MIXER, "MM200", DeviceFlags::EMS_DEVICE_FLAG_MMPLUS},
{193, DeviceType::MIXER, "MM300", DeviceFlags::EMS_DEVICE_FLAG_MMPLUS}, {193, DeviceType::MIXER, "MZ100", DeviceFlags::EMS_DEVICE_FLAG_MMPLUS},
{204, DeviceType::MIXER, "MP100", DeviceFlags::EMS_DEVICE_FLAG_MP}, // pool {204, DeviceType::MIXER, "MP100", DeviceFlags::EMS_DEVICE_FLAG_MP}, // pool
// Heat Pumps - 0x38? // Heat Pumps - 0x38?

View File

@@ -273,6 +273,17 @@ bool EMSdevice::has_tag(const uint8_t tag) const {
return false; return false;
} }
// check if the device has a command on the with this tag.
bool EMSdevice::has_cmd(const int8_t id, const char * cmd) const {
uint8_t tag = DeviceValueTAG::TAG_HC1 + id - 1;
for (const auto & dv : devicevalues_) {
if ((id < 1 || dv.tag == tag) && dv.has_cmd && strcmp(dv.short_name, cmd) == 0) {
return true;
}
}
return false;
}
// list of registered device entries // list of registered device entries
// called from the command 'entities' // called from the command 'entities'
void EMSdevice::list_device_entries(JsonObject & output) const { void EMSdevice::list_device_entries(JsonObject & output) const {
@@ -507,7 +518,7 @@ void EMSdevice::add_device_value(uint8_t tag,
} }
// add the command to our library // add the command to our library
Command::add(device_type_, short_name, f, fullname, flags); Command::add(device_type_, device_id_, short_name, f, fullname, flags);
} }
// single list of options // single list of options

View File

@@ -53,6 +53,7 @@ class EMSdevice {
static std::string tag_to_mqtt(uint8_t tag); static std::string tag_to_mqtt(uint8_t tag);
bool has_tag(const uint8_t tag) const; bool has_tag(const uint8_t tag) const;
bool has_cmd(const int8_t id, const char * cmd) const;
inline uint8_t device_id() const { inline uint8_t device_id() const {
return device_id_; return device_id_;

View File

@@ -253,7 +253,7 @@ void System::syslog_init() {
syslog_.hostname(hostname().c_str()); syslog_.hostname(hostname().c_str());
// register the command // register the command
Command::add(EMSdevice::DeviceType::SYSTEM, F_(syslog), System::command_syslog_level, FL_(changeloglevel_cmd), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, 0, F_(syslog), System::command_syslog_level, FL_(changeloglevel_cmd), 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
@@ -728,13 +728,13 @@ void System::system_check() {
// commands - takes static function pointers // commands - takes static function pointers
void System::commands_init() { void System::commands_init() {
Command::add(EMSdevice::DeviceType::SYSTEM, F_(send), System::command_send, FL_(send_cmd), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, 0, 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, 0, 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); Command::add(EMSdevice::DeviceType::SYSTEM, 0, F_(restart), System::command_restart, FL_(restart_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(watch), System::command_watch, FL_(watch_cmd)); Command::add(EMSdevice::DeviceType::SYSTEM, 0, F_(watch), System::command_watch, FL_(watch_cmd));
if (Mqtt::enabled()) { if (Mqtt::enabled()) {
Command::add(EMSdevice::DeviceType::SYSTEM, F_(publish), System::command_publish, FL_(publish_cmd)); Command::add(EMSdevice::DeviceType::SYSTEM, 0, F_(publish), System::command_publish, FL_(publish_cmd));
} }
// these commands will return data in JSON format // these commands will return data in JSON format
@@ -742,7 +742,7 @@ void System::commands_init() {
Command::add(EMSdevice::DeviceType::SYSTEM, F_(commands), System::command_commands, FL_(commands_cmd)); 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, FL_(test_cmd)); Command::add(EMSdevice::DeviceType::SYSTEM, 0, ("test"), System::command_test, FL_(test_cmd));
#endif #endif
// MQTT subscribe "ems-esp/system/#" // MQTT subscribe "ems-esp/system/#"

View File

@@ -1,4 +1,4 @@
#define EMSESP_APP_VERSION "3.5.0b12" #define EMSESP_APP_VERSION "3.5.0b13"
#if CONFIG_IDF_TARGET_ESP32C3 #if CONFIG_IDF_TARGET_ESP32C3
#define EMSESP_PLATFORM "ESP32-C3"; #define EMSESP_PLATFORM "ESP32-C3";