Fix DisplayMode MQTT (Un)Subscribe without restart

This commit is contained in:
Theo Arends 2024-06-09 13:46:54 +02:00
parent 8cfa6f28ea
commit ea5c9bc9b6

View File

@ -497,14 +497,12 @@ void DisplayText(void)
// use disp_xpos, disp_ypos // use disp_xpos, disp_ypos
DisplayDrawStringAt(disp_xpos, disp_ypos, linebuf, fg_color, 0); DisplayDrawStringAt(disp_xpos, disp_ypos, linebuf, fg_color, 0);
} }
#ifdef USE_DISPLAY_MODES1TO5
} else {
DisplayLogBufferAdd(linebuf);
}
#endif // USE_DISPLAY_MODES1TO5
memset(linebuf, ' ', sizeof(linebuf)); memset(linebuf, ' ', sizeof(linebuf));
linebuf[sizeof(linebuf)-1] = 0; linebuf[sizeof(linebuf)-1] = 0;
dp = linebuf; dp = linebuf;
#ifdef USE_DISPLAY_MODES1TO5
}
#endif // USE_DISPLAY_MODES1TO5
} }
} else { } else {
// copy chars // copy chars
@ -1236,7 +1234,9 @@ extern FS *ffsp;
alignright(linebuf); alignright(linebuf);
} }
#ifdef USE_DISPLAY_MODES1TO5 #ifdef USE_DISPLAY_MODES1TO5
if (!Settings->display_mode) { if (Settings->display_mode) {
DisplayLogBufferAdd(linebuf);
} else
#endif // USE_DISPLAY_MODES1TO5 #endif // USE_DISPLAY_MODES1TO5
if (col > 0 && lin > 0) { if (col > 0 && lin > 0) {
// use col and lin // use col and lin
@ -1245,11 +1245,6 @@ extern FS *ffsp;
// use disp_xpos, disp_ypos // use disp_xpos, disp_ypos
DisplayDrawStringAt(disp_xpos, disp_ypos, linebuf, fg_color, 0); DisplayDrawStringAt(disp_xpos, disp_ypos, linebuf, fg_color, 0);
} }
#ifdef USE_DISPLAY_MODES1TO5
} else {
DisplayLogBufferAdd(linebuf);
}
#endif // USE_DISPLAY_MODES1TO5
} }
// draw buffer // draw buffer
if (auto_draw&1) { if (auto_draw&1) {
@ -1597,6 +1592,8 @@ void DisplayAllocLogBuffer(void)
if (disp_log_buffer != nullptr) { if (disp_log_buffer != nullptr) {
disp_log_buffer_cols = Settings->display_cols[0] +1; disp_log_buffer_cols = Settings->display_cols[0] +1;
DisplayClearLogBuffer(); DisplayClearLogBuffer();
DisplayClearScreenBuffer();
DisplayClear();
} }
} }
} }
@ -1818,22 +1815,18 @@ void DisplayAnalyzeJson(char *topic, const char *json)
} }
} }
void DisplayMqttSubscribe(void) void DisplayMqttSubscribe(void) {
{
/* Subscribe to tele messages only /* Subscribe to tele messages only
* Supports the following FullTopic formats * Supports the following FullTopic formats
* - %prefix%/%topic% * - %prefix%/%topic%
* - home/%prefix%/%topic% * - home/%prefix%/%topic%
* - home/level2/%prefix%/%topic% etc. * - home/level2/%prefix%/%topic% etc.
*/ */
if (Settings->display_model && (Settings->display_mode &0x04)) {
char stopic[TOPSZ]; char stopic[TOPSZ];
char ntopic[TOPSZ];
ntopic[0] = '\0';
strlcpy(stopic, SettingsText(SET_MQTT_FULLTOPIC), sizeof(stopic)); strlcpy(stopic, SettingsText(SET_MQTT_FULLTOPIC), sizeof(stopic));
char *tp = strtok(stopic, "/"); char *tp = strtok(stopic, "/");
char ntopic[TOPSZ];
ntopic[0] = '\0';
while (tp != nullptr) { while (tp != nullptr) {
if (!strcmp_P(tp, MQTT_TOKEN_PREFIX)) { if (!strcmp_P(tp, MQTT_TOKEN_PREFIX)) {
break; break;
@ -1843,10 +1836,16 @@ void DisplayMqttSubscribe(void)
} }
strncat(ntopic, SettingsText(SET_MQTTPREFIX3), sizeof(ntopic) - strlen(ntopic) -1); // Subscribe to tele messages strncat(ntopic, SettingsText(SET_MQTTPREFIX3), sizeof(ntopic) - strlen(ntopic) -1); // Subscribe to tele messages
strncat_P(ntopic, PSTR("/#"), sizeof(ntopic) - strlen(ntopic) -1); // Add multi-level wildcard strncat_P(ntopic, PSTR("/#"), sizeof(ntopic) - strlen(ntopic) -1); // Add multi-level wildcard
MqttSubscribe(ntopic); if (Settings->display_model && (Settings->display_mode &0x04)) {
if (!disp_subscribed) {
disp_subscribed = true; disp_subscribed = true;
MqttSubscribe(ntopic);
}
} else { } else {
if (disp_subscribed) {
disp_subscribed = false; disp_subscribed = false;
MqttUnsubscribe(ntopic);
}
} }
} }
@ -2026,14 +2025,18 @@ void CmndDisplayMode(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 5)) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 5)) {
uint32_t last_display_mode = Settings->display_mode; uint32_t last_display_mode = Settings->display_mode;
Settings->display_mode = XdrvMailbox.payload; Settings->display_mode = XdrvMailbox.payload;
if (last_display_mode != Settings->display_mode) { // Switch to different mode
if (disp_subscribed != (Settings->display_mode &0x04)) { if ((!last_display_mode && Settings->display_mode) || // Switch to mode 1, 2, 3 or 4
TasmotaGlobal.restart_flag = 2; // Restart to Add/Remove MQTT subscribe (last_display_mode && !Settings->display_mode)) { // Switch to mode 0
} else { DisplayInit(DISPLAY_INIT_MODE);
if (Settings->display_mode) { // Switch to non mode 0 }
if (1 == Settings->display_mode) { // Switch to mode 1
DisplayClear();
}
else if (Settings->display_mode > 1) { // Switch to mode 2, 3 or 4
DisplayLogBufferInit(); DisplayLogBufferInit();
} }
DisplayInit(DISPLAY_INIT_MODE); DisplayMqttSubscribe();
} }
} }
#endif // USE_DISPLAY_MODES1TO5 #endif // USE_DISPLAY_MODES1TO5