mirror of
https://gitlab.com/wirelos/sprocket-plugin-mqtt.git
synced 2025-12-15 22:18:20 +01:00
fix topic comparison
This commit is contained in:
@@ -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());
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user