From 636d24a780c05570a67ade4c92f0f419e7784ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=CC=88l=20Ga=CC=88hwiler?= Date: Fri, 4 Dec 2015 14:52:18 +0100 Subject: [PATCH 1/4] added basic abstraction for running tests --- examples/tests/MQTTClient/MQTTClient.ino | 24 +++++++++++++++++ .../tests/YunMQTTClient/YunMQTTClient.ino | 22 ++++++++++++++++ src/MQTTTest.h | 26 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 examples/tests/MQTTClient/MQTTClient.ino create mode 100644 examples/tests/YunMQTTClient/YunMQTTClient.ino create mode 100644 src/MQTTTest.h diff --git a/examples/tests/MQTTClient/MQTTClient.ino b/examples/tests/MQTTClient/MQTTClient.ino new file mode 100644 index 0000000..e25defc --- /dev/null +++ b/examples/tests/MQTTClient/MQTTClient.ino @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +YunClient net; +MQTTClient client; +MQTTTest test; + +void setup() { + Bridge.begin(); + Serial.begin(9600); + + client.begin("broker.shiftr.io", net); + test.run(&client); +} + +void loop() { + test.loop(); +} + +void messageReceived(String topic, String payload, char * bytes, unsigned int length) { + test.message(topic, payload); +} diff --git a/examples/tests/YunMQTTClient/YunMQTTClient.ino b/examples/tests/YunMQTTClient/YunMQTTClient.ino new file mode 100644 index 0000000..28201dc --- /dev/null +++ b/examples/tests/YunMQTTClient/YunMQTTClient.ino @@ -0,0 +1,22 @@ +#include +#include +#include + +YunMQTTClient client; +MQTTTest test; + +void setup() { + Bridge.begin(); + Serial.begin(9600); + + client.begin("broker.shiftr.io"); + test.run(&client); +} + +void loop() { + test.loop(); +} + +void messageReceived(String topic, String payload, char * bytes, unsigned int length) { + test.message(topic, payload); +} diff --git a/src/MQTTTest.h b/src/MQTTTest.h new file mode 100644 index 0000000..a7931a2 --- /dev/null +++ b/src/MQTTTest.h @@ -0,0 +1,26 @@ +#include + +template +class MQTTTest { + T *client; +public: + void run(T *client); + void loop(); + void message(String topic, String payload); +}; + +template +void MQTTTest::run(T *client) { + this->client = client; +} + +template +void MQTTTest::loop() { + this->client->loop(); +} + +template +void MQTTTest::message(String topic, String payload) { + Serial.println(topic); + Serial.println(payload); +} From db801ad11675a380b871501bc44d6294612ecd91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=CC=88l=20Ga=CC=88hwiler?= Date: Fri, 4 Dec 2015 16:37:27 +0100 Subject: [PATCH 2/4] implemented a basic test --- examples/tests/MQTTClient/MQTTClient.ino | 5 +- .../tests/YunMQTTClient/YunMQTTClient.ino | 5 +- src/MQTTTest.h | 60 ++++++++++++++++--- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/examples/tests/MQTTClient/MQTTClient.ino b/examples/tests/MQTTClient/MQTTClient.ino index e25defc..71218a7 100644 --- a/examples/tests/MQTTClient/MQTTClient.ino +++ b/examples/tests/MQTTClient/MQTTClient.ino @@ -11,13 +11,12 @@ void setup() { Bridge.begin(); Serial.begin(9600); + // run a very basic automated test client.begin("broker.shiftr.io", net); test.run(&client); } -void loop() { - test.loop(); -} +void loop() {} void messageReceived(String topic, String payload, char * bytes, unsigned int length) { test.message(topic, payload); diff --git a/examples/tests/YunMQTTClient/YunMQTTClient.ino b/examples/tests/YunMQTTClient/YunMQTTClient.ino index 28201dc..1a63750 100644 --- a/examples/tests/YunMQTTClient/YunMQTTClient.ino +++ b/examples/tests/YunMQTTClient/YunMQTTClient.ino @@ -9,13 +9,12 @@ void setup() { Bridge.begin(); Serial.begin(9600); + // run a very basic automated test client.begin("broker.shiftr.io"); test.run(&client); } -void loop() { - test.loop(); -} +void loop() {} void messageReceived(String topic, String payload, char * bytes, unsigned int length) { test.message(topic, payload); diff --git a/src/MQTTTest.h b/src/MQTTTest.h index a7931a2..0fb295b 100644 --- a/src/MQTTTest.h +++ b/src/MQTTTest.h @@ -2,25 +2,71 @@ template class MQTTTest { - T *client; public: void run(T *client); - void loop(); void message(String topic, String payload); +private: + T *client; + void printResult(boolean res); + boolean testMessage(const char * topic, const char * payload); + boolean newMessage = false; + boolean passedTest = false; + String testTopic; + String testPayload; }; +/* Methods */ + template void MQTTTest::run(T *client) { this->client = client; + + Serial.print("[Test 1] Connect: "); + this->printResult(this->client->connect("arduino-mqtt-test", "try", "try")); + + Serial.print("[Test 2] Subscribe & Publish: "); + this->client->subscribe("arduino-mqtt-test/topic1"); + this->client->publish("arduino-mqtt-test/topic1", "test"); + this->printResult(this->testMessage("arduino-mqtt-test/topic1", "test")); + + Serial.print("[Test 3] Unsubscribe: "); + this->client->subscribe("arduino-mqtt-test/topic2"); + this->client->subscribe("arduino-mqtt-test/topic3"); + this->client->unsubscribe("arduino-mqtt-test/topic2"); + this->client->publish("arduino-mqtt-test/topic2", "test"); + this->client->publish("arduino-mqtt-test/topic3", "test"); + this->printResult(this->testMessage("arduino-mqtt-test/topic3", "test")); + + Serial.print("[Test 4] Disconnect: "); + this->client->disconnect(); + this->printResult(!this->client->connected()); +} + +template +void MQTTTest::message(String topic, String payload) { + this->newMessage = true; + this->passedTest = topic.equals(this->testTopic) && payload.equals(this->testPayload); } +/* Helpers */ + template -void MQTTTest::loop() { - this->client->loop(); +void MQTTTest::printResult(boolean res) { + res ? Serial.println("SUCCESS") : Serial.println("FAILED"); } template -void MQTTTest::message(String topic, String payload) { - Serial.println(topic); - Serial.println(payload); +boolean MQTTTest::testMessage(const char *topic, const char *payload) { + this->testTopic = String(topic); + this->testPayload = String(payload); + + while(!this->newMessage) { + this->client->loop(); + } + + boolean ret = this->passedTest; + this->newMessage = false; + this->passedTest = false; + + return ret; } From b2a06ba8340b9cfe14c3e4d13b509284b89e6c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=CC=88l=20Ga=CC=88hwiler?= Date: Fri, 4 Dec 2015 16:59:21 +0100 Subject: [PATCH 3/4] properly disconnect and close process --- yun/bridge.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/yun/bridge.py b/yun/bridge.py index 739a0c9..0bcb600 100644 --- a/yun/bridge.py +++ b/yun/bridge.py @@ -11,15 +11,16 @@ def __init__(self): self.client = None self.will_topic = "" self.will_payload = "" + self.stopped = False # Bridge Callbacks def on_connect(self, _, __, ___, rc): self.send_command("a;" if rc == 0 else "r;") def on_message(self, _, __, msg): self.send_command("m:" + msg.topic + ":" + str(len(msg.payload)) + ";" + str(msg.payload)) - def on_disconnect(self): - self.client.loop_stop() + def on_disconnect(self, _, __, ___): self.send_command("e;") + self.stopped = True # Command Helpers def parse_command(self, line): @@ -51,6 +52,7 @@ def do_connect(self, args): self.client = mqtt.Client(args[2]) self.client.on_connect = self.on_connect self.client.on_message = self.on_message + self.client.on_disconnect = self.on_disconnect if len(args) >= 5: self.client.username_pw_set(args[3], args[4]) if len(self.will_topic) > 0: @@ -68,12 +70,13 @@ def do_unsubscribe(self, args): def do_publish(self, args): self.client.publish(args[0], self.read_chunk(int(args[1]))) def do_disconnect(self): + self.client.loop_stop() self.client.disconnect() # Main def run(self): self.send_command("b;") - while True: + while not self.stopped: self.parse_command(self.read_until(self.PAYLOAD_SEPERATOR)) # Low Level Helpers From efc32429932a1223d97169f6b9eb10331a2e24a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=CC=88l=20Ga=CC=88hwiler?= Date: Fri, 4 Dec 2015 17:12:40 +0100 Subject: [PATCH 4/4] fixed disconnect tests --- src/MQTTTest.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/MQTTTest.h b/src/MQTTTest.h index 0fb295b..caaf26c 100644 --- a/src/MQTTTest.h +++ b/src/MQTTTest.h @@ -13,6 +13,7 @@ class MQTTTest { boolean passedTest = false; String testTopic; String testPayload; + boolean testConnectivity(boolean test); }; /* Methods */ @@ -21,8 +22,10 @@ template void MQTTTest::run(T *client) { this->client = client; + Serial.println("Starting tests..."); + Serial.print("[Test 1] Connect: "); - this->printResult(this->client->connect("arduino-mqtt-test", "try", "try")); + this->printResult(this->client->connect("arduino-mqtt-test", "try", "try") && this->testConnectivity(true)); Serial.print("[Test 2] Subscribe & Publish: "); this->client->subscribe("arduino-mqtt-test/topic1"); @@ -39,7 +42,9 @@ void MQTTTest::run(T *client) { Serial.print("[Test 4] Disconnect: "); this->client->disconnect(); - this->printResult(!this->client->connected()); + this->printResult(this->testConnectivity(false)); + + Serial.println("Tests finished!"); } template @@ -70,3 +75,12 @@ boolean MQTTTest::testMessage(const char *topic, const char *payload) { return ret; } + +template +boolean MQTTTest::testConnectivity(boolean test) { + while(this->client->connected() != test) { + this->client->loop(); + } + + return this->client->connected() == test; +}