mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-14 12:46:50 +01:00
adapt mesh app to use mesh plugin
This commit is contained in:
@@ -40,8 +40,8 @@ void MeshNet::sendTo(uint32_t target, String msg){
|
||||
mesh.sendSingle(target, msg);
|
||||
}
|
||||
|
||||
void MeshNet::broadcast(String msg){
|
||||
mesh.sendBroadcast(msg);
|
||||
void MeshNet::broadcast(String msg, bool self){
|
||||
mesh.sendBroadcast(msg, self);
|
||||
|
||||
}
|
||||
void MeshNet::update(){
|
||||
|
||||
@@ -29,7 +29,7 @@ class MeshNet : public Network {
|
||||
void newConnectionCallback(uint32_t nodeId);
|
||||
void changedConnectionCallback();
|
||||
void nodeTimeAdjustedCallback(int32_t offset);
|
||||
void broadcast(String msg);
|
||||
void broadcast(String msg, bool self = false);
|
||||
void sendTo(uint32_t target, String msg);
|
||||
void onReceive(std::function<void(uint32_t from, String &msg)>);
|
||||
int isConnected(){
|
||||
|
||||
@@ -16,7 +16,7 @@ class Network {
|
||||
virtual int connectStation() { return 0; };
|
||||
virtual int isConnected(){ return 0; };
|
||||
virtual void update() {};
|
||||
virtual void broadcast(String msg){};
|
||||
virtual void broadcast(String msg, bool self = false){};
|
||||
virtual void sendTo(uint32_t target, String msg) {};
|
||||
virtual void onReceive(std::function<void(uint32_t from, String &msg)>) {};
|
||||
Network* setScheduler(Scheduler* s) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef __SPROCKET_PLUGIN__
|
||||
#define __SPROCKET_PLUGIN__
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
#define _TASK_PRIORITY
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include <Network.h>
|
||||
#include <base/SprocketMessage.h>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
#define _TASK_PRIORITY
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
//#include <TaskScheduler.h>
|
||||
@@ -45,7 +46,6 @@ class Sprocket : public Mediator {
|
||||
|
||||
void addPlugin(Plugin* p);
|
||||
void activatePlugins(Scheduler* scheduler);
|
||||
void dispatchMessageToPlugins(SprocketMessage msg);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -43,7 +43,7 @@ class MeshSprocket : public Sprocket {
|
||||
|
||||
void loop() {
|
||||
//net->update();
|
||||
scheduler.execute();
|
||||
scheduler->execute();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
#ifndef __MESH_APP__
|
||||
#define __MESH_APP__
|
||||
|
||||
#include <painlessMesh.h>
|
||||
#include <base/MeshSprocket.h>
|
||||
#include <Sprocket.h>
|
||||
#include <MeshNet.h>
|
||||
#include <plugins/MeshNetworkPlugin.cpp>
|
||||
#include <plugins/WebServerConfig.h>
|
||||
//#include <plugins/OtaTcpPlugin.cpp>
|
||||
#include <plugins/WebServerPlugin.cpp>
|
||||
#include <plugins/WebConfigPlugin.cpp>
|
||||
//#include <plugins/MeshManPlugin.cpp>
|
||||
#include "Mediator.h"
|
||||
|
||||
using namespace std;
|
||||
@@ -16,48 +14,41 @@ using namespace std::placeholders;
|
||||
|
||||
AsyncWebServer WEBSERVER(80);
|
||||
|
||||
class MeshApp : public MeshSprocket {
|
||||
class MeshApp : public Sprocket {
|
||||
public:
|
||||
Task heartbeatTask;
|
||||
|
||||
MeshApp(SprocketConfig cfg, /* OtaConfig otaCfg, */ WebServerConfig webCfg) : MeshSprocket(cfg) {
|
||||
//addPlugin(new OtaTcpPlugin(otaCfg));
|
||||
MeshApp(SprocketConfig cfg, MeshConfig meshCfg, WebServerConfig webCfg) : Sprocket(cfg) {
|
||||
addPlugin(new MeshNetworkPlugin(meshCfg));
|
||||
addPlugin(new WebServerPlugin(webCfg, &WEBSERVER));
|
||||
addPlugin(new WebConfigPlugin(&WEBSERVER));
|
||||
//addPlugin(new MeshManPlugin(&WEBSERVER));
|
||||
subscribe("mesh/heartbeat", bind(&MeshApp::messageHandler, this, _1));
|
||||
subscribe("device/heartbeat", bind(&MeshApp::messageHandler, this, _1));
|
||||
}
|
||||
|
||||
Sprocket* activate(Scheduler* scheduler) {
|
||||
// call parent method that enables dispatching and plugins
|
||||
MeshSprocket::activate(scheduler);
|
||||
|
||||
Serial.println("activate MeshApp");
|
||||
Sprocket::activate(scheduler);
|
||||
// add a task that sends stuff to the mesh
|
||||
heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this, net));
|
||||
heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this));
|
||||
addTask(heartbeatTask);
|
||||
|
||||
return this;
|
||||
} using MeshSprocket::activate;
|
||||
} using Sprocket::activate;
|
||||
|
||||
void messageHandler(String msg){
|
||||
Serial.println(String("MeshApp: ") + msg);
|
||||
}
|
||||
|
||||
void heartbeat(MeshNet* network){
|
||||
SprocketMessage msg; // = { "wirelos", "broadcast", "local", "alive", 0, };
|
||||
void heartbeat(){
|
||||
SprocketMessage msg;
|
||||
msg.domain = "wirelos";
|
||||
msg.to = "broadcast";
|
||||
msg.payload = "alive";
|
||||
msg.topic = "mesh/heartbeat";
|
||||
msg.topic = "device/heartbeat";
|
||||
msg.type = SprocketMessage::APP;
|
||||
String msgStr = msg.toJsonString();
|
||||
//network->mesh.sendBroadcast(msgStr, true);
|
||||
//String mMsg = String("hoi");
|
||||
//publish("mediatorMsg", "hi mediator");
|
||||
publish("mesh/broadcast", msgStr);
|
||||
}
|
||||
//void onMessage( uint32_t from, String &msg ) {
|
||||
// Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str());
|
||||
//}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -4,6 +4,7 @@
|
||||
// Scheduler config
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
#define _TASK_PRIORITY
|
||||
|
||||
// Chip config
|
||||
#define SERIAL_BAUD_RATE 115200
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
#include "config.h"
|
||||
#include "MeshNet.h"
|
||||
#include "MeshApp.h"
|
||||
|
||||
MeshNet net({
|
||||
SPROCKET_MODE, WIFI_CHANNEL,
|
||||
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
|
||||
STATION_SSID, STATION_PASSWORD, HOSTNAME,
|
||||
MESH_DEBUG_TYPES
|
||||
});
|
||||
MeshApp sprocket(
|
||||
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
|
||||
/* { OTA_PORT, OTA_PASSWORD }, */
|
||||
{ WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE }
|
||||
);
|
||||
{STARTUP_DELAY, SERIAL_BAUD_RATE},
|
||||
{SPROCKET_MODE, WIFI_CHANNEL,
|
||||
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
|
||||
STATION_SSID, STATION_PASSWORD, HOSTNAME,
|
||||
MESH_DEBUG_TYPES},
|
||||
{WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE});
|
||||
|
||||
void setup() {
|
||||
//sprocket.join(net);
|
||||
void setup()
|
||||
{
|
||||
delay(3000);
|
||||
sprocket.activate();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
void loop()
|
||||
{
|
||||
sprocket.loop();
|
||||
yield();
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
#ifndef __MESH_NETWORK_PLUGIN__
|
||||
#define __MESH_NETWORK_PLUGIN__
|
||||
|
||||
#define _TASK_PRIORITY
|
||||
|
||||
#include "TaskSchedulerDeclarations.h"
|
||||
#include "Plugin.h"
|
||||
#include "TaskSchedulerDeclarations.h"
|
||||
#include <Network.h>
|
||||
#include <MeshNet.h>
|
||||
#include "plugins/NetworkPlugin.cpp"
|
||||
@@ -22,6 +20,7 @@ class MeshNetworkPlugin : public NetworkPlugin
|
||||
MeshNetworkPlugin(MeshConfig cfg)
|
||||
{
|
||||
network = new MeshNet(cfg);
|
||||
meshScheduler = new Scheduler();
|
||||
}
|
||||
|
||||
void activate(Scheduler *userScheduler)
|
||||
@@ -29,8 +28,13 @@ class MeshNetworkPlugin : public NetworkPlugin
|
||||
userScheduler->setHighPriorityScheduler(meshScheduler);
|
||||
network->onReceive(bind(&MeshNetworkPlugin::dispatch, this, _1, _2));
|
||||
// TODO base subscribers
|
||||
subscribe("mesh/broadcast", bind(&MeshNetworkPlugin::broadcast, this, _1));
|
||||
NetworkPlugin::activate(meshScheduler);
|
||||
}
|
||||
void broadcast(String msg)
|
||||
{
|
||||
network->broadcast(msg, true);
|
||||
}
|
||||
void dispatch(uint32_t from, String &msg)
|
||||
{
|
||||
SprocketMessage sMsg;
|
||||
|
||||
Reference in New Issue
Block a user