moar refactorrz

This commit is contained in:
2018-06-12 07:30:28 +02:00
parent 8b04d8642a
commit a6d17ba6a3
7 changed files with 27 additions and 18 deletions

View File

@@ -15,8 +15,8 @@ using namespace std::placeholders;
#define MESH_PASSWORD "somethingSneaky" #define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555 #define MESH_PORT 5555
#define STATION_SSID "Th1ngs4p" #define STATION_SSID "tErAx1d"
#define STATION_PWD "th3r31sn0sp00n" #define STATION_PWD "ramalamadingdong"
#define HOSTNAME "MeshNode" #define HOSTNAME "MeshNode"
class MeshNet : public Network { class MeshNet : public Network {

View File

@@ -5,6 +5,7 @@ Sprocket::Sprocket(){
} }
Sprocket* Sprocket::init(SprocketConfig cfg){ Sprocket* Sprocket::init(SprocketConfig cfg){
delay(cfg.startupDelay);
Serial.begin(cfg.serialBaudRate); Serial.begin(cfg.serialBaudRate);
SPIFFS.begin(); SPIFFS.begin();
return this; return this;
@@ -42,7 +43,7 @@ Sprocket* Sprocket::app(App& app){
} }
void Sprocket::loop(){ void Sprocket::loop(){
network.update(); //network.update();
scheduler.execute(); scheduler.execute();
//stack->loop(); //stack->loop();
} }

View File

@@ -10,26 +10,24 @@
#include "Network.h" #include "Network.h"
struct SprocketConfig { struct SprocketConfig {
int startupDelay;
int serialBaudRate; int serialBaudRate;
}; };
class Sprocket { class Sprocket {
protected:
Scheduler scheduler;
private: private:
AppStack* stack; // REMOVE AppStack* stack; // REMOVE
Scheduler scheduler;
Network network;
public: public:
Sprocket(); Sprocket();
Sprocket* init(SprocketConfig); Sprocket* init(SprocketConfig);
Sprocket* join(Network&); Sprocket* join(Network&);
Sprocket* join(Network&, App&); // REMOVE Sprocket* join(Network&, App&); // REMOVE
Sprocket* use(AppStack*); // REMOVE Sprocket* use(AppStack*); // REMOVE
Sprocket* addTask(Task&); // REMOVE Sprocket* addTask(Task&); // REMOVE ??
Sprocket* app(App&); // REMOVE Sprocket* app(App&); // REMOVE
void loop(); virtual void loop();
virtual Sprocket* activate() {
activate(&scheduler, &network);
}
virtual Sprocket* activate(Scheduler* scheduler, Network* network) {} virtual Sprocket* activate(Scheduler* scheduler, Network* network) {}
}; };

View File

@@ -11,7 +11,7 @@
#define SERIAL_BAUD_RATE 115200 #define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 3000 #define STARTUP_DELAY 3000
SprocketConfig config = { SERIAL_BAUD_RATE }; SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
WiFiNet net; WiFiNet net;
Sprocket sprocket; Sprocket sprocket;

View File

@@ -10,7 +10,7 @@
#define SERIAL_BAUD_RATE 115200 #define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 3000 #define STARTUP_DELAY 3000
SprocketConfig config = { SERIAL_BAUD_RATE }; SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
Sprocket sprocket; Sprocket sprocket;
//AppStack stack; //AppStack stack;

View File

@@ -4,7 +4,7 @@
#include <WiFiClient.h> #include <WiFiClient.h>
#include <painlessMesh.h> #include <painlessMesh.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include "App.h" #include "Sprocket.h"
#include "MeshNet.h" #include "MeshNet.h"
#define MQTT_CLIENT_NAME "meshBridge" #define MQTT_CLIENT_NAME "meshBridge"
@@ -18,6 +18,12 @@
using namespace std; using namespace std;
using namespace std::placeholders; using namespace std::placeholders;
struct MqttConfig {
const char* clientName;
const char* brokerHost;
int brokerPort;
};
WiFiClient wifiClient; WiFiClient wifiClient;
class MqttMeshBridge : public Sprocket { class MqttMeshBridge : public Sprocket {
@@ -27,10 +33,11 @@ class MqttMeshBridge : public Sprocket {
Task connectTask; Task connectTask;
Task processTask; Task processTask;
MqttMeshBridge() { MqttMeshBridge(MqttConfig cfg) : Sprocket() {
} }
Sprocket* activate(Scheduler* scheduler, Network* network) { Sprocket* activate(Scheduler* scheduler, Network* network) {
Serial.println("activate MQTT bridge");
net = static_cast<MeshNet*>(network); net = static_cast<MeshNet*>(network);
net->mesh.onReceive(bind(&MqttMeshBridge::receivedCallback,this, _1, _2)); net->mesh.onReceive(bind(&MqttMeshBridge::receivedCallback,this, _1, _2));
client = new PubSubClient(MQTT_BROKER_HOST, MQTT_BROKER_PORT, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient); client = new PubSubClient(MQTT_BROKER_HOST, MQTT_BROKER_PORT, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient);
@@ -39,6 +46,11 @@ class MqttMeshBridge : public Sprocket {
return this; return this;
} }
void loop() {
net->update();
scheduler.execute();
}
void enableConnectTask(Scheduler* scheduler) { void enableConnectTask(Scheduler* scheduler) {
connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::connect, this)); connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::connect, this));
scheduler->addTask(connectTask); scheduler->addTask(connectTask);

View File

@@ -10,14 +10,12 @@
#define SERIAL_BAUD_RATE 115200 #define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 3000 #define STARTUP_DELAY 3000
SprocketConfig config = { SERIAL_BAUD_RATE }; SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
Sprocket sprocket;
MeshNet net; MeshNet net;
MqttMeshBridge app; MqttMeshBridge sprocket({"","",1883});
void setup() { void setup() {
delay(STARTUP_DELAY);
sprocket.init(config); sprocket.init(config);
sprocket.join(net); sprocket.join(net);
} }