Prep virtual mqtt_data

This commit is contained in:
Theo Arends 2021-05-23 14:42:27 +02:00
parent 38c958da75
commit 408ae6a4ef
9 changed files with 27 additions and 18 deletions

View File

@ -1169,10 +1169,18 @@ char* ResponseGetTime(uint32_t format, char* time_str)
return time_str; return time_str;
} }
uint32_t ResponseLength(void) {
return strlen(TasmotaGlobal.mqtt_data);
}
void ResponseClear(void) { void ResponseClear(void) {
TasmotaGlobal.mqtt_data[0] = '\0'; TasmotaGlobal.mqtt_data[0] = '\0';
} }
void ResponseJsonStart(void) {
TasmotaGlobal.mqtt_data[0] = '{';
}
int Response_P(const char* format, ...) // Content send snprintf_P char data int Response_P(const char* format, ...) // Content send snprintf_P char data
{ {
// This uses char strings. Be aware of sending %% if % is needed // 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); 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); int len = ext_vsnprintf_P(TasmotaGlobal.mqtt_data + mlen, sizeof(TasmotaGlobal.mqtt_data) - mlen, format, args);
va_end(args); va_end(args);
return len + mlen; 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 // This uses char strings. Be aware of sending %% if % is needed
va_list args; va_list args;
va_start(args, format); 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); int len = ext_vsnprintf_P(TasmotaGlobal.mqtt_data + mlen, sizeof(TasmotaGlobal.mqtt_data) - mlen, format, args);
va_end(args); va_end(args);
return len + mlen; return len + mlen;

View File

@ -304,7 +304,7 @@ void CommandHandler(char* topicBuf, char* dataBuf, uint32_t data_len)
type = (char*)stemp1; type = (char*)stemp1;
} }
if (TasmotaGlobal.mqtt_data[0] != '\0') { if (ResponseLength()) {
/* /*
// Add "Command":"POWERONSTATE", like: // Add "Command":"POWERONSTATE", like:
// 12:15:37 MQT: stat/wemos4/RESULT = {"Command":"POWERONSTATE","PowerOnState":3} // 12:15:37 MQT: stat/wemos4/RESULT = {"Command":"POWERONSTATE","PowerOnState":3}

View File

@ -533,10 +533,10 @@ bool SendKey(uint32_t key, uint32_t device, uint32_t state)
(POWER_TOGGLE == state)) { (POWER_TOGGLE == state)) {
state = ~(TasmotaGlobal.power >> (device -1)) &1; // POWER_OFF or POWER_ON 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 #ifdef USE_DOMOTICZ
if (!(DomoticzSendKey(key, device, state, strlen(TasmotaGlobal.mqtt_data)))) { if (!(DomoticzSendKey(key, device, state, ResponseLength()))) {
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
MqttPublish(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
@ -823,7 +823,7 @@ bool MqttShowSensor(void)
{ {
ResponseAppendTime(); ResponseAppendTime();
int json_data_start = strlen(TasmotaGlobal.mqtt_data); int json_data_start = ResponseLength();
for (uint32_t i = 0; i < MAX_SWITCHES; i++) { for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
#ifdef USE_TM1638 #ifdef USE_TM1638
if (PinUsed(GPIO_SWT1, i) || (PinUsed(GPIO_TM1638CLK) && PinUsed(GPIO_TM1638DIO) && PinUsed(GPIO_TM1638STB))) { 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); XsnsCall(FUNC_JSON_APPEND);
XdrvCall(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) { if (strstr_P(TasmotaGlobal.mqtt_data, PSTR(D_JSON_PRESSURE)) != nullptr) {
ResponseAppend_P(PSTR(",\"" D_JSON_PRESSURE_UNIT "\":\"%s\""), PressureUnit().c_str()); ResponseAppend_P(PSTR(",\"" D_JSON_PRESSURE_UNIT "\":\"%s\""), PressureUnit().c_str());
} }

View File

@ -605,10 +605,11 @@ void MqttPublishLoggingAsync(bool refresh) {
char* line; char* line;
size_t len; size_t len;
while (GetLog(Settings.mqttlog_level, &index, &line, &len)) { while (GetLog(Settings.mqttlog_level, &index, &line, &len)) {
strlcpy(TasmotaGlobal.mqtt_data, line, len); // No JSON and ugly!!
char stopic[TOPSZ]; char stopic[TOPSZ];
GetTopic_P(stopic, STAT, TasmotaGlobal.mqtt_topic, PSTR("LOGGING")); 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);
} }
} }

View File

@ -985,7 +985,7 @@ void RulesEvery100ms(void) {
XsnsNextCall(FUNC_JSON_APPEND, xsns_index); // ,"INA219":{"Voltage":4.494,"Current":0.020,"Power":0.089} XsnsNextCall(FUNC_JSON_APPEND, xsns_index); // ,"INA219":{"Voltage":4.494,"Current":0.020,"Power":0.089}
TasmotaGlobal.tele_period = tele_period_save; TasmotaGlobal.tele_period = tele_period_save;
if (strlen(TasmotaGlobal.mqtt_data)) { 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(); ResponseJsonEnd();
RulesProcessEvent(TasmotaGlobal.mqtt_data); RulesProcessEvent(TasmotaGlobal.mqtt_data);
} }

View File

@ -4979,9 +4979,9 @@ void ScripterEvery100ms(void) {
TasmotaGlobal.tele_period = 2; TasmotaGlobal.tele_period = 2;
XsnsNextCall(FUNC_JSON_APPEND, xsns_index); XsnsNextCall(FUNC_JSON_APPEND, xsns_index);
TasmotaGlobal.tele_period = script_tele_period_save; TasmotaGlobal.tele_period = script_tele_period_save;
if (strlen(TasmotaGlobal.mqtt_data)) { if (ResponseLength()) {
TasmotaGlobal.mqtt_data[0] = '{'; ResponseJsonStart();
snprintf_P(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data), PSTR("%s}"), TasmotaGlobal.mqtt_data); ResponseJsonEnd();
Run_Scripter(">T", 2, TasmotaGlobal.mqtt_data); Run_Scripter(">T", 2, TasmotaGlobal.mqtt_data);
} }
} }

View File

@ -1312,9 +1312,9 @@ void get_dt_mqtt(void) {
TasmotaGlobal.tele_period = 2; TasmotaGlobal.tele_period = 2;
XsnsNextCall(FUNC_JSON_APPEND, xsns_index); XsnsNextCall(FUNC_JSON_APPEND, xsns_index);
TasmotaGlobal.tele_period = script_tele_period_save; TasmotaGlobal.tele_period = script_tele_period_save;
if (strlen(TasmotaGlobal.mqtt_data)) { if (ResponseLength()) {
TasmotaGlobal.mqtt_data[0] = '{'; ResponseJsonStart();
snprintf_P(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data), PSTR("%s}"), TasmotaGlobal.mqtt_data); ResponseJsonEnd();
} }
get_dt_vars(TasmotaGlobal.mqtt_data); get_dt_vars(TasmotaGlobal.mqtt_data);
} }

View File

@ -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 { 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? 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`? // Do we prefix with `ZbReceived`?
if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zb_received_as_subtopic) { if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zb_received_as_subtopic) {
Response_P(PSTR("{\"%s\":"), json_prefix); Response_P(PSTR("{\"%s\":"), json_prefix);

View File

@ -511,7 +511,7 @@ void sns_opentherm_flags_cmd(void)
sns_opentherm_init_boiler_status(); sns_opentherm_init_boiler_status();
} }
bool addComma = false; bool addComma = false;
TasmotaGlobal.mqtt_data[0] = 0; ResponseClear();
for (int pos = 0; pos < OT_FLAGS_COUNT; ++pos) for (int pos = 0; pos < OT_FLAGS_COUNT; ++pos)
{ {
int mask = 1 << pos; int mask = 1 << pos;