mirror of
https://gitlab.com/wirelos/sprocket-plugin-mqtt.git
synced 2025-12-14 13:51:27 +01:00
refactor for eventChannel
This commit is contained in:
@@ -56,7 +56,7 @@ void MqttPlugin::connect()
|
|||||||
// bind handlers to all local subscriptions
|
// bind handlers to all local subscriptions
|
||||||
if (!subscribed)
|
if (!subscribed)
|
||||||
{
|
{
|
||||||
for (auto const &localSub : mediator->subscriptions)
|
for (auto const &localSub : eventChannel->subscriptions)
|
||||||
{
|
{
|
||||||
if(bindUpstream){
|
if(bindUpstream){
|
||||||
// bind all local topics to the MQTT upstream once
|
// bind all local topics to the MQTT upstream once
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ private:
|
|||||||
void enableConnectTask(Scheduler *scheduler);
|
void enableConnectTask(Scheduler *scheduler);
|
||||||
void enableProcessTask(Scheduler *scheduler);
|
void enableProcessTask(Scheduler *scheduler);
|
||||||
/**
|
/**
|
||||||
* Connects to MQTT server and subscribes all topics available in the mediator
|
* Connects to MQTT server and subscribes all topics available in the eventChannel
|
||||||
* to the corresponding topic on the remote queue by prefixing the topic with inRootTopic.
|
* to the corresponding topic on the remote queue by prefixing the topic with inRootTopic.
|
||||||
* Creates shadow subscriptions of every local topic to propagate messages to the remote queue via upstreamHandler.
|
* Creates shadow subscriptions of every local topic to propagate messages to the remote queue via upstreamHandler.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
#include "Sprocket.h"
|
#include "Sprocket.h"
|
||||||
#include "MqttPlugin.h"
|
#include "MqttPlugin.h"
|
||||||
|
|
||||||
|
const char* mqChatTopic = "wirelos/chat/log";
|
||||||
|
const char* outChatTopic = "out/chat/log";
|
||||||
|
const char* chatUser = "user";
|
||||||
|
|
||||||
WiFiNet *network;
|
WiFiNet *network;
|
||||||
Sprocket *sprocket;
|
Sprocket *sprocket;
|
||||||
MqttPlugin *mqttPlugin;
|
MqttPlugin *mqttPlugin;
|
||||||
@@ -27,7 +31,7 @@ void setup()
|
|||||||
// PRINT_MSG(Serial, "CHAT", msg.c_str());
|
// PRINT_MSG(Serial, "CHAT", msg.c_str());
|
||||||
//});
|
//});
|
||||||
publishHeap.set(TASK_SECOND * 5, TASK_FOREVER, []() {
|
publishHeap.set(TASK_SECOND * 5, TASK_FOREVER, []() {
|
||||||
sprocket->publish("out/chat/log", "hi there");
|
sprocket->publish(outChatTopic, "hi there");
|
||||||
});
|
});
|
||||||
sprocket->addTask(publishHeap);
|
sprocket->addTask(publishHeap);
|
||||||
|
|
||||||
@@ -41,11 +45,7 @@ void setup()
|
|||||||
CONNECT_TIMEOUT);
|
CONNECT_TIMEOUT);
|
||||||
network->connect();
|
network->connect();
|
||||||
|
|
||||||
const char* mqChatTopic = "wirelos/chat/log";
|
sprocket->subscribe("mqtt/connect", [](String msg) {
|
||||||
const char* outChatTopic = "out/chat/log";
|
|
||||||
const char* chatUser = "user";
|
|
||||||
|
|
||||||
sprocket->subscribe("mqtt/connect", [mqChatTopic, outChatTopic, chatUser](String msg) {
|
|
||||||
if (msg.length() > 0)
|
if (msg.length() > 0)
|
||||||
{
|
{
|
||||||
mqttPlugin->client->subscribe(mqChatTopic);
|
mqttPlugin->client->subscribe(mqChatTopic);
|
||||||
@@ -55,7 +55,7 @@ void setup()
|
|||||||
});
|
});
|
||||||
|
|
||||||
// send message from WS to this topic
|
// send message from WS to this topic
|
||||||
sprocket->subscribe(outChatTopic, [mqChatTopic, chatUser](String msg) {
|
sprocket->subscribe(outChatTopic, [](String msg) {
|
||||||
PRINT_MSG(Serial, "CHAT", msg.c_str());
|
PRINT_MSG(Serial, "CHAT", msg.c_str());
|
||||||
mqttPlugin->client->publish(mqChatTopic, (String(chatUser) + ": " + msg).c_str());
|
mqttPlugin->client->publish(mqChatTopic, (String(chatUser) + ": " + msg).c_str());
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user