mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 13:08:21 +01:00
add mediator
This commit is contained in:
@@ -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:
|
||||
|
||||
31
src/Mediator.h
Normal file
31
src/Mediator.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef __MEDIATOR__
|
||||
#define __MEDIATOR__
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <list>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef std::function<void(String msg)> mediatorHandler_t;
|
||||
|
||||
class Mediator {
|
||||
public:
|
||||
std::map<std::string, vector<mediatorHandler_t>> 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
|
||||
@@ -17,7 +17,7 @@ using namespace std::placeholders;
|
||||
class MeshSprocket : public Sprocket {
|
||||
public:
|
||||
MeshNet* net;
|
||||
|
||||
MeshSprocket(){};
|
||||
MeshSprocket(SprocketConfig cfg) : Sprocket(cfg) {
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <plugins/WebServerPlugin.cpp>
|
||||
#include <plugins/WebConfigPlugin.cpp>
|
||||
#include <plugins/MeshManPlugin.cpp>
|
||||
#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());
|
||||
|
||||
Reference in New Issue
Block a user