Skip to content

Commit

Permalink
add device_id to commands, fix #826
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Dec 22, 2022
1 parent cfa486d commit 9cbb810
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_LATEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
- Add commands for analog sensors outputs
- 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)]
- Zone module MZ100 [#826](https://github.com/emsesp/EMS-ESP32/issues/826)

## Fixed

- 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

Expand Down
2 changes: 2 additions & 0 deletions src/analogsensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void AnalogSensor::start() {
CommandFlag::HIDDEN); // this command is hidden
Command::add(
EMSdevice::DeviceType::ANALOGSENSOR,
0,
F_(setvalue),
[&](const char * value, const int8_t id) { return command_setvalue(value, id); },
FL_(setiovalue_cmd),
Expand Down Expand Up @@ -120,6 +121,7 @@ void AnalogSensor::reload() {
if (sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT) {
Command::add(
EMSdevice::DeviceType::ANALOGSENSOR,
0,
sensor.name.c_str(),
[&](const char * value, const int8_t id) { return command_setvalue(value, sensor.gpio); },
sensor.type == AnalogType::COUNTER ? FL_(counter)
Expand Down
25 changes: 16 additions & 9 deletions src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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
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
// except for system commands as this is a special device without any queryable entities (device values)
Expand Down Expand Up @@ -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);
} else {
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 {
LOG_DEBUG(("%sCalling command %s with value %s"), ro.c_str(), info_s, value);
}
Expand Down Expand Up @@ -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
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 (find_command(device_type, cmd) != nullptr) {
if (find_command(device_type, device_id, cmd) != nullptr) {
return;
}

Expand All @@ -332,28 +339,28 @@ void Command::add(const uint8_t device_type, const char * cmd, const cmd_functio
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
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 (find_command(device_type, cmd) != nullptr) {
if (find_command(device_type, 0, cmd) != nullptr) {
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
// 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())) {
return nullptr;
}

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;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,22 @@ class Command {
public:
struct CmdFunction {
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_;
cmd_function_p cmdfunction_;
cmd_json_function_p cmdfunction_json_;
const char * const * description_;

CmdFunction(const uint8_t device_type,
const uint8_t device_id,
const uint8_t flags,
const char * cmd,
const cmd_function_p cmdfunction,
const cmd_json_function_p cmdfunction_json,
const char * const * description)
: device_type_(device_type)
, device_id_(device_id)
, flags_(flags)
, cmd_(cmd)
, cmdfunction_(cmdfunction)
Expand Down Expand Up @@ -98,6 +101,7 @@ class Command {

// with normal call back function taking a value and id
static void add(const uint8_t device_type,
const uint8_t device_id,
const char * cmd,
const cmd_function_p cb,
const char * const * description,
Expand All @@ -111,7 +115,7 @@ class Command {
uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT);

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 show(uuid::console::Shell & shell, uint8_t device_type, bool verbose);
Expand Down
2 changes: 1 addition & 1 deletion src/device_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
{159, DeviceType::MIXER, "MM50", DeviceFlags::EMS_DEVICE_FLAG_MMPLUS},
{160, DeviceType::MIXER, "MM100", 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

// Heat Pumps - 0x38?
Expand Down
13 changes: 12 additions & 1 deletion src/emsdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ bool EMSdevice::has_tag(const uint8_t tag) const {
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
// called from the command 'entities'
void EMSdevice::list_device_entries(JsonObject & output) const {
Expand Down Expand Up @@ -507,7 +518,7 @@ void EMSdevice::add_device_value(uint8_t tag,
}

// 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
Expand Down
1 change: 1 addition & 0 deletions src/emsdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class EMSdevice {
static std::string tag_to_mqtt(uint8_t tag);

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 {
return device_id_;
Expand Down
14 changes: 7 additions & 7 deletions src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void System::syslog_init() {
syslog_.hostname(hostname().c_str());

// 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) {
// in case service is still running, this flushes the queue
Expand Down Expand Up @@ -728,21 +728,21 @@ void System::system_check() {

// commands - takes static function pointers
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, 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, F_(watch), System::command_watch, FL_(watch_cmd));
Command::add(EMSdevice::DeviceType::SYSTEM, 0, F_(send), System::command_send, FL_(send_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, 0, F_(restart), System::command_restart, FL_(restart_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, 0, F_(watch), System::command_watch, FL_(watch_cmd));

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
Command::add(EMSdevice::DeviceType::SYSTEM, F_(info), System::command_info, FL_(system_info_cmd));
Command::add(EMSdevice::DeviceType::SYSTEM, F_(commands), System::command_commands, FL_(commands_cmd));

#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

// MQTT subscribe "ems-esp/system/#"
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define EMSESP_APP_VERSION "3.5.0b12"
#define EMSESP_APP_VERSION "3.5.0b13"

#if CONFIG_IDF_TARGET_ESP32C3
#define EMSESP_PLATFORM "ESP32-C3";
Expand Down

0 comments on commit 9cbb810

Please sign in to comment.