mirror of https://github.com/arendst/Tasmota.git
Refactor MQTT Logging and publish
This commit is contained in:
parent
442231f57c
commit
5716d08de9
|
@ -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
|
||||
}
|
||||
#endif // USE_WEBSERVER
|
||||
if (!global_state.mqtt_down && (loglevel <= Settings.mqttlog_level)) { MqttPublishLogging(mxtime); }
|
||||
if (!global_state.wifi_down && (loglevel <= syslog_level)) { Syslog(); }
|
||||
if (Settings.flag.mqtt_enabled && // SetOption3 - Enable MQTT
|
||||
!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)
|
||||
|
|
|
@ -438,9 +438,9 @@ bool SendKey(uint32_t key, uint32_t device, uint32_t state)
|
|||
#ifdef USE_DOMOTICZ
|
||||
if (!(DomoticzSendKey(key, device, state, strlen(mqtt_data)))) {
|
||||
#endif // USE_DOMOTICZ
|
||||
MqttPublishDirect(stopic, ((key) ? Settings.flag.mqtt_switch_retain // CMND_SWITCHRETAIN
|
||||
: Settings.flag.mqtt_button_retain) && // CMND_BUTTONRETAIN
|
||||
(state != POWER_HOLD || !Settings.flag3.no_hold_retain)); // SetOption62 - Don't use retain flag on HOLD messages
|
||||
MqttPublish(stopic, ((key) ? Settings.flag.mqtt_switch_retain // CMND_SWITCHRETAIN
|
||||
: Settings.flag.mqtt_button_retain) && // CMND_BUTTONRETAIN
|
||||
(state != POWER_HOLD || !Settings.flag3.no_hold_retain)); // SetOption62 - Don't use retain flag on HOLD messages
|
||||
#ifdef USE_DOMOTICZ
|
||||
}
|
||||
#endif // USE_DOMOTICZ
|
||||
|
|
|
@ -221,6 +221,17 @@ void MqttUnsubscribeLib(const char *topic)
|
|||
|
||||
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);
|
||||
yield(); // #3313
|
||||
return result;
|
||||
|
@ -291,43 +302,37 @@ void MqttUnsubscribe(const char *topic)
|
|||
|
||||
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];
|
||||
memcpy(saved_mqtt_data, mqtt_data, sizeof(saved_mqtt_data));
|
||||
// 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!!
|
||||
// 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];
|
||||
snprintf_P(romram, sizeof(romram), PSTR("LOGGING"));
|
||||
GetTopic_P(stopic, STAT, mqtt_topic, romram);
|
||||
char stopic[TOPSZ];
|
||||
GetTopic_P(stopic, STAT, mqtt_topic, PSTR("LOGGING"));
|
||||
MqttPublishLib(stopic, false);
|
||||
|
||||
char *me;
|
||||
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));
|
||||
}
|
||||
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
|
||||
ShowFreeMem(PSTR("MqttPublishDirect"));
|
||||
ShowFreeMem(PSTR("MqttPublish"));
|
||||
#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';
|
||||
char slog_type[20];
|
||||
snprintf_P(slog_type, sizeof(slog_type), PSTR(D_LOG_RESULT));
|
||||
|
||||
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)
|
||||
{
|
||||
MqttPublish(topic, false);
|
||||
|
@ -860,7 +846,7 @@ void CmndPublish(void)
|
|||
} else {
|
||||
mqtt_data[0] = '\0';
|
||||
}
|
||||
MqttPublishDirect(stemp1, (XdrvMailbox.index == 2));
|
||||
MqttPublish(stemp1, (XdrvMailbox.index == 2));
|
||||
// ResponseCmndDone();
|
||||
mqtt_data[0] = '\0';
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue