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_PORT 5555
#define STATION_SSID "Th1ngs4p"
#define STATION_PWD "th3r31sn0sp00n"
#define STATION_SSID "tErAx1d"
#define STATION_PWD "ramalamadingdong"
#define HOSTNAME "MeshNode"
class MeshNet : public Network {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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