mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 05:02:21 +01:00
refactoring
This commit is contained in:
@@ -53,6 +53,8 @@ monitor_baud = ${common.monitor_baud}
|
||||
framework = ${common.framework}
|
||||
lib_deps = ${common.lib_deps}
|
||||
painlessMesh
|
||||
ESP8266mDNS
|
||||
ArduinoOTA
|
||||
|
||||
[env:meshMqttBridge]
|
||||
src_filter = +<*> -<examples/> +<examples/meshMqttBridge/>
|
||||
|
||||
@@ -3,13 +3,16 @@
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include <Network.h>
|
||||
#include <base/MeshMessage.h>
|
||||
|
||||
class Plugin {
|
||||
protected:
|
||||
Scheduler* scheduler;
|
||||
public:
|
||||
virtual void enable(Scheduler*, Network*);
|
||||
virtual void setup(Scheduler*, Network*);
|
||||
virtual void enable();
|
||||
virtual void disable();
|
||||
virtual void onMessage(MeshMessage msg);
|
||||
};
|
||||
|
||||
#endif
|
||||
77
src/base/MeshMessage.h
Normal file
77
src/base/MeshMessage.h
Normal 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
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,8 +28,13 @@ class MeshApp : public MeshSprocket {
|
||||
} using MeshSprocket::activate;
|
||||
|
||||
void heartbeat(MeshNet* network){
|
||||
String msg = "{ \"payload \": 1 }";
|
||||
network->mesh.sendBroadcast(msg, true);
|
||||
MeshMessage msg; // = { "wirelos", "broadcast", "local", "alive", 0, };
|
||||
msg.domain = "wirelos";
|
||||
msg.to = "broadcast";
|
||||
msg.msg = "alive";
|
||||
String msgStr = msg.toJsonString();
|
||||
//String msg = "{ \"domain\": \"wirelos\",\"from\": \"0\",\"target\": \"broadcast\", \"msg \": \"alive\" }";
|
||||
network->mesh.sendBroadcast(msgStr, true);
|
||||
}
|
||||
void onMessage( uint32_t from, String &msg ) {
|
||||
Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str());
|
||||
|
||||
@@ -10,15 +10,15 @@
|
||||
#define STARTUP_DELAY 3000
|
||||
|
||||
// Mesh config
|
||||
#define STATION_MODE 0
|
||||
#define STATION_MODE 1
|
||||
#define WIFI_CHANNEL 11
|
||||
#define MESH_PORT 5555
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define STATION_SSID "Th1ngs4P"
|
||||
#define STATION_PASSWORD "th3r31sn0sp00n"
|
||||
#define STATION_SSID "tErAx1d"
|
||||
#define STATION_PASSWORD "ramalamadingdong"
|
||||
#define HOSTNAME "mesh-node"
|
||||
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
|
||||
#define MESH_DEBUG_TYPES ERROR | STARTUP
|
||||
//ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE
|
||||
|
||||
// OTA config
|
||||
|
||||
@@ -18,21 +18,31 @@ class OtaTcpPlugin : public Plugin {
|
||||
private:
|
||||
OtaConfig config;
|
||||
Task otaTask;
|
||||
MeshNet* network;
|
||||
public:
|
||||
OtaTcpPlugin(OtaConfig cfg){
|
||||
config = cfg;
|
||||
|
||||
}
|
||||
void enable(Scheduler* userScheduler, Network* network){
|
||||
scheduler = userScheduler;
|
||||
|
||||
// connect to network
|
||||
if(!network->isConnected()){
|
||||
// TODO when config is refactored, cast is not necessary anymore
|
||||
static_cast<MeshNet*>(network)->config.stationMode = 1;
|
||||
network->connectStation();
|
||||
// TODO set service message to mesh to announce station IP
|
||||
}
|
||||
void connectUpdateNetwork(Network* network) {
|
||||
// if(!network->isConnected()){
|
||||
// static_cast<MeshNet*>(network)->config.stationMode = 1;
|
||||
// }
|
||||
network->connectStation();
|
||||
}
|
||||
void enable() {
|
||||
ArduinoOTA.begin();
|
||||
otaTask.enable();
|
||||
}
|
||||
void onMessage(MeshMessage msg) {
|
||||
//enable();
|
||||
Serial.println("OTA msg received");
|
||||
}
|
||||
void setup(Scheduler* userScheduler, Network* network){
|
||||
// connect done in network class
|
||||
//connectUpdateNetwork(network);
|
||||
// setup task
|
||||
scheduler = userScheduler;
|
||||
otaTask.set(TASK_MILLISECOND * 100, TASK_FOREVER, [](){
|
||||
ArduinoOTA.handle();
|
||||
});
|
||||
@@ -62,8 +72,7 @@ class OtaTcpPlugin : public Plugin {
|
||||
else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
|
||||
else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
otaTask.enable();
|
||||
enable();
|
||||
}
|
||||
void disable(){
|
||||
otaTask.disable();
|
||||
|
||||
Reference in New Issue
Block a user