mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-16 13:25:03 +01:00
move plugins to sprocket
This commit is contained in:
@@ -55,7 +55,7 @@ lib_deps = ${common.lib_deps}
|
|||||||
painlessMesh
|
painlessMesh
|
||||||
ESP8266mDNS
|
ESP8266mDNS
|
||||||
ArduinoOTA
|
ArduinoOTA
|
||||||
upload_port = 192.168.1.247
|
;upload_port = 192.168.1.247
|
||||||
|
|
||||||
[env:meshMqttBridge]
|
[env:meshMqttBridge]
|
||||||
src_filter = +<*> -<examples/> +<examples/meshMqttBridge/>
|
src_filter = +<*> -<examples/> +<examples/meshMqttBridge/>
|
||||||
|
|||||||
43
src/JsonStruct.h
Normal file
43
src/JsonStruct.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#ifndef __JSON_STRUCT__
|
||||||
|
#define __JSON_STRUCT__
|
||||||
|
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <FS.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct JsonStruct {
|
||||||
|
SprocketConfig soricketConfig;
|
||||||
|
MeshConfig meshConfig;
|
||||||
|
int valid = 0;
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
virtual int verifyJsonObject(JsonObject& json){
|
||||||
|
return json.success();
|
||||||
|
//&& json.containsKey(JSON_DOMAIN)
|
||||||
|
};
|
||||||
|
virtual String toJsonString();
|
||||||
|
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.
|
||||||
|
virtual int fromJsonObject(JsonObject& json);
|
||||||
|
// Parse a json string and map parsed object
|
||||||
|
int fromJsonString(String& str){
|
||||||
|
//StaticJsonBuffer<200> jsonBuffer;
|
||||||
|
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||||
|
JsonObject& json = jsonBuffer.parseObject(str);
|
||||||
|
return fromJsonObject(json);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
18
src/MeshConfig.h
Normal file
18
src/MeshConfig.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef __MESHCONFIG__
|
||||||
|
#define __MESHCONFIG__
|
||||||
|
|
||||||
|
// FIXME non-mesh config should have it's own struct
|
||||||
|
struct MeshConfig {
|
||||||
|
int stationMode;
|
||||||
|
int channel;
|
||||||
|
int meshPort;
|
||||||
|
const char* meshSSID;
|
||||||
|
const char* meshPassword;
|
||||||
|
const char* stationSSID;
|
||||||
|
const char* stationPassword;
|
||||||
|
const char* hostname;
|
||||||
|
uint16_t debugTypes;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -20,9 +20,8 @@ Network* MeshNet::init(){
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
Network* MeshNet::connectStation(int doConnect) {
|
Network* MeshNet::connectStation(int doConnect) {
|
||||||
Serial.println("connect station?");
|
|
||||||
if(doConnect){
|
if(doConnect){
|
||||||
Serial.println("connect station!");
|
Serial.println("connect station");
|
||||||
mesh.stationManual(config.stationSSID, config.stationPassword);
|
mesh.stationManual(config.stationSSID, config.stationPassword);
|
||||||
mesh.setHostname(config.hostname);
|
mesh.setHostname(config.hostname);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,23 +10,11 @@
|
|||||||
#include <painlessMesh.h>
|
#include <painlessMesh.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
|
#include "MeshConfig.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
// FIXME non-mesh config should have it's own struct
|
|
||||||
struct MeshConfig {
|
|
||||||
int stationMode;
|
|
||||||
int channel;
|
|
||||||
int meshPort;
|
|
||||||
const char* meshSSID;
|
|
||||||
const char* meshPassword;
|
|
||||||
const char* stationSSID;
|
|
||||||
const char* stationPassword;
|
|
||||||
const char* hostname;
|
|
||||||
uint16_t debugTypes;
|
|
||||||
};
|
|
||||||
|
|
||||||
class MeshNet : public Network {
|
class MeshNet : public Network {
|
||||||
public:
|
public:
|
||||||
painlessMesh mesh;
|
painlessMesh mesh;
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ Sprocket* Sprocket::init(SprocketConfig cfg){
|
|||||||
Sprocket* Sprocket::activate() {
|
Sprocket* Sprocket::activate() {
|
||||||
return activate(&scheduler);
|
return activate(&scheduler);
|
||||||
}
|
}
|
||||||
|
Sprocket* Sprocket::activate(Scheduler* scheduler, Network* network) {
|
||||||
|
// setup plugins
|
||||||
|
setupPlugins(scheduler, network);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
Sprocket* Sprocket::join(Network& net){
|
Sprocket* Sprocket::join(Network& net){
|
||||||
Serial.println("join network");
|
Serial.println("join network");
|
||||||
@@ -36,3 +41,24 @@ Sprocket* Sprocket::addTask(Task& tsk){
|
|||||||
void Sprocket::loop(){
|
void Sprocket::loop(){
|
||||||
scheduler.execute();
|
scheduler.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Sprocket::addPlugin(Plugin* p){
|
||||||
|
plugins.reserve(1);
|
||||||
|
plugins.push_back(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sprocket::setupPlugins(Scheduler* scheduler, Network* network){
|
||||||
|
for(Plugin* p : plugins){
|
||||||
|
p->setup(scheduler, network);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sprocket::dispatchMessageToPlugins(MeshMessage msg){
|
||||||
|
if(msg.type != MeshMessage::NONE){ // baaaa
|
||||||
|
for(Plugin* p : plugins){
|
||||||
|
Serial.println("dispatch to plugins");
|
||||||
|
p->onMessage(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,11 +3,14 @@
|
|||||||
|
|
||||||
#include <TaskSchedulerDeclarations.h>
|
#include <TaskSchedulerDeclarations.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <vector>
|
||||||
#include "FS.h"
|
#include "FS.h"
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
#include "SPIFFS.h"
|
#include "SPIFFS.h"
|
||||||
#endif
|
#endif
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
|
#include "SprocketConfig.h"
|
||||||
|
#include "Plugin.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
@@ -15,16 +18,12 @@ using namespace std::placeholders;
|
|||||||
// FIXME move to some global fnc lib
|
// FIXME move to some global fnc lib
|
||||||
#define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0])
|
#define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0])
|
||||||
|
|
||||||
struct SprocketConfig {
|
|
||||||
int startupDelay;
|
|
||||||
int serialBaudRate;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Sprocket {
|
class Sprocket {
|
||||||
protected:
|
protected:
|
||||||
Scheduler scheduler;
|
Scheduler scheduler;
|
||||||
public:
|
public:
|
||||||
SprocketConfig config;
|
SprocketConfig config;
|
||||||
|
std::vector<Plugin*> plugins;
|
||||||
Sprocket();
|
Sprocket();
|
||||||
Sprocket(SprocketConfig);
|
Sprocket(SprocketConfig);
|
||||||
Sprocket* init(SprocketConfig);
|
Sprocket* init(SprocketConfig);
|
||||||
@@ -33,8 +32,12 @@ class Sprocket {
|
|||||||
virtual void loop();
|
virtual void loop();
|
||||||
virtual Sprocket* activate();
|
virtual Sprocket* activate();
|
||||||
virtual Sprocket* activate(Scheduler*) { return this; }
|
virtual Sprocket* activate(Scheduler*) { return this; }
|
||||||
virtual Sprocket* activate(Scheduler*, Network*) { return this; }
|
virtual Sprocket* activate(Scheduler*, Network*);
|
||||||
virtual void dispatch( uint32_t from, String &msg ) {}
|
virtual void dispatch( uint32_t from, String &msg ) {}
|
||||||
|
|
||||||
|
void addPlugin(Plugin* p);
|
||||||
|
void setupPlugins(Scheduler* scheduler, Network* network);
|
||||||
|
void dispatchMessageToPlugins(MeshMessage msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
9
src/SprocketConfig.h
Normal file
9
src/SprocketConfig.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef __SPROCKET_CONFIG__
|
||||||
|
#define __SPROCKET_CONFIG__
|
||||||
|
|
||||||
|
struct SprocketConfig {
|
||||||
|
int startupDelay;
|
||||||
|
int serialBaudRate;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <Sprocket.h>
|
#include <Sprocket.h>
|
||||||
#include <MeshNet.h>
|
#include <MeshNet.h>
|
||||||
#include <base/MeshMessage.h>
|
#include <base/MeshMessage.h>
|
||||||
|
#include <base/MeshSprocketConfig.h>
|
||||||
#include <plugins/OtaTcpPlugin.cpp>
|
#include <plugins/OtaTcpPlugin.cpp>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
@@ -16,37 +17,15 @@ using namespace std::placeholders;
|
|||||||
class MeshSprocket : public Sprocket {
|
class MeshSprocket : public Sprocket {
|
||||||
public:
|
public:
|
||||||
MeshNet* net;
|
MeshNet* net;
|
||||||
std::vector<Plugin*> plugins;
|
|
||||||
|
|
||||||
MeshSprocket(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) {
|
MeshSprocket(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) {
|
||||||
addPlugin(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){
|
|
||||||
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* scheduler, Network* network) {
|
||||||
|
Sprocket::activate(scheduler, network);
|
||||||
net = static_cast<MeshNet*>(network);
|
net = static_cast<MeshNet*>(network);
|
||||||
net->onReceive(bind(&MeshSprocket::dispatch,this, _1, _2));
|
net->onReceive(bind(&MeshSprocket::dispatch,this, _1, _2));
|
||||||
// setup plugins
|
|
||||||
setupPlugins(scheduler, network);
|
|
||||||
return this;
|
return this;
|
||||||
} using Sprocket::activate;
|
} using Sprocket::activate;
|
||||||
|
|
||||||
|
|||||||
69
src/base/MeshSprocketConfig.h
Normal file
69
src/base/MeshSprocketConfig.h
Normal 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
|
||||||
@@ -36,9 +36,12 @@ class OtaTcpPlugin : public Plugin {
|
|||||||
void onMessage(MeshMessage msg) {
|
void onMessage(MeshMessage msg) {
|
||||||
if(msg.type == MeshMessage::OTA){
|
if(msg.type == MeshMessage::OTA){
|
||||||
Serial.println("OTA msg received");
|
Serial.println("OTA msg received");
|
||||||
|
WiFi.disconnect();
|
||||||
connectUpdateNetwork();
|
connectUpdateNetwork();
|
||||||
enable();
|
enable();
|
||||||
//net->mesh.sendBroadcast("my ip:" + WiFi.localIP().toString(), true);
|
//net->mesh.sendBroadcast("my ip:" + WiFi.localIP().toString(), true);
|
||||||
|
//net->mesh.sendBroadcast("gw ip:" + WiFi.gatewayIP().toString(), true);
|
||||||
|
WiFi.gatewayIP().printTo(Serial);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void setup(Scheduler* userScheduler, Network* network){
|
void setup(Scheduler* userScheduler, Network* network){
|
||||||
|
|||||||
Reference in New Issue
Block a user