move plugins to sprocket

This commit is contained in:
2018-08-24 17:42:35 +02:00
parent 426d495fd9
commit 5334d58433
11 changed files with 182 additions and 45 deletions

View File

@@ -7,6 +7,7 @@
#include <Sprocket.h>
#include <MeshNet.h>
#include <base/MeshMessage.h>
#include <base/MeshSprocketConfig.h>
#include <plugins/OtaTcpPlugin.cpp>
#include "config.h"
@@ -16,37 +17,15 @@ using namespace std::placeholders;
class MeshSprocket : public Sprocket {
public:
MeshNet* net;
std::vector<Plugin*> plugins;
MeshSprocket(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) {
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){
if(msg.type != MeshMessage::NONE){ // baaaa
for(Plugin* p : plugins){
Serial.println("dispatch to plugins");
p->onMessage(msg);
}
}
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
Sprocket::activate(scheduler, network);
net = static_cast<MeshNet*>(network);
net->onReceive(bind(&MeshSprocket::dispatch,this, _1, _2));
// setup plugins
setupPlugins(scheduler, network);
return this;
} using Sprocket::activate;

View File

@@ -0,0 +1,69 @@
#ifndef __MESH_SPROCKET_CONFIG__
#define __MESH_SPROCKET_CONFIG__
#include <Arduino.h>
#include <ArduinoJson.h>
#include <FS.h>
#include "MeshConfig.h"
#include "SprocketConfig.h"
#include "JsonStruct.h"
#define JSON_stationMode "stationMode"
#define JSON_channel "channel"
#define JSON_meshPort "meshPort"
#define JSON_meshSSID "meshSSID"
#define JSON_meshPassword "meshPassword"
#define JSON_stationSSID "stationSSID"
#define JSON_stationPassword "stationPassword"
#define JSON_hostname "hostname"
#define JSON_startupDelay "startupDelay"
#define JSON_serialBaudRate "serialBaudRate"
struct MeshSprocketConfig : public JsonStruct {
SprocketConfig sprocket;
MeshConfig mesh;
int valid = 0;
// ------------------------------------------------------------------------------------------
String toJsonString(){
//StaticJsonBuffer<200> jsonBuffer;
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
JsonObject& root = jsonBuffer.createObject();
root[JSON_stationMode] = mesh.stationMode;
root[JSON_channel] = mesh.channel;
root[JSON_meshPort] = mesh.meshPort;
root[JSON_meshSSID] = mesh.meshSSID;
root[JSON_meshPassword] = mesh.meshPassword;
root[JSON_stationSSID] = mesh.stationSSID;
root[JSON_stationPassword] = mesh.stationPassword;
root[JSON_hostname] = mesh.hostname;
root[JSON_startupDelay] = sprocket.startupDelay;
root[JSON_serialBaudRate] = sprocket.serialBaudRate;
String jsonString;
root.printTo(jsonString);
return jsonString;
}
// Map a json object to this struct.
int fromJsonObject(JsonObject& json){
if(!verifyJsonObject(json)){
Serial.println("ERROR: cannot parse JSON object");
valid = 0;
return valid;
}
mesh.stationMode = getIntAttrFromJson(json, JSON_stationMode);
mesh.channel = getIntAttrFromJson(json, JSON_channel);
mesh.meshPort = getIntAttrFromJson(json, JSON_meshPort);
mesh.meshSSID = getAttrFromJson(json, JSON_meshSSID).c_str();
mesh.meshPassword = getAttrFromJson(json, JSON_meshPassword).c_str();
mesh.stationSSID = getAttrFromJson(json, JSON_stationSSID).c_str();
mesh.stationPassword = getAttrFromJson(json, JSON_stationPassword).c_str();
mesh.hostname = getAttrFromJson(json, JSON_hostname).c_str();
sprocket.startupDelay = getIntAttrFromJson(json, JSON_startupDelay);
sprocket.serialBaudRate = getIntAttrFromJson(json, JSON_serialBaudRate);
valid = 1;
return valid;
};
};
#endif