diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b005a81..5ea9ae4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -37,7 +37,6 @@ firmware-build: - pio run -t clean - pio run --environment basic - pio run --environment mesh - - pio run --environment meshPixel - pio run --environment meshMqttBridge - pio run --environment ota artifacts: diff --git a/src/Mediator.h b/src/Mediator.h new file mode 100644 index 0000000..4134320 --- /dev/null +++ b/src/Mediator.h @@ -0,0 +1,31 @@ +#ifndef __MEDIATOR__ +#define __MEDIATOR__ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +typedef std::function mediatorHandler_t; + +class Mediator { + public: + std::map> subscriptions; + void subscribe(String topic, mediatorHandler_t handler) { + subscriptions[topic.c_str()].reserve(1); + subscriptions[topic.c_str()].push_back(handler); + } + void publish(String topic, String msg) { + if (subscriptions.find(topic.c_str()) != subscriptions.end()){ + for(mediatorHandler_t h : subscriptions[topic.c_str()]){ + h(msg); + } + } + } +}; + +#endif \ No newline at end of file diff --git a/src/base/MeshSprocket.h b/src/base/MeshSprocket.h index 908ac15..a16338e 100644 --- a/src/base/MeshSprocket.h +++ b/src/base/MeshSprocket.h @@ -17,7 +17,7 @@ using namespace std::placeholders; class MeshSprocket : public Sprocket { public: MeshNet* net; - + MeshSprocket(){}; MeshSprocket(SprocketConfig cfg) : Sprocket(cfg) { } diff --git a/src/examples/mesh/MeshApp.h b/src/examples/mesh/MeshApp.h index 77cb146..c1864e5 100644 --- a/src/examples/mesh/MeshApp.h +++ b/src/examples/mesh/MeshApp.h @@ -9,6 +9,7 @@ #include #include #include +#include "Mediator.h" using namespace std; using namespace std::placeholders; @@ -18,12 +19,14 @@ AsyncWebServer WEBSERVER(80); class MeshApp : public MeshSprocket { public: Task heartbeatTask; - + Mediator mediator; + MeshApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : MeshSprocket(cfg) { addPlugin(new OtaTcpPlugin(otaCfg)); addPlugin(new WebServerPlugin(webCfg, &WEBSERVER)); addPlugin(new WebConfigPlugin(&WEBSERVER)); addPlugin(new MeshManPlugin(&WEBSERVER)); + mediator.subscribe("mediatorMsg", bind(&MeshApp::messageHandler, this, _1)); } Sprocket* activate(Scheduler* scheduler, Network* network) { @@ -37,6 +40,10 @@ class MeshApp : public MeshSprocket { return this; } using MeshSprocket::activate; + void messageHandler(String msg){ + Serial.println(msg); + } + void heartbeat(MeshNet* network){ MeshMessage msg; // = { "wirelos", "broadcast", "local", "alive", 0, }; msg.domain = "wirelos"; @@ -45,6 +52,8 @@ class MeshApp : public MeshSprocket { msg.type = MeshMessage::APP; String msgStr = msg.toJsonString(); network->mesh.sendBroadcast(msgStr/* , true */); + //String mMsg = String("hoi"); + mediator.publish("mediatorMsg", "hi mediator"); } void onMessage( uint32_t from, String &msg ) { Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str());