mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 05:02:21 +01:00
move plugins to sprocket
This commit is contained in:
@@ -55,7 +55,7 @@ lib_deps = ${common.lib_deps}
|
||||
painlessMesh
|
||||
ESP8266mDNS
|
||||
ArduinoOTA
|
||||
upload_port = 192.168.1.247
|
||||
;upload_port = 192.168.1.247
|
||||
|
||||
[env: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;
|
||||
}
|
||||
Network* MeshNet::connectStation(int doConnect) {
|
||||
Serial.println("connect station?");
|
||||
if(doConnect){
|
||||
Serial.println("connect station!");
|
||||
Serial.println("connect station");
|
||||
mesh.stationManual(config.stationSSID, config.stationPassword);
|
||||
mesh.setHostname(config.hostname);
|
||||
}
|
||||
|
||||
@@ -10,23 +10,11 @@
|
||||
#include <painlessMesh.h>
|
||||
#include <WiFiClient.h>
|
||||
#include "Network.h"
|
||||
#include "MeshConfig.h"
|
||||
|
||||
using namespace std;
|
||||
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 {
|
||||
public:
|
||||
painlessMesh mesh;
|
||||
|
||||
@@ -18,6 +18,11 @@ Sprocket* Sprocket::init(SprocketConfig cfg){
|
||||
Sprocket* Sprocket::activate() {
|
||||
return activate(&scheduler);
|
||||
}
|
||||
Sprocket* Sprocket::activate(Scheduler* scheduler, Network* network) {
|
||||
// setup plugins
|
||||
setupPlugins(scheduler, network);
|
||||
return this;
|
||||
}
|
||||
|
||||
Sprocket* Sprocket::join(Network& net){
|
||||
Serial.println("join network");
|
||||
@@ -35,4 +40,25 @@ Sprocket* Sprocket::addTask(Task& tsk){
|
||||
|
||||
void Sprocket::loop(){
|
||||
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 <Arduino.h>
|
||||
#include <vector>
|
||||
#include "FS.h"
|
||||
#ifdef ESP32
|
||||
#include "SPIFFS.h"
|
||||
#endif
|
||||
#include "Network.h"
|
||||
#include "SprocketConfig.h"
|
||||
#include "Plugin.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
@@ -15,16 +18,12 @@ using namespace std::placeholders;
|
||||
// FIXME move to some global fnc lib
|
||||
#define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0])
|
||||
|
||||
struct SprocketConfig {
|
||||
int startupDelay;
|
||||
int serialBaudRate;
|
||||
};
|
||||
|
||||
class Sprocket {
|
||||
protected:
|
||||
Scheduler scheduler;
|
||||
public:
|
||||
SprocketConfig config;
|
||||
std::vector<Plugin*> plugins;
|
||||
Sprocket();
|
||||
Sprocket(SprocketConfig);
|
||||
Sprocket* init(SprocketConfig);
|
||||
@@ -33,8 +32,12 @@ class Sprocket {
|
||||
virtual void loop();
|
||||
virtual Sprocket* activate();
|
||||
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 ) {}
|
||||
|
||||
void addPlugin(Plugin* p);
|
||||
void setupPlugins(Scheduler* scheduler, Network* network);
|
||||
void dispatchMessageToPlugins(MeshMessage msg);
|
||||
};
|
||||
|
||||
#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 <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;
|
||||
|
||||
|
||||
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) {
|
||||
if(msg.type == MeshMessage::OTA){
|
||||
Serial.println("OTA msg received");
|
||||
WiFi.disconnect();
|
||||
connectUpdateNetwork();
|
||||
enable();
|
||||
//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){
|
||||
|
||||
Reference in New Issue
Block a user