refactoring

This commit is contained in:
2018-08-19 16:10:56 +02:00
parent c127346394
commit 77f1b278f4
7 changed files with 146 additions and 23 deletions

77
src/base/MeshMessage.h Normal file
View File

@@ -0,0 +1,77 @@
#ifndef __MESH_MESSAGE__
#define __MESH_MESSAGE__
#include <Arduino.h>
#include <ArduinoJson.h>
#define JSON_DOMAIN "domain"
#define JSON_FROM "from"
#define JSON_TO "target"
#define JSON_MSG "msg"
struct MeshMessage {
String domain;
String to;
String from;
String msg;
enum MeshMessageType { NONE, SYSTEM, OTA } type;
int valid = 0;
// ------------------------------------------------------------------------------------------
void init() {
//from = reinterpret_cast<char*>(ESP.getChipId());
}
int verifyJsonObject(JsonObject& json){
return json.success()
//&& json.containsKey(JSON_DOMAIN)
//&& json.containsKey(JSON_TO)
//&& json.containsKey(JSON_FROM)
&& json.containsKey(JSON_MSG);
};
String toJsonString(){
//StaticJsonBuffer<200> jsonBuffer;
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
JsonObject& root = jsonBuffer.createObject();
root[JSON_DOMAIN] = domain;
root[JSON_TO] = to;
root[JSON_FROM] = from;
root[JSON_MSG] = msg;
String jsonString;
root.printTo(jsonString);
return jsonString;
}
String getAttrFromJson(JsonObject& json, const char* attr){
if(json.containsKey(attr)){
return json[attr];
}
return "";
}
int getIntAttrFromJson(JsonObject& json, const char* attr){
if(json.containsKey(attr)){
return json[attr];
}
return 0;
}
// Map a json object to this struct.
void fromJsonObject(JsonObject& json){
if(!verifyJsonObject(json)){
Serial.println("ERROR: cannot parse MeshMessage JSON object");
valid = 0;
//return;
}
domain = getAttrFromJson(json, JSON_DOMAIN);
to = getAttrFromJson(json, JSON_TO);
from = getAttrFromJson(json, JSON_FROM);
msg = getAttrFromJson(json, JSON_MSG);
type = (MeshMessageType) getIntAttrFromJson(json, "type");
valid = 1;
};
// Parse a json string and map parsed object
void fromJsonString(String& str){
//StaticJsonBuffer<200> jsonBuffer;
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
JsonObject& json = jsonBuffer.parseObject(str);
fromJsonObject(json);
};
};
#endif

View File

@@ -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);
}