mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-16 05:24:30 +01:00
add mediator
This commit is contained in:
@@ -37,7 +37,6 @@ firmware-build:
|
|||||||
- pio run -t clean
|
- pio run -t clean
|
||||||
- pio run --environment basic
|
- pio run --environment basic
|
||||||
- pio run --environment mesh
|
- pio run --environment mesh
|
||||||
- pio run --environment meshPixel
|
|
||||||
- pio run --environment meshMqttBridge
|
- pio run --environment meshMqttBridge
|
||||||
- pio run --environment ota
|
- pio run --environment ota
|
||||||
artifacts:
|
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 {
|
class MeshSprocket : public Sprocket {
|
||||||
public:
|
public:
|
||||||
MeshNet* net;
|
MeshNet* net;
|
||||||
|
MeshSprocket(){};
|
||||||
MeshSprocket(SprocketConfig cfg) : Sprocket(cfg) {
|
MeshSprocket(SprocketConfig cfg) : Sprocket(cfg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include <plugins/WebServerPlugin.cpp>
|
#include <plugins/WebServerPlugin.cpp>
|
||||||
#include <plugins/WebConfigPlugin.cpp>
|
#include <plugins/WebConfigPlugin.cpp>
|
||||||
#include <plugins/MeshManPlugin.cpp>
|
#include <plugins/MeshManPlugin.cpp>
|
||||||
|
#include "Mediator.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
@@ -18,12 +19,14 @@ AsyncWebServer WEBSERVER(80);
|
|||||||
class MeshApp : public MeshSprocket {
|
class MeshApp : public MeshSprocket {
|
||||||
public:
|
public:
|
||||||
Task heartbeatTask;
|
Task heartbeatTask;
|
||||||
|
Mediator mediator;
|
||||||
|
|
||||||
MeshApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : MeshSprocket(cfg) {
|
MeshApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : MeshSprocket(cfg) {
|
||||||
addPlugin(new OtaTcpPlugin(otaCfg));
|
addPlugin(new OtaTcpPlugin(otaCfg));
|
||||||
addPlugin(new WebServerPlugin(webCfg, &WEBSERVER));
|
addPlugin(new WebServerPlugin(webCfg, &WEBSERVER));
|
||||||
addPlugin(new WebConfigPlugin(&WEBSERVER));
|
addPlugin(new WebConfigPlugin(&WEBSERVER));
|
||||||
addPlugin(new MeshManPlugin(&WEBSERVER));
|
addPlugin(new MeshManPlugin(&WEBSERVER));
|
||||||
|
mediator.subscribe("mediatorMsg", bind(&MeshApp::messageHandler, this, _1));
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
||||||
@@ -37,6 +40,10 @@ class MeshApp : public MeshSprocket {
|
|||||||
return this;
|
return this;
|
||||||
} using MeshSprocket::activate;
|
} using MeshSprocket::activate;
|
||||||
|
|
||||||
|
void messageHandler(String msg){
|
||||||
|
Serial.println(msg);
|
||||||
|
}
|
||||||
|
|
||||||
void heartbeat(MeshNet* network){
|
void heartbeat(MeshNet* network){
|
||||||
MeshMessage msg; // = { "wirelos", "broadcast", "local", "alive", 0, };
|
MeshMessage msg; // = { "wirelos", "broadcast", "local", "alive", 0, };
|
||||||
msg.domain = "wirelos";
|
msg.domain = "wirelos";
|
||||||
@@ -45,6 +52,8 @@ class MeshApp : public MeshSprocket {
|
|||||||
msg.type = MeshMessage::APP;
|
msg.type = MeshMessage::APP;
|
||||||
String msgStr = msg.toJsonString();
|
String msgStr = msg.toJsonString();
|
||||||
network->mesh.sendBroadcast(msgStr/* , true */);
|
network->mesh.sendBroadcast(msgStr/* , true */);
|
||||||
|
//String mMsg = String("hoi");
|
||||||
|
mediator.publish("mediatorMsg", "hi mediator");
|
||||||
}
|
}
|
||||||
void onMessage( uint32_t from, String &msg ) {
|
void onMessage( uint32_t from, String &msg ) {
|
||||||
Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str());
|
Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str());
|
||||||
|
|||||||
Reference in New Issue
Block a user