diff --git a/tasmota/support.ino b/tasmota/support.ino index 25dfa833d..2165fff93 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1169,10 +1169,18 @@ char* ResponseGetTime(uint32_t format, char* time_str) return time_str; } +uint32_t ResponseLength(void) { + return strlen(TasmotaGlobal.mqtt_data); +} + void ResponseClear(void) { TasmotaGlobal.mqtt_data[0] = '\0'; } +void ResponseJsonStart(void) { + TasmotaGlobal.mqtt_data[0] = '{'; +} + int Response_P(const char* format, ...) // Content send snprintf_P char data { // This uses char strings. Be aware of sending %% if % is needed @@ -1191,7 +1199,7 @@ int ResponseTime_P(const char* format, ...) // Content send snprintf_P char d ResponseGetTime(Settings.flag2.time_format, TasmotaGlobal.mqtt_data); - int mlen = strlen(TasmotaGlobal.mqtt_data); + int mlen = ResponseLength(); int len = ext_vsnprintf_P(TasmotaGlobal.mqtt_data + mlen, sizeof(TasmotaGlobal.mqtt_data) - mlen, format, args); va_end(args); return len + mlen; @@ -1202,7 +1210,7 @@ int ResponseAppend_P(const char* format, ...) // Content send snprintf_P char d // This uses char strings. Be aware of sending %% if % is needed va_list args; va_start(args, format); - int mlen = strlen(TasmotaGlobal.mqtt_data); + int mlen = ResponseLength(); int len = ext_vsnprintf_P(TasmotaGlobal.mqtt_data + mlen, sizeof(TasmotaGlobal.mqtt_data) - mlen, format, args); va_end(args); return len + mlen; diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index d3983f403..2e1313021 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -304,7 +304,7 @@ void CommandHandler(char* topicBuf, char* dataBuf, uint32_t data_len) type = (char*)stemp1; } - if (TasmotaGlobal.mqtt_data[0] != '\0') { + if (ResponseLength()) { /* // Add "Command":"POWERONSTATE", like: // 12:15:37 MQT: stat/wemos4/RESULT = {"Command":"POWERONSTATE","PowerOnState":3} diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index b1e1e9da8..c16ad6f44 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -533,10 +533,10 @@ bool SendKey(uint32_t key, uint32_t device, uint32_t state) (POWER_TOGGLE == state)) { state = ~(TasmotaGlobal.power >> (device -1)) &1; // POWER_OFF or POWER_ON } - snprintf_P(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data), GetStateText(state)); + Response_P(GetStateText(state)); } #ifdef USE_DOMOTICZ - if (!(DomoticzSendKey(key, device, state, strlen(TasmotaGlobal.mqtt_data)))) { + if (!(DomoticzSendKey(key, device, state, ResponseLength()))) { #endif // USE_DOMOTICZ MqttPublish(stopic, ((key) ? Settings.flag.mqtt_switch_retain // CMND_SWITCHRETAIN : Settings.flag.mqtt_button_retain) && // CMND_BUTTONRETAIN @@ -823,7 +823,7 @@ bool MqttShowSensor(void) { ResponseAppendTime(); - int json_data_start = strlen(TasmotaGlobal.mqtt_data); + int json_data_start = ResponseLength(); for (uint32_t i = 0; i < MAX_SWITCHES; i++) { #ifdef USE_TM1638 if (PinUsed(GPIO_SWT1, i) || (PinUsed(GPIO_TM1638CLK) && PinUsed(GPIO_TM1638DIO) && PinUsed(GPIO_TM1638STB))) { @@ -836,7 +836,7 @@ bool MqttShowSensor(void) XsnsCall(FUNC_JSON_APPEND); XdrvCall(FUNC_JSON_APPEND); - bool json_data_available = (strlen(TasmotaGlobal.mqtt_data) - json_data_start); + bool json_data_available = (ResponseLength() - json_data_start); if (strstr_P(TasmotaGlobal.mqtt_data, PSTR(D_JSON_PRESSURE)) != nullptr) { ResponseAppend_P(PSTR(",\"" D_JSON_PRESSURE_UNIT "\":\"%s\""), PressureUnit().c_str()); } diff --git a/tasmota/xdrv_02_mqtt_9_impl.ino b/tasmota/xdrv_02_mqtt_9_impl.ino index 2e7364303..6eee64262 100644 --- a/tasmota/xdrv_02_mqtt_9_impl.ino +++ b/tasmota/xdrv_02_mqtt_9_impl.ino @@ -605,10 +605,11 @@ void MqttPublishLoggingAsync(bool refresh) { char* line; size_t len; while (GetLog(Settings.mqttlog_level, &index, &line, &len)) { - strlcpy(TasmotaGlobal.mqtt_data, line, len); // No JSON and ugly!! char stopic[TOPSZ]; GetTopic_P(stopic, STAT, TasmotaGlobal.mqtt_topic, PSTR("LOGGING")); - MqttPublishLib(stopic, (const uint8_t*)TasmotaGlobal.mqtt_data, strlen(TasmotaGlobal.mqtt_data), false); +// strlcpy(TasmotaGlobal.mqtt_data, line, len); // No JSON and ugly!! +// MqttPublishLib(stopic, (const uint8_t*)TasmotaGlobal.mqtt_data, strlen(TasmotaGlobal.mqtt_data), false); + MqttPublishLib(stopic, (const uint8_t*)line, len -1, false); } } diff --git a/tasmota/xdrv_10_rules.ino b/tasmota/xdrv_10_rules.ino index 5e74a99eb..df7016da8 100644 --- a/tasmota/xdrv_10_rules.ino +++ b/tasmota/xdrv_10_rules.ino @@ -985,7 +985,7 @@ void RulesEvery100ms(void) { XsnsNextCall(FUNC_JSON_APPEND, xsns_index); // ,"INA219":{"Voltage":4.494,"Current":0.020,"Power":0.089} TasmotaGlobal.tele_period = tele_period_save; if (strlen(TasmotaGlobal.mqtt_data)) { - TasmotaGlobal.mqtt_data[0] = '{'; // {"INA219":{"Voltage":4.494,"Current":0.020,"Power":0.089} + ResponseJsonStart(); // {"INA219":{"Voltage":4.494,"Current":0.020,"Power":0.089} ResponseJsonEnd(); RulesProcessEvent(TasmotaGlobal.mqtt_data); } diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index 8cfde19f1..00bf03e83 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -4979,9 +4979,9 @@ void ScripterEvery100ms(void) { TasmotaGlobal.tele_period = 2; XsnsNextCall(FUNC_JSON_APPEND, xsns_index); TasmotaGlobal.tele_period = script_tele_period_save; - if (strlen(TasmotaGlobal.mqtt_data)) { - TasmotaGlobal.mqtt_data[0] = '{'; - snprintf_P(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data), PSTR("%s}"), TasmotaGlobal.mqtt_data); + if (ResponseLength()) { + ResponseJsonStart(); + ResponseJsonEnd(); Run_Scripter(">T", 2, TasmotaGlobal.mqtt_data); } } diff --git a/tasmota/xdrv_13_display.ino b/tasmota/xdrv_13_display.ino index a9992cdf7..ee85c2f0d 100755 --- a/tasmota/xdrv_13_display.ino +++ b/tasmota/xdrv_13_display.ino @@ -1312,9 +1312,9 @@ void get_dt_mqtt(void) { TasmotaGlobal.tele_period = 2; XsnsNextCall(FUNC_JSON_APPEND, xsns_index); TasmotaGlobal.tele_period = script_tele_period_save; - if (strlen(TasmotaGlobal.mqtt_data)) { - TasmotaGlobal.mqtt_data[0] = '{'; - snprintf_P(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data), PSTR("%s}"), TasmotaGlobal.mqtt_data); + if (ResponseLength()) { + ResponseJsonStart(); + ResponseJsonEnd(); } get_dt_vars(TasmotaGlobal.mqtt_data); } diff --git a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino index 38ab5158b..f11ab47df 100644 --- a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino +++ b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino @@ -518,7 +518,7 @@ void Z_Devices::jsonAppend(uint16_t shortaddr, const Z_attribute_list &attr_list void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_list &attr_list) const { bool use_fname = (Settings.flag4.zigbee_use_names) && (friendlyName); // should we replace shortaddr with friendlyname? - TasmotaGlobal.mqtt_data[0] = 0; // clear string + ResponseClear(); // clear string // Do we prefix with `ZbReceived`? if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zb_received_as_subtopic) { Response_P(PSTR("{\"%s\":"), json_prefix); diff --git a/tasmota/xsns_69_opentherm.ino b/tasmota/xsns_69_opentherm.ino index 0d0cec710..9b425f3fa 100644 --- a/tasmota/xsns_69_opentherm.ino +++ b/tasmota/xsns_69_opentherm.ino @@ -511,7 +511,7 @@ void sns_opentherm_flags_cmd(void) sns_opentherm_init_boiler_status(); } bool addComma = false; - TasmotaGlobal.mqtt_data[0] = 0; + ResponseClear(); for (int pos = 0; pos < OT_FLAGS_COUNT; ++pos) { int mask = 1 << pos;