mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-09-15 11:05:08 +00:00
Merge branch 'mega' into feature/MQTT_TLS
This commit is contained in:
+51
-29
@@ -84,8 +84,9 @@ bool CPlugin_005(CPlugin::Function function, struct EventStruct *event, String&
|
||||
}
|
||||
|
||||
|
||||
String pubname = CPlugin_005_pubname;
|
||||
bool mqtt_retainFlag = CPlugin_005_mqtt_retainFlag;
|
||||
String pubname = CPlugin_005_pubname;
|
||||
const bool contains_valname = pubname.indexOf(F("%valname%")) != -1;
|
||||
bool mqtt_retainFlag = CPlugin_005_mqtt_retainFlag;
|
||||
|
||||
parseControllerVariables(pubname, event, false);
|
||||
|
||||
@@ -94,37 +95,47 @@ bool CPlugin_005(CPlugin::Function function, struct EventStruct *event, String&
|
||||
for (uint8_t x = 0; x < valueCount; x++)
|
||||
{
|
||||
// MFD: skip publishing for values with empty labels (removes unnecessary publishing of unwanted values)
|
||||
if (getTaskValueName(event->TaskIndex, x).isEmpty()) {
|
||||
if (Cache.getTaskDeviceValueName(event->TaskIndex, x).isEmpty()) {
|
||||
continue; // we skip values with empty labels
|
||||
}
|
||||
|
||||
String tmppubname = pubname;
|
||||
parseSingleControllerVariable(tmppubname, event, x, false);
|
||||
|
||||
if (contains_valname) {
|
||||
parseSingleControllerVariable(tmppubname, event, x, false);
|
||||
}
|
||||
String value;
|
||||
|
||||
if (event->sensorType == Sensor_VType::SENSOR_TYPE_STRING) {
|
||||
value = event->String2.substring(0, 20); // For the log
|
||||
# ifndef BUILD_NO_DEBUG
|
||||
if (loglevelActiveFor(LOG_LEVEL_DEBUG)) {
|
||||
value = event->String2.substring(0, 20); // For the log
|
||||
}
|
||||
# endif
|
||||
} else {
|
||||
value = formatUserVarNoCheck(event, x);
|
||||
}
|
||||
# ifndef BUILD_NO_DEBUG
|
||||
|
||||
if (loglevelActiveFor(LOG_LEVEL_DEBUG)) {
|
||||
addLogMove(LOG_LEVEL_DEBUG,
|
||||
strformat(
|
||||
F("MQTT : %s %s"),
|
||||
tmppubname.c_str(),
|
||||
value.c_str()));
|
||||
addLogMove(LOG_LEVEL_DEBUG,
|
||||
strformat(
|
||||
F("MQTT : %s %s"),
|
||||
tmppubname.c_str(),
|
||||
value.c_str()));
|
||||
}
|
||||
# endif // ifndef BUILD_NO_DEBUG
|
||||
|
||||
// Small optimization so we don't try to copy potentially large strings
|
||||
if (event->sensorType == Sensor_VType::SENSOR_TYPE_STRING) {
|
||||
if (MQTTpublish(event->ControllerIndex, event->TaskIndex, tmppubname.c_str(), event->String2.c_str(), mqtt_retainFlag))
|
||||
if (MQTTpublish(event->ControllerIndex, event->TaskIndex, tmppubname.c_str(), event->String2.c_str(), mqtt_retainFlag)) {
|
||||
success = true;
|
||||
}
|
||||
} else {
|
||||
// Publish using move operator, thus tmppubname and value are empty after this call
|
||||
if (MQTTpublish(event->ControllerIndex, event->TaskIndex, std::move(tmppubname), std::move(value), mqtt_retainFlag))
|
||||
if (MQTTpublish(event->ControllerIndex, event->TaskIndex, std::move(tmppubname), std::move(value), mqtt_retainFlag)) {
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -150,10 +161,10 @@ bool C005_parse_command(struct EventStruct *event) {
|
||||
// Topic : event->String1
|
||||
// Message: event->String2
|
||||
String cmd;
|
||||
bool validTopic = false;
|
||||
const int lastindex = event->String1.lastIndexOf('/');
|
||||
const String lastPartTopic = event->String1.substring(lastindex + 1);
|
||||
const bool has_cmd_arg_index = event->String1.lastIndexOf(F("cmd_arg")) != -1;
|
||||
bool validTopic = false;
|
||||
const int lastindex = event->String1.lastIndexOf('/');
|
||||
const String lastPartTopic = event->String1.substring(lastindex + 1);
|
||||
const bool has_cmd_arg_index = event->String1.lastIndexOf(F("cmd_arg")) != -1;
|
||||
|
||||
if (equals(lastPartTopic, F("cmd"))) {
|
||||
// Example:
|
||||
@@ -171,26 +182,30 @@ bool C005_parse_command(struct EventStruct *event) {
|
||||
// Message: 14
|
||||
// Full command: gpio,14,0
|
||||
|
||||
uint8_t topic_index = 1;
|
||||
String topic_folder = parseStringKeepCase(event->String1, topic_index, '/');
|
||||
uint8_t topic_index = 1;
|
||||
String topic_folder = parseStringKeepCase(event->String1, topic_index, '/');
|
||||
|
||||
while(!topic_folder.startsWith(F("cmd_arg")) && !topic_folder.isEmpty()) {
|
||||
while (!topic_folder.startsWith(F("cmd_arg")) && !topic_folder.isEmpty()) {
|
||||
++topic_index;
|
||||
topic_folder = parseStringKeepCase(event->String1, topic_index, '/');
|
||||
}
|
||||
|
||||
if (!topic_folder.isEmpty()) {
|
||||
int32_t cmd_arg_nr = -1;
|
||||
|
||||
if (validIntFromString(topic_folder.substring(7), cmd_arg_nr)) {
|
||||
int constructed_cmd_arg_nr = 0;
|
||||
++topic_index;
|
||||
topic_folder = parseStringKeepCase(event->String1, topic_index, '/');
|
||||
bool msg_added = false;
|
||||
while(!topic_folder.isEmpty()) {
|
||||
|
||||
while (!topic_folder.isEmpty()) {
|
||||
if (constructed_cmd_arg_nr != 0) {
|
||||
cmd += ',';
|
||||
}
|
||||
|
||||
if (constructed_cmd_arg_nr == cmd_arg_nr) {
|
||||
cmd += event->String2;
|
||||
cmd += event->String2;
|
||||
msg_added = true;
|
||||
} else {
|
||||
cmd += topic_folder;
|
||||
@@ -199,11 +214,13 @@ bool C005_parse_command(struct EventStruct *event) {
|
||||
}
|
||||
++constructed_cmd_arg_nr;
|
||||
}
|
||||
|
||||
if (!msg_added) {
|
||||
cmd += ',';
|
||||
cmd += event->String2;
|
||||
}
|
||||
//addLog(LOG_LEVEL_INFO, String(F("MQTT cmd: ")) + cmd);
|
||||
|
||||
// addLog(LOG_LEVEL_INFO, concat(F("MQTT cmd: "), cmd));
|
||||
|
||||
validTopic = true;
|
||||
}
|
||||
@@ -216,7 +233,7 @@ bool C005_parse_command(struct EventStruct *event) {
|
||||
if (lastindex > 0) {
|
||||
// Topic has at least one separator
|
||||
int32_t lastPartTopic_int;
|
||||
float value_f;
|
||||
float value_f;
|
||||
|
||||
if (validFloatFromString(event->String2, value_f) &&
|
||||
validIntFromString(lastPartTopic, lastPartTopic_int)) {
|
||||
@@ -226,8 +243,8 @@ bool C005_parse_command(struct EventStruct *event) {
|
||||
F("%s,%d,%s"),
|
||||
event->String1.substring(prevLastindex + 1, lastindex).c_str(),
|
||||
lastPartTopic_int,
|
||||
event->String2.c_str() // Just use the original format
|
||||
);
|
||||
event->String2.c_str() // Just use the original format
|
||||
);
|
||||
validTopic = true;
|
||||
}
|
||||
}
|
||||
@@ -253,19 +270,22 @@ bool C005_parse_command(struct EventStruct *event) {
|
||||
// Example: "myEvent,1,2,3", which needs to be converted to "myEvent=1,2,3"
|
||||
// N.B. This may contain the first eventvalue too
|
||||
// e.g. "myEvent=1,2,3" => "myEvent=1"
|
||||
String eventName = parseStringKeepCase(cmd, 1);
|
||||
String eventValues = parseStringToEndKeepCase(cmd, 2);
|
||||
String eventName = parseStringKeepCase(cmd, 1);
|
||||
String eventValues = parseStringToEndKeepCase(cmd, 2);
|
||||
const int equal_pos = eventName.indexOf('=');
|
||||
|
||||
if (equal_pos != -1) {
|
||||
// We found an '=' character, so the actual event name is everything before that char.
|
||||
eventName = cmd.substring(0, equal_pos);
|
||||
eventName = cmd.substring(0, equal_pos);
|
||||
eventValues = cmd.substring(equal_pos + 1); // Rest of the event, after the '=' char
|
||||
}
|
||||
|
||||
if (eventValues.startsWith(F(","))) {
|
||||
// Need to reconstruct the event to get rid of calls like these:
|
||||
// myevent=,1,2
|
||||
eventValues = eventValues.substring(1);
|
||||
}
|
||||
|
||||
// Now reconstruct the complete event
|
||||
// Without event values: "myEvent" (no '=' char)
|
||||
// With event values: "myEvent=1,2,3"
|
||||
@@ -273,18 +293,20 @@ bool C005_parse_command(struct EventStruct *event) {
|
||||
// Re-using the 'cmd' String as that has pre-allocated memory which is
|
||||
// known to be large enough to hold the entire event.
|
||||
cmd = eventName;
|
||||
|
||||
if (eventValues.length() > 0) {
|
||||
// Only append an = if there are eventvalues.
|
||||
cmd += '=';
|
||||
cmd += eventValues;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for duplicates, as sometimes a node may have multiple subscriptions to the same topic.
|
||||
// Then it may add several of the same events in a burst.
|
||||
eventQueue.addMove(std::move(cmd), true);
|
||||
}
|
||||
} else {
|
||||
ExecuteCommand_all(EventValueSource::Enum::VALUE_SOURCE_MQTT, cmd.c_str());
|
||||
ExecuteCommand_all({ EventValueSource::Enum::VALUE_SOURCE_MQTT, std::move(cmd) }, true);
|
||||
}
|
||||
}
|
||||
return validTopic;
|
||||
|
||||
Reference in New Issue
Block a user