Refactor MQTT Logging and publish

This commit is contained in:
Theo Arends 2020-01-18 15:34:01 +01:00
parent 442231f57c
commit 5716d08de9
3 changed files with 41 additions and 51 deletions

View File

@ -1631,8 +1631,12 @@ void AddLog(uint32_t loglevel)
if (!web_log_index) web_log_index++; // Index 0 is not allowed as it is the end of char string if (!web_log_index) web_log_index++; // Index 0 is not allowed as it is the end of char string
} }
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
if (!global_state.mqtt_down && (loglevel <= Settings.mqttlog_level)) { MqttPublishLogging(mxtime); } if (Settings.flag.mqtt_enabled && // SetOption3 - Enable MQTT
if (!global_state.wifi_down && (loglevel <= syslog_level)) { Syslog(); } !global_state.mqtt_down &&
(loglevel <= Settings.mqttlog_level)) { MqttPublishLogging(mxtime); }
if (!global_state.wifi_down &&
(loglevel <= syslog_level)) { Syslog(); }
} }
void AddLog_P(uint32_t loglevel, const char *formatP) void AddLog_P(uint32_t loglevel, const char *formatP)

View File

@ -438,9 +438,9 @@ bool SendKey(uint32_t key, uint32_t device, uint32_t state)
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if (!(DomoticzSendKey(key, device, state, strlen(mqtt_data)))) { if (!(DomoticzSendKey(key, device, state, strlen(mqtt_data)))) {
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
MqttPublishDirect(stopic, ((key) ? Settings.flag.mqtt_switch_retain // CMND_SWITCHRETAIN MqttPublish(stopic, ((key) ? Settings.flag.mqtt_switch_retain // CMND_SWITCHRETAIN
: Settings.flag.mqtt_button_retain) && // CMND_BUTTONRETAIN : Settings.flag.mqtt_button_retain) && // CMND_BUTTONRETAIN
(state != POWER_HOLD || !Settings.flag3.no_hold_retain)); // SetOption62 - Don't use retain flag on HOLD messages (state != POWER_HOLD || !Settings.flag3.no_hold_retain)); // SetOption62 - Don't use retain flag on HOLD messages
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
} }
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ

View File

@ -221,6 +221,17 @@ void MqttUnsubscribeLib(const char *topic)
bool MqttPublishLib(const char* topic, bool retained) bool MqttPublishLib(const char* topic, bool retained)
{ {
// If Prefix1 equals Prefix2 disable next MQTT subscription to prevent loop
if (!strcmp(SettingsText(SET_MQTTPREFIX1), SettingsText(SET_MQTTPREFIX2))) {
char *str = strstr(topic, SettingsText(SET_MQTTPREFIX1));
if (str == topic) {
if (0 == mqtt_cmnd_publish) {
mqtt_cmnd_publish += 3;
}
mqtt_cmnd_publish += 3;
}
}
bool result = MqttClient.publish(topic, mqtt_data, retained); bool result = MqttClient.publish(topic, mqtt_data, retained);
yield(); // #3313 yield(); // #3313
return result; return result;
@ -291,43 +302,37 @@ void MqttUnsubscribe(const char *topic)
void MqttPublishLogging(const char *mxtime) void MqttPublishLogging(const char *mxtime)
{ {
if (Settings.flag.mqtt_enabled) { // SetOption3 - Enable MQTT if (MqttIsConnected()) {
if (MqttIsConnected()) { char saved_mqtt_data[MESSZ];
memcpy(saved_mqtt_data, mqtt_data, sizeof(saved_mqtt_data));
char saved_mqtt_data[MESSZ]; // ResponseTime_P(PSTR(",\"Log\":{\"%s\"}}"), log_data); // Will fail as some messages contain JSON
memcpy(saved_mqtt_data, mqtt_data, sizeof(saved_mqtt_data)); Response_P(PSTR("%s%s"), mxtime, log_data); // No JSON and ugly!!
// ResponseTime_P(PSTR(",\"Log\":{\"%s\"}}"), log_data); // Will fail as some messages contain JSON
Response_P(PSTR("%s%s"), mxtime, log_data); // No JSON and ugly!!
char romram[33]; char stopic[TOPSZ];
char stopic[TOPSZ]; GetTopic_P(stopic, STAT, mqtt_topic, PSTR("LOGGING"));
snprintf_P(romram, sizeof(romram), PSTR("LOGGING")); MqttPublishLib(stopic, false);
GetTopic_P(stopic, STAT, mqtt_topic, romram);
char *me; memcpy(mqtt_data, saved_mqtt_data, sizeof(saved_mqtt_data));
if (!strcmp(SettingsText(SET_MQTTPREFIX1), SettingsText(SET_MQTTPREFIX2))) {
me = strstr(stopic, SettingsText(SET_MQTTPREFIX1));
if (me == stopic) {
mqtt_cmnd_publish += 3;
}
}
MqttPublishLib(stopic, false);
memcpy(mqtt_data, saved_mqtt_data, sizeof(saved_mqtt_data));
}
} }
} }
void MqttPublishDirect(const char* topic, bool retained) void MqttPublish(const char* topic, bool retained)
{ {
char sretained[CMDSZ];
char slog_type[20];
#ifdef USE_DEBUG_DRIVER #ifdef USE_DEBUG_DRIVER
ShowFreeMem(PSTR("MqttPublishDirect")); ShowFreeMem(PSTR("MqttPublish"));
#endif #endif
#if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT)
if (retained) {
// AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR("Retained are not supported by AWS IoT, using retained = false."));
retained = false; // AWS IoT does not support retained, it will disconnect if received
}
#endif
char sretained[CMDSZ];
sretained[0] = '\0'; sretained[0] = '\0';
char slog_type[20];
snprintf_P(slog_type, sizeof(slog_type), PSTR(D_LOG_RESULT)); snprintf_P(slog_type, sizeof(slog_type), PSTR(D_LOG_RESULT));
if (Settings.flag.mqtt_enabled) { // SetOption3 - Enable MQTT if (Settings.flag.mqtt_enabled) { // SetOption3 - Enable MQTT
@ -354,25 +359,6 @@ void MqttPublishDirect(const char* topic, bool retained)
} }
} }
void MqttPublish(const char* topic, bool retained)
{
char *me;
#if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT)
if (retained) {
AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR("Retained are not supported by AWS IoT, using retained = false."));
}
retained = false; // AWS IoT does not support retained, it will disconnect if received
#endif
if (!strcmp(SettingsText(SET_MQTTPREFIX1), SettingsText(SET_MQTTPREFIX2))) {
me = strstr(topic, SettingsText(SET_MQTTPREFIX1));
if (me == topic) {
mqtt_cmnd_publish += 3;
}
}
MqttPublishDirect(topic, retained);
}
void MqttPublish(const char* topic) void MqttPublish(const char* topic)
{ {
MqttPublish(topic, false); MqttPublish(topic, false);
@ -860,7 +846,7 @@ void CmndPublish(void)
} else { } else {
mqtt_data[0] = '\0'; mqtt_data[0] = '\0';
} }
MqttPublishDirect(stemp1, (XdrvMailbox.index == 2)); MqttPublish(stemp1, (XdrvMailbox.index == 2));
// ResponseCmndDone(); // ResponseCmndDone();
mqtt_data[0] = '\0'; mqtt_data[0] = '\0';
} }