mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-17 05:36:39 +01:00
refactoring
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
#define __MESH_SPROCKET__
|
||||
|
||||
#define DEBUG_ESP_OTA
|
||||
#include <vector>
|
||||
#include <painlessMesh.h>
|
||||
#include <Sprocket.h>
|
||||
#include <MeshNet.h>
|
||||
#include <base/MeshMessage.h>
|
||||
#include <plugins/OtaTcpPlugin.cpp>
|
||||
#include "config.h"
|
||||
|
||||
@@ -14,17 +16,37 @@ using namespace std::placeholders;
|
||||
class MeshSprocket : public Sprocket {
|
||||
public:
|
||||
MeshNet* net;
|
||||
OtaTcpPlugin* ota;
|
||||
std::vector<Plugin*> plugins;
|
||||
|
||||
MeshSprocket(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) {
|
||||
ota = new OtaTcpPlugin(otaCfg);
|
||||
addPlugin(new OtaTcpPlugin(otaCfg));
|
||||
}
|
||||
|
||||
void addPlugin(Plugin* p){
|
||||
plugins.reserve(1);
|
||||
plugins.push_back(p);
|
||||
}
|
||||
|
||||
void setupPlugins(Scheduler* scheduler, Network* network){
|
||||
for(Plugin* p : plugins){
|
||||
p->setup(scheduler, network);
|
||||
}
|
||||
}
|
||||
|
||||
void dispatchMessageToPlugins(MeshMessage msg){
|
||||
for(Plugin* p : plugins){
|
||||
if(msg.type != MeshMessage::NONE){
|
||||
Serial.println("dispatch to plugins");
|
||||
p->onMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
||||
net = static_cast<MeshNet*>(network);
|
||||
net->onReceive(bind(&MeshSprocket::dispatch,this, _1, _2));
|
||||
// enable plugins
|
||||
ota->enable(scheduler, network);
|
||||
// setup plugins
|
||||
setupPlugins(scheduler, network);
|
||||
return this;
|
||||
} using Sprocket::activate;
|
||||
|
||||
@@ -34,6 +56,11 @@ class MeshSprocket : public Sprocket {
|
||||
|
||||
void dispatch( uint32_t from, String &msg ) {
|
||||
// TODO handle OTA before passing to onMessage
|
||||
MeshMessage mMsg;
|
||||
mMsg.fromJsonString(msg);
|
||||
if(mMsg.valid){
|
||||
dispatchMessageToPlugins(mMsg);
|
||||
}
|
||||
onMessage(from, msg);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user