Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change FallbackTopic #3

Merged
merged 1 commit into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sonoff/_changelog.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Add define WIFI_SOFT_AP_CHANNEL in my_user_config.h to set Soft Access Point Channel number between 1 and 13 as used by Wifi Manager web GUI (#4673)
* Fix Alexa "this value is outside the range of the device". Needs power cycle and Alexa deletion/discovery cycle. (#3159, #4712)
* Add define USE_MQTT_TLS_CA_CERT for checking MQTT TLS against root ca using Let's Encrypt cert from sonoff_letsencrypt.h - not supported with core 2.3.0 (#4703)
* Change FallbackTopic from cmnd/<mqttclient>/ to cmnd/<mqttclient>_fb/ to discriminate from Topic (#1528)
*
* 6.4.0.2 20181221
* Fix possible dtostrf buffer overflows by increasing buffers
Expand Down
24 changes: 19 additions & 5 deletions sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,25 @@ char* GetOtaUrl(char *otaurl, size_t otaurl_size)
return otaurl;
}

void GetTopic_P(char *stopic, byte prefix, char *topic, const char* subtopic)
char* GetTopic_P(char *stopic, byte prefix, char *topic, const char* subtopic)
{
/* prefix 0 = Cmnd
prefix 1 = Stat
prefix 2 = Tele
prefix 4 = Cmnd fallback
prefix 5 = Stat fallback
prefix 6 = Tele fallback
*/
char romram[CMDSZ];
String fulltopic;

snprintf_P(romram, sizeof(romram), subtopic);
if (fallback_topic_flag) {
if (fallback_topic_flag || (prefix > 3)) {
prefix &= 3;
fulltopic = FPSTR(kPrefixes[prefix]);
fulltopic += F("/");
fulltopic += mqtt_client;
fulltopic += F("_fb"); // cmnd/<mqttclient>_fb
} else {
fulltopic = Settings.mqtt_fulltopic;
if ((0 == prefix) && (-1 == fulltopic.indexOf(F(MQTT_TOKEN_PREFIX)))) {
Expand All @@ -280,6 +285,12 @@ void GetTopic_P(char *stopic, byte prefix, char *topic, const char* subtopic)
fulltopic.replace(F("//"), "/");
if (!fulltopic.endsWith("/")) fulltopic += "/";
snprintf_P(stopic, TOPSZ, PSTR("%s%s"), fulltopic.c_str(), romram);
return stopic;
}

char* GetFallbackTopic_P(char *stopic, byte prefix, const char* subtopic)
{
return GetTopic_P(stopic, prefix +4, NULL, subtopic);
}

char* GetStateText(byte state)
Expand Down Expand Up @@ -467,8 +478,10 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len)
if (XdrvMqttData(topicBuf, sizeof(topicBuf), dataBuf, sizeof(dataBuf))) return;

grpflg = (strstr(topicBuf, Settings.mqtt_grptopic) != NULL);
snprintf_P(stemp1, sizeof(stemp1), PSTR(D_CMND "/%s/"), mqtt_client); // Full Fallback topic = cmnd/DVES_xxxxxxxx

GetFallbackTopic_P(stemp1, CMND, ""); // Full Fallback topic = cmnd/DVES_xxxxxxxx_fb/
fallback_topic_flag = (!strncmp(topicBuf, stemp1, strlen(stemp1)));

type = strrchr(topicBuf, '/'); // Last part of received topic is always the command (type)

index = 1;
Expand Down Expand Up @@ -2742,8 +2755,9 @@ void setup(void)
}
blink_powersave = power;

snprintf_P(log_data, sizeof(log_data), PSTR(D_PROJECT " %s %s (" D_CMND_TOPIC " %s, " D_FALLBACK " %s, " D_CMND_GROUPTOPIC " %s) " D_VERSION " %s%s-" ARDUINO_ESP8266_RELEASE),
PROJECT, Settings.friendlyname[0], mqtt_topic, mqtt_client, Settings.mqtt_grptopic, my_version, my_image);
char stopic[TOPSZ];
snprintf_P(log_data, sizeof(log_data), PSTR(D_PROJECT " %s %s " D_VERSION " %s%s-" ARDUINO_ESP8266_RELEASE),
PROJECT, Settings.friendlyname[0], my_version, my_image);
AddLog(LOG_LEVEL_INFO);
#ifdef BE_MINIMAL
snprintf_P(log_data, sizeof(log_data), PSTR(D_WARNING_MINIMAL_VERSION));
Expand Down
7 changes: 3 additions & 4 deletions sonoff/xdrv_01_webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1323,13 +1323,12 @@ void HandleInformation(void)
if (Settings.flag.mqtt_enabled) {
func += F("}1" D_MQTT_HOST "}2"); func += Settings.mqtt_host;
func += F("}1" D_MQTT_PORT "}2"); func += String(Settings.mqtt_port);
func += F("}1" D_MQTT_CLIENT " &<br/>&nbsp;" D_FALLBACK_TOPIC "}2"); func += mqtt_client;
func += F("}1" D_MQTT_USER "}2"); func += Settings.mqtt_user;
func += F("}1" D_MQTT_CLIENT "}2"); func += mqtt_client;
func += F("}1" D_MQTT_TOPIC "}2"); func += Settings.mqtt_topic;
func += F("}1" D_MQTT_GROUP_TOPIC "}2"); func += Settings.mqtt_grptopic;
GetTopic_P(stopic, CMND, mqtt_topic, "");
func += F("}1" D_MQTT_FULL_TOPIC "}2"); func += stopic;

func += F("}1" D_MQTT_FULL_TOPIC "}2"); func += GetTopic_P(stopic, CMND, mqtt_topic, "");
func += F("}1" D_MQTT " " D_FALLBACK_TOPIC "}2"); func += GetFallbackTopic_P(stopic, CMND, "");
} else {
func += F("}1" D_MQTT "}2" D_DISABLED);
}
Expand Down
7 changes: 3 additions & 4 deletions sonoff/xdrv_02_mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,7 @@ void MqttConnected(void)
if (strstr(Settings.mqtt_fulltopic, MQTT_TOKEN_TOPIC) != NULL) {
GetTopic_P(stopic, CMND, Settings.mqtt_grptopic, PSTR("#"));
MqttSubscribe(stopic);
fallback_topic_flag = 1;
GetTopic_P(stopic, CMND, mqtt_client, PSTR("#"));
fallback_topic_flag = 0;
GetFallbackTopic_P(stopic, CMND, PSTR("#"));
MqttSubscribe(stopic);
}

Expand All @@ -417,7 +415,8 @@ void MqttConnected(void)

if (mqtt_initial_connection_state) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MODULE "\":\"%s\",\"" D_JSON_VERSION "\":\"%s%s\",\"" D_JSON_FALLBACKTOPIC "\":\"%s\",\"" D_CMND_GROUPTOPIC "\":\"%s\"}"),
my_module.name, my_version, my_image, mqtt_client, Settings.mqtt_grptopic);
my_module.name, my_version, my_image, GetFallbackTopic_P(stopic, CMND, ""), Settings.mqtt_grptopic);
// my_module.name, my_version, my_image, mqtt_client, Settings.mqtt_grptopic);
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_INFO "1"));
#ifdef USE_WEBSERVER
if (Settings.webserver) {
Expand Down