Removed "retain" from MQTT messages.

This commit is contained in:
Blaz Kristan 2021-05-14 20:57:56 +02:00
parent 9a5917a331
commit f6a5bc9b40
4 changed files with 25 additions and 26 deletions

View File

@ -139,7 +139,7 @@ private:
char subuf[64];
strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/motion"));
mqtt->publish(subuf, 0, true, state);
mqtt->publish(subuf, 0, false, state);
}
}

View File

@ -162,9 +162,9 @@ class UsermodTemperature : public Usermod {
// the DallasTemperature library returns -127C or -196.6F when problem
// reading the sensor
strcat_P(subuf, PSTR("/temperature"));
mqtt->publish(subuf, 0, true, String(temperature).c_str());
mqtt->publish(subuf, 0, false, String(temperature).c_str());
strcat_P(subuf, PSTR("_f"));
mqtt->publish(subuf, 0, true, String((float)temperature * 1.8f + 32).c_str());
mqtt->publish(subuf, 0, false, String((float)temperature * 1.8f + 32).c_str());
} else {
// publish something else to indicate status?
}

View File

@ -56,7 +56,7 @@ class MultiRelay : public Usermod {
if (WLED_MQTT_CONNECTED){
char subuf[64];
sprintf_P(subuf, PSTR("%s/relay/%d"), mqttDeviceTopic, relay);
mqtt->publish(subuf, 0, true, state);
mqtt->publish(subuf, 0, false, state);
}
}
@ -88,7 +88,7 @@ class MultiRelay : public Usermod {
DEBUG_PRINTLN("Relays: HTML API");
String janswer;
String error = "";
int params = request->params();
//int params = request->params();
janswer = F("{\"NoOfRelays\":");
janswer += String(MULTI_RELAY_MAX_RELAYS) + ",";

View File

@ -52,24 +52,23 @@ void onMqttConnect(bool sessionPresent)
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
void onMqttMessage(char* topic, char* payload0, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
DEBUG_PRINT(F("MQTT msg: "));
DEBUG_PRINTLN(topic);
// paranoia check to avoid npe if no payload
if (payload==nullptr) {
if (payload0==nullptr) {
DEBUG_PRINTLN(F("no payload -> leave"));
return;
}
DEBUG_PRINTLN(payload);
// payload is not always null terminated
char *payload0 = new char[len+1];
if (payload0==nullptr) return; // out of memory
strncpy(payload0,payload,len);
payload0[len] = '\0';
DEBUG_PRINTLN(payload0);
char *payload = new char[len+1];
if (payload==nullptr) return; // out of memory
strncpy(payload,payload0,len);
payload[len] = '\0';
DEBUG_PRINTLN(payload);
size_t topicPrefixLen = strlen(mqttDeviceTopic);
if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) {
@ -80,8 +79,8 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
topic += topicPrefixLen;
} else {
// Non-Wled Topic used here. Probably a usermod subscribed to this topic.
usermods.onMqttMessage(topic, payload0);
delete[] payload0;
usermods.onMqttMessage(topic, payload);
delete[] payload;
return;
}
}
@ -89,26 +88,26 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
//Prefix is stripped from the topic at this point
if (strcmp_P(topic, PSTR("/col")) == 0) {
colorFromDecOrHexString(col, payload0);
colorFromDecOrHexString(col, payload);
colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE);
} else if (strcmp_P(topic, PSTR("/api")) == 0) {
if (payload0[0] == '{') { //JSON API
if (payload[0] == '{') { //JSON API
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
deserializeJson(doc, payload0);
deserializeJson(doc, payload);
deserializeState(doc.as<JsonObject>());
} else { //HTTP API
String apireq = "win&";
apireq += payload0;
apireq += payload;
handleSet(nullptr, apireq);
}
} else if (strlen(topic) != 0) {
// non standard topic, check with usermods
usermods.onMqttMessage(topic, payload0);
usermods.onMqttMessage(topic, payload);
} else {
// topmost topic (just wled/MAC)
parseMQTTBriPayload(payload0);
parseMQTTBriPayload(payload);
}
delete[] payload0;
delete[] payload;
}
@ -124,22 +123,22 @@ void publishMqtt()
sprintf_P(s, PSTR("%u"), bri);
strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/g"));
mqtt->publish(subuf, 0, true, s);
mqtt->publish(subuf, 0, false, s); // do not retain message
sprintf_P(s, PSTR("#%06X"), (col[3] << 24) | (col[0] << 16) | (col[1] << 8) | (col[2]));
strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/c"));
mqtt->publish(subuf, 0, true, s);
mqtt->publish(subuf, 0, false, s); // do not retain message
strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/status"));
mqtt->publish(subuf, 0, true, "online");
mqtt->publish(subuf, 0, false, "online"); // do not retain message
char apires[1024];
XML_response(nullptr, apires);
strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/v"));
mqtt->publish(subuf, 0, true, apires);
mqtt->publish(subuf, 0, false, apires); // do not retain message
}