From 8b04d8642aed5b68d46ce12ba9bd493b238dbb1e Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Tue, 12 Jun 2018 01:25:59 +0200 Subject: [PATCH] refactorz --- .gitlab-ci.yml | 6 +- src/MeshNet.h | 91 +++++++++++++----------- src/Sprocket.cpp | 2 +- src/Sprocket.h | 14 ++-- src/examples/mqttBridge/MqttMeshBridge.h | 16 ++--- src/examples/mqttBridge/bridge.cpp | 3 +- 6 files changed, 72 insertions(+), 60 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e597464..86d90fe 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,10 +1,10 @@ +stages: + - build + cache: paths: - firmware -stages: - - build - before_script: - "pip install -U platformio" diff --git a/src/MeshNet.h b/src/MeshNet.h index 8a3e3f9..688b2be 100644 --- a/src/MeshNet.h +++ b/src/MeshNet.h @@ -8,54 +8,65 @@ using namespace std; using namespace std::placeholders; +#define STATION_MODE 1 +#define WIFI_CHANNEL 11 -#define MESH_PREFIX "whateverYouLike" -#define MESH_PASSWORD "somethingSneaky" -#define MESH_PORT 5555 +#define MESH_PREFIX "whateverYouLike" +#define MESH_PASSWORD "somethingSneaky" +#define MESH_PORT 5555 + +#define STATION_SSID "Th1ngs4p" +#define STATION_PWD "th3r31sn0sp00n" +#define HOSTNAME "MeshNode" class MeshNet : public Network { public: - painlessMesh mesh; - Network* init(){ - Serial.println("init mesh"); - //mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on - mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION); - mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, 11 ); - //mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2)); - mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1)); - mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this)); - mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1)); + painlessMesh mesh; - return this; - } - Network* connect(){ - Serial.println("connect station"); - mesh.stationManual("tErAx1d", "ramalamadingdong"); - mesh.setHostname("MeshNode"); - return this; - } - void broadcast(String msg){ - mesh.sendBroadcast(msg); - } - void update(){ - mesh.update(); - } - void receivedCallback( uint32_t from, String &msg ) { - Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str()); - } + Network* init(){ - void newConnectionCallback(uint32_t nodeId) { - id = nodeId; - Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); - } + Serial.println("init mesh"); + //mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on + mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION); + mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, WIFI_CHANNEL ); + //mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2)); + mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1)); + mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this)); + mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1)); + + if(STATION_MODE){ + Serial.println("connect station"); + mesh.stationManual(STATION_SSID, STATION_PWD); + mesh.setHostname(HOSTNAME); + } - void changedConnectionCallback() { - Serial.printf("Changed connections %s\n",mesh.subConnectionJson().c_str()); - } + return this; + } + Network* connect(){ + return this; + } + void broadcast(String msg){ + mesh.sendBroadcast(msg); + } + void update(){ + mesh.update(); + } + void receivedCallback( uint32_t from, String &msg ) { + Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str()); + } - void nodeTimeAdjustedCallback(int32_t offset) { - Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset); - } + void newConnectionCallback(uint32_t nodeId) { + id = nodeId; + Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); + } + + void changedConnectionCallback() { + Serial.printf("Changed connections %s\n",mesh.subConnectionJson().c_str()); + } + + void nodeTimeAdjustedCallback(int32_t offset) { + Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset); + } }; #endif \ No newline at end of file diff --git a/src/Sprocket.cpp b/src/Sprocket.cpp index 11fd87b..cc66698 100644 --- a/src/Sprocket.cpp +++ b/src/Sprocket.cpp @@ -15,11 +15,11 @@ Sprocket* Sprocket::join(Network& net, App& app){ } Sprocket* Sprocket::join(Network& net){ - network = net; Serial.println("join network"); net.setScheduler(&scheduler); net.init(); net.connect(); + activate(&scheduler, &net); return this; } Sprocket* Sprocket::use(AppStack* stk){ diff --git a/src/Sprocket.h b/src/Sprocket.h index d3f6abd..7ba3adb 100644 --- a/src/Sprocket.h +++ b/src/Sprocket.h @@ -15,18 +15,22 @@ struct SprocketConfig { class Sprocket { private: - AppStack* stack; + AppStack* stack; // REMOVE Scheduler scheduler; Network network; public: Sprocket(); Sprocket* init(SprocketConfig); Sprocket* join(Network&); - Sprocket* join(Network&, App&); - Sprocket* use(AppStack*); - Sprocket* addTask(Task&); - Sprocket* app(App&); + Sprocket* join(Network&, App&); // REMOVE + Sprocket* use(AppStack*); // REMOVE + Sprocket* addTask(Task&); // REMOVE + Sprocket* app(App&); // REMOVE void loop(); + virtual Sprocket* activate() { + activate(&scheduler, &network); + } + virtual Sprocket* activate(Scheduler* scheduler, Network* network) {} }; #endif \ No newline at end of file diff --git a/src/examples/mqttBridge/MqttMeshBridge.h b/src/examples/mqttBridge/MqttMeshBridge.h index 7fff530..1c82bb1 100644 --- a/src/examples/mqttBridge/MqttMeshBridge.h +++ b/src/examples/mqttBridge/MqttMeshBridge.h @@ -20,7 +20,7 @@ using namespace std::placeholders; WiFiClient wifiClient; -class MqttMeshBridge : public App { +class MqttMeshBridge : public Sprocket { public: MeshNet* net; PubSubClient* client; @@ -30,25 +30,23 @@ class MqttMeshBridge : public App { MqttMeshBridge() { } - void activate(Scheduler* scheduler, Network* network) { + Sprocket* activate(Scheduler* scheduler, Network* network) { net = static_cast(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); - // add a task that sends stuff to the mesh enableConnectTask(scheduler); enableProcessTask(scheduler); + return this; } - void enableConnectTask(Scheduler* scheduler){ - connectTask.set(TASK_SECOND * 5, TASK_FOREVER, - bind(&MqttMeshBridge::connect, this)); + void enableConnectTask(Scheduler* scheduler) { + connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::connect, this)); scheduler->addTask(connectTask); connectTask.enable(); } - void enableProcessTask(Scheduler* scheduler){ - processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, - bind(&MqttMeshBridge::process, this)); + void enableProcessTask(Scheduler* scheduler) { + processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::process, this)); scheduler->addTask(processTask); processTask.enable(); } diff --git a/src/examples/mqttBridge/bridge.cpp b/src/examples/mqttBridge/bridge.cpp index 6ca0e24..c1ff9f4 100644 --- a/src/examples/mqttBridge/bridge.cpp +++ b/src/examples/mqttBridge/bridge.cpp @@ -19,11 +19,10 @@ MqttMeshBridge app; void setup() { delay(STARTUP_DELAY); sprocket.init(config); - sprocket.join(net, app); + sprocket.join(net); } void loop() { - net.update(); sprocket.loop(); yield(); } \ No newline at end of file