remove network from activation phase

This commit is contained in:
2018-11-08 13:46:17 +01:00
parent bb81391d00
commit f290ec6197
19 changed files with 138 additions and 66 deletions

View File

@@ -8,8 +8,8 @@
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html
[platformio]
env_default = mesh
;[platformio]
;env_default = mesh
[common]
framework = arduino

View File

@@ -13,30 +13,28 @@ MeshNet::MeshNet(MeshConfig cfg) : Network() {
}
Network* MeshNet::init(){
Serial.println("init mesh");
config.fromFile("/config.json");
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( config.debugTypes );
return this;
}
int MeshNet::connect(){
mesh.init( config.meshSSID, config.meshPassword, scheduler, config.meshPort, WIFI_AP_STA, config.channel );
mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1));
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
connectStation(config.stationMode);
return this;
if(config.stationMode){
connectStation();
}
}
int MeshNet::connectStation(int doConnect) {
if(doConnect){
int MeshNet::connectStation() {
Serial.println("connect station");
mesh.stationManual(config.stationSSID, config.stationPassword);
mesh.setHostname(config.hostname.c_str());
return 1;
}
return 0;
}
void MeshNet::sendTo(uint32_t target, String msg){
mesh.sendSingle(target, msg);

View File

@@ -22,7 +22,8 @@ class MeshNet : public Network {
MeshSprocketConfig config;
MeshNet(MeshConfig cfg);
Network* init();
int connectStation(int);
int connect();
int connectStation();
void configure(MeshSprocketConfig cfg);
void update();
void newConnectionCallback(uint32_t nodeId);

View File

@@ -9,10 +9,10 @@
class Plugin {
public:
Mediator* mediator;
virtual void activate(Scheduler*, Network*);
virtual void enable(){};
virtual void disable(){};
virtual void onMessage(SprocketMessage msg){};
virtual void activate(Scheduler*);
virtual void enable(){}
virtual void disable(){}
virtual void onMessage(SprocketMessage msg){}
Plugin* mediate(Mediator* m) {
mediator = m;
return this;

View File

@@ -18,9 +18,9 @@ Sprocket* Sprocket::init(SprocketConfig cfg){
Sprocket* Sprocket::activate() {
return activate(&scheduler);
}
Sprocket* Sprocket::activate(Scheduler* scheduler, Network* network) {
Sprocket* Sprocket::activate(Scheduler* scheduler) {
// setup plugins
activatePlugins(scheduler, network);
activatePlugins(scheduler);
return this;
}
@@ -30,7 +30,7 @@ Sprocket* Sprocket::join(Network& net){
//net.onReceive(bind(&Sprocket::dispatch,this, _1, _2));
net.connect();
network = net;
return activate(&scheduler, &net);
return activate(&scheduler);
}
Sprocket* Sprocket::addTask(Task& tsk){
@@ -57,8 +57,8 @@ void Sprocket::addPlugin(Plugin* p){
plugins.push_back(p);
}
void Sprocket::activatePlugins(Scheduler* scheduler, Network* network){
void Sprocket::activatePlugins(Scheduler* scheduler){
for(Plugin* p : plugins){
p->activate(scheduler, network);
p->activate(scheduler);
}
}
}

View File

@@ -37,12 +37,11 @@ class Sprocket : public Mediator {
Sprocket* addTask(Task&);
virtual void loop();
virtual Sprocket* activate();
virtual Sprocket* activate(Scheduler*) { return this; }
virtual Sprocket* activate(Scheduler*, Network*);
virtual Sprocket* activate(Scheduler*);
virtual void dispatch( uint32_t from, String &msg );
void addPlugin(Plugin* p);
void activatePlugins(Scheduler* scheduler, Network* network);
void activatePlugins(Scheduler* scheduler);
void dispatchMessageToPlugins(SprocketMessage msg);
};

View File

@@ -22,14 +22,12 @@ Network* WiFiNet::init() {
return this;
}
int WiFiNet::connect(){
if(config.valid){
WiFi.hostname(config.hostname);
Serial.println("Hostname: " + config.hostname);
if(!connectStation()) {
createAccessPoint();
}
startDNS();
WiFi.hostname(config.hostname);
Serial.println("Hostname: " + config.hostname);
if(!connectStation()) {
createAccessPoint();
}
startDNS();
return 1;
}
@@ -41,6 +39,7 @@ int WiFiNet::connectStation(){
WiFi.begin(config.stationSSID.c_str(), config.stationPassword.c_str());
Serial.println("connect to " + config.stationSSID);
// TODO use tasks
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");

View File

@@ -23,10 +23,10 @@ class MeshSprocket : public Sprocket {
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
Sprocket::activate(scheduler, network);
net = static_cast<MeshNet*>(network);
net->onReceive(bind(&MeshSprocket::dispatch,this, _1, _2));
Sprocket* activate(Scheduler* scheduler) {
Sprocket::activate(scheduler);
//net = static_cast<MeshNet*>(network);
//net->onReceive(bind(&MeshSprocket::dispatch,this, _1, _2));
return this;
} using Sprocket::activate;
@@ -42,7 +42,7 @@ class MeshSprocket : public Sprocket {
}
void loop() {
net->update();
//net->update();
scheduler.execute();
}
};

View File

@@ -4,11 +4,11 @@
#include <painlessMesh.h>
#include <base/MeshSprocket.h>
#include <MeshNet.h>
#include <plugins/WebSO.h>
#include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerConfig.h>
//#include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerPlugin.cpp>
#include <plugins/WebConfigPlugin.cpp>
#include <plugins/MeshManPlugin.cpp>
//#include <plugins/MeshManPlugin.cpp>
#include "Mediator.h"
using namespace std;
@@ -20,17 +20,17 @@ class MeshApp : public MeshSprocket {
public:
Task heartbeatTask;
MeshApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : MeshSprocket(cfg) {
addPlugin(new OtaTcpPlugin(otaCfg));
MeshApp(SprocketConfig cfg, /* OtaConfig otaCfg, */ WebServerConfig webCfg) : MeshSprocket(cfg) {
//addPlugin(new OtaTcpPlugin(otaCfg));
addPlugin(new WebServerPlugin(webCfg, &WEBSERVER));
addPlugin(new WebConfigPlugin(&WEBSERVER));
addPlugin(new MeshManPlugin(&WEBSERVER));
//addPlugin(new MeshManPlugin(&WEBSERVER));
subscribe("mesh/heartbeat", bind(&MeshApp::messageHandler, this, _1));
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
Sprocket* activate(Scheduler* scheduler) {
// call parent method that enables dispatching and plugins
MeshSprocket::activate(scheduler, network);
MeshSprocket::activate(scheduler);
// add a task that sends stuff to the mesh
heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this, net));
@@ -51,7 +51,7 @@ class MeshApp : public MeshSprocket {
msg.topic = "mesh/heartbeat";
msg.type = SprocketMessage::APP;
String msgStr = msg.toJsonString();
network->mesh.sendBroadcast(msgStr, true);
//network->mesh.sendBroadcast(msgStr, true);
//String mMsg = String("hoi");
//publish("mediatorMsg", "hi mediator");
}

View File

@@ -10,7 +10,7 @@ MeshNet net({
});
MeshApp sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
{ OTA_PORT, OTA_PASSWORD },
/* { OTA_PORT, OTA_PASSWORD }, */
{ WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE }
);

View File

@@ -4,8 +4,8 @@
#include <TaskScheduler.h>
#include <Sprocket.h>
#include <plugins/WebSO.h>
#include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerConfig.h>
//#include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerPlugin.cpp>
#include <plugins/WebConfigPlugin.cpp>
#include "Mediator.h"
@@ -19,15 +19,15 @@ class WiFiApp : public Sprocket {
public:
Scheduler* ts;
Task someTask;
WiFiApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) {
WiFiApp(SprocketConfig cfg, /* OtaConfig otaCfg, */ WebServerConfig webCfg) : Sprocket(cfg) {
//addPlugin(new OtaTcpPlugin(otaCfg));
addPlugin(new WebServerPlugin(webCfg, &WEBSERVER));
addPlugin(new WebConfigPlugin(&WEBSERVER));
ts = new Scheduler();
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
Sprocket::activate(ts, network);
Sprocket* activate(Scheduler* scheduler) {
Sprocket::activate(ts);
Serial.println("activate WiFiApp");
// add a task
someTask.set(TASK_SECOND, TASK_FOREVER, [](){

View File

@@ -15,7 +15,7 @@
WiFiNet net(SPROCKET_MODE,STATION_SSID, STATION_PASSWORD,AP_SSID, AP_PASSWORD,HOSTNAME,CONNECT_TIMEOUT);
WiFiApp sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
{ OTA_PORT, OTA_PASSWORD },
/* { OTA_PORT, OTA_PASSWORD }, */
{ WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE }
);

View File

@@ -5,7 +5,7 @@
#include "ArduinoOTA.h"
#include "MeshNet.h"
#include "Plugin.h"
#include <plugins/WebSO.h>
#include <plugins/WebServerConfig.h>
#include <base/MeshSprocketConfig.h>
#include <functional>

View File

@@ -0,0 +1,48 @@
#ifndef __MESH_NETWORK_PLUGIN__
#define __MESH_NETWORK_PLUGIN__
#define _TASK_PRIORITY
#include "TaskSchedulerDeclarations.h"
#include "Plugin.h"
#include <Network.h>
#include <MeshNet.h>
#include "plugins/NetworkPlugin.cpp"
#include <base/SprocketMessage.h>
using namespace std;
using namespace std::placeholders;
class MeshNetworkPlugin : public NetworkPlugin
{
private:
Scheduler *meshScheduler;
public:
MeshNetworkPlugin(MeshConfig cfg)
{
network = new MeshNet(cfg);
}
void activate(Scheduler *userScheduler)
{
userScheduler->setHighPriorityScheduler(meshScheduler);
network->onReceive(bind(&MeshNetworkPlugin::dispatch, this, _1, _2));
// TODO base subscribers
NetworkPlugin::activate(meshScheduler);
}
void dispatch(uint32_t from, String &msg)
{
SprocketMessage sMsg;
sMsg.fromJsonString(msg);
if (sMsg.valid)
{
sMsg.from = String(from);
publish(sMsg.topic, sMsg.payload);
return;
}
publish("mesh/message", msg);
}
};
#endif

View File

@@ -0,0 +1,28 @@
#ifndef __NETWORK_PLUGIN__
#define __NETWORK_PLUGIN__
#include "TaskSchedulerDeclarations.h"
#include "Plugin.h"
#include "Network.h"
class NetworkPlugin : public Plugin
{
protected:
Network *network;
public:
NetworkPlugin() {}
NetworkPlugin(Network *net)
{
network = net;
}
void activate(Scheduler *userScheduler)
{
Serial.println("join network");
network->init(userScheduler);
network->connect();
}
};
#endif

View File

@@ -26,7 +26,7 @@ class OtaTcpPlugin : public Plugin {
}
void connectUpdateNetwork() {
Serial.println("OTA connect to update-network");
net->connectStation(1);
net->connectStation();
}
void enable() {
Serial.println("OTA enable");

View File

@@ -5,7 +5,8 @@
#include "TaskSchedulerDeclarations.h"
#include "ArduinoOTA.h"
#include "Plugin.h"
#include <plugins/WebSO.h>
#include <plugins/WebServerConfig.h>
#include <ESPAsyncWebServer.h>
using namespace std;
using namespace std::placeholders;
@@ -19,7 +20,7 @@ class WebConfigPlugin : public Plugin {
server = webServer;
server->serveStatic("/config.json", SPIFFS, "config.json");
}
void activate(Scheduler* userScheduler, Network* network){
void activate(Scheduler* userScheduler){
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("GET /heap");
request->send(200, "text/plain", String(ESP.getFreeHeap()));

View File

@@ -1,8 +1,5 @@
#ifndef __SHARED_PLUGINS__
#define __SHARED_PLUGINS__
#include <ESPAsyncWebServer.h>
extern AsyncWebServer WEBSERVER;
#ifndef __WEB_SERVER_CONFIG__
#define __WEB_SERVER_CONFIG__
struct WebServerConfig {
const char* contextPath;

View File

@@ -2,9 +2,10 @@
#define __WEB_SERVER_PLUGIN__
#include <FS.h>
#include <ESPAsyncWebServer.h>
#include "TaskSchedulerDeclarations.h"
#include "Plugin.h"
#include <plugins/WebSO.h>
#include <plugins/WebServerConfig.h>
using namespace std;
using namespace std::placeholders;
@@ -18,7 +19,7 @@ class WebServerPlugin : public Plugin {
config = cfg;
server = webServer;
}
void activate(Scheduler* userScheduler, Network* network){
void activate(Scheduler* userScheduler){
server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
// TODO add auth if configured
// server->setAuthentication("user", "pass");