fix topic comparison

This commit is contained in:
2018-11-19 22:38:56 +01:00
parent 943b116a77
commit 892a513e75
5 changed files with 46 additions and 21 deletions

View File

@@ -98,8 +98,9 @@ void MqttPlugin::downstreamHandler(char *topic, uint8_t *payload, unsigned int l
String topicStr = String(topic);
int topicRootLength = topicRoot.length();
if(topicStr.length() > topicRootLength){
int baseTopicLength = topicStr.substring(0, topicRootLength).length();
String localTopic = topicStr.substring(topicRootLength);
if(localTopic.length() > 4){
if(baseTopicLength == topicRootLength){
String direction = localTopic.substring(0,4);
if(direction == "/in/" ){
String localSubTopic = localTopic.substring(direction.length());

View File

@@ -22,13 +22,12 @@ void setup()
// this subscription gets shadowed by the MqttPlugin
// to rout messages to the queue on topic wirelos/sprocket/chat/log
sprocket->subscribe("chat/log", [](String msg) {
// gets routed to MQTT broker
PRINT_MSG(Serial, "CHAT", msg.c_str());
});
//sprocket->subscribe("chat/log", [](String msg) {
// // gets routed to MQTT broker
// PRINT_MSG(Serial, "CHAT", msg.c_str());
//});
publishHeap.set(TASK_SECOND * 5, TASK_FOREVER, []() {
// locally publish a message
sprocket->publish("chat/log", "hi there");
sprocket->publish("out/chat/log", "hi there");
});
sprocket->addTask(publishHeap);
@@ -42,17 +41,29 @@ void setup()
CONNECT_TIMEOUT);
network->connect();
sprocket->activate();
const char* mqChatTopic = "wirelos/chat/log";
const char* outChatTopic = "out/chat/log";
const char* chatUser = "user";
sprocket->subscribe("mqtt/connect", [](String msg) {
sprocket->subscribe("mqtt/connect", [mqChatTopic, outChatTopic, chatUser](String msg) {
if (msg.length() > 0)
{
mqttPlugin->client->subscribe("wirelos/sprocket/out/chat/log");
sprocket->subscribe("/out/chat/log", [](String msg){
PRINT_MSG(Serial, "CHAT", String("incoming: " +msg).c_str());
mqttPlugin->client->subscribe(mqChatTopic);
sprocket->subscribe(mqChatTopic, [](String msg) {
PRINT_MSG(Serial, "CHAT", String("incoming: " + msg).c_str());
//webApiPlugin->ws->textAll(msg);
});
// send message from WS to this topic
sprocket->subscribe(outChatTopic, [mqChatTopic, chatUser](String msg) {
PRINT_MSG(Serial, "CHAT", msg.c_str());
mqttPlugin->client->publish(mqChatTopic, (String(chatUser) + ": " + msg).c_str());
});
}
});
sprocket->activate();
}
void loop()